1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRW/SRU package encoding and decoding
16 #include <yaz/wrbuf.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
24 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
29 xmlDocPtr doc = xmlParseMemory(val,len);
32 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
33 xmlNodePtr t = xmlDocGetRootElement(doc);
34 xmlAddChild(c, xmlCopyNode(t,1));
40 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
45 xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
46 xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
53 xmlNodePtr add_xsd_string_ns(xmlNodePtr ptr, const char *elem, const char *val,
58 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
59 xmlNodePtr t = xmlNewText(BAD_CAST val);
66 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
68 return add_xsd_string_ns(ptr, elem, val, 0);
71 static void add_xsd_integer(xmlNodePtr ptr, const char *elem,
77 sprintf(str, ODR_INT_PRINTF, *val);
78 xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
82 static int match_element(xmlNodePtr ptr, const char *elem)
84 if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
93 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
97 struct _xmlAttr *attr;
99 if (!match_element(ptr, elem))
102 for (attr = ptr->properties; attr; attr = attr->next)
103 if (!strcmp(attr->name, "type") &&
104 attr->children && attr->children->type == XML_TEXT_NODE)
106 const char *t = strchr(attr->children->content, ':');
110 t = attr->children->content;
111 if (!strcmp(t, "string"))
118 if (!ptr || ptr->type != XML_TEXT_NODE)
123 *val = odr_strdup(o, (const char *) ptr->content);
125 *len = xmlStrlen(ptr->content);
130 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
133 return match_xsd_string_n(ptr, elem, o, val, 0);
136 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
137 char **val, int *len)
141 if (!match_element(ptr, elem))
144 buf = xmlBufferCreate();
146 /* Copy each element nodes at top.
147 In most cases there is only one root node.. At least one server
148 http://www.theeuropeanlibrary.org/sru/sru.pl
149 has multiple root nodes in recordData.
151 for (ptr = ptr->children; ptr; ptr = ptr->next)
153 if (ptr->type == XML_ELEMENT_NODE)
155 /* copy node to get NS right (bug #740). */
156 xmlNode *tmp = xmlCopyNode(ptr, 1);
158 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
164 *val = (char *) odr_malloc(o, buf->use+1);
165 memcpy (*val, buf->content, buf->use);
166 (*val)[buf->use] = '\0';
176 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
180 struct _xmlAttr *attr;
182 if (!match_element(ptr, elem))
185 for (attr = ptr->properties; attr; attr = attr->next)
186 if (!strcmp(attr->name, "type") &&
187 attr->children && attr->children->type == XML_TEXT_NODE)
189 const char *t = strchr(attr->children->content, ':');
193 t = attr->children->content;
194 if (!strcmp(t, "integer"))
201 if (!ptr || ptr->type != XML_TEXT_NODE)
203 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
207 char *yaz_negotiate_sru_version(char *input_ver)
212 if (!strcmp(input_ver, "1.1"))
214 return "1.2"; /* our latest supported version */
217 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
218 Z_SRW_extra_record **extra,
219 void *client_data, const char *ns)
221 if (o->direction == ODR_DECODE)
223 Z_SRW_extra_record ex;
226 int pack = Z_SRW_recordPacking_string;
228 xmlNodePtr data_ptr = 0;
229 rec->recordSchema = 0;
230 rec->recordData_buf = 0;
231 rec->recordData_len = 0;
232 rec->recordPosition = 0;
235 ex.extraRecordData_buf = 0;
236 ex.extraRecordData_len = 0;
237 ex.recordIdentifier = 0;
239 for (ptr = pptr->children; ptr; ptr = ptr->next)
242 if (match_xsd_string(ptr, "recordSchema", o,
245 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
248 pack = yaz_srw_str_to_pack(spack);
250 else if (match_xsd_integer(ptr, "recordPosition", o,
251 &rec->recordPosition))
253 else if (match_element(ptr, "recordData"))
255 /* save position of Data until after the loop
256 then we will know the packing (hopefully), and
257 unpacking is done once
261 else if (match_xsd_XML_n(ptr, "extraRecordData", o,
262 &ex.extraRecordData_buf,
263 &ex.extraRecordData_len) )
265 else if (match_xsd_string(ptr, "recordIdentifier", o,
266 &ex.recordIdentifier))
274 case Z_SRW_recordPacking_XML:
275 match_xsd_XML_n(data_ptr, "recordData", o,
276 &rec->recordData_buf, &rec->recordData_len);
278 case Z_SRW_recordPacking_URL:
279 /* just store it as a string.
280 leave it to the backend to collect the document */
281 match_xsd_string_n(data_ptr, "recordData", o,
282 &rec->recordData_buf, &rec->recordData_len);
284 case Z_SRW_recordPacking_string:
285 match_xsd_string_n(data_ptr, "recordData", o,
286 &rec->recordData_buf, &rec->recordData_len);
290 rec->recordPacking = pack;
291 if (ex.extraRecordData_buf || ex.recordIdentifier)
293 *extra = (Z_SRW_extra_record *)
294 odr_malloc(o, sizeof(Z_SRW_extra_record));
295 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
298 else if (o->direction == ODR_ENCODE)
300 xmlNodePtr ptr = pptr;
301 int pack = rec->recordPacking;
302 const char *spack = yaz_srw_pack_to_str(pack);
304 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
306 add_xsd_string(ptr, "recordPacking", spack);
309 case Z_SRW_recordPacking_string:
310 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
311 rec->recordData_len);
313 case Z_SRW_recordPacking_XML:
314 add_XML_n(ptr, "recordData", rec->recordData_buf,
315 rec->recordData_len, 0);
317 case Z_SRW_recordPacking_URL:
318 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
319 rec->recordData_len);
322 if (rec->recordPosition)
323 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
326 if ((*extra)->recordIdentifier)
327 add_xsd_string(ptr, "recordIdentifier",
328 (*extra)->recordIdentifier);
329 if ((*extra)->extraRecordData_buf)
330 add_XML_n(ptr, "extraRecordData",
331 (*extra)->extraRecordData_buf,
332 (*extra)->extraRecordData_len, 0);
338 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
339 Z_SRW_extra_record ***extra,
340 int *num, void *client_data, const char *ns)
342 if (o->direction == ODR_DECODE)
347 for (ptr = pptr->children; ptr; ptr = ptr->next)
349 if (ptr->type == XML_ELEMENT_NODE &&
350 !xmlStrcmp(ptr->name, BAD_CAST "record"))
355 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
356 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
357 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
359 if (ptr->type == XML_ELEMENT_NODE &&
360 !xmlStrcmp(ptr->name, BAD_CAST "record"))
362 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, ns);
367 else if (o->direction == ODR_ENCODE)
370 for (i = 0; i < *num; i++)
372 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
374 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
381 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
382 void *client_data, const char *ns)
384 if (o->direction == ODR_DECODE)
387 rec->versionType = 0;
388 rec->versionValue = 0;
389 for (ptr = pptr->children; ptr; ptr = ptr->next)
392 if (match_xsd_string(ptr, "versionType", o,
395 else if (match_xsd_string(ptr, "versionValue", o,
400 else if (o->direction == ODR_ENCODE)
402 xmlNodePtr ptr = pptr;
403 add_xsd_string(ptr, "versionType", rec->versionType);
404 add_xsd_string(ptr, "versionValue", rec->versionValue);
409 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
410 Z_SRW_recordVersion **vers,
411 int *num, void *client_data, const char *ns)
413 if (o->direction == ODR_DECODE)
418 for (ptr = pptr->children; ptr; ptr = ptr->next)
420 if (ptr->type == XML_ELEMENT_NODE &&
421 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
426 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
427 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
429 if (ptr->type == XML_ELEMENT_NODE &&
430 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
432 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
437 else if (o->direction == ODR_ENCODE)
440 for (i = 0; i < *num; i++)
442 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
444 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
450 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node) {
454 WRBUF wrbuf = wrbuf_alloc();
455 const char *freq_string = yaz_element_attribute_value_get(node, "facetvalue", "est_representation");
458 freq = atoi(freq_string);
462 for (child = node->children; child ; child = child->next) {
463 if (child->type == XML_TEXT_NODE)
464 wrbuf_puts(wrbuf, (const char *) child->content);
466 term = term_create(odr, wrbuf_cstr(wrbuf));
467 yaz_log(YLOG_DEBUG, "sru-proxy facet: %s %d", wrbuf_cstr(wrbuf), freq);
468 wrbuf_destroy(wrbuf);
469 return facet_term_create(odr, term, freq);
472 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr) {
473 Z_AttributeList *list;
474 Z_FacetField *facet_field;
479 const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
480 yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
482 list = yaz_use_atttribute_create(odr, name);
483 for (node = ptr->children; node; node = node->next) {
484 if (match_element(node, "facetvalue"))
487 facet_field = facet_field_create(odr, list, num_terms);
489 for (node = ptr->children; node; node = node->next) {
490 if (match_element(node, "facetvalue")) {
491 facet_field_term_set(odr, facet_field, yaz_sru_proxy_get_facet_term_count(odr, node), index);
498 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root, Z_FacetList **facetList)
502 for (ptr = root->children; ptr; ptr = ptr->next)
504 if (match_element(ptr, "facets"))
507 Z_FacetList *facet_list;
509 for (node = ptr->children; node; node= node->next)
511 if (node->type == XML_ELEMENT_NODE)
514 facet_list = facet_list_create(o, num_facets);
516 for (node = ptr->children; node; node= node->next)
518 if (match_element(node, "facet")) {
519 facet_list_field_set(o, facet_list, yaz_sru_proxy_decode_facet_field(o, node), num_facets);
523 *facetList = facet_list;
532 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
533 Z_SRW_diagnostic **recs, int *num,
534 void *client_data, const char *ns)
539 for (ptr = pptr; ptr; ptr = ptr->next)
541 if (ptr->type == XML_ELEMENT_NODE &&
542 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
547 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
548 for (i = 0; i < *num; i++)
551 (*recs)[i].details = 0;
552 (*recs)[i].message = 0;
554 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
556 if (ptr->type == XML_ELEMENT_NODE &&
557 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
561 (*recs)[i].details = 0;
562 (*recs)[i].message = 0;
563 for (rptr = ptr->children; rptr; rptr = rptr->next)
565 if (match_xsd_string(rptr, "uri", o,
568 else if (match_xsd_string(rptr, "details", o,
569 &(*recs)[i].details))
571 else if (match_xsd_string(rptr, "message", o,
572 &(*recs)[i].message))
581 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
582 Z_SRW_diagnostic **diag,
586 xmlDocPtr doc = xmlParseMemory(buf, len);
589 xmlNodePtr ptr = xmlDocGetRootElement(doc);
590 while (ptr && ptr->type != XML_ELEMENT_NODE)
593 && !xmlStrcmp(ptr->ns->href,
594 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
596 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
603 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
604 int *num, void *client_data, const char *ns)
606 if (o->direction == ODR_DECODE)
608 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
610 else if (o->direction == ODR_ENCODE)
614 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
615 for (i = 0; i < *num; i++)
617 const char *std_diag = "info:srw/diagnostic/1/";
618 const char *ucp_diag = "info:srw/diagnostic/12/";
619 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
620 BAD_CAST "diagnostic", 0);
621 add_xsd_string(rptr, "uri", (*recs)[i].uri);
622 if ((*recs)[i].message)
623 add_xsd_string(rptr, "message", (*recs)[i].message);
624 else if ((*recs)[i].uri )
626 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
628 int no = atoi((*recs)[i].uri + strlen(std_diag));
629 const char *message = yaz_diag_srw_str(no);
631 add_xsd_string(rptr, "message", message);
633 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
635 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
636 const char *message = yaz_diag_sru_update_str(no);
638 add_xsd_string(rptr, "message", message);
641 add_xsd_string(rptr, "details", (*recs)[i].details);
647 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
648 void *client_data, const char *ns)
650 if (o->direction == ODR_DECODE)
654 term->numberOfRecords = 0;
655 term->displayTerm = 0;
656 term->whereInList = 0;
657 for (ptr = pptr->children; ptr; ptr = ptr->next)
659 if (match_xsd_string(ptr, "value", o, &term->value))
661 else if (match_xsd_integer(ptr, "numberOfRecords", o,
662 &term->numberOfRecords))
664 else if (match_xsd_string(ptr, "displayTerm", o,
667 else if (match_xsd_string(ptr, "whereInList", o,
672 else if (o->direction == ODR_ENCODE)
674 xmlNodePtr ptr = pptr;
675 add_xsd_string(ptr, "value", term->value);
676 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
677 add_xsd_string(ptr, "displayTerm", term->displayTerm);
678 add_xsd_string(ptr, "whereInList", term->whereInList);
683 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
684 int *num, void *client_data, const char *ns)
686 if (o->direction == ODR_DECODE)
691 for (ptr = pptr->children; ptr; ptr = ptr->next)
693 if (ptr->type == XML_ELEMENT_NODE &&
694 !xmlStrcmp(ptr->name, BAD_CAST "term"))
699 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
700 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
702 if (ptr->type == XML_ELEMENT_NODE &&
703 !xmlStrcmp(ptr->name, BAD_CAST "term"))
704 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
707 else if (o->direction == ODR_ENCODE)
710 for (i = 0; i < *num; i++)
712 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
713 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
719 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
720 void *client_data, const char *ns)
722 xmlNodePtr pptr = (xmlNodePtr) vptr;
723 if (o->direction == ODR_DECODE)
725 Z_SRW_PDU **p = handler_data;
726 xmlNodePtr method = pptr->children;
729 while (method && method->type == XML_TEXT_NODE)
730 method = method->next;
734 if (method->type != XML_ELEMENT_NODE)
737 *p = yaz_srw_get_core_v_1_1(o);
739 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
741 xmlNodePtr ptr = method->children;
742 Z_SRW_searchRetrieveRequest *req;
744 (*p)->which = Z_SRW_searchRetrieve_request;
745 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
746 odr_malloc(o, sizeof(*req));
747 req->query_type = Z_SRW_query_type_cql;
749 req->sort_type = Z_SRW_sort_type_none;
751 req->startRecord = 0;
752 req->maximumRecords = 0;
753 req->recordSchema = 0;
754 req->recordPacking = 0;
755 req->recordXPath = 0;
756 req->resultSetTTL = 0;
760 for (; ptr; ptr = ptr->next)
762 if (match_xsd_string(ptr, "version", o,
765 else if (match_xsd_string(ptr, "query", o,
767 req->query_type = Z_SRW_query_type_cql;
768 else if (match_xsd_string(ptr, "pQuery", o,
770 req->query_type = Z_SRW_query_type_pqf;
771 else if (match_xsd_string(ptr, "xQuery", o,
773 req->query_type = Z_SRW_query_type_xcql;
774 else if (match_xsd_integer(ptr, "startRecord", o,
777 else if (match_xsd_integer(ptr, "maximumRecords", o,
778 &req->maximumRecords))
780 else if (match_xsd_string(ptr, "recordPacking", o,
781 &req->recordPacking))
783 else if (match_xsd_string(ptr, "recordSchema", o,
786 else if (match_xsd_string(ptr, "recordXPath", o,
789 else if (match_xsd_integer(ptr, "resultSetTTL", o,
792 else if (match_xsd_string(ptr, "sortKeys", o,
793 &req->sort.sortKeys))
794 req->sort_type = Z_SRW_sort_type_sort;
795 else if (match_xsd_string(ptr, "stylesheet", o,
798 else if (match_xsd_string(ptr, "database", o,
802 if (!req->query.cql && !req->query.pqf && !req->query.xcql)
804 /* should put proper diagnostic here */
808 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
810 xmlNodePtr ptr = method->children;
811 Z_SRW_searchRetrieveResponse *res;
813 (*p)->which = Z_SRW_searchRetrieve_response;
814 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
815 odr_malloc(o, sizeof(*res));
817 res->numberOfRecords = 0;
818 res->resultSetId = 0;
819 res->resultSetIdleTime = 0;
821 res->num_records = 0;
822 res->diagnostics = 0;
823 res->num_diagnostics = 0;
824 res->nextRecordPosition = 0;
827 for (; ptr; ptr = ptr->next)
829 if (match_xsd_string(ptr, "version", o,
832 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
833 &(*p)->extraResponseData_buf,
834 &(*p)->extraResponseData_len))
836 else if (match_xsd_integer(ptr, "numberOfRecords", o,
837 &res->numberOfRecords))
839 else if (match_xsd_string(ptr, "resultSetId", o,
842 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
843 &res->resultSetIdleTime))
845 else if (match_element(ptr, "records"))
846 yaz_srw_records(o, ptr, &res->records,
848 &res->num_records, client_data, ns);
849 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
850 &res->nextRecordPosition))
852 else if (match_element(ptr, "diagnostics"))
853 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
854 &res->num_diagnostics,
856 else if (match_element(ptr, "facet_analysis"))
857 yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
860 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
862 Z_SRW_explainRequest *req;
863 xmlNodePtr ptr = method->children;
865 (*p)->which = Z_SRW_explain_request;
866 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
867 odr_malloc(o, sizeof(*req));
868 req->recordPacking = 0;
871 for (; ptr; ptr = ptr->next)
873 if (match_xsd_string(ptr, "version", o,
876 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
877 &(*p)->extraResponseData_buf,
878 &(*p)->extraResponseData_len))
880 else if (match_xsd_string(ptr, "stylesheet", o,
883 else if (match_xsd_string(ptr, "recordPacking", o,
884 &req->recordPacking))
886 else if (match_xsd_string(ptr, "database", o,
891 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
893 Z_SRW_explainResponse *res;
894 xmlNodePtr ptr = method->children;
896 (*p)->which = Z_SRW_explain_response;
897 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
898 odr_malloc(o, sizeof(*res));
899 res->diagnostics = 0;
900 res->num_diagnostics = 0;
901 res->record.recordSchema = 0;
902 res->record.recordData_buf = 0;
903 res->record.recordData_len = 0;
904 res->record.recordPosition = 0;
906 for (; ptr; ptr = ptr->next)
908 if (match_xsd_string(ptr, "version", o,
911 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
912 &(*p)->extraResponseData_buf,
913 &(*p)->extraResponseData_len))
915 else if (match_element(ptr, "record"))
916 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
918 else if (match_element(ptr, "diagnostics"))
919 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
920 &res->num_diagnostics,
925 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
927 Z_SRW_scanRequest *req;
928 xmlNodePtr ptr = method->children;
930 (*p)->which = Z_SRW_scan_request;
931 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
932 odr_malloc(o, sizeof(*req));
933 req->query_type = Z_SRW_query_type_cql;
934 req->scanClause.cql = 0;
935 req->responsePosition = 0;
936 req->maximumTerms = 0;
940 for (; ptr; ptr = ptr->next)
942 if (match_xsd_string(ptr, "version", o,
945 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
946 &(*p)->extraResponseData_buf,
947 &(*p)->extraResponseData_len))
949 else if (match_xsd_string(ptr, "scanClause", o,
950 &req->scanClause.cql))
952 else if (match_xsd_string(ptr, "pScanClause", o,
953 &req->scanClause.pqf))
955 req->query_type = Z_SRW_query_type_pqf;
957 else if (match_xsd_integer(ptr, "responsePosition", o,
958 &req->responsePosition))
960 else if (match_xsd_integer(ptr, "maximumTerms", o,
963 else if (match_xsd_string(ptr, "stylesheet", o,
966 else if (match_xsd_string(ptr, "database", o,
971 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
973 Z_SRW_scanResponse *res;
974 xmlNodePtr ptr = method->children;
976 (*p)->which = Z_SRW_scan_response;
977 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
978 odr_malloc(o, sizeof(*res));
981 res->diagnostics = 0;
982 res->num_diagnostics = 0;
984 for (; ptr; ptr = ptr->next)
986 if (match_xsd_string(ptr, "version", o,
989 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
990 &(*p)->extraResponseData_buf,
991 &(*p)->extraResponseData_len))
993 else if (match_element(ptr, "terms"))
994 yaz_srw_terms(o, ptr, &res->terms,
995 &res->num_terms, client_data,
997 else if (match_element(ptr, "diagnostics"))
998 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
999 &res->num_diagnostics,
1008 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
1010 (*p)->srw_version = neg_version;
1012 else if (o->direction == ODR_ENCODE)
1014 Z_SRW_PDU **p = handler_data;
1018 if ((*p)->which == Z_SRW_searchRetrieve_request)
1020 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
1021 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
1022 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1023 xmlSetNs(ptr, ns_srw);
1025 if ((*p)->srw_version)
1026 add_xsd_string(ptr, "version", (*p)->srw_version);
1027 switch(req->query_type)
1029 case Z_SRW_query_type_cql:
1030 add_xsd_string(ptr, "query", req->query.cql);
1032 case Z_SRW_query_type_xcql:
1033 add_xsd_string(ptr, "xQuery", req->query.xcql);
1035 case Z_SRW_query_type_pqf:
1036 add_xsd_string(ptr, "pQuery", req->query.pqf);
1039 add_xsd_integer(ptr, "startRecord", req->startRecord);
1040 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
1041 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1042 add_xsd_string(ptr, "recordSchema", req->recordSchema);
1043 add_xsd_string(ptr, "recordXPath", req->recordXPath);
1044 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
1045 switch(req->sort_type)
1047 case Z_SRW_sort_type_none:
1049 case Z_SRW_sort_type_sort:
1050 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
1052 case Z_SRW_sort_type_xSort:
1053 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
1056 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1057 add_xsd_string(ptr, "database", req->database);
1059 else if ((*p)->which == Z_SRW_searchRetrieve_response)
1061 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
1062 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
1063 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1064 xmlSetNs(ptr, ns_srw);
1066 if ((*p)->srw_version)
1067 add_xsd_string(ptr, "version", (*p)->srw_version);
1068 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1069 add_xsd_string(ptr, "resultSetId", res->resultSetId);
1070 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
1071 if (res->num_records)
1073 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1074 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1078 add_xsd_integer(ptr, "nextRecordPosition",
1079 res->nextRecordPosition);
1080 if (res->num_diagnostics)
1082 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1084 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1085 &res->num_diagnostics, client_data, ns);
1088 else if ((*p)->which == Z_SRW_explain_request)
1090 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1091 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1092 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1093 xmlSetNs(ptr, ns_srw);
1095 add_xsd_string(ptr, "version", (*p)->srw_version);
1096 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1097 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1098 add_xsd_string(ptr, "database", req->database);
1100 else if ((*p)->which == Z_SRW_explain_response)
1102 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1103 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1104 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1105 xmlSetNs(ptr, ns_srw);
1107 add_xsd_string(ptr, "version", (*p)->srw_version);
1110 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1111 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1114 if (res->num_diagnostics)
1116 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1118 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1119 &res->num_diagnostics, client_data, ns);
1122 else if ((*p)->which == Z_SRW_scan_request)
1124 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1125 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1126 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1127 xmlSetNs(ptr, ns_srw);
1129 add_xsd_string(ptr, "version", (*p)->srw_version);
1130 switch(req->query_type)
1132 case Z_SRW_query_type_cql:
1133 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
1135 case Z_SRW_query_type_pqf:
1136 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
1139 add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1140 add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1141 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1142 add_xsd_string(ptr, "database", req->database);
1144 else if ((*p)->which == Z_SRW_scan_response)
1146 Z_SRW_scanResponse *res = (*p)->u.scan_response;
1147 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1148 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1149 xmlSetNs(ptr, ns_srw);
1151 add_xsd_string(ptr, "version", (*p)->srw_version);
1155 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1156 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1159 if (res->num_diagnostics)
1161 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1163 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1164 &res->num_diagnostics, client_data, ns);
1169 if (ptr && (*p)->extraResponseData_len)
1170 add_XML_n(ptr, "extraResponseData",
1171 (*p)->extraResponseData_buf,
1172 (*p)->extraResponseData_len, ns_srw);
1179 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1180 void *client_data, const char *ns_ucp_str)
1182 xmlNodePtr pptr = (xmlNodePtr) vptr;
1183 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1184 if (o->direction == ODR_DECODE)
1186 Z_SRW_PDU **p = handler_data;
1187 xmlNodePtr method = pptr->children;
1189 while (method && method->type == XML_TEXT_NODE)
1190 method = method->next;
1194 if (method->type != XML_ELEMENT_NODE)
1197 *p = yaz_srw_get_core_v_1_1(o);
1199 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1201 xmlNodePtr ptr = method->children;
1202 Z_SRW_updateRequest *req;
1205 (*p)->which = Z_SRW_update_request;
1206 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1207 odr_malloc(o, sizeof(*req));
1211 req->recordVersions = 0;
1212 req->num_recordVersions = 0;
1214 req->extra_record = 0;
1215 req->extraRequestData_buf = 0;
1216 req->extraRequestData_len = 0;
1217 req->stylesheet = 0;
1219 for (; ptr; ptr = ptr->next)
1221 if (match_xsd_string(ptr, "version", o,
1222 &(*p)->srw_version))
1224 else if (match_xsd_string(ptr, "action", o,
1227 if ( !strcmp(oper, "info:srw/action/1/delete"))
1228 req->operation = "delete";
1229 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1230 req->operation = "replace";
1231 else if ( !strcmp( oper, "info:srw/action/1/create"))
1232 req->operation = "insert";
1235 else if (match_xsd_string(ptr, "recordIdentifier", o,
1238 else if (match_element(ptr, "recordVersions" ) )
1239 yaz_srw_versions( o, ptr, &req->recordVersions,
1240 &req->num_recordVersions, client_data,
1242 else if (match_element(ptr, "record"))
1244 req->record = yaz_srw_get_record(o);
1245 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1246 client_data, ns_ucp_str);
1248 else if (match_xsd_string(ptr, "stylesheet", o,
1251 else if (match_xsd_string(ptr, "database", o,
1256 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1258 xmlNodePtr ptr = method->children;
1259 Z_SRW_updateResponse *res;
1261 (*p)->which = Z_SRW_update_response;
1262 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1263 odr_malloc(o, sizeof(*res));
1265 res->operationStatus = 0;
1267 res->recordVersions = 0;
1268 res->num_recordVersions = 0;
1269 res->diagnostics = 0;
1270 res->num_diagnostics = 0;
1272 res->extra_record = 0;
1273 res->extraResponseData_buf = 0;
1274 res->extraResponseData_len = 0;
1276 for (; ptr; ptr = ptr->next)
1278 if (match_xsd_string(ptr, "version", o,
1279 &(*p)->srw_version))
1281 else if (match_xsd_string(ptr, "operationStatus", o,
1282 &res->operationStatus ))
1284 else if (match_xsd_string(ptr, "recordIdentifier", o,
1287 else if (match_element(ptr, "recordVersions" ))
1288 yaz_srw_versions(o, ptr, &res->recordVersions,
1289 &res->num_recordVersions,
1290 client_data, ns_ucp_str);
1291 else if (match_element(ptr, "record"))
1293 res->record = yaz_srw_get_record(o);
1294 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1295 client_data, ns_ucp_str);
1297 else if (match_element(ptr, "diagnostics"))
1298 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1299 &res->num_diagnostics,
1300 client_data, ns_ucp_str);
1303 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1306 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1315 else if (o->direction == ODR_ENCODE)
1317 Z_SRW_PDU **p = handler_data;
1318 xmlNsPtr ns_ucp, ns_srw;
1321 if ((*p)->which == Z_SRW_update_request)
1323 Z_SRW_updateRequest *req = (*p)->u.update_request;
1324 xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1325 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1326 xmlSetNs(ptr, ns_ucp);
1327 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1329 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1330 add_xsd_string(ptr, "action", req->operation);
1331 add_xsd_string(ptr, "recordIdentifier", req->recordId );
1332 if (req->recordVersions)
1333 yaz_srw_versions( o, ptr, &req->recordVersions,
1334 &req->num_recordVersions,
1335 client_data, ns_ucp_str);
1336 if (req->record && req->record->recordData_len)
1338 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1339 xmlSetNs(rptr, ns_srw);
1340 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1341 client_data, ns_ucp_str);
1343 if (req->extraRequestData_len)
1345 add_XML_n(ptr, "extraRequestData",
1346 req->extraRequestData_buf,
1347 req->extraRequestData_len, ns_srw);
1349 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1350 add_xsd_string(ptr, "database", req->database);
1352 else if ((*p)->which == Z_SRW_update_response)
1354 Z_SRW_updateResponse *res = (*p)->u.update_response;
1355 xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1356 "updateResponse", 0);
1357 ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1358 xmlSetNs(ptr, ns_ucp);
1359 ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1361 add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1362 add_xsd_string(ptr, "operationStatus", res->operationStatus );
1363 add_xsd_string(ptr, "recordIdentifier", res->recordId );
1364 if (res->recordVersions)
1365 yaz_srw_versions(o, ptr, &res->recordVersions,
1366 &res->num_recordVersions,
1367 client_data, ns_ucp_str);
1368 if (res->record && res->record->recordData_len)
1370 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1371 xmlSetNs(rptr, ns_srw);
1372 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1373 client_data, ns_ucp_str);
1375 if (res->num_diagnostics)
1378 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1381 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1382 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1383 &res->num_diagnostics, client_data,
1386 if (res->extraResponseData_len)
1387 add_XML_n(ptr, "extraResponseData",
1388 res->extraResponseData_buf,
1389 res->extraResponseData_len, ns_srw);
1404 * c-file-style: "Stroustrup"
1405 * indent-tabs-mode: nil
1407 * vim: shiftwidth=4 tabstop=8 expandtab