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;
571 const char *facetStart = 0;
572 const char *facetSort = 0;
574 (*p)->which = Z_SRW_searchRetrieve_request;
575 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
576 odr_malloc(o, sizeof(*req));
577 req->queryType = "cql";
579 req->sort_type = Z_SRW_sort_type_none;
581 req->startRecord = 0;
582 req->maximumRecords = 0;
583 req->recordSchema = 0;
584 req->recordPacking = 0;
586 req->recordXPath = 0;
587 req->resultSetTTL = 0;
591 for (; ptr; ptr = ptr->next)
593 if (yaz_match_xsd_string(ptr, "version", o,
596 else if (yaz_match_xsd_string(ptr, "queryType", o,
599 else if (yaz_match_xsd_string(ptr, "query", o,
602 else if (yaz_match_xsd_string(ptr, "pQuery", o,
604 req->queryType = "pqf";
605 else if (yaz_match_xsd_string(ptr, "xQuery", o,
607 req->queryType = "xcql";
608 else if (yaz_match_xsd_integer(ptr, "startRecord", o,
611 else if (yaz_match_xsd_integer(ptr, "maximumRecords", o,
612 &req->maximumRecords))
614 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
617 else if (yaz_match_xsd_string(ptr, "recordXMLEscaping", o,
620 else if (yaz_match_xsd_string(ptr, "recordSchema", o,
623 else if (yaz_match_xsd_string(ptr, "recordXPath", o,
626 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
629 else if (yaz_match_xsd_string(ptr, "sortKeys", o,
630 &req->sort.sortKeys))
631 req->sort_type = Z_SRW_sort_type_sort;
632 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
635 else if (yaz_match_xsd_string(ptr, "database", o,
638 else if (yaz_match_xsd_string(ptr, "facetLimit", o,
639 (char**) &facetLimit))
641 else if (yaz_match_xsd_string(ptr, "facetStart", o,
642 (char**) &facetStart))
644 else if (yaz_match_xsd_string(ptr, "facetSort", o,
645 (char**) &facetSort))
650 /* should put proper diagnostic here */
653 if (!strcmp((*p)->srw_version, "2.0"))
655 req->recordPacking = recordXMLEscaping;
656 req->packing = recordPacking;
660 req->recordPacking = recordPacking;
662 yaz_sru_facet_request(o, &req->facetList, &facetLimit, &facetStart,
665 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
667 xmlNodePtr ptr = method->children;
668 Z_SRW_searchRetrieveResponse *res;
670 (*p)->which = Z_SRW_searchRetrieve_response;
671 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
672 odr_malloc(o, sizeof(*res));
674 res->numberOfRecords = 0;
675 res->resultCountPrecision = 0;
676 res->resultSetId = 0;
677 res->resultSetIdleTime = 0;
679 res->num_records = 0;
680 res->diagnostics = 0;
681 res->num_diagnostics = 0;
682 res->nextRecordPosition = 0;
684 res->suggestions = 0;
686 for (; ptr; ptr = ptr->next)
688 if (yaz_match_xsd_string(ptr, "version", o,
691 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
692 &(*p)->extraResponseData_buf,
693 &(*p)->extraResponseData_len))
695 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
696 &res->numberOfRecords))
698 else if (yaz_match_xsd_string(ptr, "resultCountPrecision", o,
699 &res->resultCountPrecision))
701 else if (yaz_match_xsd_string(ptr, "resultSetId", o,
704 else if (yaz_match_xsd_integer(ptr, "resultSetIdleTime", o,
705 &res->resultSetIdleTime))
707 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
708 &res->resultSetIdleTime))
710 else if (yaz_match_xsd_element(ptr, "records"))
711 yaz_srw_records(o, ptr, &res->records,
713 &res->num_records, client_data, 0);
714 else if (yaz_match_xsd_integer(ptr, "nextRecordPosition", o,
715 &res->nextRecordPosition))
717 else if (yaz_match_xsd_element(ptr, "diagnostics"))
718 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
719 &res->num_diagnostics,
721 else if (yaz_match_xsd_element(ptr, "facet_analysis"))
722 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
723 else if (yaz_match_xsd_element(ptr, "facetedResults"))
724 yaz_sru_facet_response(o, &res->facetList, ptr);
727 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
729 Z_SRW_explainRequest *req;
730 xmlNodePtr ptr = method->children;
732 (*p)->which = Z_SRW_explain_request;
733 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
734 odr_malloc(o, sizeof(*req));
735 req->recordPacking = 0;
739 for (; ptr; ptr = ptr->next)
741 if (yaz_match_xsd_string(ptr, "version", o,
744 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
745 &(*p)->extraResponseData_buf,
746 &(*p)->extraResponseData_len))
748 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
751 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
752 &req->recordPacking))
755 yaz_match_xsd_string(ptr, "database", o, &req->database);
758 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
760 Z_SRW_explainResponse *res;
761 xmlNodePtr ptr = method->children;
763 (*p)->which = Z_SRW_explain_response;
764 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
765 odr_malloc(o, sizeof(*res));
766 res->diagnostics = 0;
767 res->num_diagnostics = 0;
768 res->record.recordSchema = 0;
769 res->record.recordData_buf = 0;
770 res->record.recordData_len = 0;
771 res->record.recordPosition = 0;
773 for (; ptr; ptr = ptr->next)
775 if (yaz_match_xsd_string(ptr, "version", o,
778 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
779 &(*p)->extraResponseData_buf,
780 &(*p)->extraResponseData_len))
782 else if (yaz_match_xsd_element(ptr, "record"))
783 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
785 else if (yaz_match_xsd_element(ptr, "diagnostics"))
786 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
787 &res->num_diagnostics,
792 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
794 Z_SRW_scanRequest *req;
795 xmlNodePtr ptr = method->children;
797 (*p)->which = Z_SRW_scan_request;
798 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
799 odr_malloc(o, sizeof(*req));
800 req->queryType = "cql";
802 req->responsePosition = 0;
803 req->maximumTerms = 0;
807 for (; ptr; ptr = ptr->next)
809 if (yaz_match_xsd_string(ptr, "version", o,
812 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
813 &(*p)->extraResponseData_buf,
814 &(*p)->extraResponseData_len))
816 else if (yaz_match_xsd_string(ptr, "scanClause", o,
819 else if (yaz_match_xsd_string(ptr, "pScanClause", o,
822 req->queryType = "pqf";
824 else if (yaz_match_xsd_integer(ptr, "responsePosition", o,
825 &req->responsePosition))
827 else if (yaz_match_xsd_integer(ptr, "maximumTerms", o,
830 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
834 yaz_match_xsd_string(ptr, "database", o, &req->database);
837 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
839 Z_SRW_scanResponse *res;
840 xmlNodePtr ptr = method->children;
842 (*p)->which = Z_SRW_scan_response;
843 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
844 odr_malloc(o, sizeof(*res));
847 res->diagnostics = 0;
848 res->num_diagnostics = 0;
850 for (; ptr; ptr = ptr->next)
852 if (yaz_match_xsd_string(ptr, "version", o,
855 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
856 &(*p)->extraResponseData_buf,
857 &(*p)->extraResponseData_len))
859 else if (yaz_match_xsd_element(ptr, "terms"))
860 yaz_srw_terms(o, ptr, &res->terms,
861 &res->num_terms, client_data,
863 else if (yaz_match_xsd_element(ptr, "diagnostics"))
864 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
865 &res->num_diagnostics,
874 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
876 (*p)->srw_version = neg_version;
878 else if (o->direction == ODR_ENCODE)
880 Z_SRW_PDU **p = handler_data;
883 int version2 = !(*p)->srw_version ||
884 strcmp((*p)->srw_version, "2.") > 0;
885 if ((*p)->which == Z_SRW_searchRetrieve_request)
887 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
888 const char *queryType = req->queryType;
890 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
891 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
892 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
893 xmlSetNs(ptr, ns_srw);
895 add_xsd_string(ptr, "version", (*p)->srw_version);
899 add_xsd_string(ptr, "queryType", queryType);
900 add_xsd_string(ptr, "query", req->query);
904 if (!queryType || !strcmp(queryType, "cql"))
905 add_xsd_string(ptr, "query", req->query);
906 else if (!strcmp(queryType, "xcql"))
907 add_xsd_string(ptr, "xQuery", req->query);
908 else if (!strcmp(queryType, "pqf"))
909 add_xsd_string(ptr, "pQuery", req->query);
911 add_xsd_integer(ptr, "startRecord", req->startRecord);
912 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
915 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
916 add_xsd_string(ptr, "recordPacking", req->packing);
919 add_xsd_string(ptr, "recordPacking", req->recordPacking);
920 add_xsd_string(ptr, "recordSchema", req->recordSchema);
921 add_xsd_string(ptr, "recordXPath", req->recordXPath);
922 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
923 switch (req->sort_type)
925 case Z_SRW_sort_type_none:
927 case Z_SRW_sort_type_sort:
928 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
930 case Z_SRW_sort_type_xSort:
931 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
934 add_xsd_string(ptr, "stylesheet", req->stylesheet);
935 add_xsd_string(ptr, "database", req->database);
937 const char *limit = 0;
938 const char *start = 0;
939 const char *sort = 0;
940 yaz_sru_facet_request(o, &req->facetList, &limit, &start,
942 add_xsd_string(ptr, "facetLimit", limit);
943 add_xsd_string(ptr, "facetStart", start);
944 add_xsd_string(ptr, "facetSort", sort);
947 else if ((*p)->which == Z_SRW_searchRetrieve_response)
949 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
951 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
952 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
953 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
954 xmlSetNs(ptr, ns_srw);
956 add_xsd_string(ptr, "version", (*p)->srw_version);
957 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
958 add_xsd_string(ptr, "resultSetId", res->resultSetId);
960 version2 ? "resultSetTTL" : "resultSetIdleTime" ,
961 res->resultSetIdleTime);
962 if (res->num_records)
964 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
965 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
967 client_data, version2);
969 add_xsd_integer(ptr, "nextRecordPosition",
970 res->nextRecordPosition);
971 if (res->num_diagnostics)
973 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
975 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
976 &res->num_diagnostics, client_data, ns);
978 if (res->resultCountPrecision)
979 add_xsd_string(ptr, "resultCountPrecision",
980 res->resultCountPrecision);
981 yaz_sru_facet_response(o, &res->facetList, ptr);
983 else if ((*p)->which == Z_SRW_explain_request)
985 Z_SRW_explainRequest *req = (*p)->u.explain_request;
986 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
987 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
988 xmlSetNs(ptr, ns_srw);
990 add_xsd_string(ptr, "version", (*p)->srw_version);
993 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
994 add_xsd_string(ptr, "recordPacking", req->packing);
997 add_xsd_string(ptr, "recordPacking", req->recordPacking);
998 add_xsd_string(ptr, "stylesheet", req->stylesheet);
999 add_xsd_string(ptr, "database", req->database);
1001 else if ((*p)->which == Z_SRW_explain_response)
1003 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1004 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1005 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1006 xmlSetNs(ptr, ns_srw);
1008 add_xsd_string(ptr, "version", (*p)->srw_version);
1011 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1012 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1013 client_data, version2);
1015 if (res->num_diagnostics)
1017 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1019 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1020 &res->num_diagnostics, client_data, ns);
1023 else if ((*p)->which == Z_SRW_scan_request)
1025 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1026 const char *queryType = req->queryType;
1028 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1029 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1030 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1031 xmlSetNs(ptr, ns_srw);
1033 add_xsd_string(ptr, "version", (*p)->srw_version);
1037 if (queryType && strcmp(queryType, "cql"))
1038 add_xsd_string(ptr, "queryType", queryType);
1039 add_xsd_string(ptr, "scanClause", req->scanClause);
1043 if (!queryType || !strcmp(queryType, "cql"))
1044 add_xsd_string(ptr, "scanClause", req->scanClause);
1045 else if (!strcmp(queryType, "pqf"))
1046 add_xsd_string(ptr, "pScanClause", req->scanClause);
1048 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1049 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1050 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1051 add_xsd_string(ptr, "database", req->database);
1053 else if ((*p)->which == Z_SRW_scan_response)
1055 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1057 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1058 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1059 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1060 xmlSetNs(ptr, ns_srw);
1062 add_xsd_string(ptr, "version", (*p)->srw_version);
1065 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1066 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1069 if (res->num_diagnostics)
1071 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1073 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1074 &res->num_diagnostics, client_data, ns);
1079 if (ptr && (*p)->extraResponseData_len)
1080 add_XML_n(ptr, "extraResponseData",
1081 (*p)->extraResponseData_buf,
1082 (*p)->extraResponseData_len, ns_srw);
1089 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1090 void *client_data, const char *ns_ucp_str)
1092 xmlNodePtr pptr = (xmlNodePtr) vptr;
1093 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1094 if (o->direction == ODR_DECODE)
1096 Z_SRW_PDU **p = handler_data;
1097 xmlNodePtr method = pptr->children;
1099 while (method && method->type == XML_TEXT_NODE)
1100 method = method->next;
1104 if (method->type != XML_ELEMENT_NODE)
1107 *p = yaz_srw_get_core_v_2_0(o);
1109 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1111 xmlNodePtr ptr = method->children;
1112 Z_SRW_updateRequest *req;
1115 (*p)->which = Z_SRW_update_request;
1116 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1117 odr_malloc(o, sizeof(*req));
1121 req->recordVersions = 0;
1122 req->num_recordVersions = 0;
1124 req->extra_record = 0;
1125 req->extraRequestData_buf = 0;
1126 req->extraRequestData_len = 0;
1127 req->stylesheet = 0;
1129 for (; ptr; ptr = ptr->next)
1131 if (yaz_match_xsd_string(ptr, "version", o,
1132 &(*p)->srw_version))
1134 else if (yaz_match_xsd_string(ptr, "action", o,
1138 if (!strcmp(oper, "info:srw/action/1/delete"))
1139 req->operation = "delete";
1140 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1141 req->operation = "replace";
1142 else if (!strcmp(oper, "info:srw/action/1/create"))
1143 req->operation = "insert";
1146 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1149 else if (yaz_match_xsd_element(ptr, "recordVersions" ) )
1150 yaz_srw_versions( o, ptr, &req->recordVersions,
1151 &req->num_recordVersions, client_data,
1153 else if (yaz_match_xsd_element(ptr, "record"))
1155 req->record = yaz_srw_get_record(o);
1156 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1159 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
1163 yaz_match_xsd_string(ptr, "database", o, &req->database);
1166 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1168 xmlNodePtr ptr = method->children;
1169 Z_SRW_updateResponse *res;
1171 (*p)->which = Z_SRW_update_response;
1172 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1173 odr_malloc(o, sizeof(*res));
1175 res->operationStatus = 0;
1177 res->recordVersions = 0;
1178 res->num_recordVersions = 0;
1179 res->diagnostics = 0;
1180 res->num_diagnostics = 0;
1182 res->extra_record = 0;
1183 res->extraResponseData_buf = 0;
1184 res->extraResponseData_len = 0;
1186 for (; ptr; ptr = ptr->next)
1188 if (yaz_match_xsd_string(ptr, "version", o,
1189 &(*p)->srw_version))
1191 else if (yaz_match_xsd_string(ptr, "operationStatus", o,
1192 &res->operationStatus ))
1194 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1197 else if (yaz_match_xsd_element(ptr, "recordVersions" ))
1198 yaz_srw_versions(o, ptr, &res->recordVersions,
1199 &res->num_recordVersions,
1200 client_data, ns_ucp_str);
1201 else if (yaz_match_xsd_element(ptr, "record"))
1203 res->record = yaz_srw_get_record(o);
1204 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1207 else if (yaz_match_xsd_element(ptr, "diagnostics"))
1208 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1209 &res->num_diagnostics,
1210 client_data, ns_ucp_str);
1213 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1216 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1225 else if (o->direction == ODR_ENCODE)
1227 Z_SRW_PDU **p = handler_data;
1228 xmlNsPtr ns_ucp, ns_srw;
1230 if ((*p)->which == Z_SRW_update_request)
1232 Z_SRW_updateRequest *req = (*p)->u.update_request;
1233 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1234 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1235 xmlSetNs(ptr, ns_ucp);
1236 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1238 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1239 add_xsd_string(ptr, "action", req->operation);
1240 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1241 if (req->recordVersions)
1242 yaz_srw_versions( o, ptr, &req->recordVersions,
1243 &req->num_recordVersions,
1244 client_data, ns_ucp_str);
1245 if (req->record && req->record->recordData_len)
1247 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1248 xmlSetNs(rptr, ns_srw);
1249 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1252 if (req->extraRequestData_len)
1254 add_XML_n(ptr, "extraRequestData",
1255 req->extraRequestData_buf,
1256 req->extraRequestData_len, ns_srw);
1258 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1259 add_xsd_string(ptr, "database", req->database);
1261 else if ((*p)->which == Z_SRW_update_response)
1263 Z_SRW_updateResponse *res = (*p)->u.update_response;
1264 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1265 "updateResponse", 0);
1266 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1267 xmlSetNs(ptr, ns_ucp);
1268 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1270 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1271 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1272 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1273 if (res->recordVersions)
1274 yaz_srw_versions(o, ptr, &res->recordVersions,
1275 &res->num_recordVersions,
1276 client_data, ns_ucp_str);
1277 if (res->record && res->record->recordData_len)
1279 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1280 xmlSetNs(rptr, ns_srw);
1281 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1284 if (res->num_diagnostics)
1287 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1290 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1291 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1292 &res->num_diagnostics, client_data,
1295 if (res->extraResponseData_len)
1296 add_XML_n(ptr, "extraResponseData",
1297 res->extraResponseData_buf,
1298 res->extraResponseData_len, ns_srw);
1312 * c-file-style: "Stroustrup"
1313 * indent-tabs-mode: nil
1315 * vim: shiftwidth=4 tabstop=8 expandtab