1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRW/SRU package encoding and decoding
16 #include <yaz/wrbuf.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
24 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
29 xmlDocPtr doc = xmlParseMemory(val,len);
32 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
33 xmlNodePtr t = xmlDocGetRootElement(doc);
34 xmlAddChild(c, xmlCopyNode(t,1));
40 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
45 xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
46 xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
53 xmlNodePtr add_xsd_string_ns(xmlNodePtr ptr, const char *elem, const char *val,
58 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
59 xmlNodePtr t = xmlNewText(BAD_CAST val);
66 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
68 return add_xsd_string_ns(ptr, elem, val, 0);
71 static void add_xsd_integer(xmlNodePtr ptr, const char *elem,
77 sprintf(str, ODR_INT_PRINTF, *val);
78 xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
82 static int match_element(xmlNodePtr ptr, const char *elem)
84 if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
93 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
97 struct _xmlAttr *attr;
99 if (!match_element(ptr, elem))
102 for (attr = ptr->properties; attr; attr = attr->next)
103 if (!strcmp(attr->name, "type") &&
104 attr->children && attr->children->type == XML_TEXT_NODE)
106 const char *t = strchr(attr->children->content, ':');
110 t = attr->children->content;
111 if (!strcmp(t, "string"))
118 if (!ptr || ptr->type != XML_TEXT_NODE)
123 *val = odr_strdup(o, (const char *) ptr->content);
125 *len = xmlStrlen(ptr->content);
130 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
133 return match_xsd_string_n(ptr, elem, o, val, 0);
136 static int match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o,
137 char **val, int *len, int fixup_root)
140 int no_root_nodes = 0;
142 if (!match_element(ptr, elem))
145 buf = xmlBufferCreate();
147 /* Copy each element nodes at top.
148 In most cases there is only one root node.. At least one server
149 http://www.theeuropeanlibrary.org/sru/sru.pl
150 has multiple root nodes in recordData.
152 for (ptr = ptr->children; ptr; ptr = ptr->next)
154 if (ptr->type == XML_ELEMENT_NODE)
156 /* copy node to get NS right (bug #740). */
157 xmlNode *tmp = xmlCopyNode(ptr, 1);
159 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
165 if (no_root_nodes != 1 && fixup_root)
167 /* does not appear to be an XML document. Make it so */
168 xmlBufferAddHead(buf, (const xmlChar *) "<yaz_record>", -1);
169 xmlBufferAdd(buf, (const xmlChar *) "</yaz_record>", -1);
171 *val = (char *) odr_malloc(o, buf->use + 1);
172 memcpy(*val, buf->content, buf->use);
173 (*val)[buf->use] = '\0';
183 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
184 char **val, int *len)
186 return match_xsd_XML_n2(ptr, elem, o, val, len, 0);
189 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
193 struct _xmlAttr *attr;
195 if (!match_element(ptr, elem))
198 for (attr = ptr->properties; attr; attr = attr->next)
199 if (!strcmp(attr->name, "type") &&
200 attr->children && attr->children->type == XML_TEXT_NODE)
202 const char *t = strchr(attr->children->content, ':');
206 t = attr->children->content;
207 if (!strcmp(t, "integer"))
214 if (!ptr || ptr->type != XML_TEXT_NODE)
216 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
220 char *yaz_negotiate_sru_version(char *input_ver)
225 if (!strcmp(input_ver, "1.1"))
227 return "1.2"; /* our latest supported version */
230 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
231 Z_SRW_extra_record **extra,
232 void *client_data, const char *ns)
234 if (o->direction == ODR_DECODE)
236 Z_SRW_extra_record ex;
240 rec->recordSchema = 0;
241 rec->recordData_buf = 0;
242 rec->recordData_len = 0;
243 rec->recordPosition = 0;
246 ex.extraRecordData_buf = 0;
247 ex.extraRecordData_len = 0;
248 ex.recordIdentifier = 0;
250 for (ptr = pptr->children; ptr; ptr = ptr->next)
253 if (match_xsd_string(ptr, "recordSchema", o,
256 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
257 ; /* can't rely on it: in SRU 2.0 it's different semantics */
258 else if (match_xsd_integer(ptr, "recordPosition", o,
259 &rec->recordPosition))
261 else if (match_element(ptr, "recordData"))
263 /* we assume XML packing, if any element nodes exist below
264 recordData. Unfortunately, in SRU 2.0 recordPacking
265 means something different */
266 xmlNode *p = ptr->children;
267 for (; p; p = p->next)
268 if (p->type == XML_ELEMENT_NODE)
273 ptr, "recordData", o,
274 &rec->recordData_buf, &rec->recordData_len, 1);
275 rec->recordPacking = Z_SRW_recordPacking_XML;
280 ptr, "recordData", o,
281 &rec->recordData_buf, &rec->recordData_len);
282 rec->recordPacking = Z_SRW_recordPacking_string;
285 else if (match_xsd_XML_n(ptr, "extraRecordData", o,
286 &ex.extraRecordData_buf,
287 &ex.extraRecordData_len) )
290 match_xsd_string(ptr, "recordIdentifier", o,
291 &ex.recordIdentifier);
293 if (ex.extraRecordData_buf || ex.recordIdentifier)
295 *extra = (Z_SRW_extra_record *)
296 odr_malloc(o, sizeof(Z_SRW_extra_record));
297 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
300 else if (o->direction == ODR_ENCODE)
302 xmlNodePtr ptr = pptr;
303 int pack = rec->recordPacking;
304 const char *spack = yaz_srw_pack_to_str(pack);
306 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
308 add_xsd_string(ptr, "recordPacking", spack);
311 case Z_SRW_recordPacking_string:
312 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
313 rec->recordData_len);
315 case Z_SRW_recordPacking_XML:
316 add_XML_n(ptr, "recordData", rec->recordData_buf,
317 rec->recordData_len, 0);
319 case Z_SRW_recordPacking_URL:
320 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
321 rec->recordData_len);
324 if (rec->recordPosition)
325 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
328 if ((*extra)->recordIdentifier)
329 add_xsd_string(ptr, "recordIdentifier",
330 (*extra)->recordIdentifier);
331 if ((*extra)->extraRecordData_buf)
332 add_XML_n(ptr, "extraRecordData",
333 (*extra)->extraRecordData_buf,
334 (*extra)->extraRecordData_len, 0);
340 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
341 Z_SRW_extra_record ***extra,
342 int *num, void *client_data, const char *ns)
344 if (o->direction == ODR_DECODE)
349 for (ptr = pptr->children; ptr; ptr = ptr->next)
351 if (ptr->type == XML_ELEMENT_NODE &&
352 !xmlStrcmp(ptr->name, BAD_CAST "record"))
357 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
358 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
359 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
361 if (ptr->type == XML_ELEMENT_NODE &&
362 !xmlStrcmp(ptr->name, BAD_CAST "record"))
364 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, ns);
369 else if (o->direction == ODR_ENCODE)
372 for (i = 0; i < *num; i++)
374 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
376 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
383 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
384 void *client_data, const char *ns)
386 if (o->direction == ODR_DECODE)
389 rec->versionType = 0;
390 rec->versionValue = 0;
391 for (ptr = pptr->children; ptr; ptr = ptr->next)
394 if (match_xsd_string(ptr, "versionType", o,
398 match_xsd_string(ptr, "versionValue", o, &rec->versionValue);
401 else if (o->direction == ODR_ENCODE)
403 xmlNodePtr ptr = pptr;
404 add_xsd_string(ptr, "versionType", rec->versionType);
405 add_xsd_string(ptr, "versionValue", rec->versionValue);
410 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
411 Z_SRW_recordVersion **vers,
412 int *num, void *client_data, const char *ns)
414 if (o->direction == ODR_DECODE)
419 for (ptr = pptr->children; ptr; ptr = ptr->next)
421 if (ptr->type == XML_ELEMENT_NODE &&
422 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
427 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
428 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
430 if (ptr->type == XML_ELEMENT_NODE &&
431 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
433 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
438 else if (o->direction == ODR_ENCODE)
441 for (i = 0; i < *num; i++)
443 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
445 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
451 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
455 WRBUF wrbuf = wrbuf_alloc();
456 Z_FacetTerm *facet_term;
457 const char *freq_string = yaz_element_attribute_value_get(
458 node, "facetvalue", "est_representation");
460 freq = odr_atoi(freq_string);
464 for (child = node->children; child ; child = child->next)
466 if (child->type == XML_TEXT_NODE)
467 wrbuf_puts(wrbuf, (const char *) child->content);
469 facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
470 wrbuf_destroy(wrbuf);
474 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
476 Z_AttributeList *list;
477 Z_FacetField *facet_field;
482 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
483 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
485 list = yaz_use_attribute_create(odr, name);
486 for (node = ptr->children; node; node = node->next) {
487 if (match_element(node, "facetvalue"))
490 facet_field = facet_field_create(odr, list, num_terms);
492 for (node = ptr->children; node; node = node->next)
494 if (match_element(node, "facetvalue"))
496 facet_field_term_set(odr, facet_field,
497 yaz_sru_proxy_get_facet_term_count(odr, node),
505 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
506 Z_FacetList **facetList)
510 for (ptr = root->children; ptr; ptr = ptr->next)
512 if (match_element(ptr, "facets"))
515 Z_FacetList *facet_list;
517 for (node = ptr->children; node; node= node->next)
519 if (node->type == XML_ELEMENT_NODE)
522 facet_list = facet_list_create(o, num_facets);
524 for (node = ptr->children; node; node= node->next)
526 if (match_element(node, "facet"))
528 facet_list_field_set(
530 yaz_sru_proxy_decode_facet_field(o, node), num_facets);
534 *facetList = facet_list;
543 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
544 Z_SRW_diagnostic **recs, int *num,
545 void *client_data, const char *ns)
550 for (ptr = pptr; ptr; ptr = ptr->next)
552 if (ptr->type == XML_ELEMENT_NODE &&
553 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
558 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
559 for (i = 0; i < *num; i++)
562 (*recs)[i].details = 0;
563 (*recs)[i].message = 0;
565 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
567 if (ptr->type == XML_ELEMENT_NODE &&
568 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
572 (*recs)[i].details = 0;
573 (*recs)[i].message = 0;
574 for (rptr = ptr->children; rptr; rptr = rptr->next)
576 if (match_xsd_string(rptr, "uri", o,
579 else if (match_xsd_string(rptr, "details", o,
580 &(*recs)[i].details))
583 match_xsd_string(rptr, "message", o, &(*recs)[i].message);
591 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
592 Z_SRW_diagnostic **diag,
596 xmlDocPtr doc = xmlParseMemory(buf, len);
599 xmlNodePtr ptr = xmlDocGetRootElement(doc);
600 while (ptr && ptr->type != XML_ELEMENT_NODE)
603 && !xmlStrcmp(ptr->ns->href,
604 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
606 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
613 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
614 int *num, void *client_data, const char *ns)
616 if (o->direction == ODR_DECODE)
618 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
620 else if (o->direction == ODR_ENCODE)
624 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
625 for (i = 0; i < *num; i++)
627 const char *std_diag = "info:srw/diagnostic/1/";
628 const char *ucp_diag = "info:srw/diagnostic/12/";
629 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
630 BAD_CAST "diagnostic", 0);
631 add_xsd_string(rptr, "uri", (*recs)[i].uri);
632 if ((*recs)[i].message)
633 add_xsd_string(rptr, "message", (*recs)[i].message);
634 else if ((*recs)[i].uri )
636 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
638 int no = atoi((*recs)[i].uri + strlen(std_diag));
639 const char *message = yaz_diag_srw_str(no);
641 add_xsd_string(rptr, "message", message);
643 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
645 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
646 const char *message = yaz_diag_sru_update_str(no);
648 add_xsd_string(rptr, "message", message);
651 add_xsd_string(rptr, "details", (*recs)[i].details);
657 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
658 void *client_data, const char *ns)
660 if (o->direction == ODR_DECODE)
664 term->numberOfRecords = 0;
665 term->displayTerm = 0;
666 term->whereInList = 0;
667 for (ptr = pptr->children; ptr; ptr = ptr->next)
669 if (match_xsd_string(ptr, "value", o, &term->value))
671 else if (match_xsd_integer(ptr, "numberOfRecords", o,
672 &term->numberOfRecords))
674 else if (match_xsd_string(ptr, "displayTerm", o,
678 match_xsd_string(ptr, "whereInList", o, &term->whereInList);
681 else if (o->direction == ODR_ENCODE)
683 xmlNodePtr ptr = pptr;
684 add_xsd_string(ptr, "value", term->value);
685 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
686 add_xsd_string(ptr, "displayTerm", term->displayTerm);
687 add_xsd_string(ptr, "whereInList", term->whereInList);
692 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
693 int *num, void *client_data, const char *ns)
695 if (o->direction == ODR_DECODE)
700 for (ptr = pptr->children; ptr; ptr = ptr->next)
702 if (ptr->type == XML_ELEMENT_NODE &&
703 !xmlStrcmp(ptr->name, BAD_CAST "term"))
708 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
709 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
711 if (ptr->type == XML_ELEMENT_NODE &&
712 !xmlStrcmp(ptr->name, BAD_CAST "term"))
713 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
716 else if (o->direction == ODR_ENCODE)
719 for (i = 0; i < *num; i++)
721 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
722 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
728 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
729 void *client_data, const char *ns)
731 xmlNodePtr pptr = (xmlNodePtr) vptr;
732 if (o->direction == ODR_DECODE)
734 Z_SRW_PDU **p = handler_data;
735 xmlNodePtr method = pptr->children;
738 while (method && method->type == XML_TEXT_NODE)
739 method = method->next;
743 if (method->type != XML_ELEMENT_NODE)
746 *p = yaz_srw_get_core_v_1_1(o);
748 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
750 xmlNodePtr ptr = method->children;
751 Z_SRW_searchRetrieveRequest *req;
753 (*p)->which = Z_SRW_searchRetrieve_request;
754 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
755 odr_malloc(o, sizeof(*req));
756 req->query_type = Z_SRW_query_type_cql;
758 req->sort_type = Z_SRW_sort_type_none;
760 req->startRecord = 0;
761 req->maximumRecords = 0;
762 req->recordSchema = 0;
763 req->recordPacking = 0;
764 req->recordXPath = 0;
765 req->resultSetTTL = 0;
769 for (; ptr; ptr = ptr->next)
771 if (match_xsd_string(ptr, "version", o,
774 else if (match_xsd_string(ptr, "query", o,
776 req->query_type = Z_SRW_query_type_cql;
777 else if (match_xsd_string(ptr, "pQuery", o,
779 req->query_type = Z_SRW_query_type_pqf;
780 else if (match_xsd_string(ptr, "xQuery", o,
782 req->query_type = Z_SRW_query_type_xcql;
783 else if (match_xsd_integer(ptr, "startRecord", o,
786 else if (match_xsd_integer(ptr, "maximumRecords", o,
787 &req->maximumRecords))
789 else if (match_xsd_string(ptr, "recordPacking", o,
790 &req->recordPacking))
792 else if (match_xsd_string(ptr, "recordSchema", o,
795 else if (match_xsd_string(ptr, "recordXPath", o,
798 else if (match_xsd_integer(ptr, "resultSetTTL", o,
801 else if (match_xsd_string(ptr, "sortKeys", o,
802 &req->sort.sortKeys))
803 req->sort_type = Z_SRW_sort_type_sort;
804 else if (match_xsd_string(ptr, "stylesheet", o,
808 match_xsd_string(ptr, "database", o, &req->database);
810 if (!req->query.cql && !req->query.pqf && !req->query.xcql)
812 /* should put proper diagnostic here */
816 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
818 xmlNodePtr ptr = method->children;
819 Z_SRW_searchRetrieveResponse *res;
821 (*p)->which = Z_SRW_searchRetrieve_response;
822 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
823 odr_malloc(o, sizeof(*res));
825 res->numberOfRecords = 0;
826 res->resultSetId = 0;
827 res->resultSetIdleTime = 0;
829 res->num_records = 0;
830 res->diagnostics = 0;
831 res->num_diagnostics = 0;
832 res->nextRecordPosition = 0;
834 res->suggestions = 0;
836 for (; ptr; ptr = ptr->next)
838 if (match_xsd_string(ptr, "version", o,
841 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
842 &(*p)->extraResponseData_buf,
843 &(*p)->extraResponseData_len))
845 else if (match_xsd_integer(ptr, "numberOfRecords", o,
846 &res->numberOfRecords))
848 else if (match_xsd_string(ptr, "resultSetId", o,
851 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
852 &res->resultSetIdleTime))
854 else if (match_element(ptr, "records"))
855 yaz_srw_records(o, ptr, &res->records,
857 &res->num_records, client_data, ns);
858 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
859 &res->nextRecordPosition))
861 else if (match_element(ptr, "diagnostics"))
862 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
863 &res->num_diagnostics,
865 else if (match_element(ptr, "facet_analysis"))
866 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
869 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
871 Z_SRW_explainRequest *req;
872 xmlNodePtr ptr = method->children;
874 (*p)->which = Z_SRW_explain_request;
875 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
876 odr_malloc(o, sizeof(*req));
877 req->recordPacking = 0;
880 for (; ptr; ptr = ptr->next)
882 if (match_xsd_string(ptr, "version", o,
885 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
886 &(*p)->extraResponseData_buf,
887 &(*p)->extraResponseData_len))
889 else if (match_xsd_string(ptr, "stylesheet", o,
892 else if (match_xsd_string(ptr, "recordPacking", o,
893 &req->recordPacking))
896 match_xsd_string(ptr, "database", o, &req->database);
899 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
901 Z_SRW_explainResponse *res;
902 xmlNodePtr ptr = method->children;
904 (*p)->which = Z_SRW_explain_response;
905 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
906 odr_malloc(o, sizeof(*res));
907 res->diagnostics = 0;
908 res->num_diagnostics = 0;
909 res->record.recordSchema = 0;
910 res->record.recordData_buf = 0;
911 res->record.recordData_len = 0;
912 res->record.recordPosition = 0;
914 for (; ptr; ptr = ptr->next)
916 if (match_xsd_string(ptr, "version", o,
919 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
920 &(*p)->extraResponseData_buf,
921 &(*p)->extraResponseData_len))
923 else if (match_element(ptr, "record"))
924 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
926 else if (match_element(ptr, "diagnostics"))
927 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
928 &res->num_diagnostics,
933 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
935 Z_SRW_scanRequest *req;
936 xmlNodePtr ptr = method->children;
938 (*p)->which = Z_SRW_scan_request;
939 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
940 odr_malloc(o, sizeof(*req));
941 req->query_type = Z_SRW_query_type_cql;
942 req->scanClause.cql = 0;
943 req->responsePosition = 0;
944 req->maximumTerms = 0;
948 for (; ptr; ptr = ptr->next)
950 if (match_xsd_string(ptr, "version", o,
953 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
954 &(*p)->extraResponseData_buf,
955 &(*p)->extraResponseData_len))
957 else if (match_xsd_string(ptr, "scanClause", o,
958 &req->scanClause.cql))
960 else if (match_xsd_string(ptr, "pScanClause", o,
961 &req->scanClause.pqf))
963 req->query_type = Z_SRW_query_type_pqf;
965 else if (match_xsd_integer(ptr, "responsePosition", o,
966 &req->responsePosition))
968 else if (match_xsd_integer(ptr, "maximumTerms", o,
971 else if (match_xsd_string(ptr, "stylesheet", o,
975 match_xsd_string(ptr, "database", o, &req->database);
978 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
980 Z_SRW_scanResponse *res;
981 xmlNodePtr ptr = method->children;
983 (*p)->which = Z_SRW_scan_response;
984 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
985 odr_malloc(o, sizeof(*res));
988 res->diagnostics = 0;
989 res->num_diagnostics = 0;
991 for (; ptr; ptr = ptr->next)
993 if (match_xsd_string(ptr, "version", o,
996 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
997 &(*p)->extraResponseData_buf,
998 &(*p)->extraResponseData_len))
1000 else if (match_element(ptr, "terms"))
1001 yaz_srw_terms(o, ptr, &res->terms,
1002 &res->num_terms, client_data,
1004 else if (match_element(ptr, "diagnostics"))
1005 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1006 &res->num_diagnostics,
1015 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
1017 (*p)->srw_version = neg_version;
1019 else if (o->direction == ODR_ENCODE)
1021 Z_SRW_PDU **p = handler_data;
1025 if ((*p)->which == Z_SRW_searchRetrieve_request)
1027 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
1028 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
1029 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1030 xmlSetNs(ptr, ns_srw);
1032 if ((*p)->srw_version)
1033 add_xsd_string(ptr, "version", (*p)->srw_version);
1034 switch (req->query_type)
1036 case Z_SRW_query_type_cql:
1037 add_xsd_string(ptr, "query", req->query.cql);
1039 case Z_SRW_query_type_xcql:
1040 add_xsd_string(ptr, "xQuery", req->query.xcql);
1042 case Z_SRW_query_type_pqf:
1043 add_xsd_string(ptr, "pQuery", req->query.pqf);
1046 add_xsd_integer(ptr, "startRecord", req->startRecord);
1047 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
1048 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1049 add_xsd_string(ptr, "recordSchema", req->recordSchema);
1050 add_xsd_string(ptr, "recordXPath", req->recordXPath);
1051 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
1052 switch (req->sort_type)
1054 case Z_SRW_sort_type_none:
1056 case Z_SRW_sort_type_sort:
1057 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
1059 case Z_SRW_sort_type_xSort:
1060 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
1063 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1064 add_xsd_string(ptr, "database", req->database);
1066 else if ((*p)->which == Z_SRW_searchRetrieve_response)
1068 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
1069 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
1070 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1071 xmlSetNs(ptr, ns_srw);
1073 if ((*p)->srw_version)
1074 add_xsd_string(ptr, "version", (*p)->srw_version);
1075 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1076 add_xsd_string(ptr, "resultSetId", res->resultSetId);
1077 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
1078 if (res->num_records)
1080 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1081 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1085 add_xsd_integer(ptr, "nextRecordPosition",
1086 res->nextRecordPosition);
1087 if (res->num_diagnostics)
1089 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1091 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1092 &res->num_diagnostics, client_data, ns);
1095 else if ((*p)->which == Z_SRW_explain_request)
1097 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1098 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1099 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1100 xmlSetNs(ptr, ns_srw);
1102 add_xsd_string(ptr, "version", (*p)->srw_version);
1103 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1104 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1105 add_xsd_string(ptr, "database", req->database);
1107 else if ((*p)->which == Z_SRW_explain_response)
1109 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1110 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1111 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1112 xmlSetNs(ptr, ns_srw);
1114 add_xsd_string(ptr, "version", (*p)->srw_version);
1117 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1118 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1121 if (res->num_diagnostics)
1123 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1125 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1126 &res->num_diagnostics, client_data, ns);
1129 else if ((*p)->which == Z_SRW_scan_request)
1131 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1132 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1133 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1134 xmlSetNs(ptr, ns_srw);
1136 add_xsd_string(ptr, "version", (*p)->srw_version);
1137 switch (req->query_type)
1139 case Z_SRW_query_type_cql:
1140 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
1142 case Z_SRW_query_type_pqf:
1143 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
1146 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1147 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1148 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1149 add_xsd_string(ptr, "database", req->database);
1151 else if ((*p)->which == Z_SRW_scan_response)
1153 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1154 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1155 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1156 xmlSetNs(ptr, ns_srw);
1158 add_xsd_string(ptr, "version", (*p)->srw_version);
1162 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1163 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1166 if (res->num_diagnostics)
1168 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1170 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1171 &res->num_diagnostics, client_data, ns);
1176 if (ptr && (*p)->extraResponseData_len)
1177 add_XML_n(ptr, "extraResponseData",
1178 (*p)->extraResponseData_buf,
1179 (*p)->extraResponseData_len, ns_srw);
1186 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1187 void *client_data, const char *ns_ucp_str)
1189 xmlNodePtr pptr = (xmlNodePtr) vptr;
1190 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1191 if (o->direction == ODR_DECODE)
1193 Z_SRW_PDU **p = handler_data;
1194 xmlNodePtr method = pptr->children;
1196 while (method && method->type == XML_TEXT_NODE)
1197 method = method->next;
1201 if (method->type != XML_ELEMENT_NODE)
1204 *p = yaz_srw_get_core_v_1_1(o);
1206 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1208 xmlNodePtr ptr = method->children;
1209 Z_SRW_updateRequest *req;
1212 (*p)->which = Z_SRW_update_request;
1213 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1214 odr_malloc(o, sizeof(*req));
1218 req->recordVersions = 0;
1219 req->num_recordVersions = 0;
1221 req->extra_record = 0;
1222 req->extraRequestData_buf = 0;
1223 req->extraRequestData_len = 0;
1224 req->stylesheet = 0;
1226 for (; ptr; ptr = ptr->next)
1228 if (match_xsd_string(ptr, "version", o,
1229 &(*p)->srw_version))
1231 else if (match_xsd_string(ptr, "action", o,
1235 if (!strcmp(oper, "info:srw/action/1/delete"))
1236 req->operation = "delete";
1237 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1238 req->operation = "replace";
1239 else if (!strcmp(oper, "info:srw/action/1/create"))
1240 req->operation = "insert";
1243 else if (match_xsd_string(ptr, "recordIdentifier", o,
1246 else if (match_element(ptr, "recordVersions" ) )
1247 yaz_srw_versions( o, ptr, &req->recordVersions,
1248 &req->num_recordVersions, client_data,
1250 else if (match_element(ptr, "record"))
1252 req->record = yaz_srw_get_record(o);
1253 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1254 client_data, ns_ucp_str);
1256 else if (match_xsd_string(ptr, "stylesheet", o,
1260 match_xsd_string(ptr, "database", o, &req->database);
1263 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1265 xmlNodePtr ptr = method->children;
1266 Z_SRW_updateResponse *res;
1268 (*p)->which = Z_SRW_update_response;
1269 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1270 odr_malloc(o, sizeof(*res));
1272 res->operationStatus = 0;
1274 res->recordVersions = 0;
1275 res->num_recordVersions = 0;
1276 res->diagnostics = 0;
1277 res->num_diagnostics = 0;
1279 res->extra_record = 0;
1280 res->extraResponseData_buf = 0;
1281 res->extraResponseData_len = 0;
1283 for (; ptr; ptr = ptr->next)
1285 if (match_xsd_string(ptr, "version", o,
1286 &(*p)->srw_version))
1288 else if (match_xsd_string(ptr, "operationStatus", o,
1289 &res->operationStatus ))
1291 else if (match_xsd_string(ptr, "recordIdentifier", o,
1294 else if (match_element(ptr, "recordVersions" ))
1295 yaz_srw_versions(o, ptr, &res->recordVersions,
1296 &res->num_recordVersions,
1297 client_data, ns_ucp_str);
1298 else if (match_element(ptr, "record"))
1300 res->record = yaz_srw_get_record(o);
1301 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1302 client_data, ns_ucp_str);
1304 else if (match_element(ptr, "diagnostics"))
1305 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1306 &res->num_diagnostics,
1307 client_data, ns_ucp_str);
1310 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1313 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1322 else if (o->direction == ODR_ENCODE)
1324 Z_SRW_PDU **p = handler_data;
1325 xmlNsPtr ns_ucp, ns_srw;
1327 if ((*p)->which == Z_SRW_update_request)
1329 Z_SRW_updateRequest *req = (*p)->u.update_request;
1330 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1331 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1332 xmlSetNs(ptr, ns_ucp);
1333 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1335 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1336 add_xsd_string(ptr, "action", req->operation);
1337 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1338 if (req->recordVersions)
1339 yaz_srw_versions( o, ptr, &req->recordVersions,
1340 &req->num_recordVersions,
1341 client_data, ns_ucp_str);
1342 if (req->record && req->record->recordData_len)
1344 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1345 xmlSetNs(rptr, ns_srw);
1346 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1347 client_data, ns_ucp_str);
1349 if (req->extraRequestData_len)
1351 add_XML_n(ptr, "extraRequestData",
1352 req->extraRequestData_buf,
1353 req->extraRequestData_len, ns_srw);
1355 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1356 add_xsd_string(ptr, "database", req->database);
1358 else if ((*p)->which == Z_SRW_update_response)
1360 Z_SRW_updateResponse *res = (*p)->u.update_response;
1361 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1362 "updateResponse", 0);
1363 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1364 xmlSetNs(ptr, ns_ucp);
1365 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1367 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1368 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1369 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1370 if (res->recordVersions)
1371 yaz_srw_versions(o, ptr, &res->recordVersions,
1372 &res->num_recordVersions,
1373 client_data, ns_ucp_str);
1374 if (res->record && res->record->recordData_len)
1376 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1377 xmlSetNs(rptr, ns_srw);
1378 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1379 client_data, ns_ucp_str);
1381 if (res->num_diagnostics)
1384 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1387 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1388 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1389 &res->num_diagnostics, client_data,
1392 if (res->extraResponseData_len)
1393 add_XML_n(ptr, "extraResponseData",
1394 res->extraResponseData_buf,
1395 res->extraResponseData_len, ns_srw);
1409 * c-file-style: "Stroustrup"
1410 * indent-tabs-mode: nil
1412 * vim: shiftwidth=4 tabstop=8 expandtab