1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRW/SRU package encoding and decoding
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
16 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
21 xmlDocPtr doc = xmlParseMemory(val,len);
24 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
25 xmlNodePtr t = xmlDocGetRootElement(doc);
26 xmlAddChild(c, xmlCopyNode(t,1));
32 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
37 xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
38 xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
45 xmlNodePtr add_xsd_string_ns(xmlNodePtr ptr, const char *elem, const char *val,
50 xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
51 xmlNodePtr t = xmlNewText(BAD_CAST val);
58 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
60 return add_xsd_string_ns(ptr, elem, val, 0);
63 static void add_xsd_integer(xmlNodePtr ptr, const char *elem,
69 sprintf(str, ODR_INT_PRINTF, *val);
70 xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
74 static int match_element(xmlNodePtr ptr, const char *elem)
76 if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
85 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
89 struct _xmlAttr *attr;
91 if (!match_element(ptr, elem))
94 for (attr = ptr->properties; attr; attr = attr->next)
95 if (!strcmp(attr->name, "type") &&
96 attr->children && attr->children->type == XML_TEXT_NODE)
98 const char *t = strchr(attr->children->content, ':');
102 t = attr->children->content;
103 if (!strcmp(t, "string"))
110 if (!ptr || ptr->type != XML_TEXT_NODE)
115 *val = odr_strdup(o, (const char *) ptr->content);
117 *len = xmlStrlen(ptr->content);
122 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
125 return match_xsd_string_n(ptr, elem, o, val, 0);
128 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
129 char **val, int *len)
133 if (!match_element(ptr, elem))
136 buf = xmlBufferCreate();
138 /* Copy each element nodes at top.
139 In most cases there is only one root node.. At least one server
140 http://www.theeuropeanlibrary.org/sru/sru.pl
141 has multiple root nodes in recordData.
143 for (ptr = ptr->children; ptr; ptr = ptr->next)
145 if (ptr->type == XML_ELEMENT_NODE)
147 /* copy node to get NS right (bug #740). */
148 xmlNode *tmp = xmlCopyNode(ptr, 1);
150 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
156 *val = (char *) odr_malloc(o, buf->use+1);
157 memcpy (*val, buf->content, buf->use);
158 (*val)[buf->use] = '\0';
168 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
172 struct _xmlAttr *attr;
174 if (!match_element(ptr, elem))
177 for (attr = ptr->properties; attr; attr = attr->next)
178 if (!strcmp(attr->name, "type") &&
179 attr->children && attr->children->type == XML_TEXT_NODE)
181 const char *t = strchr(attr->children->content, ':');
185 t = attr->children->content;
186 if (!strcmp(t, "integer"))
193 if (!ptr || ptr->type != XML_TEXT_NODE)
195 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
199 char *yaz_negotiate_sru_version(char *input_ver)
204 if (!strcmp(input_ver, "1.1"))
206 return "1.2"; /* our latest supported version */
209 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
210 Z_SRW_extra_record **extra,
211 void *client_data, const char *ns)
213 if (o->direction == ODR_DECODE)
215 Z_SRW_extra_record ex;
218 int pack = Z_SRW_recordPacking_string;
220 xmlNodePtr data_ptr = 0;
221 rec->recordSchema = 0;
222 rec->recordData_buf = 0;
223 rec->recordData_len = 0;
224 rec->recordPosition = 0;
227 ex.extraRecordData_buf = 0;
228 ex.extraRecordData_len = 0;
229 ex.recordIdentifier = 0;
231 for (ptr = pptr->children; ptr; ptr = ptr->next)
234 if (match_xsd_string(ptr, "recordSchema", o,
237 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
240 pack = yaz_srw_str_to_pack(spack);
242 else if (match_xsd_integer(ptr, "recordPosition", o,
243 &rec->recordPosition))
245 else if (match_element(ptr, "recordData"))
247 /* save position of Data until after the loop
248 then we will know the packing (hopefully), and
249 unpacking is done once
253 else if (match_xsd_XML_n(ptr, "extraRecordData", o,
254 &ex.extraRecordData_buf,
255 &ex.extraRecordData_len) )
257 else if (match_xsd_string(ptr, "recordIdentifier", o,
258 &ex.recordIdentifier))
266 case Z_SRW_recordPacking_XML:
267 match_xsd_XML_n(data_ptr, "recordData", o,
268 &rec->recordData_buf, &rec->recordData_len);
270 case Z_SRW_recordPacking_URL:
271 /* just store it as a string.
272 leave it to the backend to collect the document */
273 match_xsd_string_n(data_ptr, "recordData", o,
274 &rec->recordData_buf, &rec->recordData_len);
276 case Z_SRW_recordPacking_string:
277 match_xsd_string_n(data_ptr, "recordData", o,
278 &rec->recordData_buf, &rec->recordData_len);
282 rec->recordPacking = pack;
283 if (ex.extraRecordData_buf || ex.recordIdentifier)
285 *extra = (Z_SRW_extra_record *)
286 odr_malloc(o, sizeof(Z_SRW_extra_record));
287 memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
290 else if (o->direction == ODR_ENCODE)
292 xmlNodePtr ptr = pptr;
293 int pack = rec->recordPacking;
294 const char *spack = yaz_srw_pack_to_str(pack);
296 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
298 add_xsd_string(ptr, "recordPacking", spack);
301 case Z_SRW_recordPacking_string:
302 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
303 rec->recordData_len);
305 case Z_SRW_recordPacking_XML:
306 add_XML_n(ptr, "recordData", rec->recordData_buf,
307 rec->recordData_len, 0);
309 case Z_SRW_recordPacking_URL:
310 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
311 rec->recordData_len);
314 if (rec->recordPosition)
315 add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
318 if ((*extra)->recordIdentifier)
319 add_xsd_string(ptr, "recordIdentifier",
320 (*extra)->recordIdentifier);
321 if ((*extra)->extraRecordData_buf)
322 add_XML_n(ptr, "extraRecordData",
323 (*extra)->extraRecordData_buf,
324 (*extra)->extraRecordData_len, 0);
330 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
331 Z_SRW_extra_record ***extra,
332 int *num, void *client_data, const char *ns)
334 if (o->direction == ODR_DECODE)
339 for (ptr = pptr->children; ptr; ptr = ptr->next)
341 if (ptr->type == XML_ELEMENT_NODE &&
342 !xmlStrcmp(ptr->name, BAD_CAST "record"))
347 *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
348 *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
349 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
351 if (ptr->type == XML_ELEMENT_NODE &&
352 !xmlStrcmp(ptr->name, BAD_CAST "record"))
354 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, ns);
359 else if (o->direction == ODR_ENCODE)
362 for (i = 0; i < *num; i++)
364 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
366 yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
373 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
374 void *client_data, const char *ns)
376 if (o->direction == ODR_DECODE)
379 rec->versionType = 0;
380 rec->versionValue = 0;
381 for (ptr = pptr->children; ptr; ptr = ptr->next)
384 if (match_xsd_string(ptr, "versionType", o,
387 else if (match_xsd_string(ptr, "versionValue", o,
392 else if (o->direction == ODR_ENCODE)
394 xmlNodePtr ptr = pptr;
395 add_xsd_string(ptr, "versionType", rec->versionType);
396 add_xsd_string(ptr, "versionValue", rec->versionValue);
401 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
402 Z_SRW_recordVersion **vers,
403 int *num, void *client_data, const char *ns)
405 if (o->direction == ODR_DECODE)
410 for (ptr = pptr->children; ptr; ptr = ptr->next)
412 if (ptr->type == XML_ELEMENT_NODE &&
413 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
418 *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
419 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
421 if (ptr->type == XML_ELEMENT_NODE &&
422 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
424 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
429 else if (o->direction == ODR_ENCODE)
432 for (i = 0; i < *num; i++)
434 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
436 yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
443 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
444 Z_SRW_diagnostic **recs, int *num,
445 void *client_data, const char *ns)
451 for (ptr = pptr; ptr; ptr = ptr->next)
453 if (ptr->type == XML_ELEMENT_NODE &&
454 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
459 *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
460 for (i = 0; i < *num; i++)
463 (*recs)[i].details = 0;
464 (*recs)[i].message = 0;
466 for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
468 if (ptr->type == XML_ELEMENT_NODE &&
469 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
473 (*recs)[i].details = 0;
474 (*recs)[i].message = 0;
475 for (rptr = ptr->children; rptr; rptr = rptr->next)
477 if (match_xsd_string(rptr, "uri", o,
480 else if (match_xsd_string(rptr, "details", o,
481 &(*recs)[i].details))
483 else if (match_xsd_string(rptr, "message", o,
484 &(*recs)[i].message))
493 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
494 Z_SRW_diagnostic **diag,
498 xmlDocPtr doc = xmlParseMemory(buf, len);
501 xmlNodePtr ptr = xmlDocGetRootElement(doc);
502 while (ptr && ptr->type != XML_ELEMENT_NODE)
505 && !xmlStrcmp(ptr->ns->href,
506 BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
508 ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
515 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
516 int *num, void *client_data, const char *ns)
518 if (o->direction == ODR_DECODE)
520 return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
522 else if (o->direction == ODR_ENCODE)
526 xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
527 for (i = 0; i < *num; i++)
529 const char *std_diag = "info:srw/diagnostic/1/";
530 const char *ucp_diag = "info:srw/diagnostic/12/";
531 xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
532 BAD_CAST "diagnostic", 0);
533 add_xsd_string(rptr, "uri", (*recs)[i].uri);
534 if ((*recs)[i].message)
535 add_xsd_string(rptr, "message", (*recs)[i].message);
536 else if ((*recs)[i].uri )
538 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
540 int no = atoi((*recs)[i].uri + strlen(std_diag));
541 const char *message = yaz_diag_srw_str(no);
543 add_xsd_string(rptr, "message", message);
545 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
547 int no = atoi((*recs)[i].uri + strlen(ucp_diag));
548 const char *message = yaz_diag_sru_update_str(no);
550 add_xsd_string(rptr, "message", message);
553 add_xsd_string(rptr, "details", (*recs)[i].details);
559 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
560 void *client_data, const char *ns)
562 if (o->direction == ODR_DECODE)
566 term->numberOfRecords = 0;
567 term->displayTerm = 0;
568 term->whereInList = 0;
569 for (ptr = pptr->children; ptr; ptr = ptr->next)
571 if (match_xsd_string(ptr, "value", o, &term->value))
573 else if (match_xsd_integer(ptr, "numberOfRecords", o,
574 &term->numberOfRecords))
576 else if (match_xsd_string(ptr, "displayTerm", o,
579 else if (match_xsd_string(ptr, "whereInList", o,
584 else if (o->direction == ODR_ENCODE)
586 xmlNodePtr ptr = pptr;
587 add_xsd_string(ptr, "value", term->value);
588 add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
589 add_xsd_string(ptr, "displayTerm", term->displayTerm);
590 add_xsd_string(ptr, "whereInList", term->whereInList);
595 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
596 int *num, void *client_data, const char *ns)
598 if (o->direction == ODR_DECODE)
603 for (ptr = pptr->children; ptr; ptr = ptr->next)
605 if (ptr->type == XML_ELEMENT_NODE &&
606 !xmlStrcmp(ptr->name, BAD_CAST "term"))
611 *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
612 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
614 if (ptr->type == XML_ELEMENT_NODE &&
615 !xmlStrcmp(ptr->name, BAD_CAST "term"))
616 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
619 else if (o->direction == ODR_ENCODE)
622 for (i = 0; i < *num; i++)
624 xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
625 yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
631 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
632 void *client_data, const char *ns)
634 xmlNodePtr pptr = (xmlNodePtr) vptr;
635 if (o->direction == ODR_DECODE)
637 Z_SRW_PDU **p = handler_data;
638 xmlNodePtr method = pptr->children;
641 while (method && method->type == XML_TEXT_NODE)
642 method = method->next;
646 if (method->type != XML_ELEMENT_NODE)
649 *p = yaz_srw_get_core_v_1_1(o);
651 if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
653 xmlNodePtr ptr = method->children;
654 Z_SRW_searchRetrieveRequest *req;
656 (*p)->which = Z_SRW_searchRetrieve_request;
657 req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
658 odr_malloc(o, sizeof(*req));
659 req->query_type = Z_SRW_query_type_cql;
661 req->sort_type = Z_SRW_sort_type_none;
663 req->startRecord = 0;
664 req->maximumRecords = 0;
665 req->recordSchema = 0;
666 req->recordPacking = 0;
667 req->recordXPath = 0;
668 req->resultSetTTL = 0;
672 for (; ptr; ptr = ptr->next)
674 if (match_xsd_string(ptr, "version", o,
677 else if (match_xsd_string(ptr, "query", o,
679 req->query_type = Z_SRW_query_type_cql;
680 else if (match_xsd_string(ptr, "pQuery", o,
682 req->query_type = Z_SRW_query_type_pqf;
683 else if (match_xsd_string(ptr, "xQuery", o,
685 req->query_type = Z_SRW_query_type_xcql;
686 else if (match_xsd_integer(ptr, "startRecord", o,
689 else if (match_xsd_integer(ptr, "maximumRecords", o,
690 &req->maximumRecords))
692 else if (match_xsd_string(ptr, "recordPacking", o,
693 &req->recordPacking))
695 else if (match_xsd_string(ptr, "recordSchema", o,
698 else if (match_xsd_string(ptr, "recordXPath", o,
701 else if (match_xsd_integer(ptr, "resultSetTTL", o,
704 else if (match_xsd_string(ptr, "sortKeys", o,
705 &req->sort.sortKeys))
706 req->sort_type = Z_SRW_sort_type_sort;
707 else if (match_xsd_string(ptr, "stylesheet", o,
710 else if (match_xsd_string(ptr, "database", o,
714 if (!req->query.cql && !req->query.pqf && !req->query.xcql)
716 /* should put proper diagnostic here */
720 else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
722 xmlNodePtr ptr = method->children;
723 Z_SRW_searchRetrieveResponse *res;
725 (*p)->which = Z_SRW_searchRetrieve_response;
726 res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
727 odr_malloc(o, sizeof(*res));
729 res->numberOfRecords = 0;
730 res->resultSetId = 0;
731 res->resultSetIdleTime = 0;
733 res->num_records = 0;
734 res->diagnostics = 0;
735 res->num_diagnostics = 0;
736 res->nextRecordPosition = 0;
738 for (; ptr; ptr = ptr->next)
740 if (match_xsd_string(ptr, "version", o,
743 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
744 &(*p)->extraResponseData_buf,
745 &(*p)->extraResponseData_len))
747 else if (match_xsd_integer(ptr, "numberOfRecords", o,
748 &res->numberOfRecords))
750 else if (match_xsd_string(ptr, "resultSetId", o,
753 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
754 &res->resultSetIdleTime))
756 else if (match_element(ptr, "records"))
757 yaz_srw_records(o, ptr, &res->records,
759 &res->num_records, client_data, ns);
760 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
761 &res->nextRecordPosition))
763 else if (match_element(ptr, "diagnostics"))
764 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
765 &res->num_diagnostics,
769 else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
771 Z_SRW_explainRequest *req;
772 xmlNodePtr ptr = method->children;
774 (*p)->which = Z_SRW_explain_request;
775 req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
776 odr_malloc(o, sizeof(*req));
777 req->recordPacking = 0;
780 for (; ptr; ptr = ptr->next)
782 if (match_xsd_string(ptr, "version", o,
785 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
786 &(*p)->extraResponseData_buf,
787 &(*p)->extraResponseData_len))
789 else if (match_xsd_string(ptr, "stylesheet", o,
792 else if (match_xsd_string(ptr, "recordPacking", o,
793 &req->recordPacking))
795 else if (match_xsd_string(ptr, "database", o,
800 else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
802 Z_SRW_explainResponse *res;
803 xmlNodePtr ptr = method->children;
805 (*p)->which = Z_SRW_explain_response;
806 res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
807 odr_malloc(o, sizeof(*res));
808 res->diagnostics = 0;
809 res->num_diagnostics = 0;
810 res->record.recordSchema = 0;
811 res->record.recordData_buf = 0;
812 res->record.recordData_len = 0;
813 res->record.recordPosition = 0;
815 for (; ptr; ptr = ptr->next)
817 if (match_xsd_string(ptr, "version", o,
820 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
821 &(*p)->extraResponseData_buf,
822 &(*p)->extraResponseData_len))
824 else if (match_element(ptr, "record"))
825 yaz_srw_record(o, ptr, &res->record, &res->extra_record,
827 else if (match_element(ptr, "diagnostics"))
828 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
829 &res->num_diagnostics,
834 else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
836 Z_SRW_scanRequest *req;
837 xmlNodePtr ptr = method->children;
839 (*p)->which = Z_SRW_scan_request;
840 req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
841 odr_malloc(o, sizeof(*req));
842 req->query_type = Z_SRW_query_type_cql;
843 req->scanClause.cql = 0;
844 req->responsePosition = 0;
845 req->maximumTerms = 0;
849 for (; ptr; ptr = ptr->next)
851 if (match_xsd_string(ptr, "version", o,
854 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
855 &(*p)->extraResponseData_buf,
856 &(*p)->extraResponseData_len))
858 else if (match_xsd_string(ptr, "scanClause", o,
859 &req->scanClause.cql))
861 else if (match_xsd_string(ptr, "pScanClause", o,
862 &req->scanClause.pqf))
864 req->query_type = Z_SRW_query_type_pqf;
866 else if (match_xsd_integer(ptr, "responsePosition", o,
867 &req->responsePosition))
869 else if (match_xsd_integer(ptr, "maximumTerms", o,
872 else if (match_xsd_string(ptr, "stylesheet", o,
875 else if (match_xsd_string(ptr, "database", o,
880 else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
882 Z_SRW_scanResponse *res;
883 xmlNodePtr ptr = method->children;
885 (*p)->which = Z_SRW_scan_response;
886 res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
887 odr_malloc(o, sizeof(*res));
890 res->diagnostics = 0;
891 res->num_diagnostics = 0;
893 for (; ptr; ptr = ptr->next)
895 if (match_xsd_string(ptr, "version", o,
898 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
899 &(*p)->extraResponseData_buf,
900 &(*p)->extraResponseData_len))
902 else if (match_element(ptr, "terms"))
903 yaz_srw_terms(o, ptr, &res->terms,
904 &res->num_terms, client_data,
906 else if (match_element(ptr, "diagnostics"))
907 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
908 &res->num_diagnostics,
917 neg_version = yaz_negotiate_sru_version((*p)->srw_version);
919 (*p)->srw_version = neg_version;
921 else if (o->direction == ODR_ENCODE)
923 Z_SRW_PDU **p = handler_data;
927 if ((*p)->which == Z_SRW_searchRetrieve_request)
929 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
930 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
931 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
932 xmlSetNs(ptr, ns_srw);
934 if ((*p)->srw_version)
935 add_xsd_string(ptr, "version", (*p)->srw_version);
936 switch(req->query_type)
938 case Z_SRW_query_type_cql:
939 add_xsd_string(ptr, "query", req->query.cql);
941 case Z_SRW_query_type_xcql:
942 add_xsd_string(ptr, "xQuery", req->query.xcql);
944 case Z_SRW_query_type_pqf:
945 add_xsd_string(ptr, "pQuery", req->query.pqf);
948 add_xsd_integer(ptr, "startRecord", req->startRecord);
949 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
950 add_xsd_string(ptr, "recordPacking", req->recordPacking);
951 add_xsd_string(ptr, "recordSchema", req->recordSchema);
952 add_xsd_string(ptr, "recordXPath", req->recordXPath);
953 add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
954 switch(req->sort_type)
956 case Z_SRW_sort_type_none:
958 case Z_SRW_sort_type_sort:
959 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
961 case Z_SRW_sort_type_xSort:
962 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
965 add_xsd_string(ptr, "stylesheet", req->stylesheet);
966 add_xsd_string(ptr, "database", req->database);
968 else if ((*p)->which == Z_SRW_searchRetrieve_response)
970 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
971 ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
972 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
973 xmlSetNs(ptr, ns_srw);
975 if ((*p)->srw_version)
976 add_xsd_string(ptr, "version", (*p)->srw_version);
977 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
978 add_xsd_string(ptr, "resultSetId", res->resultSetId);
979 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
980 if (res->num_records)
982 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
983 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
987 add_xsd_integer(ptr, "nextRecordPosition",
988 res->nextRecordPosition);
989 if (res->num_diagnostics)
991 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
993 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
994 &res->num_diagnostics, client_data, ns);
997 else if ((*p)->which == Z_SRW_explain_request)
999 Z_SRW_explainRequest *req = (*p)->u.explain_request;
1000 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1001 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1002 xmlSetNs(ptr, ns_srw);
1004 add_xsd_string(ptr, "version", (*p)->srw_version);
1005 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1006 add_xsd_string(ptr, "stylesheet", req->stylesheet);
1007 add_xsd_string(ptr, "database", req->database);
1009 else if ((*p)->which == Z_SRW_explain_response)
1011 Z_SRW_explainResponse *res = (*p)->u.explain_response;
1012 ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1013 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1014 xmlSetNs(ptr, ns_srw);
1016 add_xsd_string(ptr, "version", (*p)->srw_version);
1019 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1020 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1023 if (res->num_diagnostics)
1025 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1027 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1028 &res->num_diagnostics, client_data, ns);
1031 else if ((*p)->which == Z_SRW_scan_request)
1033 Z_SRW_scanRequest *req = (*p)->u.scan_request;
1034 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1035 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1036 xmlSetNs(ptr, ns_srw);
1038 add_xsd_string(ptr, "version", (*p)->srw_version);
1039 switch(req->query_type)
1041 case Z_SRW_query_type_cql:
1042 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
1044 case Z_SRW_query_type_pqf:
1045 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
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;
1056 ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1057 ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1058 xmlSetNs(ptr, ns_srw);
1060 add_xsd_string(ptr, "version", (*p)->srw_version);
1064 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1065 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1068 if (res->num_diagnostics)
1070 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1072 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1073 &res->num_diagnostics, client_data, ns);
1078 if (ptr && (*p)->extraResponseData_len)
1079 add_XML_n(ptr, "extraResponseData",
1080 (*p)->extraResponseData_buf,
1081 (*p)->extraResponseData_len, ns_srw);
1088 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1089 void *client_data, const char *ns_ucp_str)
1091 xmlNodePtr pptr = (xmlNodePtr) vptr;
1092 const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1093 if (o->direction == ODR_DECODE)
1095 Z_SRW_PDU **p = handler_data;
1096 xmlNodePtr method = pptr->children;
1098 while (method && method->type == XML_TEXT_NODE)
1099 method = method->next;
1103 if (method->type != XML_ELEMENT_NODE)
1106 *p = yaz_srw_get_core_v_1_1(o);
1108 if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1110 xmlNodePtr ptr = method->children;
1111 Z_SRW_updateRequest *req;
1114 (*p)->which = Z_SRW_update_request;
1115 req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1116 odr_malloc(o, sizeof(*req));
1120 req->recordVersions = 0;
1121 req->num_recordVersions = 0;
1123 req->extra_record = 0;
1124 req->extraRequestData_buf = 0;
1125 req->extraRequestData_len = 0;
1126 req->stylesheet = 0;
1128 for (; ptr; ptr = ptr->next)
1130 if (match_xsd_string(ptr, "version", o,
1131 &(*p)->srw_version))
1133 else if (match_xsd_string(ptr, "action", o,
1136 if ( !strcmp(oper, "info:srw/action/1/delete"))
1137 req->operation = "delete";
1138 else if (!strcmp(oper,"info:srw/action/1/replace" ))
1139 req->operation = "replace";
1140 else if ( !strcmp( oper, "info:srw/action/1/create"))
1141 req->operation = "insert";
1144 else if (match_xsd_string(ptr, "recordIdentifier", o,
1147 else if (match_element(ptr, "recordVersions" ) )
1148 yaz_srw_versions( o, ptr, &req->recordVersions,
1149 &req->num_recordVersions, client_data,
1151 else if (match_element(ptr, "record"))
1153 req->record = yaz_srw_get_record(o);
1154 yaz_srw_record(o, ptr, req->record, &req->extra_record,
1155 client_data, ns_ucp_str);
1157 else if (match_xsd_string(ptr, "stylesheet", o,
1160 else if (match_xsd_string(ptr, "database", o,
1165 else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1167 xmlNodePtr ptr = method->children;
1168 Z_SRW_updateResponse *res;
1170 (*p)->which = Z_SRW_update_response;
1171 res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1172 odr_malloc(o, sizeof(*res));
1174 res->operationStatus = 0;
1176 res->recordVersions = 0;
1177 res->num_recordVersions = 0;
1178 res->diagnostics = 0;
1179 res->num_diagnostics = 0;
1181 res->extra_record = 0;
1182 res->extraResponseData_buf = 0;
1183 res->extraResponseData_len = 0;
1185 for (; ptr; ptr = ptr->next)
1187 if (match_xsd_string(ptr, "version", o,
1188 &(*p)->srw_version))
1190 else if (match_xsd_string(ptr, "operationStatus", o,
1191 &res->operationStatus ))
1193 else if (match_xsd_string(ptr, "recordIdentifier", o,
1196 else if (match_element(ptr, "recordVersions" ))
1197 yaz_srw_versions(o, ptr, &res->recordVersions,
1198 &res->num_recordVersions,
1199 client_data, ns_ucp_str);
1200 else if (match_element(ptr, "record"))
1202 res->record = yaz_srw_get_record(o);
1203 yaz_srw_record(o, ptr, res->record, &res->extra_record,
1204 client_data, ns_ucp_str);
1206 else if (match_element(ptr, "diagnostics"))
1207 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1208 &res->num_diagnostics,
1209 client_data, ns_ucp_str);
1212 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1215 else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1224 else if (o->direction == ODR_ENCODE)
1226 Z_SRW_PDU **p = handler_data;
1227 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,
1250 client_data, ns_ucp_str);
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,
1282 client_data, ns_ucp_str);
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);
1313 * c-file-style: "Stroustrup"
1314 * indent-tabs-mode: nil
1316 * vim: shiftwidth=4 tabstop=8 expandtab