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 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 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 char *yaz_negotiate_sru_version(char *input_ver)
86 if (!strcmp(input_ver, "1.1"))
88 if (!strncmp(input_ver, "1.", 2))
90 if (!strncmp(input_ver, "2.", 2))
95 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
96 Z_SRW_extra_record **extra,
97 void *client_data, int version2)
99 if (o->direction == ODR_DECODE)
101 Z_SRW_extra_record ex;
108 rec->recordSchema = 0;
109 rec->recordData_buf = 0;
110 rec->recordData_len = 0;
111 rec->recordPosition = 0;
114 ex.extraRecordData_buf = 0;
115 ex.extraRecordData_len = 0;
116 ex.recordIdentifier = 0;
118 for (ptr = pptr->children; ptr; ptr = ptr->next)
121 if (yaz_match_xsd_string(ptr, "recordSchema", o,
124 else if (yaz_match_xsd_string(ptr, "recordPacking", o, &spack))
125 ; /* can't rely on it: in SRU 2.0 it's different semantics */
126 else if (yaz_match_xsd_integer(ptr, "recordPosition", o,
127 &rec->recordPosition))
129 else if (yaz_match_xsd_element(ptr, "recordData"))
131 /* we assume XML packing, if any element nodes exist below
132 recordData. Unfortunately, in SRU 2.0 recordPacking
133 means something different */
134 xmlNode *p = ptr->children;
135 for (; p; p = p->next)
136 if (p->type == XML_ELEMENT_NODE)
140 yaz_match_xsd_XML_n2(
141 ptr, "recordData", o,
142 &rec->recordData_buf, &rec->recordData_len, 1);
143 rec->recordPacking = Z_SRW_recordPacking_XML;
147 yaz_match_xsd_string_n(
148 ptr, "recordData", o,
149 &rec->recordData_buf, &rec->recordData_len);
150 rec->recordPacking = Z_SRW_recordPacking_string;
153 else if (yaz_match_xsd_XML_n(ptr, "extraRecordData", o,
154 &ex.extraRecordData_buf,
155 &ex.extraRecordData_len) )
158 yaz_match_xsd_string(ptr, "recordIdentifier", o,
159 &ex.recordIdentifier);
161 if (ex.extraRecordData_buf || ex.recordIdentifier)
163 *extra = (Z_SRW_extra_record *)
164 odr_malloc(o, sizeof(Z_SRW_extra_record));
165 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
168 else if (o->direction == ODR_ENCODE)
170 xmlNodePtr ptr = pptr;
171 int pack = rec->recordPacking;
172 const char *spack = yaz_srw_pack_to_str(pack);
174 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
178 add_xsd_string(ptr, "recordXMLEscaping", spack);
180 add_xsd_string(ptr, "recordPacking", spack);
184 case Z_SRW_recordPacking_string:
185 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
186 rec->recordData_len);
188 case Z_SRW_recordPacking_XML:
189 add_XML_n(ptr, "recordData", rec->recordData_buf,
190 rec->recordData_len, 0);
192 case Z_SRW_recordPacking_URL:
193 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
194 rec->recordData_len);
197 if (rec->recordPosition)
198 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
201 if ((*extra)->recordIdentifier)
202 add_xsd_string(ptr, "recordIdentifier",
203 (*extra)->recordIdentifier);
204 if ((*extra)->extraRecordData_buf)
205 add_XML_n(ptr, "extraRecordData",
206 (*extra)->extraRecordData_buf,
207 (*extra)->extraRecordData_len, 0);
213 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
214 Z_SRW_extra_record ***extra,
215 int *num, void *client_data, int version2)
217 if (o->direction == ODR_DECODE)
222 for (ptr = pptr->children; ptr; ptr = ptr->next)
224 if (ptr->type == XML_ELEMENT_NODE &&
225 !xmlStrcmp(ptr->name, BAD_CAST "record"))
230 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
231 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
232 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
234 if (ptr->type == XML_ELEMENT_NODE &&
235 !xmlStrcmp(ptr->name, BAD_CAST "record"))
237 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, 0);
242 else if (o->direction == ODR_ENCODE)
245 for (i = 0; i < *num; i++)
247 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
249 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
250 client_data, version2);
256 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
257 void *client_data, const char *ns)
259 if (o->direction == ODR_DECODE)
262 rec->versionType = 0;
263 rec->versionValue = 0;
264 for (ptr = pptr->children; ptr; ptr = ptr->next)
267 if (yaz_match_xsd_string(ptr, "versionType", o,
271 yaz_match_xsd_string(ptr, "versionValue", o,
275 else if (o->direction == ODR_ENCODE)
277 xmlNodePtr ptr = pptr;
278 add_xsd_string(ptr, "versionType", rec->versionType);
279 add_xsd_string(ptr, "versionValue", rec->versionValue);
284 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
285 Z_SRW_recordVersion **vers,
286 int *num, void *client_data, const char *ns)
288 if (o->direction == ODR_DECODE)
293 for (ptr = pptr->children; ptr; ptr = ptr->next)
295 if (ptr->type == XML_ELEMENT_NODE &&
296 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
301 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
302 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
304 if (ptr->type == XML_ELEMENT_NODE &&
305 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
307 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
312 else if (o->direction == ODR_ENCODE)
315 for (i = 0; i < *num; i++)
317 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
319 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
325 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
329 WRBUF wrbuf = wrbuf_alloc();
330 Z_FacetTerm *facet_term;
331 const char *freq_string = yaz_element_attribute_value_get(
332 node, "facetvalue", "est_representation");
334 freq = odr_atoi(freq_string);
338 for (child = node->children; child ; child = child->next)
340 if (child->type == XML_TEXT_NODE)
341 wrbuf_puts(wrbuf, (const char *) child->content);
343 facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
344 wrbuf_destroy(wrbuf);
348 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
350 Z_AttributeList *list;
351 Z_FacetField *facet_field;
356 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
357 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
359 list = yaz_use_attribute_create(odr, name);
360 for (node = ptr->children; node; node = node->next) {
361 if (yaz_match_xsd_element(node, "facetvalue"))
364 facet_field = facet_field_create(odr, list, num_terms);
366 for (node = ptr->children; node; node = node->next)
368 if (yaz_match_xsd_element(node, "facetvalue"))
370 facet_field_term_set(odr, facet_field,
371 yaz_sru_proxy_get_facet_term_count(odr, node),
379 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
380 Z_FacetList **facetList)
384 for (ptr = root->children; ptr; ptr = ptr->next)
386 if (yaz_match_xsd_element(ptr, "facets"))
389 Z_FacetList *facet_list;
391 for (node = ptr->children; node; node= node->next)
393 if (node->type == XML_ELEMENT_NODE)
396 facet_list = facet_list_create(o, num_facets);
398 for (node = ptr->children; node; node= node->next)
400 if (yaz_match_xsd_element(node, "facet"))
402 facet_list_field_set(
404 yaz_sru_proxy_decode_facet_field(o, node), num_facets);
408 *facetList = facet_list;
417 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
418 Z_SRW_diagnostic **recs, int *num,
419 void *client_data, const char *ns)
424 for (ptr = pptr; ptr; ptr = ptr->next)
426 if (ptr->type == XML_ELEMENT_NODE &&
427 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
432 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
433 for (i = 0; i < *num; i++)
436 (*recs)[i].details = 0;
437 (*recs)[i].message = 0;
439 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
441 if (ptr->type == XML_ELEMENT_NODE &&
442 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
446 (*recs)[i].details = 0;
447 (*recs)[i].message = 0;
448 for (rptr = ptr->children; rptr; rptr = rptr->next)
450 if (yaz_match_xsd_string(rptr, "uri", o, &(*recs)[i].uri))
452 else if (yaz_match_xsd_string(rptr, "details", o,
453 &(*recs)[i].details))
456 yaz_match_xsd_string(rptr, "message", o,
457 &(*recs)[i].message);
465 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
466 Z_SRW_diagnostic **diag,
470 xmlDocPtr doc = xmlParseMemory(buf, len);
473 xmlNodePtr ptr = xmlDocGetRootElement(doc);
474 while (ptr && ptr->type != XML_ELEMENT_NODE)
477 && !xmlStrcmp(ptr->ns->href,
478 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
480 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
487 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
488 int *num, void *client_data, const char *ns)
490 if (o->direction == ODR_DECODE)
492 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
494 else if (o->direction == ODR_ENCODE)
498 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
499 for (i = 0; i < *num; i++)
501 const char *std_diag = "info:srw/diagnostic/1/";
502 const char *ucp_diag = "info:srw/diagnostic/12/";
503 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
504 BAD_CAST "diagnostic", 0);
505 add_xsd_string(rptr, "uri", (*recs)[i].uri);
506 if ((*recs)[i].message)
507 add_xsd_string(rptr, "message", (*recs)[i].message);
508 else if ((*recs)[i].uri )
510 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
512 int no = atoi((*recs)[i].uri + strlen(std_diag));
513 const char *message = yaz_diag_srw_str(no);
515 add_xsd_string(rptr, "message", message);
517 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
519 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
520 const char *message = yaz_diag_sru_update_str(no);
522 add_xsd_string(rptr, "message", message);
525 add_xsd_string(rptr, "details", (*recs)[i].details);
531 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
532 void *client_data, const char *ns)
534 if (o->direction == ODR_DECODE)
538 term->numberOfRecords = 0;
539 term->displayTerm = 0;
540 term->whereInList = 0;
541 for (ptr = pptr->children; ptr; ptr = ptr->next)
543 if (yaz_match_xsd_string(ptr, "value", o, &term->value))
545 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
546 &term->numberOfRecords))
548 else if (yaz_match_xsd_string(ptr, "displayTerm", o,
552 yaz_match_xsd_string(ptr, "whereInList", o, &term->whereInList);
555 else if (o->direction == ODR_ENCODE)
557 xmlNodePtr ptr = pptr;
558 add_xsd_string(ptr, "value", term->value);
559 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
560 add_xsd_string(ptr, "displayTerm", term->displayTerm);
561 add_xsd_string(ptr, "whereInList", term->whereInList);
566 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
567 int *num, void *client_data, const char *ns)
569 if (o->direction == ODR_DECODE)
574 for (ptr = pptr->children; ptr; ptr = ptr->next)
576 if (ptr->type == XML_ELEMENT_NODE &&
577 !xmlStrcmp(ptr->name, BAD_CAST "term"))
582 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
583 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
585 if (ptr->type == XML_ELEMENT_NODE &&
586 !xmlStrcmp(ptr->name, BAD_CAST "term"))
587 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
590 else if (o->direction == ODR_ENCODE)
593 for (i = 0; i < *num; i++)
595 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
596 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
602 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
603 void *client_data, const char *ns)
605 xmlNodePtr pptr = (xmlNodePtr) vptr;
606 if (o->direction == ODR_DECODE)
608 Z_SRW_PDU **p = handler_data;
609 xmlNodePtr method = pptr->children;
612 while (method && method->type == XML_TEXT_NODE)
613 method = method->next;
617 if (method->type != XML_ELEMENT_NODE)
620 *p = yaz_srw_get_core_v_2_0(o);
622 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
624 xmlNodePtr ptr = method->children;
625 Z_SRW_searchRetrieveRequest *req;
626 char *recordPacking = 0;
627 char *recordXMLEscaping = 0;
628 const char *facetLimit = 0;
630 (*p)->which = Z_SRW_searchRetrieve_request;
631 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
632 odr_malloc(o, sizeof(*req));
633 req->queryType = "cql";
635 req->sort_type = Z_SRW_sort_type_none;
637 req->startRecord = 0;
638 req->maximumRecords = 0;
639 req->recordSchema = 0;
640 req->recordPacking = 0;
642 req->recordXPath = 0;
643 req->resultSetTTL = 0;
647 for (; ptr; ptr = ptr->next)
649 if (yaz_match_xsd_string(ptr, "version", o,
652 else if (yaz_match_xsd_string(ptr, "queryType", o,
655 else if (yaz_match_xsd_string(ptr, "query", o,
658 else if (yaz_match_xsd_string(ptr, "pQuery", o,
660 req->queryType = "pqf";
661 else if (yaz_match_xsd_string(ptr, "xQuery", o,
663 req->queryType = "xcql";
664 else if (yaz_match_xsd_integer(ptr, "startRecord", o,
667 else if (yaz_match_xsd_integer(ptr, "maximumRecords", o,
668 &req->maximumRecords))
670 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
673 else if (yaz_match_xsd_string(ptr, "recordXMLEscaping", o,
676 else if (yaz_match_xsd_string(ptr, "recordSchema", o,
679 else if (yaz_match_xsd_string(ptr, "recordXPath", o,
682 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
685 else if (yaz_match_xsd_string(ptr, "sortKeys", o,
686 &req->sort.sortKeys))
687 req->sort_type = Z_SRW_sort_type_sort;
688 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
691 else if (yaz_match_xsd_string(ptr, "database", o, &req->database))
693 else if (yaz_match_xsd_string(ptr, "facetLimit", o,
694 (char**) &facetLimit))
699 /* should put proper diagnostic here */
702 if (!strcmp((*p)->srw_version, "2.0"))
704 req->recordPacking = recordXMLEscaping;
705 req->packing = recordPacking;
709 req->recordPacking = recordPacking;
711 yaz_sru_facet_request(o, &req->facetList, &facetLimit);
713 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
715 xmlNodePtr ptr = method->children;
716 Z_SRW_searchRetrieveResponse *res;
718 (*p)->which = Z_SRW_searchRetrieve_response;
719 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
720 odr_malloc(o, sizeof(*res));
722 res->numberOfRecords = 0;
723 res->resultCountPrecision = 0;
724 res->resultSetId = 0;
725 res->resultSetIdleTime = 0;
727 res->num_records = 0;
728 res->diagnostics = 0;
729 res->num_diagnostics = 0;
730 res->nextRecordPosition = 0;
732 res->suggestions = 0;
734 for (; ptr; ptr = ptr->next)
736 if (yaz_match_xsd_string(ptr, "version", o,
739 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
740 &(*p)->extraResponseData_buf,
741 &(*p)->extraResponseData_len))
743 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
744 &res->numberOfRecords))
746 else if (yaz_match_xsd_string(ptr, "resultCountPrecision", o,
747 &res->resultCountPrecision))
749 else if (yaz_match_xsd_string(ptr, "resultSetId", o,
752 else if (yaz_match_xsd_integer(ptr, "resultSetIdleTime", o,
753 &res->resultSetIdleTime))
755 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
756 &res->resultSetIdleTime))
758 else if (yaz_match_xsd_element(ptr, "records"))
759 yaz_srw_records(o, ptr, &res->records,
761 &res->num_records, client_data, 0);
762 else if (yaz_match_xsd_integer(ptr, "nextRecordPosition", o,
763 &res->nextRecordPosition))
765 else if (yaz_match_xsd_element(ptr, "diagnostics"))
766 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
767 &res->num_diagnostics,
769 else if (yaz_match_xsd_element(ptr, "facet_analysis"))
770 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
771 else if (yaz_match_xsd_element(ptr, "facetedResults"))
772 yaz_sru_facet_response(o, &res->facetList, ptr);
775 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
777 Z_SRW_explainRequest *req;
778 xmlNodePtr ptr = method->children;
780 (*p)->which = Z_SRW_explain_request;
781 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
782 odr_malloc(o, sizeof(*req));
783 req->recordPacking = 0;
787 for (; ptr; ptr = ptr->next)
789 if (yaz_match_xsd_string(ptr, "version", o,
792 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
793 &(*p)->extraResponseData_buf,
794 &(*p)->extraResponseData_len))
796 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
799 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
800 &req->recordPacking))
803 yaz_match_xsd_string(ptr, "database", o, &req->database);
806 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
808 Z_SRW_explainResponse *res;
809 xmlNodePtr ptr = method->children;
811 (*p)->which = Z_SRW_explain_response;
812 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
813 odr_malloc(o, sizeof(*res));
814 res->diagnostics = 0;
815 res->num_diagnostics = 0;
816 res->record.recordSchema = 0;
817 res->record.recordData_buf = 0;
818 res->record.recordData_len = 0;
819 res->record.recordPosition = 0;
821 for (; ptr; ptr = ptr->next)
823 if (yaz_match_xsd_string(ptr, "version", o,
826 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
827 &(*p)->extraResponseData_buf,
828 &(*p)->extraResponseData_len))
830 else if (yaz_match_xsd_element(ptr, "record"))
831 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
833 else if (yaz_match_xsd_element(ptr, "diagnostics"))
834 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
835 &res->num_diagnostics,
840 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
842 Z_SRW_scanRequest *req;
843 xmlNodePtr ptr = method->children;
845 (*p)->which = Z_SRW_scan_request;
846 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
847 odr_malloc(o, sizeof(*req));
848 req->queryType = "cql";
850 req->responsePosition = 0;
851 req->maximumTerms = 0;
855 for (; ptr; ptr = ptr->next)
857 if (yaz_match_xsd_string(ptr, "version", o,
860 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
861 &(*p)->extraResponseData_buf,
862 &(*p)->extraResponseData_len))
864 else if (yaz_match_xsd_string(ptr, "scanClause", o,
867 else if (yaz_match_xsd_string(ptr, "pScanClause", o,
870 req->queryType = "pqf";
872 else if (yaz_match_xsd_integer(ptr, "responsePosition", o,
873 &req->responsePosition))
875 else if (yaz_match_xsd_integer(ptr, "maximumTerms", o,
878 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
882 yaz_match_xsd_string(ptr, "database", o, &req->database);
885 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
887 Z_SRW_scanResponse *res;
888 xmlNodePtr ptr = method->children;
890 (*p)->which = Z_SRW_scan_response;
891 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
892 odr_malloc(o, sizeof(*res));
895 res->diagnostics = 0;
896 res->num_diagnostics = 0;
898 for (; ptr; ptr = ptr->next)
900 if (yaz_match_xsd_string(ptr, "version", o,
903 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
904 &(*p)->extraResponseData_buf,
905 &(*p)->extraResponseData_len))
907 else if (yaz_match_xsd_element(ptr, "terms"))
908 yaz_srw_terms(o, ptr, &res->terms,
909 &res->num_terms, client_data,
911 else if (yaz_match_xsd_element(ptr, "diagnostics"))
912 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
913 &res->num_diagnostics,
922 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
924 (*p)->srw_version = neg_version;
926 else if (o->direction == ODR_ENCODE)
928 Z_SRW_PDU **p = handler_data;
931 int version2 = !(*p)->srw_version ||
932 strcmp((*p)->srw_version, "2.") > 0;
933 if ((*p)->which == Z_SRW_searchRetrieve_request)
935 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
936 const char *queryType = req->queryType;
938 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
939 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
940 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
941 xmlSetNs(ptr, ns_srw);
943 add_xsd_string(ptr, "version", (*p)->srw_version);
947 add_xsd_string(ptr, "queryType", queryType);
948 add_xsd_string(ptr, "query", req->query);
952 if (!queryType || !strcmp(queryType, "cql"))
953 add_xsd_string(ptr, "query", req->query);
954 else if (!strcmp(queryType, "xcql"))
955 add_xsd_string(ptr, "xQuery", req->query);
956 else if (!strcmp(queryType, "pqf"))
957 add_xsd_string(ptr, "pQuery", req->query);
959 add_xsd_integer(ptr, "startRecord", req->startRecord);
960 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
963 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
964 add_xsd_string(ptr, "recordPacking", req->packing);
967 add_xsd_string(ptr, "recordPacking", req->recordPacking);
968 add_xsd_string(ptr, "recordSchema", req->recordSchema);
969 add_xsd_string(ptr, "recordXPath", req->recordXPath);
970 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
971 switch (req->sort_type)
973 case Z_SRW_sort_type_none:
975 case Z_SRW_sort_type_sort:
976 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
978 case Z_SRW_sort_type_xSort:
979 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
982 add_xsd_string(ptr, "stylesheet", req->stylesheet);
983 add_xsd_string(ptr, "database", req->database);
985 const char *limit = 0;
986 yaz_sru_facet_request(o, &req->facetList, &limit);
987 add_xsd_string(ptr, "facetLimit", limit);
990 else if ((*p)->which == Z_SRW_searchRetrieve_response)
992 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
994 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
995 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
996 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
997 xmlSetNs(ptr, ns_srw);
999 add_xsd_string(ptr, "version", (*p)->srw_version);
1000 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1001 add_xsd_string(ptr, "resultSetId", res->resultSetId);
1002 add_xsd_integer(ptr,
1003 version2 ? "resultSetTTL" : "resultSetIdleTime" ,
1004 res->resultSetIdleTime);
1005 if (res->num_records)
1007 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1008 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1010 client_data, version2);
1012 add_xsd_integer(ptr, "nextRecordPosition",
1013 res->nextRecordPosition);
1014 if (res->num_diagnostics)
1016 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1018 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1019 &res->num_diagnostics, client_data, ns);
1021 if (res->resultCountPrecision)
1022 add_xsd_string(ptr, "resultCountPrecision",
1023 res->resultCountPrecision);
1024 yaz_sru_facet_response(o, &res->facetList, ptr);
1026 else if ((*p)->which == Z_SRW_explain_request)
1028 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1029 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 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);
1036 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1037 add_xsd_string(ptr, "recordPacking", req->packing);
1040 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1041 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1042 add_xsd_string(ptr, "database", req->database);
1044 else if ((*p)->which == Z_SRW_explain_response)
1046 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1047 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1048 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1049 xmlSetNs(ptr, ns_srw);
1051 add_xsd_string(ptr, "version", (*p)->srw_version);
1054 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1055 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1056 client_data, version2);
1058 if (res->num_diagnostics)
1060 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1062 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1063 &res->num_diagnostics, client_data, ns);
1066 else if ((*p)->which == Z_SRW_scan_request)
1068 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1069 const char *queryType = req->queryType;
1071 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1072 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1073 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1074 xmlSetNs(ptr, ns_srw);
1076 add_xsd_string(ptr, "version", (*p)->srw_version);
1080 if (queryType && strcmp(queryType, "cql"))
1081 add_xsd_string(ptr, "queryType", queryType);
1082 add_xsd_string(ptr, "scanClause", req->scanClause);
1086 if (!queryType || !strcmp(queryType, "cql"))
1087 add_xsd_string(ptr, "scanClause", req->scanClause);
1088 else if (!strcmp(queryType, "pqf"))
1089 add_xsd_string(ptr, "pScanClause", req->scanClause);
1091 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1092 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1093 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1094 add_xsd_string(ptr, "database", req->database);
1096 else if ((*p)->which == Z_SRW_scan_response)
1098 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1100 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1101 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1102 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1103 xmlSetNs(ptr, ns_srw);
1105 add_xsd_string(ptr, "version", (*p)->srw_version);
1108 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1109 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1112 if (res->num_diagnostics)
1114 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1116 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1117 &res->num_diagnostics, client_data, ns);
1122 if (ptr && (*p)->extraResponseData_len)
1123 add_XML_n(ptr, "extraResponseData",
1124 (*p)->extraResponseData_buf,
1125 (*p)->extraResponseData_len, ns_srw);
1132 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1133 void *client_data, const char *ns_ucp_str)
1135 xmlNodePtr pptr = (xmlNodePtr) vptr;
1136 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1137 if (o->direction == ODR_DECODE)
1139 Z_SRW_PDU **p = handler_data;
1140 xmlNodePtr method = pptr->children;
1142 while (method && method->type == XML_TEXT_NODE)
1143 method = method->next;
1147 if (method->type != XML_ELEMENT_NODE)
1150 *p = yaz_srw_get_core_v_2_0(o);
1152 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1154 xmlNodePtr ptr = method->children;
1155 Z_SRW_updateRequest *req;
1158 (*p)->which = Z_SRW_update_request;
1159 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1160 odr_malloc(o, sizeof(*req));
1164 req->recordVersions = 0;
1165 req->num_recordVersions = 0;
1167 req->extra_record = 0;
1168 req->extraRequestData_buf = 0;
1169 req->extraRequestData_len = 0;
1170 req->stylesheet = 0;
1172 for (; ptr; ptr = ptr->next)
1174 if (yaz_match_xsd_string(ptr, "version", o,
1175 &(*p)->srw_version))
1177 else if (yaz_match_xsd_string(ptr, "action", o,
1181 if (!strcmp(oper, "info:srw/action/1/delete"))
1182 req->operation = "delete";
1183 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1184 req->operation = "replace";
1185 else if (!strcmp(oper, "info:srw/action/1/create"))
1186 req->operation = "insert";
1189 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1192 else if (yaz_match_xsd_element(ptr, "recordVersions" ) )
1193 yaz_srw_versions( o, ptr, &req->recordVersions,
1194 &req->num_recordVersions, client_data,
1196 else if (yaz_match_xsd_element(ptr, "record"))
1198 req->record = yaz_srw_get_record(o);
1199 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1202 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
1206 yaz_match_xsd_string(ptr, "database", o, &req->database);
1209 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1211 xmlNodePtr ptr = method->children;
1212 Z_SRW_updateResponse *res;
1214 (*p)->which = Z_SRW_update_response;
1215 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1216 odr_malloc(o, sizeof(*res));
1218 res->operationStatus = 0;
1220 res->recordVersions = 0;
1221 res->num_recordVersions = 0;
1222 res->diagnostics = 0;
1223 res->num_diagnostics = 0;
1225 res->extra_record = 0;
1226 res->extraResponseData_buf = 0;
1227 res->extraResponseData_len = 0;
1229 for (; ptr; ptr = ptr->next)
1231 if (yaz_match_xsd_string(ptr, "version", o,
1232 &(*p)->srw_version))
1234 else if (yaz_match_xsd_string(ptr, "operationStatus", o,
1235 &res->operationStatus ))
1237 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1240 else if (yaz_match_xsd_element(ptr, "recordVersions" ))
1241 yaz_srw_versions(o, ptr, &res->recordVersions,
1242 &res->num_recordVersions,
1243 client_data, ns_ucp_str);
1244 else if (yaz_match_xsd_element(ptr, "record"))
1246 res->record = yaz_srw_get_record(o);
1247 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1250 else if (yaz_match_xsd_element(ptr, "diagnostics"))
1251 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1252 &res->num_diagnostics,
1253 client_data, ns_ucp_str);
1256 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1259 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1268 else if (o->direction == ODR_ENCODE)
1270 Z_SRW_PDU **p = handler_data;
1271 xmlNsPtr ns_ucp, ns_srw;
1273 if ((*p)->which == Z_SRW_update_request)
1275 Z_SRW_updateRequest *req = (*p)->u.update_request;
1276 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1277 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1278 xmlSetNs(ptr, ns_ucp);
1279 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1281 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1282 add_xsd_string(ptr, "action", req->operation);
1283 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1284 if (req->recordVersions)
1285 yaz_srw_versions( o, ptr, &req->recordVersions,
1286 &req->num_recordVersions,
1287 client_data, ns_ucp_str);
1288 if (req->record && req->record->recordData_len)
1290 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1291 xmlSetNs(rptr, ns_srw);
1292 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1295 if (req->extraRequestData_len)
1297 add_XML_n(ptr, "extraRequestData",
1298 req->extraRequestData_buf,
1299 req->extraRequestData_len, ns_srw);
1301 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1302 add_xsd_string(ptr, "database", req->database);
1304 else if ((*p)->which == Z_SRW_update_response)
1306 Z_SRW_updateResponse *res = (*p)->u.update_response;
1307 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1308 "updateResponse", 0);
1309 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1310 xmlSetNs(ptr, ns_ucp);
1311 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1313 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1314 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1315 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1316 if (res->recordVersions)
1317 yaz_srw_versions(o, ptr, &res->recordVersions,
1318 &res->num_recordVersions,
1319 client_data, ns_ucp_str);
1320 if (res->record && res->record->recordData_len)
1322 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1323 xmlSetNs(rptr, ns_srw);
1324 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1327 if (res->num_diagnostics)
1330 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1333 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1334 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1335 &res->num_diagnostics, client_data,
1338 if (res->extraResponseData_len)
1339 add_XML_n(ptr, "extraResponseData",
1340 res->extraResponseData_buf,
1341 res->extraResponseData_len, ns_srw);
1355 * c-file-style: "Stroustrup"
1356 * indent-tabs-mode: nil
1358 * vim: shiftwidth=4 tabstop=8 expandtab