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>
21 #include <yaz/facet.h>
24 char *yaz_negotiate_sru_version(char *input_ver)
28 if (!strcmp(input_ver, "1.1"))
30 if (!strncmp(input_ver, "1.", 2))
32 if (!strncmp(input_ver, "2.", 2))
37 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
38 Z_SRW_extra_record **extra,
39 void *client_data, int version2)
41 if (o->direction == ODR_DECODE)
43 Z_SRW_extra_record ex;
50 rec->recordSchema = 0;
51 rec->recordData_buf = 0;
52 rec->recordData_len = 0;
53 rec->recordPosition = 0;
56 ex.extraRecordData_buf = 0;
57 ex.extraRecordData_len = 0;
58 ex.recordIdentifier = 0;
60 for (ptr = pptr->children; ptr; ptr = ptr->next)
63 if (yaz_match_xsd_string(ptr, "recordSchema", o,
66 else if (yaz_match_xsd_string(ptr, "recordPacking", o, &spack))
67 ; /* can't rely on it: in SRU 2.0 it's different semantics */
68 else if (yaz_match_xsd_integer(ptr, "recordPosition", o,
69 &rec->recordPosition))
71 else if (yaz_match_xsd_element(ptr, "recordData"))
73 /* we assume XML packing, if any element nodes exist below
74 recordData. Unfortunately, in SRU 2.0 recordPacking
75 means something different */
76 xmlNode *p = ptr->children;
77 for (; p; p = p->next)
78 if (p->type == XML_ELEMENT_NODE)
84 &rec->recordData_buf, &rec->recordData_len, 1);
85 rec->recordPacking = Z_SRW_recordPacking_XML;
89 yaz_match_xsd_string_n(
91 &rec->recordData_buf, &rec->recordData_len);
92 rec->recordPacking = Z_SRW_recordPacking_string;
95 else if (yaz_match_xsd_XML_n(ptr, "extraRecordData", o,
96 &ex.extraRecordData_buf,
97 &ex.extraRecordData_len) )
100 yaz_match_xsd_string(ptr, "recordIdentifier", o,
101 &ex.recordIdentifier);
103 if (ex.extraRecordData_buf || ex.recordIdentifier)
105 *extra = (Z_SRW_extra_record *)
106 odr_malloc(o, sizeof(Z_SRW_extra_record));
107 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
110 else if (o->direction == ODR_ENCODE)
112 xmlNodePtr ptr = pptr;
113 int pack = rec->recordPacking;
114 const char *spack = yaz_srw_pack_to_str(pack);
116 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
120 add_xsd_string(ptr, "recordXMLEscaping", spack);
122 add_xsd_string(ptr, "recordPacking", spack);
126 case Z_SRW_recordPacking_string:
127 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
128 rec->recordData_len);
130 case Z_SRW_recordPacking_XML:
131 add_XML_n(ptr, "recordData", rec->recordData_buf,
132 rec->recordData_len, 0);
134 case Z_SRW_recordPacking_URL:
135 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
136 rec->recordData_len);
139 if (rec->recordPosition)
140 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
143 if ((*extra)->recordIdentifier)
144 add_xsd_string(ptr, "recordIdentifier",
145 (*extra)->recordIdentifier);
146 if ((*extra)->extraRecordData_buf)
147 add_XML_n(ptr, "extraRecordData",
148 (*extra)->extraRecordData_buf,
149 (*extra)->extraRecordData_len, 0);
155 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
156 Z_SRW_extra_record ***extra,
157 int *num, void *client_data, int version2)
159 if (o->direction == ODR_DECODE)
164 for (ptr = pptr->children; ptr; ptr = ptr->next)
166 if (ptr->type == XML_ELEMENT_NODE &&
167 !xmlStrcmp(ptr->name, BAD_CAST "record"))
172 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
173 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
174 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
176 if (ptr->type == XML_ELEMENT_NODE &&
177 !xmlStrcmp(ptr->name, BAD_CAST "record"))
179 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, 0);
184 else if (o->direction == ODR_ENCODE)
187 for (i = 0; i < *num; i++)
189 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
191 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
192 client_data, version2);
198 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
199 void *client_data, const char *ns)
201 if (o->direction == ODR_DECODE)
204 rec->versionType = 0;
205 rec->versionValue = 0;
206 for (ptr = pptr->children; ptr; ptr = ptr->next)
209 if (yaz_match_xsd_string(ptr, "versionType", o,
213 yaz_match_xsd_string(ptr, "versionValue", o,
217 else if (o->direction == ODR_ENCODE)
219 xmlNodePtr ptr = pptr;
220 add_xsd_string(ptr, "versionType", rec->versionType);
221 add_xsd_string(ptr, "versionValue", rec->versionValue);
226 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
227 Z_SRW_recordVersion **vers,
228 int *num, void *client_data, const char *ns)
230 if (o->direction == ODR_DECODE)
235 for (ptr = pptr->children; ptr; ptr = ptr->next)
237 if (ptr->type == XML_ELEMENT_NODE &&
238 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
243 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
244 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
246 if (ptr->type == XML_ELEMENT_NODE &&
247 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
249 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
254 else if (o->direction == ODR_ENCODE)
257 for (i = 0; i < *num; i++)
259 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
261 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
267 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
271 WRBUF wrbuf = wrbuf_alloc();
272 Z_FacetTerm *facet_term;
273 const char *freq_string = yaz_element_attribute_value_get(
274 node, "facetvalue", "est_representation");
276 freq = odr_atoi(freq_string);
280 for (child = node->children; child ; child = child->next)
282 if (child->type == XML_TEXT_NODE)
283 wrbuf_puts(wrbuf, (const char *) child->content);
285 facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
286 wrbuf_destroy(wrbuf);
290 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
292 Z_AttributeList *list;
293 Z_FacetField *facet_field;
298 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
299 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
301 list = yaz_use_attribute_create(odr, name);
302 for (node = ptr->children; node; node = node->next) {
303 if (yaz_match_xsd_element(node, "facetvalue"))
306 facet_field = facet_field_create(odr, list, num_terms);
308 for (node = ptr->children; node; node = node->next)
310 if (yaz_match_xsd_element(node, "facetvalue"))
312 facet_field_term_set(odr, facet_field,
313 yaz_sru_proxy_get_facet_term_count(odr, node),
321 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
322 Z_FacetList **facetList)
326 for (ptr = root->children; ptr; ptr = ptr->next)
328 if (yaz_match_xsd_element(ptr, "facets"))
331 Z_FacetList *facet_list;
333 for (node = ptr->children; node; node= node->next)
335 if (node->type == XML_ELEMENT_NODE)
338 facet_list = facet_list_create(o, num_facets);
340 for (node = ptr->children; node; node= node->next)
342 if (yaz_match_xsd_element(node, "facet"))
344 facet_list_field_set(
346 yaz_sru_proxy_decode_facet_field(o, node), num_facets);
350 *facetList = facet_list;
359 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
360 Z_SRW_diagnostic **recs, int *num,
361 void *client_data, const char *ns)
366 for (ptr = pptr; ptr; ptr = ptr->next)
368 if (ptr->type == XML_ELEMENT_NODE &&
369 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
374 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
375 for (i = 0; i < *num; i++)
378 (*recs)[i].details = 0;
379 (*recs)[i].message = 0;
381 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
383 if (ptr->type == XML_ELEMENT_NODE &&
384 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
388 (*recs)[i].details = 0;
389 (*recs)[i].message = 0;
390 for (rptr = ptr->children; rptr; rptr = rptr->next)
392 if (yaz_match_xsd_string(rptr, "uri", o, &(*recs)[i].uri))
394 else if (yaz_match_xsd_string(rptr, "details", o,
395 &(*recs)[i].details))
398 yaz_match_xsd_string(rptr, "message", o,
399 &(*recs)[i].message);
407 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
408 Z_SRW_diagnostic **diag,
412 xmlDocPtr doc = xmlParseMemory(buf, len);
415 xmlNodePtr ptr = xmlDocGetRootElement(doc);
416 while (ptr && ptr->type != XML_ELEMENT_NODE)
419 && !xmlStrcmp(ptr->ns->href,
420 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
422 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
429 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
430 int *num, void *client_data, const char *ns)
432 if (o->direction == ODR_DECODE)
434 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
436 else if (o->direction == ODR_ENCODE)
440 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
441 for (i = 0; i < *num; i++)
443 const char *std_diag = "info:srw/diagnostic/1/";
444 const char *ucp_diag = "info:srw/diagnostic/12/";
445 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
446 BAD_CAST "diagnostic", 0);
447 add_xsd_string(rptr, "uri", (*recs)[i].uri);
448 if ((*recs)[i].message)
449 add_xsd_string(rptr, "message", (*recs)[i].message);
450 else if ((*recs)[i].uri )
452 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
454 int no = atoi((*recs)[i].uri + strlen(std_diag));
455 const char *message = yaz_diag_srw_str(no);
457 add_xsd_string(rptr, "message", message);
459 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
461 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
462 const char *message = yaz_diag_sru_update_str(no);
464 add_xsd_string(rptr, "message", message);
467 add_xsd_string(rptr, "details", (*recs)[i].details);
473 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
474 void *client_data, const char *ns)
476 if (o->direction == ODR_DECODE)
480 term->numberOfRecords = 0;
481 term->displayTerm = 0;
482 term->whereInList = 0;
483 for (ptr = pptr->children; ptr; ptr = ptr->next)
485 if (yaz_match_xsd_string(ptr, "value", o, &term->value))
487 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
488 &term->numberOfRecords))
490 else if (yaz_match_xsd_string(ptr, "displayTerm", o,
494 yaz_match_xsd_string(ptr, "whereInList", o, &term->whereInList);
497 else if (o->direction == ODR_ENCODE)
499 xmlNodePtr ptr = pptr;
500 add_xsd_string(ptr, "value", term->value);
501 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
502 add_xsd_string(ptr, "displayTerm", term->displayTerm);
503 add_xsd_string(ptr, "whereInList", term->whereInList);
508 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
509 int *num, void *client_data, const char *ns)
511 if (o->direction == ODR_DECODE)
516 for (ptr = pptr->children; ptr; ptr = ptr->next)
518 if (ptr->type == XML_ELEMENT_NODE &&
519 !xmlStrcmp(ptr->name, BAD_CAST "term"))
524 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
525 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
527 if (ptr->type == XML_ELEMENT_NODE &&
528 !xmlStrcmp(ptr->name, BAD_CAST "term"))
529 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
532 else if (o->direction == ODR_ENCODE)
535 for (i = 0; i < *num; i++)
537 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
538 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
544 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
545 void *client_data, const char *ns)
547 xmlNodePtr pptr = (xmlNodePtr) vptr;
548 if (o->direction == ODR_DECODE)
550 Z_SRW_PDU **p = handler_data;
551 xmlNodePtr method = pptr->children;
554 while (method && method->type == XML_TEXT_NODE)
555 method = method->next;
559 if (method->type != XML_ELEMENT_NODE)
562 *p = yaz_srw_get_core_v_2_0(o);
564 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
566 xmlNodePtr ptr = method->children;
567 Z_SRW_searchRetrieveRequest *req;
568 char *recordPacking = 0;
569 char *recordXMLEscaping = 0;
570 const char *facetLimit = 0;
572 (*p)->which = Z_SRW_searchRetrieve_request;
573 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
574 odr_malloc(o, sizeof(*req));
575 req->queryType = "cql";
577 req->sort_type = Z_SRW_sort_type_none;
579 req->startRecord = 0;
580 req->maximumRecords = 0;
581 req->recordSchema = 0;
582 req->recordPacking = 0;
584 req->recordXPath = 0;
585 req->resultSetTTL = 0;
589 for (; ptr; ptr = ptr->next)
591 if (yaz_match_xsd_string(ptr, "version", o,
594 else if (yaz_match_xsd_string(ptr, "queryType", o,
597 else if (yaz_match_xsd_string(ptr, "query", o,
600 else if (yaz_match_xsd_string(ptr, "pQuery", o,
602 req->queryType = "pqf";
603 else if (yaz_match_xsd_string(ptr, "xQuery", o,
605 req->queryType = "xcql";
606 else if (yaz_match_xsd_integer(ptr, "startRecord", o,
609 else if (yaz_match_xsd_integer(ptr, "maximumRecords", o,
610 &req->maximumRecords))
612 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
615 else if (yaz_match_xsd_string(ptr, "recordXMLEscaping", o,
618 else if (yaz_match_xsd_string(ptr, "recordSchema", o,
621 else if (yaz_match_xsd_string(ptr, "recordXPath", o,
624 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
627 else if (yaz_match_xsd_string(ptr, "sortKeys", o,
628 &req->sort.sortKeys))
629 req->sort_type = Z_SRW_sort_type_sort;
630 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
633 else if (yaz_match_xsd_string(ptr, "database", o, &req->database))
635 else if (yaz_match_xsd_string(ptr, "facetLimit", o,
636 (char**) &facetLimit))
641 /* should put proper diagnostic here */
644 if (!strcmp((*p)->srw_version, "2.0"))
646 req->recordPacking = recordXMLEscaping;
647 req->packing = recordPacking;
651 req->recordPacking = recordPacking;
653 yaz_sru_facet_request(o, &req->facetList, &facetLimit);
655 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
657 xmlNodePtr ptr = method->children;
658 Z_SRW_searchRetrieveResponse *res;
660 (*p)->which = Z_SRW_searchRetrieve_response;
661 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
662 odr_malloc(o, sizeof(*res));
664 res->numberOfRecords = 0;
665 res->resultCountPrecision = 0;
666 res->resultSetId = 0;
667 res->resultSetIdleTime = 0;
669 res->num_records = 0;
670 res->diagnostics = 0;
671 res->num_diagnostics = 0;
672 res->nextRecordPosition = 0;
674 res->suggestions = 0;
676 for (; ptr; ptr = ptr->next)
678 if (yaz_match_xsd_string(ptr, "version", o,
681 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
682 &(*p)->extraResponseData_buf,
683 &(*p)->extraResponseData_len))
685 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
686 &res->numberOfRecords))
688 else if (yaz_match_xsd_string(ptr, "resultCountPrecision", o,
689 &res->resultCountPrecision))
691 else if (yaz_match_xsd_string(ptr, "resultSetId", o,
694 else if (yaz_match_xsd_integer(ptr, "resultSetIdleTime", o,
695 &res->resultSetIdleTime))
697 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
698 &res->resultSetIdleTime))
700 else if (yaz_match_xsd_element(ptr, "records"))
701 yaz_srw_records(o, ptr, &res->records,
703 &res->num_records, client_data, 0);
704 else if (yaz_match_xsd_integer(ptr, "nextRecordPosition", o,
705 &res->nextRecordPosition))
707 else if (yaz_match_xsd_element(ptr, "diagnostics"))
708 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
709 &res->num_diagnostics,
711 else if (yaz_match_xsd_element(ptr, "facet_analysis"))
712 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
713 else if (yaz_match_xsd_element(ptr, "facetedResults"))
714 yaz_sru_facet_response(o, &res->facetList, ptr);
717 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
719 Z_SRW_explainRequest *req;
720 xmlNodePtr ptr = method->children;
722 (*p)->which = Z_SRW_explain_request;
723 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
724 odr_malloc(o, sizeof(*req));
725 req->recordPacking = 0;
729 for (; ptr; ptr = ptr->next)
731 if (yaz_match_xsd_string(ptr, "version", o,
734 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
735 &(*p)->extraResponseData_buf,
736 &(*p)->extraResponseData_len))
738 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
741 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
742 &req->recordPacking))
745 yaz_match_xsd_string(ptr, "database", o, &req->database);
748 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
750 Z_SRW_explainResponse *res;
751 xmlNodePtr ptr = method->children;
753 (*p)->which = Z_SRW_explain_response;
754 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
755 odr_malloc(o, sizeof(*res));
756 res->diagnostics = 0;
757 res->num_diagnostics = 0;
758 res->record.recordSchema = 0;
759 res->record.recordData_buf = 0;
760 res->record.recordData_len = 0;
761 res->record.recordPosition = 0;
763 for (; ptr; ptr = ptr->next)
765 if (yaz_match_xsd_string(ptr, "version", o,
768 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
769 &(*p)->extraResponseData_buf,
770 &(*p)->extraResponseData_len))
772 else if (yaz_match_xsd_element(ptr, "record"))
773 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
775 else if (yaz_match_xsd_element(ptr, "diagnostics"))
776 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
777 &res->num_diagnostics,
782 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
784 Z_SRW_scanRequest *req;
785 xmlNodePtr ptr = method->children;
787 (*p)->which = Z_SRW_scan_request;
788 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
789 odr_malloc(o, sizeof(*req));
790 req->queryType = "cql";
792 req->responsePosition = 0;
793 req->maximumTerms = 0;
797 for (; ptr; ptr = ptr->next)
799 if (yaz_match_xsd_string(ptr, "version", o,
802 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
803 &(*p)->extraResponseData_buf,
804 &(*p)->extraResponseData_len))
806 else if (yaz_match_xsd_string(ptr, "scanClause", o,
809 else if (yaz_match_xsd_string(ptr, "pScanClause", o,
812 req->queryType = "pqf";
814 else if (yaz_match_xsd_integer(ptr, "responsePosition", o,
815 &req->responsePosition))
817 else if (yaz_match_xsd_integer(ptr, "maximumTerms", o,
820 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
824 yaz_match_xsd_string(ptr, "database", o, &req->database);
827 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
829 Z_SRW_scanResponse *res;
830 xmlNodePtr ptr = method->children;
832 (*p)->which = Z_SRW_scan_response;
833 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
834 odr_malloc(o, sizeof(*res));
837 res->diagnostics = 0;
838 res->num_diagnostics = 0;
840 for (; ptr; ptr = ptr->next)
842 if (yaz_match_xsd_string(ptr, "version", o,
845 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
846 &(*p)->extraResponseData_buf,
847 &(*p)->extraResponseData_len))
849 else if (yaz_match_xsd_element(ptr, "terms"))
850 yaz_srw_terms(o, ptr, &res->terms,
851 &res->num_terms, client_data,
853 else if (yaz_match_xsd_element(ptr, "diagnostics"))
854 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
855 &res->num_diagnostics,
864 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
866 (*p)->srw_version = neg_version;
868 else if (o->direction == ODR_ENCODE)
870 Z_SRW_PDU **p = handler_data;
873 int version2 = !(*p)->srw_version ||
874 strcmp((*p)->srw_version, "2.") > 0;
875 if ((*p)->which == Z_SRW_searchRetrieve_request)
877 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
878 const char *queryType = req->queryType;
880 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
881 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
882 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
883 xmlSetNs(ptr, ns_srw);
885 add_xsd_string(ptr, "version", (*p)->srw_version);
889 add_xsd_string(ptr, "queryType", queryType);
890 add_xsd_string(ptr, "query", req->query);
894 if (!queryType || !strcmp(queryType, "cql"))
895 add_xsd_string(ptr, "query", req->query);
896 else if (!strcmp(queryType, "xcql"))
897 add_xsd_string(ptr, "xQuery", req->query);
898 else if (!strcmp(queryType, "pqf"))
899 add_xsd_string(ptr, "pQuery", req->query);
901 add_xsd_integer(ptr, "startRecord", req->startRecord);
902 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
905 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
906 add_xsd_string(ptr, "recordPacking", req->packing);
909 add_xsd_string(ptr, "recordPacking", req->recordPacking);
910 add_xsd_string(ptr, "recordSchema", req->recordSchema);
911 add_xsd_string(ptr, "recordXPath", req->recordXPath);
912 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
913 switch (req->sort_type)
915 case Z_SRW_sort_type_none:
917 case Z_SRW_sort_type_sort:
918 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
920 case Z_SRW_sort_type_xSort:
921 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
924 add_xsd_string(ptr, "stylesheet", req->stylesheet);
925 add_xsd_string(ptr, "database", req->database);
927 const char *limit = 0;
928 yaz_sru_facet_request(o, &req->facetList, &limit);
929 add_xsd_string(ptr, "facetLimit", limit);
932 else if ((*p)->which == Z_SRW_searchRetrieve_response)
934 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
936 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
937 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
938 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
939 xmlSetNs(ptr, ns_srw);
941 add_xsd_string(ptr, "version", (*p)->srw_version);
942 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
943 add_xsd_string(ptr, "resultSetId", res->resultSetId);
945 version2 ? "resultSetTTL" : "resultSetIdleTime" ,
946 res->resultSetIdleTime);
947 if (res->num_records)
949 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
950 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
952 client_data, version2);
954 add_xsd_integer(ptr, "nextRecordPosition",
955 res->nextRecordPosition);
956 if (res->num_diagnostics)
958 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
960 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
961 &res->num_diagnostics, client_data, ns);
963 if (res->resultCountPrecision)
964 add_xsd_string(ptr, "resultCountPrecision",
965 res->resultCountPrecision);
966 yaz_sru_facet_response(o, &res->facetList, ptr);
968 else if ((*p)->which == Z_SRW_explain_request)
970 Z_SRW_explainRequest *req = (*p)->u.explain_request;
971 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
972 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
973 xmlSetNs(ptr, ns_srw);
975 add_xsd_string(ptr, "version", (*p)->srw_version);
978 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
979 add_xsd_string(ptr, "recordPacking", req->packing);
982 add_xsd_string(ptr, "recordPacking", req->recordPacking);
983 add_xsd_string(ptr, "stylesheet", req->stylesheet);
984 add_xsd_string(ptr, "database", req->database);
986 else if ((*p)->which == Z_SRW_explain_response)
988 Z_SRW_explainResponse *res = (*p)->u.explain_response;
989 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
990 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
991 xmlSetNs(ptr, ns_srw);
993 add_xsd_string(ptr, "version", (*p)->srw_version);
996 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
997 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
998 client_data, version2);
1000 if (res->num_diagnostics)
1002 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1004 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1005 &res->num_diagnostics, client_data, ns);
1008 else if ((*p)->which == Z_SRW_scan_request)
1010 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1011 const char *queryType = req->queryType;
1013 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1014 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1015 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1016 xmlSetNs(ptr, ns_srw);
1018 add_xsd_string(ptr, "version", (*p)->srw_version);
1022 if (queryType && strcmp(queryType, "cql"))
1023 add_xsd_string(ptr, "queryType", queryType);
1024 add_xsd_string(ptr, "scanClause", req->scanClause);
1028 if (!queryType || !strcmp(queryType, "cql"))
1029 add_xsd_string(ptr, "scanClause", req->scanClause);
1030 else if (!strcmp(queryType, "pqf"))
1031 add_xsd_string(ptr, "pScanClause", req->scanClause);
1033 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1034 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1035 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1036 add_xsd_string(ptr, "database", req->database);
1038 else if ((*p)->which == Z_SRW_scan_response)
1040 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1042 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1043 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1044 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1045 xmlSetNs(ptr, ns_srw);
1047 add_xsd_string(ptr, "version", (*p)->srw_version);
1050 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1051 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1054 if (res->num_diagnostics)
1056 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1058 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1059 &res->num_diagnostics, client_data, ns);
1064 if (ptr && (*p)->extraResponseData_len)
1065 add_XML_n(ptr, "extraResponseData",
1066 (*p)->extraResponseData_buf,
1067 (*p)->extraResponseData_len, ns_srw);
1074 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1075 void *client_data, const char *ns_ucp_str)
1077 xmlNodePtr pptr = (xmlNodePtr) vptr;
1078 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1079 if (o->direction == ODR_DECODE)
1081 Z_SRW_PDU **p = handler_data;
1082 xmlNodePtr method = pptr->children;
1084 while (method && method->type == XML_TEXT_NODE)
1085 method = method->next;
1089 if (method->type != XML_ELEMENT_NODE)
1092 *p = yaz_srw_get_core_v_2_0(o);
1094 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1096 xmlNodePtr ptr = method->children;
1097 Z_SRW_updateRequest *req;
1100 (*p)->which = Z_SRW_update_request;
1101 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1102 odr_malloc(o, sizeof(*req));
1106 req->recordVersions = 0;
1107 req->num_recordVersions = 0;
1109 req->extra_record = 0;
1110 req->extraRequestData_buf = 0;
1111 req->extraRequestData_len = 0;
1112 req->stylesheet = 0;
1114 for (; ptr; ptr = ptr->next)
1116 if (yaz_match_xsd_string(ptr, "version", o,
1117 &(*p)->srw_version))
1119 else if (yaz_match_xsd_string(ptr, "action", o,
1123 if (!strcmp(oper, "info:srw/action/1/delete"))
1124 req->operation = "delete";
1125 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1126 req->operation = "replace";
1127 else if (!strcmp(oper, "info:srw/action/1/create"))
1128 req->operation = "insert";
1131 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1134 else if (yaz_match_xsd_element(ptr, "recordVersions" ) )
1135 yaz_srw_versions( o, ptr, &req->recordVersions,
1136 &req->num_recordVersions, client_data,
1138 else if (yaz_match_xsd_element(ptr, "record"))
1140 req->record = yaz_srw_get_record(o);
1141 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1144 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
1148 yaz_match_xsd_string(ptr, "database", o, &req->database);
1151 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1153 xmlNodePtr ptr = method->children;
1154 Z_SRW_updateResponse *res;
1156 (*p)->which = Z_SRW_update_response;
1157 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1158 odr_malloc(o, sizeof(*res));
1160 res->operationStatus = 0;
1162 res->recordVersions = 0;
1163 res->num_recordVersions = 0;
1164 res->diagnostics = 0;
1165 res->num_diagnostics = 0;
1167 res->extra_record = 0;
1168 res->extraResponseData_buf = 0;
1169 res->extraResponseData_len = 0;
1171 for (; ptr; ptr = ptr->next)
1173 if (yaz_match_xsd_string(ptr, "version", o,
1174 &(*p)->srw_version))
1176 else if (yaz_match_xsd_string(ptr, "operationStatus", o,
1177 &res->operationStatus ))
1179 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1182 else if (yaz_match_xsd_element(ptr, "recordVersions" ))
1183 yaz_srw_versions(o, ptr, &res->recordVersions,
1184 &res->num_recordVersions,
1185 client_data, ns_ucp_str);
1186 else if (yaz_match_xsd_element(ptr, "record"))
1188 res->record = yaz_srw_get_record(o);
1189 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1192 else if (yaz_match_xsd_element(ptr, "diagnostics"))
1193 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1194 &res->num_diagnostics,
1195 client_data, ns_ucp_str);
1198 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1201 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1210 else if (o->direction == ODR_ENCODE)
1212 Z_SRW_PDU **p = handler_data;
1213 xmlNsPtr ns_ucp, ns_srw;
1215 if ((*p)->which == Z_SRW_update_request)
1217 Z_SRW_updateRequest *req = (*p)->u.update_request;
1218 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1219 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1220 xmlSetNs(ptr, ns_ucp);
1221 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1223 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1224 add_xsd_string(ptr, "action", req->operation);
1225 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1226 if (req->recordVersions)
1227 yaz_srw_versions( o, ptr, &req->recordVersions,
1228 &req->num_recordVersions,
1229 client_data, ns_ucp_str);
1230 if (req->record && req->record->recordData_len)
1232 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1233 xmlSetNs(rptr, ns_srw);
1234 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1237 if (req->extraRequestData_len)
1239 add_XML_n(ptr, "extraRequestData",
1240 req->extraRequestData_buf,
1241 req->extraRequestData_len, ns_srw);
1243 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1244 add_xsd_string(ptr, "database", req->database);
1246 else if ((*p)->which == Z_SRW_update_response)
1248 Z_SRW_updateResponse *res = (*p)->u.update_response;
1249 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1250 "updateResponse", 0);
1251 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1252 xmlSetNs(ptr, ns_ucp);
1253 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1255 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1256 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1257 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1258 if (res->recordVersions)
1259 yaz_srw_versions(o, ptr, &res->recordVersions,
1260 &res->num_recordVersions,
1261 client_data, ns_ucp_str);
1262 if (res->record && res->record->recordData_len)
1264 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1265 xmlSetNs(rptr, ns_srw);
1266 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1269 if (res->num_diagnostics)
1272 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1275 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1276 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1277 &res->num_diagnostics, client_data,
1280 if (res->extraResponseData_len)
1281 add_XML_n(ptr, "extraResponseData",
1282 res->extraResponseData_buf,
1283 res->extraResponseData_len, ns_srw);
1297 * c-file-style: "Stroustrup"
1298 * indent-tabs-mode: nil
1300 * vim: shiftwidth=4 tabstop=8 expandtab