2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.4 2003-12-20 22:40:46 adam Exp $
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
14 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len)
18 xmlDocPtr doc = xmlParseMemory(val,len);
21 xmlNodePtr c = xmlNewChild(ptr, 0, elem, 0);
22 xmlNodePtr t = xmlDocGetRootElement(doc);
23 xmlAddChild(c, xmlCopyNode(t,1));
29 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, char *val,
34 xmlNodePtr c = xmlNewChild(ptr, 0, elem, 0);
35 xmlNodePtr t = xmlNewTextLen(val, len);
42 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, char *val)
45 return xmlNewChild(ptr, 0, elem, val);
49 static void add_xsd_integer(xmlNodePtr ptr, const char *elem, int *val)
54 sprintf(str, "%d", *val);
55 xmlNewChild(ptr, 0, elem, str);
59 static int match_element(xmlNodePtr ptr, const char *elem)
61 if (ptr->type == XML_ELEMENT_NODE && !strcmp(ptr->name, elem))
68 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
72 struct _xmlAttr *attr;
74 if (!match_element(ptr, elem))
77 for (attr = ptr->properties; attr; attr = attr->next)
78 if (!strcmp(attr->name, "type") &&
79 attr->children && attr->children->type == XML_TEXT_NODE)
81 const char *t = strchr(attr->children->content, ':');
85 t = attr->children->content;
86 if (!strcmp(t, "string"))
93 if (!ptr || ptr->type != XML_TEXT_NODE)
95 *val = odr_strdup(o, ptr->content);
97 *len = strlen(ptr->content);
102 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
105 return match_xsd_string_n(ptr, elem, o, val, 0);
108 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
109 char **val, int *len)
113 if (!match_element(ptr, elem))
118 buf = xmlBufferCreate();
120 xmlNodeDump(buf, ptr->doc, ptr, 0, 0);
122 *val = odr_malloc(o, buf->use+1);
123 memcpy (*val, buf->content, buf->use);
124 (*val)[buf->use] = '\0';
135 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
138 struct _xmlAttr *attr;
140 if (!match_element(ptr, elem))
143 for (attr = ptr->properties; attr; attr = attr->next)
144 if (!strcmp(attr->name, "type") &&
145 attr->children && attr->children->type == XML_TEXT_NODE)
147 const char *t = strchr(attr->children->content, ':');
151 t = attr->children->content;
152 if (!strcmp(t, "integer"))
159 if (!ptr || ptr->type != XML_TEXT_NODE)
161 *val = odr_intdup(o, atoi(ptr->content));
165 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
166 void *client_data, const char *ns)
168 if (o->direction == ODR_DECODE)
170 int pack = Z_SRW_recordPacking_string;
172 rec->recordSchema = 0;
173 rec->recordData_buf = 0;
174 rec->recordData_len = 0;
175 rec->recordPosition = 0;
176 for (ptr = pptr->children; ptr; ptr = ptr->next)
180 if (match_xsd_string(ptr, "recordSchema", o,
183 else if (match_xsd_string(ptr, "recordPacking", o, &spack))
185 if (pack && !strcmp(spack, "xml"))
186 pack = Z_SRW_recordPacking_XML;
187 if (pack && !strcmp(spack, "string"))
188 pack = Z_SRW_recordPacking_string;
190 else if (match_xsd_integer(ptr, "recordPosition", o,
191 &rec->recordPosition))
195 if (pack == Z_SRW_recordPacking_XML)
196 match_xsd_XML_n(ptr, "recordData", o,
197 &rec->recordData_buf,
198 &rec->recordData_len);
199 if (pack == Z_SRW_recordPacking_string)
200 match_xsd_string_n(ptr, "recordData", o,
201 &rec->recordData_buf,
202 &rec->recordData_len);
205 rec->recordPacking = pack;
207 else if (o->direction == ODR_ENCODE)
209 xmlNodePtr ptr = pptr;
210 add_xsd_string(ptr, "recordSchema", rec->recordSchema);
211 switch(rec->recordPacking)
213 case Z_SRW_recordPacking_string:
214 add_xsd_string(ptr, "recordPacking", "string");
215 add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
216 rec->recordData_len);
218 case Z_SRW_recordPacking_XML:
219 add_xsd_string(ptr, "recordPacking", "xml");
220 add_XML_n(ptr, "recordData", rec->recordData_buf,
221 rec->recordData_len);
224 add_xsd_integer(ptr, "recordPosition", rec->recordPosition);
228 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
229 int *num, void *client_data, const char *ns)
231 if (o->direction == ODR_DECODE)
236 for (ptr = pptr->children; ptr; ptr = ptr->next)
238 if (ptr->type == XML_ELEMENT_NODE &&
239 !strcmp(ptr->name, "record"))
244 *recs = odr_malloc(o, *num * sizeof(**recs));
245 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
247 if (ptr->type == XML_ELEMENT_NODE &&
248 !strcmp(ptr->name, "record"))
249 yaz_srw_record(o, ptr, (*recs)+i, client_data, ns);
252 else if (o->direction == ODR_ENCODE)
255 for (i = 0; i < *num; i++)
257 xmlNodePtr rptr = xmlNewChild(pptr, 0, "record", 0);
258 yaz_srw_record(o, rptr, (*recs)+i, client_data, ns);
264 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
265 int *num, void *client_data, const char *ns)
267 if (o->direction == ODR_DECODE)
272 for (ptr = pptr->children; ptr; ptr = ptr->next)
274 if (ptr->type == XML_ELEMENT_NODE &&
275 !strcmp(ptr->name, "diagnostic"))
280 *recs = odr_malloc(o, *num * sizeof(**recs));
281 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
283 if (ptr->type == XML_ELEMENT_NODE &&
284 !strcmp(ptr->name, "diagnostic"))
288 (*recs)[i].details = 0;
289 for (rptr = ptr->children; rptr; rptr = rptr->next)
291 if (match_xsd_integer(rptr, "code", o,
294 else if (match_xsd_string(rptr, "details", o,
295 &(*recs)[i].details))
302 else if (o->direction == ODR_ENCODE)
305 for (i = 0; i < *num; i++)
307 xmlNodePtr rptr = xmlNewChild(pptr, 0, "diagnostic", 0);
308 add_xsd_integer(rptr, "code", (*recs)[i].code);
309 add_xsd_string(rptr, "details", (*recs)[i].details);
316 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
317 void *client_data, const char *ns)
319 xmlNodePtr pptr = vptr;
320 if (o->direction == ODR_DECODE)
322 Z_SRW_PDU **p = handler_data;
323 xmlNodePtr method = pptr->children;
325 while (method && method->type == XML_TEXT_NODE)
326 method = method->next;
330 if (method->type != XML_ELEMENT_NODE)
333 *p = odr_malloc(o, sizeof(**p));
334 (*p)->srw_version = odr_strdup(o, "1.1");
336 if (!strcmp(method->name, "searchRetrieveRequest"))
338 xmlNodePtr ptr = method->children;
339 Z_SRW_searchRetrieveRequest *req;
341 (*p)->which = Z_SRW_searchRetrieve_request;
342 req = (*p)->u.request = odr_malloc(o, sizeof(*req));
343 req->query_type = Z_SRW_query_type_cql;
345 req->sort_type = Z_SRW_sort_type_none;
347 req->startRecord = 0;
348 req->maximumRecords = 0;
349 req->recordSchema = 0;
350 req->recordPacking = 0;
353 for (; ptr; ptr = ptr->next)
355 if (match_xsd_string(ptr, "query", o,
357 req->query_type = Z_SRW_query_type_cql;
358 else if (match_xsd_string(ptr, "pQuery", o,
360 req->query_type = Z_SRW_query_type_pqf;
361 else if (match_xsd_string(ptr, "xQuery", o,
363 req->query_type = Z_SRW_query_type_xcql;
364 else if (match_xsd_string(ptr, "sortKeys", o,
365 &req->sort.sortKeys))
366 req->sort_type = Z_SRW_sort_type_sort;
367 else if (match_xsd_string(ptr, "recordSchema", o,
370 else if (match_xsd_string(ptr, "recordPacking", o,
371 &req->recordPacking))
373 else if (match_xsd_integer(ptr, "startRecord", o,
376 else if (match_xsd_integer(ptr, "maximumRecords", o,
377 &req->maximumRecords))
379 else if (match_xsd_string(ptr, "database", o,
382 else if (match_xsd_string(ptr, "version", o,
385 /* missing is xQuery, xSortKeys .. */
388 else if (!strcmp(method->name, "searchRetrieveResponse"))
390 xmlNodePtr ptr = method->children;
391 Z_SRW_searchRetrieveResponse *res;
393 (*p)->which = Z_SRW_searchRetrieve_response;
394 res = (*p)->u.response = odr_malloc(o, sizeof(*res));
396 res->numberOfRecords = 0;
397 res->resultSetId = 0;
398 res->resultSetIdleTime = 0;
400 res->num_records = 0;
401 res->diagnostics = 0;
402 res->num_diagnostics = 0;
403 res->nextRecordPosition = 0;
405 for (; ptr; ptr = ptr->next)
407 if (match_xsd_integer(ptr, "numberOfRecords", o,
408 &res->numberOfRecords))
410 else if (match_xsd_string(ptr, "resultSetId", o,
413 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
414 &res->resultSetIdleTime))
416 else if (match_element(ptr, "records"))
417 yaz_srw_records(o, ptr, &res->records,
418 &res->num_records, client_data,
420 else if (match_element(ptr, "diagnostics"))
421 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
422 &res->num_diagnostics,
424 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
425 &res->nextRecordPosition))
427 else if (match_xsd_string(ptr, "version", o,
432 else if (!strcmp(method->name, "explainRequest"))
434 Z_SRW_explainRequest *req;
435 xmlNodePtr ptr = method->children;
437 (*p)->which = Z_SRW_explain_request;
438 req = (*p)->u.explain_request = odr_malloc(o, sizeof(*req));
439 req->recordPacking = 0;
440 for (; ptr; ptr = ptr->next)
442 if (match_xsd_string(ptr, "recordPacking", o,
443 &req->recordPacking))
445 else if (match_xsd_string(ptr, "version", o,
450 else if (!strcmp(method->name, "explainResponse"))
452 Z_SRW_explainResponse *res;
453 xmlNodePtr ptr = method->children;
455 (*p)->which = Z_SRW_explain_response;
456 res = (*p)->u.explain_response = odr_malloc(o, sizeof(*res));
458 for (; ptr; ptr = ptr->next)
460 if (match_element(ptr, "record"))
461 yaz_srw_record(o, ptr, &res->record, client_data, ns);
462 else if (match_xsd_string(ptr, "version", o,
473 else if (o->direction == ODR_ENCODE)
475 Z_SRW_PDU **p = handler_data;
478 if ((*p)->which == Z_SRW_searchRetrieve_request)
480 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
481 xmlNodePtr ptr = xmlNewChild(pptr, 0,
482 "searchRetrieveRequest", 0);
483 ns_srw = xmlNewNs(ptr, ns, "zs");
484 xmlSetNs(ptr, ns_srw);
486 if ((*p)->srw_version)
487 add_xsd_string(ptr, "version", (*p)->srw_version);
488 switch(req->query_type)
490 case Z_SRW_query_type_cql:
491 add_xsd_string(ptr, "query", req->query.cql);
493 case Z_SRW_query_type_xcql:
494 add_xsd_string(ptr, "xQuery", req->query.xcql);
496 case Z_SRW_query_type_pqf:
497 add_xsd_string(ptr, "pQuery", req->query.pqf);
500 switch(req->sort_type)
502 case Z_SRW_sort_type_none:
504 case Z_SRW_sort_type_sort:
505 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
507 case Z_SRW_sort_type_xSort:
508 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
511 add_xsd_integer(ptr, "startRecord", req->startRecord);
512 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
513 add_xsd_string(ptr, "recordSchema", req->recordSchema);
514 add_xsd_string(ptr, "recordPacking", req->recordPacking);
515 add_xsd_string(ptr, "database", req->database);
517 else if ((*p)->which == Z_SRW_searchRetrieve_response)
519 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
520 xmlNodePtr ptr = xmlNewChild(pptr, 0,
521 "searchRetrieveResponse", 0);
522 ns_srw = xmlNewNs(ptr, ns, "zs");
523 xmlSetNs(ptr, ns_srw);
525 if ((*p)->srw_version)
526 add_xsd_string(ptr, "version", (*p)->srw_version);
527 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
528 add_xsd_string(ptr, "resultSetId", res->resultSetId);
529 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
530 if (res->num_records)
532 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
533 yaz_srw_records(o, rptr, &res->records, &res->num_records,
536 if (res->num_diagnostics)
538 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
539 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
540 &res->num_diagnostics, client_data, ns);
542 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
544 else if ((*p)->which == Z_SRW_explain_request)
546 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainRequest", 0);
547 ns_srw = xmlNewNs(ptr, ns, "zs");
548 xmlSetNs(ptr, ns_srw);
550 if ((*p)->srw_version)
551 add_xsd_string(ptr, "version", (*p)->srw_version);
553 else if ((*p)->which == Z_SRW_explain_response)
555 Z_SRW_explainResponse *res = (*p)->u.explain_response;
556 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainResponse", 0);
557 ns_srw = xmlNewNs(ptr, ns, "zs");
558 xmlSetNs(ptr, ns_srw);
560 if ((*p)->srw_version)
561 add_xsd_string(ptr, "version", (*p)->srw_version);
562 ptr = xmlNewChild(ptr, 0, "record", 0);
563 yaz_srw_record(o, ptr, &res->record, client_data, ns);
572 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
574 Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o));
576 sr->srw_version = odr_strdup(o, "1.1");
580 case Z_SRW_searchRetrieve_request:
581 sr->u.request = (Z_SRW_searchRetrieveRequest *)
582 odr_malloc(o, sizeof(*sr->u.request));
583 sr->u.request->query_type = Z_SRW_query_type_cql;
584 sr->u.request->query.cql = 0;
585 sr->u.request->sort_type = Z_SRW_sort_type_none;
586 sr->u.request->sort.none = 0;
587 sr->u.request->startRecord = 0;
588 sr->u.request->maximumRecords = 0;
589 sr->u.request->recordSchema = 0;
590 sr->u.request->recordPacking = 0;
591 sr->u.request->database = 0;
593 case Z_SRW_searchRetrieve_response:
594 sr->u.response = (Z_SRW_searchRetrieveResponse *)
595 odr_malloc(o, sizeof(*sr->u.response));
596 sr->u.response->numberOfRecords = 0;
597 sr->u.response->resultSetId = 0;
598 sr->u.response->resultSetIdleTime = 0;
599 sr->u.response->records = 0;
600 sr->u.response->num_records = 0;
601 sr->u.response->diagnostics = 0;
602 sr->u.response->num_diagnostics = 0;
603 sr->u.response->nextRecordPosition = 0;
605 case Z_SRW_explain_request:
606 sr->u.explain_request = (Z_SRW_explainRequest *)
607 odr_malloc(o, sizeof(*sr->u.explain_request));
608 sr->u.explain_request->recordPacking = 0;
609 sr->u.explain_request->database = 0;
611 case Z_SRW_explain_response:
612 sr->u.explain_response = (Z_SRW_explainResponse *)
613 odr_malloc(o, sizeof(*sr->u.explain_response));
614 sr->u.explain_response->record.recordData_buf = 0;
615 sr->u.explain_response->record.recordData_len = 0;
616 sr->u.explain_response->record.recordSchema = 0;
617 sr->u.explain_response->record.recordPosition = 0;
618 sr->u.explain_response->record.recordPacking = Z_SRW_recordPacking_string;
629 } yaz_srw_codes [] = {
630 {1, "Permanent system error"},
631 {2, "System temporarily unavailable"},
632 {3, "Authentication error"},
633 /* Diagnostics Relating to CQL */
634 {10, "Illegal query"},
635 {11, "Unsupported query type (XCQL vs CQL)"},
636 {12, "Too many characters in query"},
637 {13, "Unbalanced or illegal use of parentheses"},
638 {14, "Unbalanced or illegal use of quotes"},
639 {15, "Illegal or unsupported context set"},
640 {16, "Illegal or unsupported index"},
641 {17, "Illegal or unsupported combination of index and context set"},
642 {18, "Illegal or unsupported combination of indexes"},
643 {19, "Illegal or unsupported relation"},
644 {20, "Illegal or unsupported relation modifier"},
645 {21, "Illegal or unsupported combination of relation modifers"},
646 {22, "Illegal or unsupported combination of relation and index"},
647 {23, "Too many characters in term"},
648 {24, "Illegal combination of relation and term"},
649 {25, "Special characters not quoted in term"},
650 {26, "Non special character escaped in term"},
651 {27, "Empty term unsupported"},
652 {28, "Masking character not supported"},
653 {29, "Masked words too short"},
654 {30, "Too many masking characters in term"},
655 {31, "Anchoring character not supported"},
656 {32, "Anchoring character in illegal or unsupported position"},
657 {33, "Combination of proximity/adjacency and masking characters not supported"},
658 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
659 {35, "Terms only exclusion (stop) words"},
660 {36, "Term in invalid format for index or relation"},
661 {37, "Illegal or unsupported boolean operator"},
662 {38, "Too many boolean operators in query"},
663 {39, "Proximity not supported"},
664 {40, "Illegal or unsupported proximity relation"},
665 {41, "Illegal or unsupported proximity distance"},
666 {42, "Illegal or unsupported proximity unit"},
667 {43, "Illegal or unsupported proximity ordering"},
668 {44, "Illegal or unsupported combination of proximity modifiers"},
669 {45, "context set name (prefix) assigned to multiple identifiers"},
670 /* Diagnostics Relating to Result Sets */
671 {50, "Result sets not supported"},
672 {51, "Result set does not exist"},
673 {52, "Result set temporarily unavailable"},
674 {53, "Result sets only supported for retrieval"},
675 {54, "Retrieval may only occur from an existing result set"},
676 {55, "Combination of result sets with search terms not supported"},
677 {56, "Only combination of single result set with search terms supported"},
678 {57, "Result set created but no records available"},
679 {58, "Result set created with unpredictable partial results available"},
680 {59, "Result set created with valid partial results available"},
681 /* Diagnostics Relating to Records */
682 {60, "Too many records retrieved"},
683 {61, "First record position out of range"},
684 {62, "Negative number of records requested"},
685 {63, "System error in retrieving records"},
686 {64, "Record temporarily unavailable"},
687 {65, "Record does not exist"},
688 {66, "Unknown schema for retrieval"},
689 {67, "Record not available in this schema"},
690 {68, "Not authorised to send record"},
691 {69, "Not authorised to send record in this schema"},
692 {70, "Record too large to send"},
693 /* Diagnostics Relating to Sorting */
694 {80, "Sort not supported"},
695 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
696 {82, "Illegal or unsupported sort sequence"},
697 {83, "Too many records"},
698 {84, "Too many sort keys"},
699 {85, "Duplicate sort keys"},
700 {86, "Incompatible record formats"},
701 {87, "Unsupported schema for sort"},
702 {88, "Unsupported tag path for sort"},
703 {89, "Tag path illegal or unsupported for schema"},
704 {90, "Illegal or unsupported direction value"},
705 {91, "Illegal or unsupported case value"},
706 {92, "Illegal or unsupported missing value action"},
707 /* Diagnostics Relating to Explain */
708 {100, "Explain not supported"},
709 {101, "Explain request type not supported (SOAP vs GET)"},
710 {102, "Explain record temporarily unavailable"},
714 const char *yaz_diag_srw_str (int code)
717 for (i = 0; yaz_srw_codes[i].code; i++)
718 if (yaz_srw_codes[i].code == code)
719 return yaz_srw_codes[i].msg;
725 static int srw_bib1_map[] = {
754 24, 63, /* bad map */
755 25, 63, /* bad map */
756 26, 63, /* bad map */
764 100, 1, /* bad map */
812 205, 1, /* bad map */
813 206, 1, /* bad map */
815 208, 1, /* bad map */
826 218, 1, /* bad map */
827 219, 1, /* bad map */
828 220, 1, /* bad map */
829 221, 1, /* bad map */
830 222, 1, /* bad map */
831 223, 1, /* bad map */
832 224, 1, /* bad map */
833 225, 1, /* bad map */
834 226, 1, /* bad map */
836 228, 1, /* bad map */
841 233, 1, /* bad map */
842 234, 1, /* bad map */
848 240, 1, /* bad map */
849 241, 1, /* bad map */
851 243, 1, /* bad map */
856 1001, 1, /* bad map */
857 1002, 1, /* bad map */
858 1003, 1, /* bad map */
859 1004, 1, /* bad map */
860 1005, 1, /* bad map */
861 1006, 1, /* bad map */
893 int yaz_diag_bib1_to_srw (int code)
895 const int *p = srw_bib1_map;
905 int yaz_diag_srw_to_bib1(int code)
907 const int *p = srw_bib1_map;