2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.9 2003-03-18 13:34: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))
341 else if (o->direction == ODR_ENCODE)
343 Z_SRW_PDU **p = handler_data;
344 if ((*p)->which == Z_SRW_searchRetrieve_request)
346 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
347 xmlNodePtr ptr = xmlNewChild(pptr, 0,
348 "searchRetrieveRequest", 0);
349 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
351 xmlSetNs(ptr, ns_srw);
353 switch(req->query_type)
355 case Z_SRW_query_type_cql:
356 add_xsd_string(ptr, "query", req->query.cql);
358 case Z_SRW_query_type_xcql:
359 add_xsd_string(ptr, "xQuery", req->query.xcql);
361 case Z_SRW_query_type_pqf:
362 add_xsd_string(ptr, "pQuery", req->query.pqf);
365 switch(req->sort_type)
367 case Z_SRW_sort_type_none:
369 case Z_SRW_sort_type_sort:
370 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
372 case Z_SRW_sort_type_xSort:
373 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
376 add_xsd_integer(ptr, "startRecord", req->startRecord);
377 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
378 add_xsd_string(ptr, "recordSchema", req->recordSchema);
379 add_xsd_string(ptr, "recordPacking", req->recordPacking);
380 add_xsd_string(ptr, "database", req->database);
382 else if ((*p)->which == Z_SRW_searchRetrieve_response)
384 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
385 xmlNodePtr ptr = xmlNewChild(pptr, 0,
386 "searchRetrieveResponse", 0);
387 xmlNsPtr ns_srw = xmlNewNs(ptr, ns, "zs");
389 xmlSetNs(ptr, ns_srw);
390 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
391 add_xsd_string(ptr, "resultSetId", res->resultSetId);
392 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
393 if (res->num_records)
395 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
396 yaz_srw_records(o, rptr, &res->records, &res->num_records,
399 if (res->num_diagnostics)
401 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
402 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
403 &res->num_diagnostics, client_data, ns);
405 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
414 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
416 Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o));
420 case Z_SRW_searchRetrieve_request:
421 sr->u.request = odr_malloc(o, sizeof(*sr->u.request));
422 sr->u.request->query_type = Z_SRW_query_type_cql;
423 sr->u.request->query.cql = 0;
424 sr->u.request->sort_type = Z_SRW_sort_type_none;
425 sr->u.request->sort.none = 0;
426 sr->u.request->startRecord = 0;
427 sr->u.request->maximumRecords = 0;
428 sr->u.request->recordSchema = 0;
429 sr->u.request->recordPacking = 0;
430 sr->u.request->database = 0;
432 case Z_SRW_searchRetrieve_response:
433 sr->u.response = odr_malloc(o, sizeof(*sr->u.response));
434 sr->u.response->numberOfRecords = 0;
435 sr->u.response->resultSetId = 0;
436 sr->u.response->resultSetIdleTime = 0;
437 sr->u.response->records = 0;
438 sr->u.response->num_records = 0;
439 sr->u.response->diagnostics = 0;
440 sr->u.response->num_diagnostics = 0;
441 sr->u.response->nextRecordPosition = 0;
452 } yaz_srw_codes [] = {
453 {1, "Permanent system error"},
454 {2, "System temporarily unavailable"},
455 {3, "Authentication error"},
456 /* Diagnostics Relating to CQL */
457 {10, "Illegal query"},
458 {11, "Unsupported query type (XCQL vs CQL)"},
459 {12, "Too many characters in query"},
460 {13, "Unbalanced or illegal use of parentheses"},
461 {14, "Unbalanced or illegal use of quotes"},
462 {15, "Illegal or unsupported index set"},
463 {16, "Illegal or unsupported index"},
464 {17, "Illegal or unsupported combination of index and index set"},
465 {18, "Illegal or unsupported combination of indexes"},
466 {19, "Illegal or unsupported relation"},
467 {20, "Illegal or unsupported relation modifier"},
468 {21, "Illegal or unsupported combination of relation modifers"},
469 {22, "Illegal or unsupported combination of relation and index"},
470 {23, "Too many characters in term"},
471 {24, "Illegal combination of relation and term"},
472 {25, "Special characters not quoted in term"},
473 {26, "Non special character escaped in term"},
474 {27, "Empty term unsupported"},
475 {28, "Masking character not supported"},
476 {29, "Masked words too short"},
477 {30, "Too many masking characters in term"},
478 {31, "Anchoring character not supported"},
479 {32, "Anchoring character in illegal or unsupported position"},
480 {33, "Combination of proximity/adjacency and masking characters not supported"},
481 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
482 {35, "Terms only exclusion (stop) words"},
483 {36, "Term in invalid format for index or relation"},
484 {37, "Illegal or unsupported boolean operator"},
485 {38, "Too many boolean operators in query"},
486 {39, "Proximity not supported"},
487 {40, "Illegal or unsupported proximity relation"},
488 {41, "Illegal or unsupported proximity distance"},
489 {42, "Illegal or unsupported proximity unit"},
490 {43, "Illegal or unsupported proximity ordering"},
491 {44, "Illegal or unsupported combination of proximity modifiers"},
492 {45, "Index set name (prefix) assigned to multiple identifiers"},
493 /* Diagnostics Relating to Result Sets */
494 {50, "Result sets not supported"},
495 {51, "Result set does not exist"},
496 {52, "Result set temporarily unavailable"},
497 {53, "Result sets only supported for retrieval"},
498 {54, "Retrieval may only occur from an existing result set"},
499 {55, "Combination of result sets with search terms not supported"},
500 {56, "Only combination of single result set with search terms supported"},
501 {57, "Result set created but no records available"},
502 {58, "Result set created with unpredictable partial results available"},
503 {59, "Result set created with valid partial results available"},
504 /* Diagnostics Relating to Records */
505 {60, "Too many records retrieved"},
506 {61, "First record position out of range"},
507 {62, "Negative number of records requested"},
508 {63, "System error in retrieving records"},
509 {64, "Record temporarily unavailable"},
510 {65, "Record does not exist"},
511 {66, "Unknown schema for retrieval"},
512 {67, "Record not available in this schema"},
513 {68, "Not authorised to send record"},
514 {69, "Not authorised to send record in this schema"},
515 {70, "Record too large to send"},
516 /* Diagnostics Relating to Sorting */
517 {80, "Sort not supported"},
518 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
519 {82, "Illegal or unsupported sort sequence"},
520 {83, "Too many records"},
521 {84, "Too many sort keys"},
522 {85, "Duplicate sort keys"},
523 {86, "Incompatible record formats"},
524 {87, "Unsupported schema for sort"},
525 {88, "Unsupported tag path for sort"},
526 {89, "Tag path illegal or unsupported for schema"},
527 {90, "Illegal or unsupported direction value"},
528 {91, "Illegal or unsupported case value"},
529 {92, "Illegal or unsupported missing value action"},
530 /* Diagnostics Relating to Explain */
531 {100, "Explain not supported"},
532 {101, "Explain request type not supported (SOAP vs GET)"},
533 {102, "Explain record temporarily unavailable"},
537 const char *yaz_diag_srw_str (int code)
540 for (i = 0; yaz_srw_codes[i].code; i++)
541 if (yaz_srw_codes[i].code == code)
542 return yaz_srw_codes[i].msg;
548 static int srw_bib1_map[] = {
577 24, 63, /* bad map */
578 25, 63, /* bad map */
579 26, 63, /* bad map */
587 100, 1, /* bad map */
635 205, 1, /* bad map */
636 206, 1, /* bad map */
638 208, 1, /* bad map */
649 218, 1, /* bad map */
650 219, 1, /* bad map */
651 220, 1, /* bad map */
652 221, 1, /* bad map */
653 222, 1, /* bad map */
654 223, 1, /* bad map */
655 224, 1, /* bad map */
656 225, 1, /* bad map */
657 226, 1, /* bad map */
659 228, 1, /* bad map */
664 233, 1, /* bad map */
665 234, 1, /* bad map */
671 240, 1, /* bad map */
672 241, 1, /* bad map */
674 243, 1, /* bad map */
679 1001, 1, /* bad map */
680 1002, 1, /* bad map */
681 1003, 1, /* bad map */
682 1004, 1, /* bad map */
683 1005, 1, /* bad map */
684 1006, 1, /* bad map */
716 int yaz_diag_bib1_to_srw (int code)
718 const int *p = srw_bib1_map;
728 int yaz_diag_srw_to_bib1(int code)
730 const int *p = srw_bib1_map;