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)
224 if (!strcmp(input_ver, "1.1"))
226 if (!strncmp(input_ver, "1.", 2))
228 if (!strncmp(input_ver, "2.", 2))
233 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
234 Z_SRW_extra_record **extra,
235 void *client_data, int version2)
237 if (o->direction == ODR_DECODE)
239 Z_SRW_extra_record ex;
246 rec->recordSchema = 0;
247 rec->recordData_buf = 0;
248 rec->recordData_len = 0;
249 rec->recordPosition = 0;
252 ex.extraRecordData_buf = 0;
253 ex.extraRecordData_len = 0;
254 ex.recordIdentifier = 0;
256 for (ptr = pptr->children; ptr; ptr = ptr->next)
259 if (match_xsd_string(ptr, "recordSchema", o,
262 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
263 ; /* can't rely on it: in SRU 2.0 it's different semantics */
264 else if (match_xsd_integer(ptr, "recordPosition", o,
265 &rec->recordPosition))
267 else if (match_element(ptr, "recordData"))
269 /* we assume XML packing, if any element nodes exist below
270 recordData. Unfortunately, in SRU 2.0 recordPacking
271 means something different */
272 xmlNode *p = ptr->children;
273 for (; p; p = p->next)
274 if (p->type == XML_ELEMENT_NODE)
279 ptr, "recordData", o,
280 &rec->recordData_buf, &rec->recordData_len, 1);
281 rec->recordPacking = Z_SRW_recordPacking_XML;
286 ptr, "recordData", o,
287 &rec->recordData_buf, &rec->recordData_len);
288 rec->recordPacking = Z_SRW_recordPacking_string;
291 else if (match_xsd_XML_n(ptr, "extraRecordData", o,
292 &ex.extraRecordData_buf,
293 &ex.extraRecordData_len) )
296 match_xsd_string(ptr, "recordIdentifier", o,
297 &ex.recordIdentifier);
299 if (ex.extraRecordData_buf || ex.recordIdentifier)
301 *extra = (Z_SRW_extra_record *)
302 odr_malloc(o, sizeof(Z_SRW_extra_record));
303 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
306 else if (o->direction == ODR_ENCODE)
308 xmlNodePtr ptr = pptr;
309 int pack = rec->recordPacking;
310 const char *spack = yaz_srw_pack_to_str(pack);
312 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
316 add_xsd_string(ptr, "recordXMLEscaping", spack);
318 add_xsd_string(ptr, "recordPacking", spack);
322 case Z_SRW_recordPacking_string:
323 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
324 rec->recordData_len);
326 case Z_SRW_recordPacking_XML:
327 add_XML_n(ptr, "recordData", rec->recordData_buf,
328 rec->recordData_len, 0);
330 case Z_SRW_recordPacking_URL:
331 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
332 rec->recordData_len);
335 if (rec->recordPosition)
336 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
339 if ((*extra)->recordIdentifier)
340 add_xsd_string(ptr, "recordIdentifier",
341 (*extra)->recordIdentifier);
342 if ((*extra)->extraRecordData_buf)
343 add_XML_n(ptr, "extraRecordData",
344 (*extra)->extraRecordData_buf,
345 (*extra)->extraRecordData_len, 0);
351 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
352 Z_SRW_extra_record ***extra,
353 int *num, void *client_data, int version2)
355 if (o->direction == ODR_DECODE)
360 for (ptr = pptr->children; ptr; ptr = ptr->next)
362 if (ptr->type == XML_ELEMENT_NODE &&
363 !xmlStrcmp(ptr->name, BAD_CAST "record"))
368 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
369 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
370 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
372 if (ptr->type == XML_ELEMENT_NODE &&
373 !xmlStrcmp(ptr->name, BAD_CAST "record"))
375 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, 0);
380 else if (o->direction == ODR_ENCODE)
383 for (i = 0; i < *num; i++)
385 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
387 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
388 client_data, version2);
394 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
395 void *client_data, const char *ns)
397 if (o->direction == ODR_DECODE)
400 rec->versionType = 0;
401 rec->versionValue = 0;
402 for (ptr = pptr->children; ptr; ptr = ptr->next)
405 if (match_xsd_string(ptr, "versionType", o,
409 match_xsd_string(ptr, "versionValue", o, &rec->versionValue);
412 else if (o->direction == ODR_ENCODE)
414 xmlNodePtr ptr = pptr;
415 add_xsd_string(ptr, "versionType", rec->versionType);
416 add_xsd_string(ptr, "versionValue", rec->versionValue);
421 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
422 Z_SRW_recordVersion **vers,
423 int *num, void *client_data, const char *ns)
425 if (o->direction == ODR_DECODE)
430 for (ptr = pptr->children; ptr; ptr = ptr->next)
432 if (ptr->type == XML_ELEMENT_NODE &&
433 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
438 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
439 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
441 if (ptr->type == XML_ELEMENT_NODE &&
442 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
444 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
449 else if (o->direction == ODR_ENCODE)
452 for (i = 0; i < *num; i++)
454 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
456 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
462 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
466 WRBUF wrbuf = wrbuf_alloc();
467 Z_FacetTerm *facet_term;
468 const char *freq_string = yaz_element_attribute_value_get(
469 node, "facetvalue", "est_representation");
471 freq = odr_atoi(freq_string);
475 for (child = node->children; child ; child = child->next)
477 if (child->type == XML_TEXT_NODE)
478 wrbuf_puts(wrbuf, (const char *) child->content);
480 facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
481 wrbuf_destroy(wrbuf);
485 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
487 Z_AttributeList *list;
488 Z_FacetField *facet_field;
493 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
494 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
496 list = yaz_use_attribute_create(odr, name);
497 for (node = ptr->children; node; node = node->next) {
498 if (match_element(node, "facetvalue"))
501 facet_field = facet_field_create(odr, list, num_terms);
503 for (node = ptr->children; node; node = node->next)
505 if (match_element(node, "facetvalue"))
507 facet_field_term_set(odr, facet_field,
508 yaz_sru_proxy_get_facet_term_count(odr, node),
516 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
517 Z_FacetList **facetList)
521 for (ptr = root->children; ptr; ptr = ptr->next)
523 if (match_element(ptr, "facets"))
526 Z_FacetList *facet_list;
528 for (node = ptr->children; node; node= node->next)
530 if (node->type == XML_ELEMENT_NODE)
533 facet_list = facet_list_create(o, num_facets);
535 for (node = ptr->children; node; node= node->next)
537 if (match_element(node, "facet"))
539 facet_list_field_set(
541 yaz_sru_proxy_decode_facet_field(o, node), num_facets);
545 *facetList = facet_list;
554 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
555 Z_SRW_diagnostic **recs, int *num,
556 void *client_data, const char *ns)
561 for (ptr = pptr; ptr; ptr = ptr->next)
563 if (ptr->type == XML_ELEMENT_NODE &&
564 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
569 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
570 for (i = 0; i < *num; i++)
573 (*recs)[i].details = 0;
574 (*recs)[i].message = 0;
576 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
578 if (ptr->type == XML_ELEMENT_NODE &&
579 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
583 (*recs)[i].details = 0;
584 (*recs)[i].message = 0;
585 for (rptr = ptr->children; rptr; rptr = rptr->next)
587 if (match_xsd_string(rptr, "uri", o,
590 else if (match_xsd_string(rptr, "details", o,
591 &(*recs)[i].details))
594 match_xsd_string(rptr, "message", o, &(*recs)[i].message);
602 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
603 Z_SRW_diagnostic **diag,
607 xmlDocPtr doc = xmlParseMemory(buf, len);
610 xmlNodePtr ptr = xmlDocGetRootElement(doc);
611 while (ptr && ptr->type != XML_ELEMENT_NODE)
614 && !xmlStrcmp(ptr->ns->href,
615 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
617 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
624 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
625 int *num, void *client_data, const char *ns)
627 if (o->direction == ODR_DECODE)
629 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
631 else if (o->direction == ODR_ENCODE)
635 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
636 for (i = 0; i < *num; i++)
638 const char *std_diag = "info:srw/diagnostic/1/";
639 const char *ucp_diag = "info:srw/diagnostic/12/";
640 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
641 BAD_CAST "diagnostic", 0);
642 add_xsd_string(rptr, "uri", (*recs)[i].uri);
643 if ((*recs)[i].message)
644 add_xsd_string(rptr, "message", (*recs)[i].message);
645 else if ((*recs)[i].uri )
647 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
649 int no = atoi((*recs)[i].uri + strlen(std_diag));
650 const char *message = yaz_diag_srw_str(no);
652 add_xsd_string(rptr, "message", message);
654 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
656 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
657 const char *message = yaz_diag_sru_update_str(no);
659 add_xsd_string(rptr, "message", message);
662 add_xsd_string(rptr, "details", (*recs)[i].details);
668 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
669 void *client_data, const char *ns)
671 if (o->direction == ODR_DECODE)
675 term->numberOfRecords = 0;
676 term->displayTerm = 0;
677 term->whereInList = 0;
678 for (ptr = pptr->children; ptr; ptr = ptr->next)
680 if (match_xsd_string(ptr, "value", o, &term->value))
682 else if (match_xsd_integer(ptr, "numberOfRecords", o,
683 &term->numberOfRecords))
685 else if (match_xsd_string(ptr, "displayTerm", o,
689 match_xsd_string(ptr, "whereInList", o, &term->whereInList);
692 else if (o->direction == ODR_ENCODE)
694 xmlNodePtr ptr = pptr;
695 add_xsd_string(ptr, "value", term->value);
696 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
697 add_xsd_string(ptr, "displayTerm", term->displayTerm);
698 add_xsd_string(ptr, "whereInList", term->whereInList);
703 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
704 int *num, void *client_data, const char *ns)
706 if (o->direction == ODR_DECODE)
711 for (ptr = pptr->children; ptr; ptr = ptr->next)
713 if (ptr->type == XML_ELEMENT_NODE &&
714 !xmlStrcmp(ptr->name, BAD_CAST "term"))
719 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
720 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
722 if (ptr->type == XML_ELEMENT_NODE &&
723 !xmlStrcmp(ptr->name, BAD_CAST "term"))
724 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
727 else if (o->direction == ODR_ENCODE)
730 for (i = 0; i < *num; i++)
732 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
733 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
739 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
740 void *client_data, const char *ns)
742 xmlNodePtr pptr = (xmlNodePtr) vptr;
743 if (o->direction == ODR_DECODE)
745 Z_SRW_PDU **p = handler_data;
746 xmlNodePtr method = pptr->children;
749 while (method && method->type == XML_TEXT_NODE)
750 method = method->next;
754 if (method->type != XML_ELEMENT_NODE)
757 *p = yaz_srw_get_core_v_2_0(o);
759 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
761 xmlNodePtr ptr = method->children;
762 Z_SRW_searchRetrieveRequest *req;
763 char *recordPacking = 0;
764 char *recordXMLEscaping = 0;
766 (*p)->which = Z_SRW_searchRetrieve_request;
767 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
768 odr_malloc(o, sizeof(*req));
769 req->queryType = "cql";
771 req->sort_type = Z_SRW_sort_type_none;
773 req->startRecord = 0;
774 req->maximumRecords = 0;
775 req->recordSchema = 0;
776 req->recordPacking = 0;
778 req->recordXPath = 0;
779 req->resultSetTTL = 0;
783 for (; ptr; ptr = ptr->next)
785 if (match_xsd_string(ptr, "version", o,
788 else if (match_xsd_string(ptr, "queryType", o,
791 else if (match_xsd_string(ptr, "query", o,
794 else if (match_xsd_string(ptr, "pQuery", o,
796 req->queryType = "pqf";
797 else if (match_xsd_string(ptr, "xQuery", o,
799 req->queryType = "xcql";
800 else if (match_xsd_integer(ptr, "startRecord", o,
803 else if (match_xsd_integer(ptr, "maximumRecords", o,
804 &req->maximumRecords))
806 else if (match_xsd_string(ptr, "recordPacking", o,
809 else if (match_xsd_string(ptr, "recordXMLEscaping", o,
812 else if (match_xsd_string(ptr, "recordSchema", o,
815 else if (match_xsd_string(ptr, "recordXPath", o,
818 else if (match_xsd_integer(ptr, "resultSetTTL", o,
821 else if (match_xsd_string(ptr, "sortKeys", o,
822 &req->sort.sortKeys))
823 req->sort_type = Z_SRW_sort_type_sort;
824 else if (match_xsd_string(ptr, "stylesheet", o,
828 match_xsd_string(ptr, "database", o, &req->database);
832 /* should put proper diagnostic here */
835 if (!strcmp((*p)->srw_version, "2.0"))
837 req->recordPacking = recordXMLEscaping;
838 req->packing = recordPacking;
842 req->recordPacking = recordPacking;
845 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
847 xmlNodePtr ptr = method->children;
848 Z_SRW_searchRetrieveResponse *res;
850 (*p)->which = Z_SRW_searchRetrieve_response;
851 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
852 odr_malloc(o, sizeof(*res));
854 res->numberOfRecords = 0;
855 res->resultSetId = 0;
856 res->resultSetIdleTime = 0;
858 res->num_records = 0;
859 res->diagnostics = 0;
860 res->num_diagnostics = 0;
861 res->nextRecordPosition = 0;
863 res->suggestions = 0;
865 for (; ptr; ptr = ptr->next)
867 if (match_xsd_string(ptr, "version", o,
870 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
871 &(*p)->extraResponseData_buf,
872 &(*p)->extraResponseData_len))
874 else if (match_xsd_integer(ptr, "numberOfRecords", o,
875 &res->numberOfRecords))
877 else if (match_xsd_string(ptr, "resultSetId", o,
880 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
881 &res->resultSetIdleTime))
883 else if (match_element(ptr, "records"))
884 yaz_srw_records(o, ptr, &res->records,
886 &res->num_records, client_data, 0);
887 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
888 &res->nextRecordPosition))
890 else if (match_element(ptr, "diagnostics"))
891 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
892 &res->num_diagnostics,
894 else if (match_element(ptr, "facet_analysis"))
895 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
898 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
900 Z_SRW_explainRequest *req;
901 xmlNodePtr ptr = method->children;
903 (*p)->which = Z_SRW_explain_request;
904 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
905 odr_malloc(o, sizeof(*req));
906 req->recordPacking = 0;
910 for (; ptr; ptr = ptr->next)
912 if (match_xsd_string(ptr, "version", o,
915 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
916 &(*p)->extraResponseData_buf,
917 &(*p)->extraResponseData_len))
919 else if (match_xsd_string(ptr, "stylesheet", o,
922 else if (match_xsd_string(ptr, "recordPacking", o,
923 &req->recordPacking))
926 match_xsd_string(ptr, "database", o, &req->database);
929 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
931 Z_SRW_explainResponse *res;
932 xmlNodePtr ptr = method->children;
934 (*p)->which = Z_SRW_explain_response;
935 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
936 odr_malloc(o, sizeof(*res));
937 res->diagnostics = 0;
938 res->num_diagnostics = 0;
939 res->record.recordSchema = 0;
940 res->record.recordData_buf = 0;
941 res->record.recordData_len = 0;
942 res->record.recordPosition = 0;
944 for (; ptr; ptr = ptr->next)
946 if (match_xsd_string(ptr, "version", o,
949 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
950 &(*p)->extraResponseData_buf,
951 &(*p)->extraResponseData_len))
953 else if (match_element(ptr, "record"))
954 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
956 else if (match_element(ptr, "diagnostics"))
957 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
958 &res->num_diagnostics,
963 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
965 Z_SRW_scanRequest *req;
966 xmlNodePtr ptr = method->children;
968 (*p)->which = Z_SRW_scan_request;
969 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
970 odr_malloc(o, sizeof(*req));
971 req->queryType = "cql";
973 req->responsePosition = 0;
974 req->maximumTerms = 0;
978 for (; ptr; ptr = ptr->next)
980 if (match_xsd_string(ptr, "version", o,
983 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
984 &(*p)->extraResponseData_buf,
985 &(*p)->extraResponseData_len))
987 else if (match_xsd_string(ptr, "scanClause", o,
990 else if (match_xsd_string(ptr, "pScanClause", o,
993 req->queryType = "pqf";
995 else if (match_xsd_integer(ptr, "responsePosition", o,
996 &req->responsePosition))
998 else if (match_xsd_integer(ptr, "maximumTerms", o,
1001 else if (match_xsd_string(ptr, "stylesheet", o,
1005 match_xsd_string(ptr, "database", o, &req->database);
1008 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
1010 Z_SRW_scanResponse *res;
1011 xmlNodePtr ptr = method->children;
1013 (*p)->which = Z_SRW_scan_response;
1014 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
1015 odr_malloc(o, sizeof(*res));
1018 res->diagnostics = 0;
1019 res->num_diagnostics = 0;
1021 for (; ptr; ptr = ptr->next)
1023 if (match_xsd_string(ptr, "version", o,
1024 &(*p)->srw_version))
1026 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
1027 &(*p)->extraResponseData_buf,
1028 &(*p)->extraResponseData_len))
1030 else if (match_element(ptr, "terms"))
1031 yaz_srw_terms(o, ptr, &res->terms,
1032 &res->num_terms, client_data,
1034 else if (match_element(ptr, "diagnostics"))
1035 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1036 &res->num_diagnostics,
1045 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
1047 (*p)->srw_version = neg_version;
1049 else if (o->direction == ODR_ENCODE)
1051 Z_SRW_PDU **p = handler_data;
1054 int version2 = !(*p)->srw_version ||
1055 strcmp((*p)->srw_version, "2.") > 0;
1056 if ((*p)->which == Z_SRW_searchRetrieve_request)
1058 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
1059 const char *queryType = req->queryType;
1061 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
1062 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
1063 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1064 xmlSetNs(ptr, ns_srw);
1067 add_xsd_string(ptr, "version", (*p)->srw_version);
1072 add_xsd_string(ptr, "queryType", queryType);
1073 add_xsd_string(ptr, "query", req->query);
1077 if (!queryType || !strcmp(queryType, "cql"))
1078 add_xsd_string(ptr, "query", req->query);
1079 else if (!strcmp(queryType, "xcql"))
1080 add_xsd_string(ptr, "xQuery", req->query);
1081 else if (!strcmp(queryType, "pqf"))
1082 add_xsd_string(ptr, "pQuery", req->query);
1084 add_xsd_integer(ptr, "startRecord", req->startRecord);
1085 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
1088 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1089 add_xsd_string(ptr, "recordPacking", req->packing);
1092 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1093 add_xsd_string(ptr, "recordSchema", req->recordSchema);
1094 add_xsd_string(ptr, "recordXPath", req->recordXPath);
1095 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
1096 switch (req->sort_type)
1098 case Z_SRW_sort_type_none:
1100 case Z_SRW_sort_type_sort:
1101 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
1103 case Z_SRW_sort_type_xSort:
1104 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
1107 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1108 add_xsd_string(ptr, "database", req->database);
1110 else if ((*p)->which == Z_SRW_searchRetrieve_response)
1112 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
1114 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
1115 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
1116 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1117 xmlSetNs(ptr, ns_srw);
1120 add_xsd_string(ptr, "version", (*p)->srw_version);
1121 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1122 add_xsd_string(ptr, "resultSetId", res->resultSetId);
1123 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
1124 if (res->num_records)
1126 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1127 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1129 client_data, version2);
1131 add_xsd_integer(ptr, "nextRecordPosition",
1132 res->nextRecordPosition);
1133 if (res->num_diagnostics)
1135 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1137 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1138 &res->num_diagnostics, client_data, ns);
1141 else if ((*p)->which == Z_SRW_explain_request)
1143 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1144 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1145 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1146 xmlSetNs(ptr, ns_srw);
1149 add_xsd_string(ptr, "version", (*p)->srw_version);
1152 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1153 add_xsd_string(ptr, "recordPacking", req->packing);
1156 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1157 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1158 add_xsd_string(ptr, "database", req->database);
1160 else if ((*p)->which == Z_SRW_explain_response)
1162 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1163 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1164 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1165 xmlSetNs(ptr, ns_srw);
1167 add_xsd_string(ptr, "version", (*p)->srw_version);
1170 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1171 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1172 client_data, version2);
1174 if (res->num_diagnostics)
1176 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1178 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1179 &res->num_diagnostics, client_data, ns);
1182 else if ((*p)->which == Z_SRW_scan_request)
1184 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1185 const char *queryType = req->queryType;
1187 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1188 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1189 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1190 xmlSetNs(ptr, ns_srw);
1193 add_xsd_string(ptr, "version", (*p)->srw_version);
1195 if (strcmp((*p)->srw_version, "2.") > 0)
1197 if (queryType && strcmp(queryType, "cql"))
1198 add_xsd_string(ptr, "queryType", queryType);
1199 add_xsd_string(ptr, "scanClause", req->scanClause);
1203 if (!queryType || !strcmp(queryType, "cql"))
1204 add_xsd_string(ptr, "scanClause", req->scanClause);
1205 else if (!strcmp(queryType, "pqf"))
1206 add_xsd_string(ptr, "pScanClause", req->scanClause);
1208 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1209 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1210 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1211 add_xsd_string(ptr, "database", req->database);
1213 else if ((*p)->which == Z_SRW_scan_response)
1215 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1217 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1218 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1219 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1220 xmlSetNs(ptr, ns_srw);
1223 add_xsd_string(ptr, "version", (*p)->srw_version);
1227 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1228 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1231 if (res->num_diagnostics)
1233 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1235 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1236 &res->num_diagnostics, client_data, ns);
1241 if (ptr && (*p)->extraResponseData_len)
1242 add_XML_n(ptr, "extraResponseData",
1243 (*p)->extraResponseData_buf,
1244 (*p)->extraResponseData_len, ns_srw);
1251 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1252 void *client_data, const char *ns_ucp_str)
1254 xmlNodePtr pptr = (xmlNodePtr) vptr;
1255 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1256 if (o->direction == ODR_DECODE)
1258 Z_SRW_PDU **p = handler_data;
1259 xmlNodePtr method = pptr->children;
1261 while (method && method->type == XML_TEXT_NODE)
1262 method = method->next;
1266 if (method->type != XML_ELEMENT_NODE)
1269 *p = yaz_srw_get_core_v_2_0(o);
1271 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1273 xmlNodePtr ptr = method->children;
1274 Z_SRW_updateRequest *req;
1277 (*p)->which = Z_SRW_update_request;
1278 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1279 odr_malloc(o, sizeof(*req));
1283 req->recordVersions = 0;
1284 req->num_recordVersions = 0;
1286 req->extra_record = 0;
1287 req->extraRequestData_buf = 0;
1288 req->extraRequestData_len = 0;
1289 req->stylesheet = 0;
1291 for (; ptr; ptr = ptr->next)
1293 if (match_xsd_string(ptr, "version", o,
1294 &(*p)->srw_version))
1296 else if (match_xsd_string(ptr, "action", o,
1300 if (!strcmp(oper, "info:srw/action/1/delete"))
1301 req->operation = "delete";
1302 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1303 req->operation = "replace";
1304 else if (!strcmp(oper, "info:srw/action/1/create"))
1305 req->operation = "insert";
1308 else if (match_xsd_string(ptr, "recordIdentifier", o,
1311 else if (match_element(ptr, "recordVersions" ) )
1312 yaz_srw_versions( o, ptr, &req->recordVersions,
1313 &req->num_recordVersions, client_data,
1315 else if (match_element(ptr, "record"))
1317 req->record = yaz_srw_get_record(o);
1318 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1321 else if (match_xsd_string(ptr, "stylesheet", o,
1325 match_xsd_string(ptr, "database", o, &req->database);
1328 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1330 xmlNodePtr ptr = method->children;
1331 Z_SRW_updateResponse *res;
1333 (*p)->which = Z_SRW_update_response;
1334 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1335 odr_malloc(o, sizeof(*res));
1337 res->operationStatus = 0;
1339 res->recordVersions = 0;
1340 res->num_recordVersions = 0;
1341 res->diagnostics = 0;
1342 res->num_diagnostics = 0;
1344 res->extra_record = 0;
1345 res->extraResponseData_buf = 0;
1346 res->extraResponseData_len = 0;
1348 for (; ptr; ptr = ptr->next)
1350 if (match_xsd_string(ptr, "version", o,
1351 &(*p)->srw_version))
1353 else if (match_xsd_string(ptr, "operationStatus", o,
1354 &res->operationStatus ))
1356 else if (match_xsd_string(ptr, "recordIdentifier", o,
1359 else if (match_element(ptr, "recordVersions" ))
1360 yaz_srw_versions(o, ptr, &res->recordVersions,
1361 &res->num_recordVersions,
1362 client_data, ns_ucp_str);
1363 else if (match_element(ptr, "record"))
1365 res->record = yaz_srw_get_record(o);
1366 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1369 else if (match_element(ptr, "diagnostics"))
1370 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1371 &res->num_diagnostics,
1372 client_data, ns_ucp_str);
1375 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1378 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1387 else if (o->direction == ODR_ENCODE)
1389 Z_SRW_PDU **p = handler_data;
1390 xmlNsPtr ns_ucp, ns_srw;
1392 if ((*p)->which == Z_SRW_update_request)
1394 Z_SRW_updateRequest *req = (*p)->u.update_request;
1395 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1396 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1397 xmlSetNs(ptr, ns_ucp);
1398 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1400 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1401 add_xsd_string(ptr, "action", req->operation);
1402 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1403 if (req->recordVersions)
1404 yaz_srw_versions( o, ptr, &req->recordVersions,
1405 &req->num_recordVersions,
1406 client_data, ns_ucp_str);
1407 if (req->record && req->record->recordData_len)
1409 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1410 xmlSetNs(rptr, ns_srw);
1411 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1414 if (req->extraRequestData_len)
1416 add_XML_n(ptr, "extraRequestData",
1417 req->extraRequestData_buf,
1418 req->extraRequestData_len, ns_srw);
1420 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1421 add_xsd_string(ptr, "database", req->database);
1423 else if ((*p)->which == Z_SRW_update_response)
1425 Z_SRW_updateResponse *res = (*p)->u.update_response;
1426 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1427 "updateResponse", 0);
1428 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1429 xmlSetNs(ptr, ns_ucp);
1430 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1432 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1433 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1434 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1435 if (res->recordVersions)
1436 yaz_srw_versions(o, ptr, &res->recordVersions,
1437 &res->num_recordVersions,
1438 client_data, ns_ucp_str);
1439 if (res->record && res->record->recordData_len)
1441 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1442 xmlSetNs(rptr, ns_srw);
1443 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1446 if (res->num_diagnostics)
1449 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1452 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1453 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1454 &res->num_diagnostics, client_data,
1457 if (res->extraResponseData_len)
1458 add_XML_n(ptr, "extraResponseData",
1459 res->extraResponseData_buf,
1460 res->extraResponseData_len, ns_srw);
1474 * c-file-style: "Stroustrup"
1475 * indent-tabs-mode: nil
1477 * vim: shiftwidth=4 tabstop=8 expandtab