1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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_n(xmlNodePtr ptr, const char *elem, ODR o,
137 char **val, int *len)
141 if (!match_element(ptr, elem))
144 buf = xmlBufferCreate();
146 /* Copy each element nodes at top.
147 In most cases there is only one root node.. At least one server
148 http://www.theeuropeanlibrary.org/sru/sru.pl
149 has multiple root nodes in recordData.
151 for (ptr = ptr->children; ptr; ptr = ptr->next)
153 if (ptr->type == XML_ELEMENT_NODE)
155 /* copy node to get NS right (bug #740). */
156 xmlNode *tmp = xmlCopyNode(ptr, 1);
158 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
164 *val = (char *) odr_malloc(o, buf->use+1);
165 memcpy (*val, buf->content, buf->use);
166 (*val)[buf->use] = '\0';
176 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
180 struct _xmlAttr *attr;
182 if (!match_element(ptr, elem))
185 for (attr = ptr->properties; attr; attr = attr->next)
186 if (!strcmp(attr->name, "type") &&
187 attr->children && attr->children->type == XML_TEXT_NODE)
189 const char *t = strchr(attr->children->content, ':');
193 t = attr->children->content;
194 if (!strcmp(t, "integer"))
201 if (!ptr || ptr->type != XML_TEXT_NODE)
203 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
207 char *yaz_negotiate_sru_version(char *input_ver)
212 if (!strcmp(input_ver, "1.1"))
214 return "1.2"; /* our latest supported version */
217 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
218 Z_SRW_extra_record **extra,
219 void *client_data, const char *ns)
221 if (o->direction == ODR_DECODE)
223 Z_SRW_extra_record ex;
226 int pack = Z_SRW_recordPacking_string;
228 xmlNodePtr data_ptr = 0;
229 rec->recordSchema = 0;
230 rec->recordData_buf = 0;
231 rec->recordData_len = 0;
232 rec->recordPosition = 0;
235 ex.extraRecordData_buf = 0;
236 ex.extraRecordData_len = 0;
237 ex.recordIdentifier = 0;
239 for (ptr = pptr->children; ptr; ptr = ptr->next)
242 if (match_xsd_string(ptr, "recordSchema", o,
245 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
248 pack = yaz_srw_str_to_pack(spack);
250 else if (match_xsd_integer(ptr, "recordPosition", o,
251 &rec->recordPosition))
253 else if (match_element(ptr, "recordData"))
255 /* save position of Data until after the loop
256 then we will know the packing (hopefully), and
257 unpacking is done once
261 else if (match_xsd_XML_n(ptr, "extraRecordData", o,
262 &ex.extraRecordData_buf,
263 &ex.extraRecordData_len) )
266 match_xsd_string(ptr, "recordIdentifier", o,
267 &ex.recordIdentifier);
273 case Z_SRW_recordPacking_XML:
274 match_xsd_XML_n(data_ptr, "recordData", o,
275 &rec->recordData_buf, &rec->recordData_len);
277 case Z_SRW_recordPacking_URL:
278 /* just store it as a string.
279 leave it to the backend to collect the document */
280 match_xsd_string_n(data_ptr, "recordData", o,
281 &rec->recordData_buf, &rec->recordData_len);
283 case Z_SRW_recordPacking_string:
284 match_xsd_string_n(data_ptr, "recordData", o,
285 &rec->recordData_buf, &rec->recordData_len);
289 rec->recordPacking = pack;
290 if (ex.extraRecordData_buf || ex.recordIdentifier)
292 *extra = (Z_SRW_extra_record *)
293 odr_malloc(o, sizeof(Z_SRW_extra_record));
294 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
297 else if (o->direction == ODR_ENCODE)
299 xmlNodePtr ptr = pptr;
300 int pack = rec->recordPacking;
301 const char *spack = yaz_srw_pack_to_str(pack);
303 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
305 add_xsd_string(ptr, "recordPacking", spack);
308 case Z_SRW_recordPacking_string:
309 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
310 rec->recordData_len);
312 case Z_SRW_recordPacking_XML:
313 add_XML_n(ptr, "recordData", rec->recordData_buf,
314 rec->recordData_len, 0);
316 case Z_SRW_recordPacking_URL:
317 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
318 rec->recordData_len);
321 if (rec->recordPosition)
322 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
325 if ((*extra)->recordIdentifier)
326 add_xsd_string(ptr, "recordIdentifier",
327 (*extra)->recordIdentifier);
328 if ((*extra)->extraRecordData_buf)
329 add_XML_n(ptr, "extraRecordData",
330 (*extra)->extraRecordData_buf,
331 (*extra)->extraRecordData_len, 0);
337 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
338 Z_SRW_extra_record ***extra,
339 int *num, void *client_data, const char *ns)
341 if (o->direction == ODR_DECODE)
346 for (ptr = pptr->children; ptr; ptr = ptr->next)
348 if (ptr->type == XML_ELEMENT_NODE &&
349 !xmlStrcmp(ptr->name, BAD_CAST "record"))
354 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
355 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
356 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
358 if (ptr->type == XML_ELEMENT_NODE &&
359 !xmlStrcmp(ptr->name, BAD_CAST "record"))
361 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, ns);
366 else if (o->direction == ODR_ENCODE)
369 for (i = 0; i < *num; i++)
371 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
373 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
380 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
381 void *client_data, const char *ns)
383 if (o->direction == ODR_DECODE)
386 rec->versionType = 0;
387 rec->versionValue = 0;
388 for (ptr = pptr->children; ptr; ptr = ptr->next)
391 if (match_xsd_string(ptr, "versionType", o,
395 match_xsd_string(ptr, "versionValue", o, &rec->versionValue);
398 else if (o->direction == ODR_ENCODE)
400 xmlNodePtr ptr = pptr;
401 add_xsd_string(ptr, "versionType", rec->versionType);
402 add_xsd_string(ptr, "versionValue", rec->versionValue);
407 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
408 Z_SRW_recordVersion **vers,
409 int *num, void *client_data, const char *ns)
411 if (o->direction == ODR_DECODE)
416 for (ptr = pptr->children; ptr; ptr = ptr->next)
418 if (ptr->type == XML_ELEMENT_NODE &&
419 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
424 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
425 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
427 if (ptr->type == XML_ELEMENT_NODE &&
428 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
430 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
435 else if (o->direction == ODR_ENCODE)
438 for (i = 0; i < *num; i++)
440 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
442 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
448 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
452 WRBUF wrbuf = wrbuf_alloc();
453 const char *freq_string = yaz_element_attribute_value_get(node, "facetvalue", "est_representation");
456 freq = atoi(freq_string);
460 for (child = node->children; child ; child = child->next) {
461 if (child->type == XML_TEXT_NODE)
462 wrbuf_puts(wrbuf, (const char *) child->content);
464 term = term_create(odr, wrbuf_cstr(wrbuf));
465 yaz_log(YLOG_DEBUG, "sru-proxy facet: %s %d", wrbuf_cstr(wrbuf), freq);
466 wrbuf_destroy(wrbuf);
467 return facet_term_create(odr, term, freq);
470 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
472 Z_AttributeList *list;
473 Z_FacetField *facet_field;
478 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
479 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
481 list = yaz_use_attribute_create(odr, name);
482 for (node = ptr->children; node; node = node->next) {
483 if (match_element(node, "facetvalue"))
486 facet_field = facet_field_create(odr, list, num_terms);
488 for (node = ptr->children; node; node = node->next)
490 if (match_element(node, "facetvalue"))
492 facet_field_term_set(odr, facet_field, yaz_sru_proxy_get_facet_term_count(odr, node), index);
499 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
500 Z_FacetList **facetList)
504 for (ptr = root->children; ptr; ptr = ptr->next)
506 if (match_element(ptr, "facets"))
509 Z_FacetList *facet_list;
511 for (node = ptr->children; node; node= node->next)
513 if (node->type == XML_ELEMENT_NODE)
516 facet_list = facet_list_create(o, num_facets);
518 for (node = ptr->children; node; node= node->next)
520 if (match_element(node, "facet")) {
521 facet_list_field_set(o, facet_list, yaz_sru_proxy_decode_facet_field(o, node), num_facets);
525 *facetList = facet_list;
534 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
535 Z_SRW_diagnostic **recs, int *num,
536 void *client_data, const char *ns)
541 for (ptr = pptr; ptr; ptr = ptr->next)
543 if (ptr->type == XML_ELEMENT_NODE &&
544 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
549 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
550 for (i = 0; i < *num; i++)
553 (*recs)[i].details = 0;
554 (*recs)[i].message = 0;
556 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
558 if (ptr->type == XML_ELEMENT_NODE &&
559 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
563 (*recs)[i].details = 0;
564 (*recs)[i].message = 0;
565 for (rptr = ptr->children; rptr; rptr = rptr->next)
567 if (match_xsd_string(rptr, "uri", o,
570 else if (match_xsd_string(rptr, "details", o,
571 &(*recs)[i].details))
574 match_xsd_string(rptr, "message", o, &(*recs)[i].message);
582 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
583 Z_SRW_diagnostic **diag,
587 xmlDocPtr doc = xmlParseMemory(buf, len);
590 xmlNodePtr ptr = xmlDocGetRootElement(doc);
591 while (ptr && ptr->type != XML_ELEMENT_NODE)
594 && !xmlStrcmp(ptr->ns->href,
595 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
597 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
604 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
605 int *num, void *client_data, const char *ns)
607 if (o->direction == ODR_DECODE)
609 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
611 else if (o->direction == ODR_ENCODE)
615 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
616 for (i = 0; i < *num; i++)
618 const char *std_diag = "info:srw/diagnostic/1/";
619 const char *ucp_diag = "info:srw/diagnostic/12/";
620 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
621 BAD_CAST "diagnostic", 0);
622 add_xsd_string(rptr, "uri", (*recs)[i].uri);
623 if ((*recs)[i].message)
624 add_xsd_string(rptr, "message", (*recs)[i].message);
625 else if ((*recs)[i].uri )
627 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
629 int no = atoi((*recs)[i].uri + strlen(std_diag));
630 const char *message = yaz_diag_srw_str(no);
632 add_xsd_string(rptr, "message", message);
634 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
636 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
637 const char *message = yaz_diag_sru_update_str(no);
639 add_xsd_string(rptr, "message", message);
642 add_xsd_string(rptr, "details", (*recs)[i].details);
648 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
649 void *client_data, const char *ns)
651 if (o->direction == ODR_DECODE)
655 term->numberOfRecords = 0;
656 term->displayTerm = 0;
657 term->whereInList = 0;
658 for (ptr = pptr->children; ptr; ptr = ptr->next)
660 if (match_xsd_string(ptr, "value", o, &term->value))
662 else if (match_xsd_integer(ptr, "numberOfRecords", o,
663 &term->numberOfRecords))
665 else if (match_xsd_string(ptr, "displayTerm", o,
669 match_xsd_string(ptr, "whereInList", o, &term->whereInList);
672 else if (o->direction == ODR_ENCODE)
674 xmlNodePtr ptr = pptr;
675 add_xsd_string(ptr, "value", term->value);
676 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
677 add_xsd_string(ptr, "displayTerm", term->displayTerm);
678 add_xsd_string(ptr, "whereInList", term->whereInList);
683 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
684 int *num, void *client_data, const char *ns)
686 if (o->direction == ODR_DECODE)
691 for (ptr = pptr->children; ptr; ptr = ptr->next)
693 if (ptr->type == XML_ELEMENT_NODE &&
694 !xmlStrcmp(ptr->name, BAD_CAST "term"))
699 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
700 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
702 if (ptr->type == XML_ELEMENT_NODE &&
703 !xmlStrcmp(ptr->name, BAD_CAST "term"))
704 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
707 else if (o->direction == ODR_ENCODE)
710 for (i = 0; i < *num; i++)
712 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
713 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
719 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
720 void *client_data, const char *ns)
722 xmlNodePtr pptr = (xmlNodePtr) vptr;
723 if (o->direction == ODR_DECODE)
725 Z_SRW_PDU **p = handler_data;
726 xmlNodePtr method = pptr->children;
729 while (method && method->type == XML_TEXT_NODE)
730 method = method->next;
734 if (method->type != XML_ELEMENT_NODE)
737 *p = yaz_srw_get_core_v_1_1(o);
739 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
741 xmlNodePtr ptr = method->children;
742 Z_SRW_searchRetrieveRequest *req;
744 (*p)->which = Z_SRW_searchRetrieve_request;
745 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
746 odr_malloc(o, sizeof(*req));
747 req->query_type = Z_SRW_query_type_cql;
749 req->sort_type = Z_SRW_sort_type_none;
751 req->startRecord = 0;
752 req->maximumRecords = 0;
753 req->recordSchema = 0;
754 req->recordPacking = 0;
755 req->recordXPath = 0;
756 req->resultSetTTL = 0;
760 for (; ptr; ptr = ptr->next)
762 if (match_xsd_string(ptr, "version", o,
765 else if (match_xsd_string(ptr, "query", o,
767 req->query_type = Z_SRW_query_type_cql;
768 else if (match_xsd_string(ptr, "pQuery", o,
770 req->query_type = Z_SRW_query_type_pqf;
771 else if (match_xsd_string(ptr, "xQuery", o,
773 req->query_type = Z_SRW_query_type_xcql;
774 else if (match_xsd_integer(ptr, "startRecord", o,
777 else if (match_xsd_integer(ptr, "maximumRecords", o,
778 &req->maximumRecords))
780 else if (match_xsd_string(ptr, "recordPacking", o,
781 &req->recordPacking))
783 else if (match_xsd_string(ptr, "recordSchema", o,
786 else if (match_xsd_string(ptr, "recordXPath", o,
789 else if (match_xsd_integer(ptr, "resultSetTTL", o,
792 else if (match_xsd_string(ptr, "sortKeys", o,
793 &req->sort.sortKeys))
794 req->sort_type = Z_SRW_sort_type_sort;
795 else if (match_xsd_string(ptr, "stylesheet", o,
799 match_xsd_string(ptr, "database", o, &req->database);
801 if (!req->query.cql && !req->query.pqf && !req->query.xcql)
803 /* should put proper diagnostic here */
807 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
809 xmlNodePtr ptr = method->children;
810 Z_SRW_searchRetrieveResponse *res;
812 (*p)->which = Z_SRW_searchRetrieve_response;
813 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
814 odr_malloc(o, sizeof(*res));
816 res->numberOfRecords = 0;
817 res->resultSetId = 0;
818 res->resultSetIdleTime = 0;
820 res->num_records = 0;
821 res->diagnostics = 0;
822 res->num_diagnostics = 0;
823 res->nextRecordPosition = 0;
826 for (; ptr; ptr = ptr->next)
828 if (match_xsd_string(ptr, "version", o,
831 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
832 &(*p)->extraResponseData_buf,
833 &(*p)->extraResponseData_len))
835 else if (match_xsd_integer(ptr, "numberOfRecords", o,
836 &res->numberOfRecords))
838 else if (match_xsd_string(ptr, "resultSetId", o,
841 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
842 &res->resultSetIdleTime))
844 else if (match_element(ptr, "records"))
845 yaz_srw_records(o, ptr, &res->records,
847 &res->num_records, client_data, ns);
848 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
849 &res->nextRecordPosition))
851 else if (match_element(ptr, "diagnostics"))
852 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
853 &res->num_diagnostics,
855 else if (match_element(ptr, "facet_analysis"))
856 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
859 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
861 Z_SRW_explainRequest *req;
862 xmlNodePtr ptr = method->children;
864 (*p)->which = Z_SRW_explain_request;
865 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
866 odr_malloc(o, sizeof(*req));
867 req->recordPacking = 0;
870 for (; ptr; ptr = ptr->next)
872 if (match_xsd_string(ptr, "version", o,
875 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
876 &(*p)->extraResponseData_buf,
877 &(*p)->extraResponseData_len))
879 else if (match_xsd_string(ptr, "stylesheet", o,
882 else if (match_xsd_string(ptr, "recordPacking", o,
883 &req->recordPacking))
886 match_xsd_string(ptr, "database", o, &req->database);
889 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
891 Z_SRW_explainResponse *res;
892 xmlNodePtr ptr = method->children;
894 (*p)->which = Z_SRW_explain_response;
895 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
896 odr_malloc(o, sizeof(*res));
897 res->diagnostics = 0;
898 res->num_diagnostics = 0;
899 res->record.recordSchema = 0;
900 res->record.recordData_buf = 0;
901 res->record.recordData_len = 0;
902 res->record.recordPosition = 0;
904 for (; ptr; ptr = ptr->next)
906 if (match_xsd_string(ptr, "version", o,
909 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
910 &(*p)->extraResponseData_buf,
911 &(*p)->extraResponseData_len))
913 else if (match_element(ptr, "record"))
914 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
916 else if (match_element(ptr, "diagnostics"))
917 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
918 &res->num_diagnostics,
923 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
925 Z_SRW_scanRequest *req;
926 xmlNodePtr ptr = method->children;
928 (*p)->which = Z_SRW_scan_request;
929 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
930 odr_malloc(o, sizeof(*req));
931 req->query_type = Z_SRW_query_type_cql;
932 req->scanClause.cql = 0;
933 req->responsePosition = 0;
934 req->maximumTerms = 0;
938 for (; ptr; ptr = ptr->next)
940 if (match_xsd_string(ptr, "version", o,
943 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
944 &(*p)->extraResponseData_buf,
945 &(*p)->extraResponseData_len))
947 else if (match_xsd_string(ptr, "scanClause", o,
948 &req->scanClause.cql))
950 else if (match_xsd_string(ptr, "pScanClause", o,
951 &req->scanClause.pqf))
953 req->query_type = Z_SRW_query_type_pqf;
955 else if (match_xsd_integer(ptr, "responsePosition", o,
956 &req->responsePosition))
958 else if (match_xsd_integer(ptr, "maximumTerms", o,
961 else if (match_xsd_string(ptr, "stylesheet", o,
965 match_xsd_string(ptr, "database", o, &req->database);
968 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
970 Z_SRW_scanResponse *res;
971 xmlNodePtr ptr = method->children;
973 (*p)->which = Z_SRW_scan_response;
974 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
975 odr_malloc(o, sizeof(*res));
978 res->diagnostics = 0;
979 res->num_diagnostics = 0;
981 for (; ptr; ptr = ptr->next)
983 if (match_xsd_string(ptr, "version", o,
986 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
987 &(*p)->extraResponseData_buf,
988 &(*p)->extraResponseData_len))
990 else if (match_element(ptr, "terms"))
991 yaz_srw_terms(o, ptr, &res->terms,
992 &res->num_terms, client_data,
994 else if (match_element(ptr, "diagnostics"))
995 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
996 &res->num_diagnostics,
1005 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
1007 (*p)->srw_version = neg_version;
1009 else if (o->direction == ODR_ENCODE)
1011 Z_SRW_PDU **p = handler_data;
1015 if ((*p)->which == Z_SRW_searchRetrieve_request)
1017 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
1018 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
1019 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1020 xmlSetNs(ptr, ns_srw);
1022 if ((*p)->srw_version)
1023 add_xsd_string(ptr, "version", (*p)->srw_version);
1024 switch(req->query_type)
1026 case Z_SRW_query_type_cql:
1027 add_xsd_string(ptr, "query", req->query.cql);
1029 case Z_SRW_query_type_xcql:
1030 add_xsd_string(ptr, "xQuery", req->query.xcql);
1032 case Z_SRW_query_type_pqf:
1033 add_xsd_string(ptr, "pQuery", req->query.pqf);
1036 add_xsd_integer(ptr, "startRecord", req->startRecord);
1037 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
1038 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1039 add_xsd_string(ptr, "recordSchema", req->recordSchema);
1040 add_xsd_string(ptr, "recordXPath", req->recordXPath);
1041 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
1042 switch(req->sort_type)
1044 case Z_SRW_sort_type_none:
1046 case Z_SRW_sort_type_sort:
1047 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
1049 case Z_SRW_sort_type_xSort:
1050 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
1053 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1054 add_xsd_string(ptr, "database", req->database);
1056 else if ((*p)->which == Z_SRW_searchRetrieve_response)
1058 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
1059 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
1060 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1061 xmlSetNs(ptr, ns_srw);
1063 if ((*p)->srw_version)
1064 add_xsd_string(ptr, "version", (*p)->srw_version);
1065 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1066 add_xsd_string(ptr, "resultSetId", res->resultSetId);
1067 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
1068 if (res->num_records)
1070 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1071 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1075 add_xsd_integer(ptr, "nextRecordPosition",
1076 res->nextRecordPosition);
1077 if (res->num_diagnostics)
1079 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1081 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1082 &res->num_diagnostics, client_data, ns);
1085 else if ((*p)->which == Z_SRW_explain_request)
1087 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1088 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1089 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1090 xmlSetNs(ptr, ns_srw);
1092 add_xsd_string(ptr, "version", (*p)->srw_version);
1093 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1094 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1095 add_xsd_string(ptr, "database", req->database);
1097 else if ((*p)->which == Z_SRW_explain_response)
1099 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1100 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1101 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1102 xmlSetNs(ptr, ns_srw);
1104 add_xsd_string(ptr, "version", (*p)->srw_version);
1107 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1108 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1111 if (res->num_diagnostics)
1113 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1115 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1116 &res->num_diagnostics, client_data, ns);
1119 else if ((*p)->which == Z_SRW_scan_request)
1121 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1122 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1123 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1124 xmlSetNs(ptr, ns_srw);
1126 add_xsd_string(ptr, "version", (*p)->srw_version);
1127 switch(req->query_type)
1129 case Z_SRW_query_type_cql:
1130 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
1132 case Z_SRW_query_type_pqf:
1133 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
1136 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1137 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1138 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1139 add_xsd_string(ptr, "database", req->database);
1141 else if ((*p)->which == Z_SRW_scan_response)
1143 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1144 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1145 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1146 xmlSetNs(ptr, ns_srw);
1148 add_xsd_string(ptr, "version", (*p)->srw_version);
1152 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1153 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1156 if (res->num_diagnostics)
1158 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1160 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1161 &res->num_diagnostics, client_data, ns);
1166 if (ptr && (*p)->extraResponseData_len)
1167 add_XML_n(ptr, "extraResponseData",
1168 (*p)->extraResponseData_buf,
1169 (*p)->extraResponseData_len, ns_srw);
1176 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1177 void *client_data, const char *ns_ucp_str)
1179 xmlNodePtr pptr = (xmlNodePtr) vptr;
1180 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1181 if (o->direction == ODR_DECODE)
1183 Z_SRW_PDU **p = handler_data;
1184 xmlNodePtr method = pptr->children;
1186 while (method && method->type == XML_TEXT_NODE)
1187 method = method->next;
1191 if (method->type != XML_ELEMENT_NODE)
1194 *p = yaz_srw_get_core_v_1_1(o);
1196 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1198 xmlNodePtr ptr = method->children;
1199 Z_SRW_updateRequest *req;
1202 (*p)->which = Z_SRW_update_request;
1203 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1204 odr_malloc(o, sizeof(*req));
1208 req->recordVersions = 0;
1209 req->num_recordVersions = 0;
1211 req->extra_record = 0;
1212 req->extraRequestData_buf = 0;
1213 req->extraRequestData_len = 0;
1214 req->stylesheet = 0;
1216 for (; ptr; ptr = ptr->next)
1218 if (match_xsd_string(ptr, "version", o,
1219 &(*p)->srw_version))
1221 else if (match_xsd_string(ptr, "action", o,
1224 if ( !strcmp(oper, "info:srw/action/1/delete"))
1225 req->operation = "delete";
1226 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1227 req->operation = "replace";
1228 else if ( !strcmp( oper, "info:srw/action/1/create"))
1229 req->operation = "insert";
1232 else if (match_xsd_string(ptr, "recordIdentifier", o,
1235 else if (match_element(ptr, "recordVersions" ) )
1236 yaz_srw_versions( o, ptr, &req->recordVersions,
1237 &req->num_recordVersions, client_data,
1239 else if (match_element(ptr, "record"))
1241 req->record = yaz_srw_get_record(o);
1242 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1243 client_data, ns_ucp_str);
1245 else if (match_xsd_string(ptr, "stylesheet", o,
1249 match_xsd_string(ptr, "database", o, &req->database);
1252 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1254 xmlNodePtr ptr = method->children;
1255 Z_SRW_updateResponse *res;
1257 (*p)->which = Z_SRW_update_response;
1258 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1259 odr_malloc(o, sizeof(*res));
1261 res->operationStatus = 0;
1263 res->recordVersions = 0;
1264 res->num_recordVersions = 0;
1265 res->diagnostics = 0;
1266 res->num_diagnostics = 0;
1268 res->extra_record = 0;
1269 res->extraResponseData_buf = 0;
1270 res->extraResponseData_len = 0;
1272 for (; ptr; ptr = ptr->next)
1274 if (match_xsd_string(ptr, "version", o,
1275 &(*p)->srw_version))
1277 else if (match_xsd_string(ptr, "operationStatus", o,
1278 &res->operationStatus ))
1280 else if (match_xsd_string(ptr, "recordIdentifier", o,
1283 else if (match_element(ptr, "recordVersions" ))
1284 yaz_srw_versions(o, ptr, &res->recordVersions,
1285 &res->num_recordVersions,
1286 client_data, ns_ucp_str);
1287 else if (match_element(ptr, "record"))
1289 res->record = yaz_srw_get_record(o);
1290 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1291 client_data, ns_ucp_str);
1293 else if (match_element(ptr, "diagnostics"))
1294 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1295 &res->num_diagnostics,
1296 client_data, ns_ucp_str);
1299 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1302 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1311 else if (o->direction == ODR_ENCODE)
1313 Z_SRW_PDU **p = handler_data;
1314 xmlNsPtr ns_ucp, ns_srw;
1317 if ((*p)->which == Z_SRW_update_request)
1319 Z_SRW_updateRequest *req = (*p)->u.update_request;
1320 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1321 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1322 xmlSetNs(ptr, ns_ucp);
1323 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1325 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1326 add_xsd_string(ptr, "action", req->operation);
1327 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1328 if (req->recordVersions)
1329 yaz_srw_versions( o, ptr, &req->recordVersions,
1330 &req->num_recordVersions,
1331 client_data, ns_ucp_str);
1332 if (req->record && req->record->recordData_len)
1334 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1335 xmlSetNs(rptr, ns_srw);
1336 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1337 client_data, ns_ucp_str);
1339 if (req->extraRequestData_len)
1341 add_XML_n(ptr, "extraRequestData",
1342 req->extraRequestData_buf,
1343 req->extraRequestData_len, ns_srw);
1345 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1346 add_xsd_string(ptr, "database", req->database);
1348 else if ((*p)->which == Z_SRW_update_response)
1350 Z_SRW_updateResponse *res = (*p)->u.update_response;
1351 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1352 "updateResponse", 0);
1353 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1354 xmlSetNs(ptr, ns_ucp);
1355 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1357 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1358 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1359 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1360 if (res->recordVersions)
1361 yaz_srw_versions(o, ptr, &res->recordVersions,
1362 &res->num_recordVersions,
1363 client_data, ns_ucp_str);
1364 if (res->record && res->record->recordData_len)
1366 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1367 xmlSetNs(rptr, ns_srw);
1368 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1369 client_data, ns_ucp_str);
1371 if (res->num_diagnostics)
1374 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1377 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1378 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1379 &res->num_diagnostics, client_data,
1382 if (res->extraResponseData_len)
1383 add_XML_n(ptr, "extraResponseData",
1384 res->extraResponseData_buf,
1385 res->extraResponseData_len, ns_srw);
1400 * c-file-style: "Stroustrup"
1401 * indent-tabs-mode: nil
1403 * vim: shiftwidth=4 tabstop=8 expandtab