2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.2 2003-12-18 17:05:20 mike 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 static void 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);
40 static void add_xsd_string(xmlNodePtr ptr, const char *elem, char *val)
43 xmlNewChild(ptr, 0, elem, val);
46 static void add_xsd_integer(xmlNodePtr ptr, const char *elem, int *val)
51 sprintf(str, "%d", *val);
52 xmlNewChild(ptr, 0, elem, str);
56 static int match_element(xmlNodePtr ptr, const char *elem)
58 if (ptr->type == XML_ELEMENT_NODE && !strcmp(ptr->name, elem))
65 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
69 struct _xmlAttr *attr;
71 if (!match_element(ptr, elem))
74 for (attr = ptr->properties; attr; attr = attr->next)
75 if (!strcmp(attr->name, "type") &&
76 attr->children && attr->children->type == XML_TEXT_NODE)
78 const char *t = strchr(attr->children->content, ':');
82 t = attr->children->content;
83 if (!strcmp(t, "string"))
90 if (!ptr || ptr->type != XML_TEXT_NODE)
92 *val = odr_strdup(o, ptr->content);
94 *len = strlen(ptr->content);
99 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
102 return match_xsd_string_n(ptr, elem, o, val, 0);
105 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
106 char **val, int *len)
110 if (!match_element(ptr, elem))
115 buf = xmlBufferCreate();
117 xmlNodeDump(buf, ptr->doc, ptr, 0, 0);
119 *val = odr_malloc(o, buf->use+1);
120 memcpy (*val, buf->content, buf->use);
121 (*val)[buf->use] = '\0';
132 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
135 struct _xmlAttr *attr;
137 if (!match_element(ptr, elem))
140 for (attr = ptr->properties; attr; attr = attr->next)
141 if (!strcmp(attr->name, "type") &&
142 attr->children && attr->children->type == XML_TEXT_NODE)
144 const char *t = strchr(attr->children->content, ':');
148 t = attr->children->content;
149 if (!strcmp(t, "integer"))
156 if (!ptr || ptr->type != XML_TEXT_NODE)
158 *val = odr_intdup(o, atoi(ptr->content));
162 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
163 int *num, void *client_data, const char *ns)
165 if (o->direction == ODR_DECODE)
170 for (ptr = pptr->children; ptr; ptr = ptr->next)
172 if (ptr->type == XML_ELEMENT_NODE &&
173 !strcmp(ptr->name, "record"))
178 *recs = odr_malloc(o, *num * sizeof(**recs));
179 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
181 if (ptr->type == XML_ELEMENT_NODE &&
182 !strcmp(ptr->name, "record"))
185 (*recs)[i].recordSchema = 0;
186 (*recs)[i].recordPacking = Z_SRW_recordPacking_string;
187 (*recs)[i].recordData_buf = 0;
188 (*recs)[i].recordData_len = 0;
189 (*recs)[i].recordPosition = 0;
190 for (rptr = ptr->children; rptr; rptr = rptr->next)
192 if (match_xsd_string(rptr, "recordSchema", o,
193 &(*recs)[i].recordSchema))
195 else if (match_xsd_string_n(rptr, "recordData", o,
196 &(*recs)[i].recordData_buf,
197 &(*recs)[i].recordData_len))
199 else if (match_xsd_XML_n(rptr, "recordXML", o,
200 &(*recs)[i].recordData_buf,
201 &(*recs)[i].recordData_len))
202 (*recs)[i].recordPacking = Z_SRW_recordPacking_XML;
203 else if (match_xsd_integer(rptr, "recordPosition", o,
204 &(*recs)[i].recordPosition))
210 else if (o->direction == ODR_ENCODE)
213 for (i = 0; i < *num; i++)
215 xmlNodePtr rptr = xmlNewChild(pptr, 0, "record", 0);
216 add_xsd_string(rptr, "recordSchema", (*recs)[i].recordSchema);
217 switch((*recs)[i].recordPacking)
219 case Z_SRW_recordPacking_string:
220 add_xsd_string_n(rptr, "recordData", (*recs)[i].recordData_buf,
221 (*recs)[i].recordData_len);
223 case Z_SRW_recordPacking_XML:
224 add_XML_n(rptr, "recordXML", (*recs)[i].recordData_buf,
225 (*recs)[i].recordData_len);
228 add_xsd_integer(rptr, "recordPosition", (*recs)[i].recordPosition);
234 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
235 int *num, void *client_data, const char *ns)
237 if (o->direction == ODR_DECODE)
242 for (ptr = pptr->children; ptr; ptr = ptr->next)
244 if (ptr->type == XML_ELEMENT_NODE &&
245 !strcmp(ptr->name, "diagnostic"))
250 *recs = odr_malloc(o, *num * sizeof(**recs));
251 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
253 if (ptr->type == XML_ELEMENT_NODE &&
254 !strcmp(ptr->name, "diagnostic"))
258 (*recs)[i].details = 0;
259 for (rptr = ptr->children; rptr; rptr = rptr->next)
261 if (match_xsd_integer(rptr, "code", o,
264 else if (match_xsd_string(rptr, "details", o,
265 &(*recs)[i].details))
272 else if (o->direction == ODR_ENCODE)
275 for (i = 0; i < *num; i++)
277 xmlNodePtr rptr = xmlNewChild(pptr, 0, "diagnostic", 0);
278 add_xsd_integer(rptr, "code", (*recs)[i].code);
279 add_xsd_string(rptr, "details", (*recs)[i].details);
286 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
287 void *client_data, const char *ns)
289 xmlNodePtr pptr = vptr;
290 if (o->direction == ODR_DECODE)
292 xmlNodePtr method = pptr->children;
294 while (method && method->type == XML_TEXT_NODE)
295 method = method->next;
297 if (!method || method->type != XML_ELEMENT_NODE)
299 if (!strcmp(method->name, "searchRetrieveRequest"))
301 Z_SRW_PDU **p = handler_data;
302 xmlNodePtr ptr = method->children;
303 Z_SRW_searchRetrieveRequest *req;
305 *p = odr_malloc(o, sizeof(**p));
306 (*p)->which = Z_SRW_searchRetrieve_request;
307 req = (*p)->u.request = odr_malloc(o, sizeof(*req));
308 req->query_type = Z_SRW_query_type_cql;
310 req->sort_type = Z_SRW_sort_type_none;
312 req->startRecord = 0;
313 req->maximumRecords = 0;
314 req->recordSchema = 0;
315 req->recordPacking = 0;
318 for (; ptr; ptr = ptr->next)
320 if (match_xsd_string(ptr, "query", o,
322 req->query_type = Z_SRW_query_type_cql;
323 else if (match_xsd_string(ptr, "pQuery", o,
325 req->query_type = Z_SRW_query_type_pqf;
326 else if (match_xsd_string(ptr, "xQuery", o,
328 req->query_type = Z_SRW_query_type_xcql;
329 else if (match_xsd_string(ptr, "sortKeys", o,
330 &req->sort.sortKeys))
331 req->sort_type = Z_SRW_sort_type_sort;
332 else if (match_xsd_string(ptr, "recordSchema", o,
335 else if (match_xsd_string(ptr, "recordPacking", o,
336 &req->recordPacking))
338 else if (match_xsd_integer(ptr, "startRecord", o,
341 else if (match_xsd_integer(ptr, "maximumRecords", o,
342 &req->maximumRecords))
344 else if (match_xsd_string(ptr, "database", o,
347 /* missing is xQuery, xSortKeys .. */
350 else if (!strcmp(method->name, "searchRetrieveResponse"))
352 Z_SRW_PDU **p = handler_data;
353 xmlNodePtr ptr = method->children;
354 Z_SRW_searchRetrieveResponse *res;
356 *p = odr_malloc(o, sizeof(**p));
357 (*p)->which = Z_SRW_searchRetrieve_response;
358 res = (*p)->u.response = odr_malloc(o, sizeof(*res));
360 res->numberOfRecords = 0;
361 res->resultSetId = 0;
362 res->resultSetIdleTime = 0;
364 res->num_records = 0;
365 res->diagnostics = 0;
366 res->num_diagnostics = 0;
367 res->nextRecordPosition = 0;
369 for (; ptr; ptr = ptr->next)
371 if (match_xsd_integer(ptr, "numberOfRecords", o,
372 &res->numberOfRecords))
374 else if (match_xsd_string(ptr, "resultSetId", o,
377 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
378 &res->resultSetIdleTime))
380 else if (match_element(ptr, "records"))
381 yaz_srw_records(o, ptr, &res->records,
382 &res->num_records, client_data,
384 else if (match_element(ptr, "diagnostics"))
385 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
386 &res->num_diagnostics,
388 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
389 &res->nextRecordPosition))
393 else if (!strcmp(method->name, "explainRequest"))
395 Z_SRW_PDU **p = handler_data;
396 Z_SRW_explainRequest *req;
398 *p = odr_malloc(o, sizeof(**p));
399 (*p)->which = Z_SRW_explain_request;
400 req = (*p)->u.explain_request = odr_malloc(o, sizeof(*req));
403 else if (!strcmp(method->name, "explainResponse"))
405 Z_SRW_PDU **p = handler_data;
406 Z_SRW_explainResponse *res;
407 xmlNodePtr ptr = method->children;
409 *p = odr_malloc(o, sizeof(**p));
410 (*p)->which = Z_SRW_explain_response;
411 res = (*p)->u.explain_response = odr_malloc(o, sizeof(*res));
412 res->explainData_buf = 0;
413 res->explainData_len = 0;
414 res->explainPacking = Z_SRW_recordPacking_string;
415 for (; ptr; ptr = ptr->next)
417 match_xsd_string_n(ptr, "Explain", o,
418 &res->explainData_buf,
419 &res->explainData_len);
426 else if (o->direction == ODR_ENCODE)
428 Z_SRW_PDU **p = handler_data;
429 if ((*p)->which == Z_SRW_searchRetrieve_request)
431 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
432 xmlNodePtr ptr = xmlNewChild(pptr, 0,
433 "searchRetrieveRequest", 0);
434 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
436 xmlSetNs(ptr, ns_srw);
438 switch(req->query_type)
440 case Z_SRW_query_type_cql:
441 add_xsd_string(ptr, "query", req->query.cql);
443 case Z_SRW_query_type_xcql:
444 add_xsd_string(ptr, "xQuery", req->query.xcql);
446 case Z_SRW_query_type_pqf:
447 add_xsd_string(ptr, "pQuery", req->query.pqf);
450 switch(req->sort_type)
452 case Z_SRW_sort_type_none:
454 case Z_SRW_sort_type_sort:
455 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
457 case Z_SRW_sort_type_xSort:
458 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
461 add_xsd_integer(ptr, "startRecord", req->startRecord);
462 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
463 add_xsd_string(ptr, "recordSchema", req->recordSchema);
464 add_xsd_string(ptr, "recordPacking", req->recordPacking);
465 add_xsd_string(ptr, "database", req->database);
467 else if ((*p)->which == Z_SRW_searchRetrieve_response)
469 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
470 xmlNodePtr ptr = xmlNewChild(pptr, 0,
471 "searchRetrieveResponse", 0);
472 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
474 xmlSetNs(ptr, ns_srw);
475 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
476 add_xsd_string(ptr, "resultSetId", res->resultSetId);
477 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
478 if (res->num_records)
480 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
481 yaz_srw_records(o, rptr, &res->records, &res->num_records,
484 if (res->num_diagnostics)
486 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
487 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
488 &res->num_diagnostics, client_data, ns);
490 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
492 else if ((*p)->which == Z_SRW_explain_request)
494 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainRequest", 0);
495 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
497 xmlSetNs(ptr, ns_srw);
499 else if ((*p)->which == Z_SRW_explain_response)
501 Z_SRW_explainResponse *res = (*p)->u.explain_response;
502 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainResponse", 0);
503 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
505 xmlSetNs(ptr, ns_srw);
507 add_xsd_string_n(ptr, "Explain", res->explainData_buf,
508 res->explainData_len);
517 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
519 Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o));
523 case Z_SRW_searchRetrieve_request:
524 sr->u.request = (Z_SRW_searchRetrieveRequest *)
525 odr_malloc(o, sizeof(*sr->u.request));
526 sr->u.request->query_type = Z_SRW_query_type_cql;
527 sr->u.request->query.cql = 0;
528 sr->u.request->sort_type = Z_SRW_sort_type_none;
529 sr->u.request->sort.none = 0;
530 sr->u.request->startRecord = 0;
531 sr->u.request->maximumRecords = 0;
532 sr->u.request->recordSchema = 0;
533 sr->u.request->recordPacking = 0;
534 sr->u.request->database = 0;
536 case Z_SRW_searchRetrieve_response:
537 sr->u.response = (Z_SRW_searchRetrieveResponse *)
538 odr_malloc(o, sizeof(*sr->u.response));
539 sr->u.response->numberOfRecords = 0;
540 sr->u.response->resultSetId = 0;
541 sr->u.response->resultSetIdleTime = 0;
542 sr->u.response->records = 0;
543 sr->u.response->num_records = 0;
544 sr->u.response->diagnostics = 0;
545 sr->u.response->num_diagnostics = 0;
546 sr->u.response->nextRecordPosition = 0;
548 case Z_SRW_explain_request:
549 sr->u.explain_request = (Z_SRW_explainRequest *)
550 odr_malloc(o, sizeof(*sr->u.explain_request));
551 sr->u.explain_request->dummy = 0;
553 case Z_SRW_explain_response:
554 sr->u.explain_response = (Z_SRW_explainResponse *)
555 odr_malloc(o, sizeof(*sr->u.explain_response));
556 sr->u.explain_response->explainPacking = 0;
557 sr->u.explain_response->explainData_buf = 0;
558 sr->u.explain_response->explainData_len = 0;
569 } yaz_srw_codes [] = {
570 {1, "Permanent system error"},
571 {2, "System temporarily unavailable"},
572 {3, "Authentication error"},
573 /* Diagnostics Relating to CQL */
574 {10, "Illegal query"},
575 {11, "Unsupported query type (XCQL vs CQL)"},
576 {12, "Too many characters in query"},
577 {13, "Unbalanced or illegal use of parentheses"},
578 {14, "Unbalanced or illegal use of quotes"},
579 {15, "Illegal or unsupported context set"},
580 {16, "Illegal or unsupported index"},
581 {17, "Illegal or unsupported combination of index and context set"},
582 {18, "Illegal or unsupported combination of indexes"},
583 {19, "Illegal or unsupported relation"},
584 {20, "Illegal or unsupported relation modifier"},
585 {21, "Illegal or unsupported combination of relation modifers"},
586 {22, "Illegal or unsupported combination of relation and index"},
587 {23, "Too many characters in term"},
588 {24, "Illegal combination of relation and term"},
589 {25, "Special characters not quoted in term"},
590 {26, "Non special character escaped in term"},
591 {27, "Empty term unsupported"},
592 {28, "Masking character not supported"},
593 {29, "Masked words too short"},
594 {30, "Too many masking characters in term"},
595 {31, "Anchoring character not supported"},
596 {32, "Anchoring character in illegal or unsupported position"},
597 {33, "Combination of proximity/adjacency and masking characters not supported"},
598 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
599 {35, "Terms only exclusion (stop) words"},
600 {36, "Term in invalid format for index or relation"},
601 {37, "Illegal or unsupported boolean operator"},
602 {38, "Too many boolean operators in query"},
603 {39, "Proximity not supported"},
604 {40, "Illegal or unsupported proximity relation"},
605 {41, "Illegal or unsupported proximity distance"},
606 {42, "Illegal or unsupported proximity unit"},
607 {43, "Illegal or unsupported proximity ordering"},
608 {44, "Illegal or unsupported combination of proximity modifiers"},
609 {45, "context set name (prefix) assigned to multiple identifiers"},
610 /* Diagnostics Relating to Result Sets */
611 {50, "Result sets not supported"},
612 {51, "Result set does not exist"},
613 {52, "Result set temporarily unavailable"},
614 {53, "Result sets only supported for retrieval"},
615 {54, "Retrieval may only occur from an existing result set"},
616 {55, "Combination of result sets with search terms not supported"},
617 {56, "Only combination of single result set with search terms supported"},
618 {57, "Result set created but no records available"},
619 {58, "Result set created with unpredictable partial results available"},
620 {59, "Result set created with valid partial results available"},
621 /* Diagnostics Relating to Records */
622 {60, "Too many records retrieved"},
623 {61, "First record position out of range"},
624 {62, "Negative number of records requested"},
625 {63, "System error in retrieving records"},
626 {64, "Record temporarily unavailable"},
627 {65, "Record does not exist"},
628 {66, "Unknown schema for retrieval"},
629 {67, "Record not available in this schema"},
630 {68, "Not authorised to send record"},
631 {69, "Not authorised to send record in this schema"},
632 {70, "Record too large to send"},
633 /* Diagnostics Relating to Sorting */
634 {80, "Sort not supported"},
635 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
636 {82, "Illegal or unsupported sort sequence"},
637 {83, "Too many records"},
638 {84, "Too many sort keys"},
639 {85, "Duplicate sort keys"},
640 {86, "Incompatible record formats"},
641 {87, "Unsupported schema for sort"},
642 {88, "Unsupported tag path for sort"},
643 {89, "Tag path illegal or unsupported for schema"},
644 {90, "Illegal or unsupported direction value"},
645 {91, "Illegal or unsupported case value"},
646 {92, "Illegal or unsupported missing value action"},
647 /* Diagnostics Relating to Explain */
648 {100, "Explain not supported"},
649 {101, "Explain request type not supported (SOAP vs GET)"},
650 {102, "Explain record temporarily unavailable"},
654 const char *yaz_diag_srw_str (int code)
657 for (i = 0; yaz_srw_codes[i].code; i++)
658 if (yaz_srw_codes[i].code == code)
659 return yaz_srw_codes[i].msg;
665 static int srw_bib1_map[] = {
694 24, 63, /* bad map */
695 25, 63, /* bad map */
696 26, 63, /* bad map */
704 100, 1, /* bad map */
752 205, 1, /* bad map */
753 206, 1, /* bad map */
755 208, 1, /* bad map */
766 218, 1, /* bad map */
767 219, 1, /* bad map */
768 220, 1, /* bad map */
769 221, 1, /* bad map */
770 222, 1, /* bad map */
771 223, 1, /* bad map */
772 224, 1, /* bad map */
773 225, 1, /* bad map */
774 226, 1, /* bad map */
776 228, 1, /* bad map */
781 233, 1, /* bad map */
782 234, 1, /* bad map */
788 240, 1, /* bad map */
789 241, 1, /* bad map */
791 243, 1, /* bad map */
796 1001, 1, /* bad map */
797 1002, 1, /* bad map */
798 1003, 1, /* bad map */
799 1004, 1, /* bad map */
800 1005, 1, /* bad map */
801 1006, 1, /* bad map */
833 int yaz_diag_bib1_to_srw (int code)
835 const int *p = srw_bib1_map;
845 int yaz_diag_srw_to_bib1(int code)
847 const int *p = srw_bib1_map;