2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.8 2003-03-03 19:57:37 adam Exp $
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
14 static void add_xsd_string_n(xmlNodePtr ptr, const char *elem, char *val,
19 xmlNodePtr c = xmlNewChild(ptr, 0, elem, 0);
20 xmlNodePtr t = xmlNewTextLen(val, len);
25 static void add_xsd_string(xmlNodePtr ptr, const char *elem, char *val)
28 xmlNewChild(ptr, 0, elem, val);
31 static void add_xsd_integer(xmlNodePtr ptr, const char *elem, int *val)
36 sprintf(str, "%d", *val);
37 xmlNewChild(ptr, 0, elem, str);
41 static int match_element(xmlNodePtr ptr, const char *elem)
43 if (ptr->type == XML_ELEMENT_NODE && !strcmp(ptr->name, elem))
50 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
54 struct _xmlAttr *attr;
56 if (!match_element(ptr, elem))
59 for (attr = ptr->properties; attr; attr = attr->next)
60 if (!strcmp(attr->name, "type") &&
61 attr->children && attr->children->type == XML_TEXT_NODE)
63 const char *t = strchr(attr->children->content, ':');
67 t = attr->children->content;
68 if (!strcmp(t, "string"))
75 if (!ptr || ptr->type != XML_TEXT_NODE)
77 *val = odr_strdup(o, ptr->content);
79 *len = strlen(ptr->content);
84 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
87 return match_xsd_string_n(ptr, elem, o, val, 0);
90 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
93 struct _xmlAttr *attr;
95 if (!match_element(ptr, elem))
98 for (attr = ptr->properties; attr; attr = attr->next)
99 if (!strcmp(attr->name, "type") &&
100 attr->children && attr->children->type == XML_TEXT_NODE)
102 const char *t = strchr(attr->children->content, ':');
106 t = attr->children->content;
107 if (!strcmp(t, "integer"))
114 if (!ptr || ptr->type != XML_TEXT_NODE)
116 *val = odr_intdup(o, atoi(ptr->content));
120 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
121 int *num, void *client_data, const char *ns)
123 if (o->direction == ODR_DECODE)
128 for (ptr = pptr->children; ptr; ptr = ptr->next)
130 if (ptr->type == XML_ELEMENT_NODE &&
131 !strcmp(ptr->name, "record"))
136 *recs = odr_malloc(o, *num * sizeof(**recs));
137 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
139 if (ptr->type == XML_ELEMENT_NODE &&
140 !strcmp(ptr->name, "record"))
143 (*recs)[i].recordSchema = 0;
144 (*recs)[i].recordData_buf = 0;
145 (*recs)[i].recordData_len = 0;
146 (*recs)[i].recordPosition = 0;
147 for (rptr = ptr->children; rptr; rptr = rptr->next)
149 if (match_xsd_string(rptr, "recordSchema", o,
150 &(*recs)[i].recordSchema))
152 else if (match_xsd_string_n(rptr, "recordData", o,
153 &(*recs)[i].recordData_buf,
154 &(*recs)[i].recordData_len))
156 else if (match_xsd_integer(rptr, "recordPosition", o,
157 &(*recs)[i].recordPosition))
163 else if (o->direction == ODR_ENCODE)
166 for (i = 0; i < *num; i++)
168 xmlNodePtr rptr = xmlNewChild(pptr, 0, "record", 0);
169 add_xsd_string(rptr, "recordSchema", (*recs)[i].recordSchema);
170 add_xsd_string_n(rptr, "recordData", (*recs)[i].recordData_buf,
171 (*recs)[i].recordData_len);
172 add_xsd_integer(rptr, "recordPosition", (*recs)[i].recordPosition);
178 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
179 int *num, void *client_data, const char *ns)
181 if (o->direction == ODR_DECODE)
186 for (ptr = pptr->children; ptr; ptr = ptr->next)
188 if (ptr->type == XML_ELEMENT_NODE &&
189 !strcmp(ptr->name, "diagnostic"))
194 *recs = odr_malloc(o, *num * sizeof(**recs));
195 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
197 if (ptr->type == XML_ELEMENT_NODE &&
198 !strcmp(ptr->name, "diagnostic"))
202 (*recs)[i].details = 0;
203 for (rptr = ptr->children; rptr; rptr = rptr->next)
205 if (match_xsd_integer(rptr, "code", o,
208 else if (match_xsd_string(rptr, "details", o,
209 &(*recs)[i].details))
216 else if (o->direction == ODR_ENCODE)
219 for (i = 0; i < *num; i++)
221 xmlNodePtr rptr = xmlNewChild(pptr, 0, "diagnostic", 0);
222 add_xsd_integer(rptr, "code", (*recs)[i].code);
223 add_xsd_string(rptr, "details", (*recs)[i].details);
230 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
231 void *client_data, const char *ns)
233 xmlNodePtr pptr = vptr;
234 if (o->direction == ODR_DECODE)
236 xmlNodePtr method = pptr->children;
238 while (method && method->type == XML_TEXT_NODE)
239 method = method->next;
241 if (method->type != XML_ELEMENT_NODE)
243 if (method && !strcmp(method->name, "searchRetrieveRequest"))
245 Z_SRW_PDU **p = handler_data;
246 xmlNodePtr ptr = method->children;
247 Z_SRW_searchRetrieveRequest *req;
249 *p = odr_malloc(o, sizeof(**p));
250 (*p)->which = Z_SRW_searchRetrieve_request;
251 req = (*p)->u.request = odr_malloc(o, sizeof(*req));
252 req->query_type = Z_SRW_query_type_cql;
254 req->sort_type = Z_SRW_sort_type_none;
256 req->startRecord = 0;
257 req->maximumRecords = 0;
258 req->recordSchema = 0;
259 req->recordPacking = 0;
262 for (; ptr; ptr = ptr->next)
264 if (match_xsd_string(ptr, "query", o,
266 req->query_type = Z_SRW_query_type_cql;
267 else if (match_xsd_string(ptr, "pQuery", o,
269 req->query_type = Z_SRW_query_type_pqf;
270 else if (match_xsd_string(ptr, "xQuery", o,
272 req->query_type = Z_SRW_query_type_xcql;
273 else if (match_xsd_string(ptr, "sortKeys", o,
274 &req->sort.sortKeys))
275 req->sort_type = Z_SRW_sort_type_sort;
276 else if (match_xsd_string(ptr, "recordSchema", o,
279 else if (match_xsd_string(ptr, "recordPacking", o,
280 &req->recordPacking))
282 else if (match_xsd_integer(ptr, "startRecord", o,
285 else if (match_xsd_integer(ptr, "maximumRecords", o,
286 &req->maximumRecords))
288 else if (match_xsd_string(ptr, "database", o,
291 /* missing is xQuery, xSortKeys .. */
294 else if (method && !strcmp(method->name, "searchRetrieveResponse"))
296 Z_SRW_PDU **p = handler_data;
297 xmlNodePtr ptr = method->children;
298 Z_SRW_searchRetrieveResponse *res;
300 *p = odr_malloc(o, sizeof(**p));
301 (*p)->which = Z_SRW_searchRetrieve_response;
302 res = (*p)->u.response = odr_malloc(o, sizeof(*res));
304 res->numberOfRecords = 0;
305 res->resultSetId = 0;
306 res->resultSetIdleTime = 0;
308 res->num_records = 0;
309 res->diagnostics = 0;
310 res->num_diagnostics = 0;
311 res->nextRecordPosition = 0;
313 for (; ptr; ptr = ptr->next)
315 if (match_xsd_integer(ptr, "numberOfRecords", o,
316 &res->numberOfRecords))
318 else if (match_xsd_string(ptr, "resultSetId", o,
321 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
322 &res->resultSetIdleTime))
324 else if (match_element(ptr, "records"))
325 yaz_srw_records(o, ptr, &res->records,
326 &res->num_records, client_data,
328 else if (match_element(ptr, "diagnostics"))
329 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
330 &res->num_diagnostics,
332 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
333 &res->nextRecordPosition))
342 else if (o->direction == ODR_ENCODE)
344 Z_SRW_PDU **p = handler_data;
345 if ((*p)->which == Z_SRW_searchRetrieve_request)
347 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
348 xmlNsPtr ns_srw = xmlNewNs(pptr, ns, "zs");
349 xmlNodePtr ptr = xmlNewChild(pptr, ns_srw,
350 "searchRetrieveRequest", 0);
351 switch(req->query_type)
353 case Z_SRW_query_type_cql:
354 add_xsd_string(ptr, "query", req->query.cql);
356 case Z_SRW_query_type_xcql:
357 add_xsd_string(ptr, "xQuery", req->query.xcql);
359 case Z_SRW_query_type_pqf:
360 add_xsd_string(ptr, "pQuery", req->query.pqf);
363 switch(req->sort_type)
365 case Z_SRW_sort_type_none:
367 case Z_SRW_sort_type_sort:
368 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
370 case Z_SRW_sort_type_xSort:
371 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
374 add_xsd_integer(ptr, "startRecord", req->startRecord);
375 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
376 add_xsd_string(ptr, "recordSchema", req->recordSchema);
377 add_xsd_string(ptr, "recordPacking", req->recordPacking);
378 add_xsd_string(ptr, "database", req->database);
380 else if ((*p)->which == Z_SRW_searchRetrieve_response)
382 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
383 xmlNsPtr ns_srw = xmlNewNs(pptr, ns, "zs");
384 xmlNodePtr ptr = xmlNewChild(pptr, ns_srw,
385 "searchRetrieveResponse", 0);
387 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
388 add_xsd_string(ptr, "resultSetId", res->resultSetId);
389 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
390 if (res->num_records)
392 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
393 yaz_srw_records(o, rptr, &res->records, &res->num_records,
396 if (res->num_diagnostics)
398 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
399 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
400 &res->num_diagnostics, client_data, ns);
402 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
411 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
413 Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o));
417 case Z_SRW_searchRetrieve_request:
418 sr->u.request = odr_malloc(o, sizeof(*sr->u.request));
419 sr->u.request->query_type = Z_SRW_query_type_cql;
420 sr->u.request->query.cql = 0;
421 sr->u.request->sort_type = Z_SRW_sort_type_none;
422 sr->u.request->sort.none = 0;
423 sr->u.request->startRecord = 0;
424 sr->u.request->maximumRecords = 0;
425 sr->u.request->recordSchema = 0;
426 sr->u.request->recordPacking = 0;
427 sr->u.request->database = 0;
429 case Z_SRW_searchRetrieve_response:
430 sr->u.response = odr_malloc(o, sizeof(*sr->u.response));
431 sr->u.response->numberOfRecords = 0;
432 sr->u.response->resultSetId = 0;
433 sr->u.response->resultSetIdleTime = 0;
434 sr->u.response->records = 0;
435 sr->u.response->num_records = 0;
436 sr->u.response->diagnostics = 0;
437 sr->u.response->num_diagnostics = 0;
438 sr->u.response->nextRecordPosition = 0;
449 } yaz_srw_codes [] = {
450 {1, "Permanent system error"},
451 {2, "System temporarily unavailable"},
452 {3, "Authentication error"},
453 /* Diagnostics Relating to CQL */
454 {10, "Illegal query"},
455 {11, "Unsupported query type (XCQL vs CQL)"},
456 {12, "Too many characters in query"},
457 {13, "Unbalanced or illegal use of parentheses"},
458 {14, "Unbalanced or illegal use of quotes"},
459 {15, "Illegal or unsupported index set"},
460 {16, "Illegal or unsupported index"},
461 {17, "Illegal or unsupported combination of index and index set"},
462 {18, "Illegal or unsupported combination of indexes"},
463 {19, "Illegal or unsupported relation"},
464 {20, "Illegal or unsupported relation modifier"},
465 {21, "Illegal or unsupported combination of relation modifers"},
466 {22, "Illegal or unsupported combination of relation and index"},
467 {23, "Too many characters in term"},
468 {24, "Illegal combination of relation and term"},
469 {25, "Special characters not quoted in term"},
470 {26, "Non special character escaped in term"},
471 {27, "Empty term unsupported"},
472 {28, "Masking character not supported"},
473 {29, "Masked words too short"},
474 {30, "Too many masking characters in term"},
475 {31, "Anchoring character not supported"},
476 {32, "Anchoring character in illegal or unsupported position"},
477 {33, "Combination of proximity/adjacency and masking characters not supported"},
478 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
479 {35, "Terms only exclusion (stop) words"},
480 {36, "Term in invalid format for index or relation"},
481 {37, "Illegal or unsupported boolean operator"},
482 {38, "Too many boolean operators in query"},
483 {39, "Proximity not supported"},
484 {40, "Illegal or unsupported proximity relation"},
485 {41, "Illegal or unsupported proximity distance"},
486 {42, "Illegal or unsupported proximity unit"},
487 {43, "Illegal or unsupported proximity ordering"},
488 {44, "Illegal or unsupported combination of proximity modifiers"},
489 {45, "Index set name (prefix) assigned to multiple identifiers"},
490 /* Diagnostics Relating to Result Sets */
491 {50, "Result sets not supported"},
492 {51, "Result set does not exist"},
493 {52, "Result set temporarily unavailable"},
494 {53, "Result sets only supported for retrieval"},
495 {54, "Retrieval may only occur from an existing result set"},
496 {55, "Combination of result sets with search terms not supported"},
497 {56, "Only combination of single result set with search terms supported"},
498 {57, "Result set created but no records available"},
499 {58, "Result set created with unpredictable partial results available"},
500 {59, "Result set created with valid partial results available"},
501 /* Diagnostics Relating to Records */
502 {60, "Too many records retrieved"},
503 {61, "First record position out of range"},
504 {62, "Negative number of records requested"},
505 {63, "System error in retrieving records"},
506 {64, "Record temporarily unavailable"},
507 {65, "Record does not exist"},
508 {66, "Unknown schema for retrieval"},
509 {67, "Record not available in this schema"},
510 {68, "Not authorised to send record"},
511 {69, "Not authorised to send record in this schema"},
512 {70, "Record too large to send"},
513 /* Diagnostics Relating to Sorting */
514 {80, "Sort not supported"},
515 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
516 {82, "Illegal or unsupported sort sequence"},
517 {83, "Too many records"},
518 {84, "Too many sort keys"},
519 {85, "Duplicate sort keys"},
520 {86, "Incompatible record formats"},
521 {87, "Unsupported schema for sort"},
522 {88, "Unsupported tag path for sort"},
523 {89, "Tag path illegal or unsupported for schema"},
524 {90, "Illegal or unsupported direction value"},
525 {91, "Illegal or unsupported case value"},
526 {92, "Illegal or unsupported missing value action"},
527 /* Diagnostics Relating to Explain */
528 {100, "Explain not supported"},
529 {101, "Explain request type not supported (SOAP vs GET)"},
530 {102, "Explain record temporarily unavailable"},
534 const char *yaz_diag_srw_str (int code)
537 for (i = 0; yaz_srw_codes[i].code; i++)
538 if (yaz_srw_codes[i].code == code)
539 return yaz_srw_codes[i].msg;
545 static int srw_bib1_map[] = {
574 24, 63, /* bad map */
575 25, 63, /* bad map */
576 26, 63, /* bad map */
584 100, 1, /* bad map */
632 205, 1, /* bad map */
633 206, 1, /* bad map */
635 208, 1, /* bad map */
646 218, 1, /* bad map */
647 219, 1, /* bad map */
648 220, 1, /* bad map */
649 221, 1, /* bad map */
650 222, 1, /* bad map */
651 223, 1, /* bad map */
652 224, 1, /* bad map */
653 225, 1, /* bad map */
654 226, 1, /* bad map */
656 228, 1, /* bad map */
661 233, 1, /* bad map */
662 234, 1, /* bad map */
668 240, 1, /* bad map */
669 241, 1, /* bad map */
671 243, 1, /* bad map */
676 1001, 1, /* bad map */
677 1002, 1, /* bad map */
678 1003, 1, /* bad map */
679 1004, 1, /* bad map */
680 1005, 1, /* bad map */
681 1006, 1, /* bad map */
713 int yaz_diag_bib1_to_srw (int code)
715 const int *p = srw_bib1_map;
725 int yaz_diag_srw_to_bib1(int code)
727 const int *p = srw_bib1_map;