2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srw.c,v 1.6 2003-02-19 15:22:11 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))
48 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
51 struct _xmlAttr *attr;
52 if (!match_element(ptr, elem))
55 for (attr = ptr->properties; attr; attr = attr->next)
56 if (!strcmp(attr->name, "type") &&
57 attr->children && attr->children->type == XML_TEXT_NODE)
59 const char *t = strchr(attr->children->content, ':');
63 t = attr->children->content;
64 if (!strcmp(t, "string"))
71 if (!ptr || ptr->type != XML_TEXT_NODE)
73 *val = odr_strdup(o, ptr->content);
75 *len = strlen(ptr->content);
80 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
83 return match_xsd_string_n(ptr, elem, o, val, 0);
86 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
88 struct _xmlAttr *attr;
89 if (!match_element(ptr, elem))
92 for (attr = ptr->properties; attr; attr = attr->next)
93 if (!strcmp(attr->name, "type") &&
94 attr->children && attr->children->type == XML_TEXT_NODE)
96 const char *t = strchr(attr->children->content, ':');
100 t = attr->children->content;
101 if (!strcmp(t, "integer"))
108 if (!ptr || ptr->type != XML_TEXT_NODE)
110 *val = odr_intdup(o, atoi(ptr->content));
114 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
115 int *num, void *client_data, const char *ns)
117 if (o->direction == ODR_DECODE)
122 for (ptr = pptr->children; ptr; ptr = ptr->next)
124 if (ptr->type == XML_ELEMENT_NODE &&
125 !strcmp(ptr->name, "record"))
130 *recs = odr_malloc(o, *num * sizeof(**recs));
131 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
133 if (ptr->type == XML_ELEMENT_NODE &&
134 !strcmp(ptr->name, "record"))
137 (*recs)[i].recordSchema = 0;
138 (*recs)[i].recordData_buf = 0;
139 (*recs)[i].recordData_len = 0;
140 (*recs)[i].recordPosition = 0;
141 for (rptr = ptr->children; rptr; rptr = rptr->next)
143 if (match_xsd_string(rptr, "recordSchema", o,
144 &(*recs)[i].recordSchema))
146 else if (match_xsd_string_n(rptr, "recordData", o,
147 &(*recs)[i].recordData_buf,
148 &(*recs)[i].recordData_len))
150 else if (match_xsd_integer(rptr, "recordPosition", o,
151 &(*recs)[i].recordPosition))
157 else if (o->direction == ODR_ENCODE)
160 for (i = 0; i < *num; i++)
162 xmlNodePtr rptr = xmlNewChild(pptr, 0, "record", 0);
163 add_xsd_string(rptr, "recordSchema", (*recs)[i].recordSchema);
164 add_xsd_string_n(rptr, "recordData", (*recs)[i].recordData_buf,
165 (*recs)[i].recordData_len);
166 add_xsd_integer(rptr, "recordPosition", (*recs)[i].recordPosition);
172 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
173 int *num, void *client_data, const char *ns)
175 if (o->direction == ODR_DECODE)
180 for (ptr = pptr->children; ptr; ptr = ptr->next)
182 if (ptr->type == XML_ELEMENT_NODE &&
183 !strcmp(ptr->name, "diagnostic"))
188 *recs = odr_malloc(o, *num * sizeof(**recs));
189 for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
191 if (ptr->type == XML_ELEMENT_NODE &&
192 !strcmp(ptr->name, "diagnostic"))
196 (*recs)[i].details = 0;
197 for (rptr = ptr->children; rptr; rptr = rptr->next)
199 if (match_xsd_integer(rptr, "code", o,
202 else if (match_xsd_string(rptr, "details", o,
203 &(*recs)[i].details))
210 else if (o->direction == ODR_ENCODE)
213 for (i = 0; i < *num; i++)
215 xmlNodePtr rptr = xmlNewChild(pptr, 0, "diagnostic", 0);
216 add_xsd_integer(rptr, "code", (*recs)[i].code);
217 add_xsd_string(rptr, "details", (*recs)[i].details);
224 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_searchRetrieve **handler_data,
225 void *client_data, const char *ns)
227 xmlNodePtr pptr = vptr;
228 if (o->direction == ODR_DECODE)
230 xmlNodePtr method = pptr->children;
232 while (method && method->type == XML_TEXT_NODE)
233 method = method->next;
235 if (method->type != XML_ELEMENT_NODE)
237 if (method && !strcmp(method->name, "searchRetrieveRequest"))
239 Z_SRW_searchRetrieve **p = handler_data;
240 xmlNodePtr ptr = method->children;
241 Z_SRW_searchRetrieveRequest *req;
243 *p = odr_malloc(o, sizeof(**p));
244 (*p)->which = Z_SRW_searchRetrieve_request;
245 req = (*p)->u.request = odr_malloc(o, sizeof(*req));
251 req->startRecord = 0;
252 req->maximumRecords = 0;
253 req->recordSchema = 0;
254 req->recordPacking = 0;
257 for (; ptr; ptr = ptr->next)
259 if (match_xsd_string(ptr, "query", o,
262 else if (match_xsd_string(ptr, "pQuery", o,
265 else if (match_xsd_string(ptr, "sortKeys", o,
268 else if (match_xsd_string(ptr, "recordSchema", o,
271 else if (match_xsd_string(ptr, "recordPacking", o,
272 &req->recordPacking))
274 else if (match_xsd_integer(ptr, "startRecord", o,
277 else if (match_xsd_integer(ptr, "maximumRecords", o,
278 &req->maximumRecords))
280 else if (match_xsd_string(ptr, "database", o,
283 /* missing is xQuery, xSortKeys .. */
286 else if (method && !strcmp(method->name, "searchRetrieveResponse"))
288 Z_SRW_searchRetrieve **p = handler_data;
289 xmlNodePtr ptr = method->children;
290 Z_SRW_searchRetrieveResponse *res;
292 *p = odr_malloc(o, sizeof(**p));
293 (*p)->which = Z_SRW_searchRetrieve_response;
294 res = (*p)->u.response = odr_malloc(o, sizeof(*res));
296 res->numberOfRecords = 0;
297 res->resultSetId = 0;
298 res->resultSetIdleTime = 0;
300 res->num_records = 0;
301 res->diagnostics = 0;
302 res->num_diagnostics = 0;
303 res->nextRecordPosition = 0;
305 for (; ptr; ptr = ptr->next)
307 if (match_xsd_integer(ptr, "numberOfRecords", o,
308 &res->numberOfRecords))
310 else if (match_xsd_string(ptr, "resultSetId", o,
313 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
314 &res->resultSetIdleTime))
316 else if (match_element(ptr, "records"))
317 yaz_srw_records(o, ptr, &res->records,
318 &res->num_records, client_data,
320 else if (match_element(ptr, "diagnostics"))
321 yaz_srw_diagnostics(o, ptr, &res->diagnostics,
322 &res->num_diagnostics,
324 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
325 &res->nextRecordPosition))
334 else if (o->direction == ODR_ENCODE)
336 Z_SRW_searchRetrieve **p = handler_data;
337 if ((*p)->which == Z_SRW_searchRetrieve_request)
339 Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
340 xmlNsPtr ns_srw = xmlNewNs(pptr, ns, "zs");
341 xmlNodePtr ptr = xmlNewChild(pptr, ns_srw,
342 "searchRetrieveRequest", 0);
344 add_xsd_string(ptr, "query", req->query);
345 add_xsd_string(ptr, "pQuery", req->pQuery);
346 add_xsd_string(ptr, "sortKeys", req->sortKeys);
347 add_xsd_integer(ptr, "startRecord", req->startRecord);
348 add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
349 add_xsd_string(ptr, "recordSchema", req->recordSchema);
350 add_xsd_string(ptr, "recordPacking", req->recordPacking);
351 add_xsd_string(ptr, "database", req->database);
353 else if ((*p)->which == Z_SRW_searchRetrieve_response)
355 Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
356 xmlNsPtr ns_srw = xmlNewNs(pptr, ns, "zs");
357 xmlNodePtr ptr = xmlNewChild(pptr, ns_srw,
358 "searchRetrieveResponse", 0);
360 add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
361 add_xsd_string(ptr, "resultSetId", res->resultSetId);
362 add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
363 if (res->num_records)
365 xmlNodePtr rptr = xmlNewChild(ptr, 0, "records", 0);
366 yaz_srw_records(o, rptr, &res->records, &res->num_records,
369 if (res->num_diagnostics)
371 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
372 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
373 &res->num_diagnostics, client_data, ns);
375 add_xsd_integer(ptr, "nextRecordPosition", res->nextRecordPosition);
384 Z_SRW_searchRetrieve *yaz_srw_get(ODR o, int which)
386 Z_SRW_searchRetrieve *sr = odr_malloc(o, sizeof(*o));
390 case Z_SRW_searchRetrieve_request:
391 sr->u.request = odr_malloc(o, sizeof(*sr->u.request));
392 sr->u.request->query = 0;
393 sr->u.request->xQuery = 0;
394 sr->u.request->pQuery = 0;
395 sr->u.request->sortKeys = 0;
396 sr->u.request->xSortKeys = 0;
397 sr->u.request->startRecord = 0;
398 sr->u.request->maximumRecords = 0;
399 sr->u.request->recordSchema = 0;
400 sr->u.request->recordPacking = 0;
401 sr->u.request->database = 0;
403 case Z_SRW_searchRetrieve_response:
404 sr->u.response = odr_malloc(o, sizeof(*sr->u.response));
405 sr->u.response->numberOfRecords = 0;
406 sr->u.response->resultSetId = 0;
407 sr->u.response->resultSetIdleTime = 0;
408 sr->u.response->records = 0;
409 sr->u.response->num_records = 0;
410 sr->u.response->diagnostics = 0;
411 sr->u.response->num_diagnostics = 0;
412 sr->u.response->nextRecordPosition = 0;
423 } yaz_srw_codes [] = {
424 {1, "Permanent system error"},
425 {2, "System temporarily unavailable"},
426 {3, "Authentication error"},
427 /* Diagnostics Relating to CQL */
428 {10, "Illegal query"},
429 {11, "Unsupported query type (XCQL vs CQL)"},
430 {12, "Too many characters in query"},
431 {13, "Unbalanced or illegal use of parentheses"},
432 {14, "Unbalanced or illegal use of quotes"},
433 {15, "Illegal or unsupported index set"},
434 {16, "Illegal or unsupported index"},
435 {17, "Illegal or unsupported combination of index and index set"},
436 {18, "Illegal or unsupported combination of indexes"},
437 {19, "Illegal or unsupported relation"},
438 {20, "Illegal or unsupported relation modifier"},
439 {21, "Illegal or unsupported combination of relation modifers"},
440 {22, "Illegal or unsupported combination of relation and index"},
441 {23, "Too many characters in term"},
442 {24, "Illegal combination of relation and term"},
443 {25, "Special characters not quoted in term"},
444 {26, "Non special character escaped in term"},
445 {27, "Empty term unsupported"},
446 {28, "Masking character not supported"},
447 {29, "Masked words too short"},
448 {30, "Too many masking characters in term"},
449 {31, "Anchoring character not supported"},
450 {32, "Anchoring character in illegal or unsupported position"},
451 {33, "Combination of proximity/adjacency and masking characters not supported"},
452 {34, "Combination of proximity/adjacency and anchoring characters not supported"},
453 {35, "Terms only exclusion (stop) words"},
454 {36, "Term in invalid format for index or relation"},
455 {37, "Illegal or unsupported boolean operator"},
456 {38, "Too many boolean operators in query"},
457 {39, "Proximity not supported"},
458 {40, "Illegal or unsupported proximity relation"},
459 {41, "Illegal or unsupported proximity distance"},
460 {42, "Illegal or unsupported proximity unit"},
461 {43, "Illegal or unsupported proximity ordering"},
462 {44, "Illegal or unsupported combination of proximity modifiers"},
463 {45, "Index set name (prefix) assigned to multiple identifiers"},
464 /* Diagnostics Relating to Result Sets */
465 {50, "Result sets not supported"},
466 {51, "Result set does not exist"},
467 {52, "Result set temporarily unavailable"},
468 {53, "Result sets only supported for retrieval"},
469 {54, "Retrieval may only occur from an existing result set"},
470 {55, "Combination of result sets with search terms not supported"},
471 {56, "Only combination of single result set with search terms supported"},
472 {57, "Result set created but no records available"},
473 {58, "Result set created with unpredictable partial results available"},
474 {59, "Result set created with valid partial results available"},
475 /* Diagnostics Relating to Records */
476 {60, "Too many records retrieved"},
477 {61, "First record position out of range"},
478 {62, "Negative number of records requested"},
479 {63, "System error in retrieving records"},
480 {64, "Record temporarily unavailable"},
481 {65, "Record does not exist"},
482 {66, "Unknown schema for retrieval"},
483 {67, "Record not available in this schema"},
484 {68, "Not authorised to send record"},
485 {69, "Not authorised to send record in this schema"},
486 {70, "Record too large to send"},
487 /* Diagnostics Relating to Sorting */
488 {80, "Sort not supported"},
489 {81, "Unsupported sort type (sortKeys vs xSortKeys)"},
490 {82, "Illegal or unsupported sort sequence"},
491 {83, "Too many records"},
492 {84, "Too many sort keys"},
493 {85, "Duplicate sort keys"},
494 {86, "Incompatible record formats"},
495 {87, "Unsupported schema for sort"},
496 {88, "Unsupported tag path for sort"},
497 {89, "Tag path illegal or unsupported for schema"},
498 {90, "Illegal or unsupported direction value"},
499 {91, "Illegal or unsupported case value"},
500 {92, "Illegal or unsupported missing value action"},
501 /* Diagnostics Relating to Explain */
502 {100, "Explain not supported"},
503 {101, "Explain request type not supported (SOAP vs GET)"},
504 {102, "Explain record temporarily unavailable"},
508 const char *yaz_diag_srw_str (int code)
511 for (i = 0; yaz_srw_codes[i].code; i++)
512 if (yaz_srw_codes[i].code == code)
513 return yaz_srw_codes[i].msg;
519 static int srw_bib1_map[] = {
548 24, 63, /* bad map */
549 25, 63, /* bad map */
550 26, 63, /* bad map */
558 100, 1, /* bad map */
606 205, 1, /* bad map */
607 206, 1, /* bad map */
609 208, 1, /* bad map */
620 218, 1, /* bad map */
621 219, 1, /* bad map */
622 220, 1, /* bad map */
623 221, 1, /* bad map */
624 222, 1, /* bad map */
625 223, 1, /* bad map */
626 224, 1, /* bad map */
627 225, 1, /* bad map */
628 226, 1, /* bad map */
630 228, 1, /* bad map */
635 233, 1, /* bad map */
636 234, 1, /* bad map */
642 240, 1, /* bad map */
643 241, 1, /* bad map */
645 243, 1, /* bad map */
650 1001, 1, /* bad map */
651 1002, 1, /* bad map */
652 1003, 1, /* bad map */
653 1004, 1, /* bad map */
654 1005, 1, /* bad map */
655 1006, 1, /* bad map */
687 int yaz_diag_bib1_to_srw (int code)
689 const int *p = srw_bib1_map;
699 int yaz_diag_srw_to_bib1(int code)
701 const int *p = srw_bib1_map;