2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.11 2003-03-23 20:27:16 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 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_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
108 struct _xmlAttr *attr;
110 if (!match_element(ptr, elem))
113 for (attr = ptr->properties; attr; attr = attr->next)
114 if (!strcmp(attr->name, "type") &&
115 attr->children && attr->children->type == XML_TEXT_NODE)
117 const char *t = strchr(attr->children->content, ':');
121 t = attr->children->content;
122 if (!strcmp(t, "integer"))
129 if (!ptr || ptr->type != XML_TEXT_NODE)
131 *val = odr_intdup(o, atoi(ptr->content));
135 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
136 int *num, void *client_data, const char *ns)
138 if (o->direction == ODR_DECODE)
143 for (ptr = pptr->children; ptr; ptr = ptr->next)
145 if (ptr->type == XML_ELEMENT_NODE &&
146 !strcmp(ptr->name, "record"))
151 *recs = odr_malloc(o, *num * sizeof(**recs));
152 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
154 if (ptr->type == XML_ELEMENT_NODE &&
155 !strcmp(ptr->name, "record"))
158 (*recs)[i].recordSchema = 0;
159 (*recs)[i].recordPacking = Z_SRW_recordPacking_string;
160 (*recs)[i].recordData_buf = 0;
161 (*recs)[i].recordData_len = 0;
162 (*recs)[i].recordPosition = 0;
163 for (rptr = ptr->children; rptr; rptr = rptr->next)
165 if (match_xsd_string(rptr, "recordSchema", o,
166 &(*recs)[i].recordSchema))
168 else if (match_xsd_string_n(rptr, "recordData", o,
169 &(*recs)[i].recordData_buf,
170 &(*recs)[i].recordData_len))
172 else if (match_xsd_integer(rptr, "recordPosition", o,
173 &(*recs)[i].recordPosition))
179 else if (o->direction == ODR_ENCODE)
182 for (i = 0; i < *num; i++)
184 xmlNodePtr rptr = xmlNewChild(pptr, 0, "record", 0);
185 add_xsd_string(rptr, "recordSchema", (*recs)[i].recordSchema);
186 switch((*recs)[i].recordPacking)
188 case Z_SRW_recordPacking_string:
189 add_xsd_string_n(rptr, "recordData", (*recs)[i].recordData_buf,
190 (*recs)[i].recordData_len);
192 case Z_SRW_recordPacking_XML:
193 add_XML_n(rptr, "recordXML", (*recs)[i].recordData_buf,
194 (*recs)[i].recordData_len);
197 add_xsd_integer(rptr, "recordPosition", (*recs)[i].recordPosition);
203 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
204 int *num, void *client_data, const char *ns)
206 if (o->direction == ODR_DECODE)
211 for (ptr = pptr->children; ptr; ptr = ptr->next)
213 if (ptr->type == XML_ELEMENT_NODE &&
214 !strcmp(ptr->name, "diagnostic"))
219 *recs = odr_malloc(o, *num * sizeof(**recs));
220 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
222 if (ptr->type == XML_ELEMENT_NODE &&
223 !strcmp(ptr->name, "diagnostic"))
227 (*recs)[i].details = 0;
228 for (rptr = ptr->children; rptr; rptr = rptr->next)
230 if (match_xsd_integer(rptr, "code", o,
233 else if (match_xsd_string(rptr, "details", o,
234 &(*recs)[i].details))
241 else if (o->direction == ODR_ENCODE)
244 for (i = 0; i < *num; i++)
246 xmlNodePtr rptr = xmlNewChild(pptr, 0, "diagnostic", 0);
247 add_xsd_integer(rptr, "code", (*recs)[i].code);
248 add_xsd_string(rptr, "details", (*recs)[i].details);
255 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
256 void *client_data, const char *ns)
258 xmlNodePtr pptr = vptr;
259 if (o->direction == ODR_DECODE)
261 xmlNodePtr method = pptr->children;
263 while (method && method->type == XML_TEXT_NODE)
264 method = method->next;
266 if (!method || method->type != XML_ELEMENT_NODE)
268 if (!strcmp(method->name, "searchRetrieveRequest"))
270 Z_SRW_PDU **p = handler_data;
271 xmlNodePtr ptr = method->children;
272 Z_SRW_searchRetrieveRequest *req;
274 *p = odr_malloc(o, sizeof(**p));
275 (*p)->which = Z_SRW_searchRetrieve_request;
276 req = (*p)->u.request = odr_malloc(o, sizeof(*req));
277 req->query_type = Z_SRW_query_type_cql;
279 req->sort_type = Z_SRW_sort_type_none;
281 req->startRecord = 0;
282 req->maximumRecords = 0;
283 req->recordSchema = 0;
284 req->recordPacking = 0;
287 for (; ptr; ptr = ptr->next)
289 if (match_xsd_string(ptr, "query", o,
291 req->query_type = Z_SRW_query_type_cql;
292 else if (match_xsd_string(ptr, "pQuery", o,
294 req->query_type = Z_SRW_query_type_pqf;
295 else if (match_xsd_string(ptr, "xQuery", o,
297 req->query_type = Z_SRW_query_type_xcql;
298 else if (match_xsd_string(ptr, "sortKeys", o,
299 &req->sort.sortKeys))
300 req->sort_type = Z_SRW_sort_type_sort;
301 else if (match_xsd_string(ptr, "recordSchema", o,
304 else if (match_xsd_string(ptr, "recordPacking", o,
305 &req->recordPacking))
307 else if (match_xsd_integer(ptr, "startRecord", o,
310 else if (match_xsd_integer(ptr, "maximumRecords", o,
311 &req->maximumRecords))
313 else if (match_xsd_string(ptr, "database", o,
316 /* missing is xQuery, xSortKeys .. */
319 else if (!strcmp(method->name, "searchRetrieveResponse"))
321 Z_SRW_PDU **p = handler_data;
322 xmlNodePtr ptr = method->children;
323 Z_SRW_searchRetrieveResponse *res;
325 *p = odr_malloc(o, sizeof(**p));
326 (*p)->which = Z_SRW_searchRetrieve_response;
327 res = (*p)->u.response = odr_malloc(o, sizeof(*res));
329 res->numberOfRecords = 0;
330 res->resultSetId = 0;
331 res->resultSetIdleTime = 0;
333 res->num_records = 0;
334 res->diagnostics = 0;
335 res->num_diagnostics = 0;
336 res->nextRecordPosition = 0;
338 for (; ptr; ptr = ptr->next)
340 if (match_xsd_integer(ptr, "numberOfRecords", o,
341 &res->numberOfRecords))
343 else if (match_xsd_string(ptr, "resultSetId", o,
346 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
347 &res->resultSetIdleTime))
349 else if (match_element(ptr, "records"))
350 yaz_srw_records(o, ptr, &res->records,
351 &res->num_records, client_data,
353 else if (match_element(ptr, "diagnostics"))
354 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
355 &res->num_diagnostics,
357 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
358 &res->nextRecordPosition))
362 else if (!strcmp(method->name, "explainRequest"))
364 Z_SRW_PDU **p = handler_data;
365 Z_SRW_explainRequest *req;
367 *p = odr_malloc(o, sizeof(**p));
368 (*p)->which = Z_SRW_explain_request;
369 req = (*p)->u.explain_request = odr_malloc(o, sizeof(*req));
372 else if (!strcmp(method->name, "explainResponse"))
374 Z_SRW_PDU **p = handler_data;
375 Z_SRW_explainResponse *res;
377 *p = odr_malloc(o, sizeof(**p));
378 (*p)->which = Z_SRW_explain_response;
379 res = (*p)->u.explain_response = odr_malloc(o, sizeof(*res));
380 res->explainData_buf = 0;
381 res->explainData_len = 0;
382 res->explainPacking = Z_SRW_recordPacking_string;
383 match_xsd_string_n(method, "explainResponse", o,
384 &res->explainData_buf, &res->explainData_len);
390 else if (o->direction == ODR_ENCODE)
392 Z_SRW_PDU **p = handler_data;
393 if ((*p)->which == Z_SRW_searchRetrieve_request)
395 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
396 xmlNodePtr ptr = xmlNewChild(pptr, 0,
397 "searchRetrieveRequest", 0);
398 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
400 xmlSetNs(ptr, ns_srw);
402 switch(req->query_type)
404 case Z_SRW_query_type_cql:
405 add_xsd_string(ptr, "query", req->query.cql);
407 case Z_SRW_query_type_xcql:
408 add_xsd_string(ptr, "xQuery", req->query.xcql);
410 case Z_SRW_query_type_pqf:
411 add_xsd_string(ptr, "pQuery", req->query.pqf);
414 switch(req->sort_type)
416 case Z_SRW_sort_type_none:
418 case Z_SRW_sort_type_sort:
419 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
421 case Z_SRW_sort_type_xSort:
422 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
425 add_xsd_integer(ptr, "startRecord", req->startRecord);
426 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
427 add_xsd_string(ptr, "recordSchema", req->recordSchema);
428 add_xsd_string(ptr, "recordPacking", req->recordPacking);
429 add_xsd_string(ptr, "database", req->database);
431 else if ((*p)->which == Z_SRW_searchRetrieve_response)
433 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
434 xmlNodePtr ptr = xmlNewChild(pptr, 0,
435 "searchRetrieveResponse", 0);
436 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
438 xmlSetNs(ptr, ns_srw);
439 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
440 add_xsd_string(ptr, "resultSetId", res->resultSetId);
441 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
442 if (res->num_records)
444 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
445 yaz_srw_records(o, rptr, &res->records, &res->num_records,
448 if (res->num_diagnostics)
450 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
451 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
452 &res->num_diagnostics, client_data, ns);
454 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
456 else if ((*p)->which == Z_SRW_explain_request)
458 Z_SRW_explainRequest *req = (*p)->u.explain_request;
459 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainRequest", 0);
460 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
462 xmlSetNs(ptr, ns_srw);
464 else if ((*p)->which == Z_SRW_explain_response)
466 Z_SRW_explainResponse *res = (*p)->u.explain_response;
467 xmlNodePtr ptr = xmlNewChild(pptr, 0, "explainResponse", 0);
468 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
470 xmlSetNs(ptr, ns_srw);
472 if (res->explainData_buf)
474 xmlNodePtr t = xmlNewTextLen(res->explainData_buf,
475 res->explainData_len);
486 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
488 Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o));
492 case Z_SRW_searchRetrieve_request:
493 sr->u.request = (Z_SRW_searchRetrieveRequest *)
494 odr_malloc(o, sizeof(*sr->u.request));
495 sr->u.request->query_type = Z_SRW_query_type_cql;
496 sr->u.request->query.cql = 0;
497 sr->u.request->sort_type = Z_SRW_sort_type_none;
498 sr->u.request->sort.none = 0;
499 sr->u.request->startRecord = 0;
500 sr->u.request->maximumRecords = 0;
501 sr->u.request->recordSchema = 0;
502 sr->u.request->recordPacking = 0;
503 sr->u.request->database = 0;
505 case Z_SRW_searchRetrieve_response:
506 sr->u.response = (Z_SRW_searchRetrieveResponse *)
507 odr_malloc(o, sizeof(*sr->u.response));
508 sr->u.response->numberOfRecords = 0;
509 sr->u.response->resultSetId = 0;
510 sr->u.response->resultSetIdleTime = 0;
511 sr->u.response->records = 0;
512 sr->u.response->num_records = 0;
513 sr->u.response->diagnostics = 0;
514 sr->u.response->num_diagnostics = 0;
515 sr->u.response->nextRecordPosition = 0;
517 case Z_SRW_explain_request:
518 sr->u.explain_request = (Z_SRW_explainRequest *)
519 odr_malloc(o, sizeof(*sr->u.explain_request));
520 sr->u.explain_request->dummy = 0;
522 case Z_SRW_explain_response:
523 sr->u.explain_response = (Z_SRW_explainResponse *)
524 odr_malloc(o, sizeof(*sr->u.explain_response));
525 sr->u.explain_response->explainPacking = 0;
526 sr->u.explain_response->explainData_buf = 0;
527 sr->u.explain_response->explainData_len = 0;
538 } yaz_srw_codes [] = {
539 {1, "Permanent system error"},
540 {2, "System temporarily unavailable"},
541 {3, "Authentication error"},
542 /* Diagnostics Relating to CQL */
543 {10, "Illegal query"},
544 {11, "Unsupported query type (XCQL vs CQL)"},
545 {12, "Too many characters in query"},
546 {13, "Unbalanced or illegal use of parentheses"},
547 {14, "Unbalanced or illegal use of quotes"},
548 {15, "Illegal or unsupported index set"},
549 {16, "Illegal or unsupported index"},
550 {17, "Illegal or unsupported combination of index and index set"},
551 {18, "Illegal or unsupported combination of indexes"},
552 {19, "Illegal or unsupported relation"},
553 {20, "Illegal or unsupported relation modifier"},
554 {21, "Illegal or unsupported combination of relation modifers"},
555 {22, "Illegal or unsupported combination of relation and index"},
556 {23, "Too many characters in term"},
557 {24, "Illegal combination of relation and term"},
558 {25, "Special characters not quoted in term"},
559 {26, "Non special character escaped in term"},
560 {27, "Empty term unsupported"},
561 {28, "Masking character not supported"},
562 {29, "Masked words too short"},
563 {30, "Too many masking characters in term"},
564 {31, "Anchoring character not supported"},
565 {32, "Anchoring character in illegal or unsupported position"},
566 {33, "Combination of proximity/adjacency and masking characters not supported"},
567 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
568 {35, "Terms only exclusion (stop) words"},
569 {36, "Term in invalid format for index or relation"},
570 {37, "Illegal or unsupported boolean operator"},
571 {38, "Too many boolean operators in query"},
572 {39, "Proximity not supported"},
573 {40, "Illegal or unsupported proximity relation"},
574 {41, "Illegal or unsupported proximity distance"},
575 {42, "Illegal or unsupported proximity unit"},
576 {43, "Illegal or unsupported proximity ordering"},
577 {44, "Illegal or unsupported combination of proximity modifiers"},
578 {45, "Index set name (prefix) assigned to multiple identifiers"},
579 /* Diagnostics Relating to Result Sets */
580 {50, "Result sets not supported"},
581 {51, "Result set does not exist"},
582 {52, "Result set temporarily unavailable"},
583 {53, "Result sets only supported for retrieval"},
584 {54, "Retrieval may only occur from an existing result set"},
585 {55, "Combination of result sets with search terms not supported"},
586 {56, "Only combination of single result set with search terms supported"},
587 {57, "Result set created but no records available"},
588 {58, "Result set created with unpredictable partial results available"},
589 {59, "Result set created with valid partial results available"},
590 /* Diagnostics Relating to Records */
591 {60, "Too many records retrieved"},
592 {61, "First record position out of range"},
593 {62, "Negative number of records requested"},
594 {63, "System error in retrieving records"},
595 {64, "Record temporarily unavailable"},
596 {65, "Record does not exist"},
597 {66, "Unknown schema for retrieval"},
598 {67, "Record not available in this schema"},
599 {68, "Not authorised to send record"},
600 {69, "Not authorised to send record in this schema"},
601 {70, "Record too large to send"},
602 /* Diagnostics Relating to Sorting */
603 {80, "Sort not supported"},
604 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
605 {82, "Illegal or unsupported sort sequence"},
606 {83, "Too many records"},
607 {84, "Too many sort keys"},
608 {85, "Duplicate sort keys"},
609 {86, "Incompatible record formats"},
610 {87, "Unsupported schema for sort"},
611 {88, "Unsupported tag path for sort"},
612 {89, "Tag path illegal or unsupported for schema"},
613 {90, "Illegal or unsupported direction value"},
614 {91, "Illegal or unsupported case value"},
615 {92, "Illegal or unsupported missing value action"},
616 /* Diagnostics Relating to Explain */
617 {100, "Explain not supported"},
618 {101, "Explain request type not supported (SOAP vs GET)"},
619 {102, "Explain record temporarily unavailable"},
623 const char *yaz_diag_srw_str (int code)
626 for (i = 0; yaz_srw_codes[i].code; i++)
627 if (yaz_srw_codes[i].code == code)
628 return yaz_srw_codes[i].msg;
634 static int srw_bib1_map[] = {
663 24, 63, /* bad map */
664 25, 63, /* bad map */
665 26, 63, /* bad map */
673 100, 1, /* bad map */
721 205, 1, /* bad map */
722 206, 1, /* bad map */
724 208, 1, /* bad map */
735 218, 1, /* bad map */
736 219, 1, /* bad map */
737 220, 1, /* bad map */
738 221, 1, /* bad map */
739 222, 1, /* bad map */
740 223, 1, /* bad map */
741 224, 1, /* bad map */
742 225, 1, /* bad map */
743 226, 1, /* bad map */
745 228, 1, /* bad map */
750 233, 1, /* bad map */
751 234, 1, /* bad map */
757 240, 1, /* bad map */
758 241, 1, /* bad map */
760 243, 1, /* bad map */
765 1001, 1, /* bad map */
766 1002, 1, /* bad map */
767 1003, 1, /* bad map */
768 1004, 1, /* bad map */
769 1005, 1, /* bad map */
770 1006, 1, /* bad map */
802 int yaz_diag_bib1_to_srw (int code)
804 const int *p = srw_bib1_map;
814 int yaz_diag_srw_to_bib1(int code)
816 const int *p = srw_bib1_map;