1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
6 \brief Query / XML conversions
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
20 #include <yaz/logrpn.h>
21 #include <yaz/xmlquery.h>
22 #include <yaz/nmem_xml.h>
23 #include <yaz/xml_get.h>
24 #include <yaz/oid_db.h>
26 static int check_diagnostic(const xmlNode *ptr, ODR odr,
27 int *error_code, const char **addinfo)
29 if (ptr && ptr->type == XML_ELEMENT_NODE &&
30 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
32 struct _xmlAttr *attr;
33 const char *code_str = 0;
34 const char *addinfo_str = 0;
35 for (attr = ptr->properties; attr; attr = attr->next)
37 if (!xmlStrcmp(attr->name, BAD_CAST "code") &&
38 attr->children && attr->children->type == XML_TEXT_NODE)
39 code_str = (const char *) attr->children->content;
40 else if (!xmlStrcmp(attr->name, BAD_CAST "addinfo") &&
41 attr->children && attr->children->type == XML_TEXT_NODE)
42 addinfo_str = (const char *) attr->children->content;
46 *addinfo = "bad attribute for diagnostic element";
53 *addinfo = "missing @code for diagnostic element";
56 *error_code = atoi(code_str);
58 *addinfo = odr_strdup(odr, addinfo_str);
65 static void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
69 const char *setname = 0;
70 char oid_name_str[OID_STR_MAX];
72 if (element->attributeSet)
74 setname = yaz_oid_to_string_buf(element->attributeSet,
78 if (element->which == Z_AttributeValue_numeric)
80 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
83 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
85 assert(*element->attributeType > 0 && *element->attributeType < 20);
86 sprintf(formstr, ODR_INT_PRINTF, *element->attributeType);
87 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
89 sprintf(formstr, ODR_INT_PRINTF, *element->value.numeric);
90 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
92 else if (element->which == Z_AttributeValue_complex)
95 for (i = 0; i<element->value.complex->num_list; i++)
97 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
100 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
102 sprintf(formstr, ODR_INT_PRINTF, *element->attributeType);
103 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
105 if (element->value.complex->list[i]->which ==
106 Z_StringOrNumeric_string)
108 xmlNewProp(node, BAD_CAST "value", BAD_CAST
109 element->value.complex->list[i]->u.string);
111 else if (element->value.complex->list[i]->which ==
112 Z_StringOrNumeric_numeric)
114 sprintf(formstr, ODR_INT_PRINTF,
115 *element->value.complex->list[i]->u.numeric);
116 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
123 static xmlNodePtr yaz_query2xml_term(const Z_Term *term, xmlNodePtr parent)
126 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
128 const char *type = 0;
134 t = xmlNewTextLen(BAD_CAST term->u.general->buf, term->u.general->len);
138 sprintf(formstr, ODR_INT_PRINTF, *term->u.numeric);
139 t = xmlNewText(BAD_CAST formstr);
141 case Z_Term_characterString:
143 t = xmlNewText(BAD_CAST term->u.characterString);
148 case Z_Term_dateTime:
151 case Z_Term_external:
154 case Z_Term_integerAndUnit:
155 type ="integerAndUnit";
163 if (t) /* got a term node ? */
164 xmlAddChild(node, t);
166 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
170 static xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
173 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
174 int num_attributes = zapt->attributes->num_attributes;
176 for (i = 0; i<num_attributes; i++)
177 yaz_query2xml_attribute_element(zapt->attributes->attributes[i], node);
178 yaz_query2xml_term(zapt->term, node);
184 static void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
186 const char *type = 0;
195 case Z_Operator_and_not:
198 case Z_Operator_prox:
204 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
206 if (op->which == Z_Operator_prox)
210 if (op->u.prox->exclusion)
212 if (*op->u.prox->exclusion)
213 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "true");
215 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "false");
217 sprintf(formstr, ODR_INT_PRINTF, *op->u.prox->distance);
218 xmlNewProp(node, BAD_CAST "distance", BAD_CAST formstr);
220 if (*op->u.prox->ordered)
221 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
223 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
225 sprintf(formstr, ODR_INT_PRINTF, *op->u.prox->relationType);
226 xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
228 switch(op->u.prox->which)
230 case Z_ProximityOperator_known:
231 sprintf(formstr, ODR_INT_PRINTF, *op->u.prox->u.known);
232 xmlNewProp(node, BAD_CAST "knownProximityUnit",
235 case Z_ProximityOperator_private:
237 xmlNewProp(node, BAD_CAST "privateProximityUnit",
244 static xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
247 if (zs->which == Z_RPNStructure_complex)
249 Z_Complex *zc = zs->u.complex;
251 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
253 yaz_query2xml_operator(zc->roperator, node);
254 yaz_query2xml_rpnstructure(zc->s1, node);
255 yaz_query2xml_rpnstructure(zc->s2, node);
258 else if (zs->which == Z_RPNStructure_simple)
260 if (zs->u.simple->which == Z_Operand_APT)
261 return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
263 else if (zs->u.simple->which == Z_Operand_resultSetId)
264 return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset",
265 BAD_CAST zs->u.simple->u.resultSetId);
270 static xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
272 if (rpn->attributeSetId)
274 char oid_name_str[OID_STR_MAX];
275 const char *setname = yaz_oid_to_string_buf(rpn->attributeSetId,
278 xmlNewProp(parent, BAD_CAST "set", BAD_CAST setname);
280 return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
283 static xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
288 static xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
293 static xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
298 void yaz_rpnquery2xml(const Z_RPNQuery *rpn, xmlDocPtr *docp)
302 query.which = Z_Query_type_1;
303 query.u.type_1 = (Z_RPNQuery *) rpn;
304 yaz_query2xml(&query, docp);
307 void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
309 xmlNodePtr top_node, q_node = 0, child_node = 0;
314 top_node = xmlNewNode(0, BAD_CAST "query");
319 case Z_Query_type_101:
320 q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
321 child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
324 q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
325 child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
327 case Z_Query_type_100:
328 q_node = xmlNewChild(top_node, 0, BAD_CAST "z39.58", 0);
329 child_node = yaz_query2xml_z3958(q->u.type_100, q_node);
331 case Z_Query_type_104:
332 if (q->u.type_104->which == Z_External_CQL)
334 q_node = xmlNewChild(top_node, 0, BAD_CAST "cql", 0);
335 child_node = yaz_query2xml_cql(q->u.type_104->u.cql, q_node);
338 if (child_node && q_node)
340 *docp = xmlNewDoc(BAD_CAST "1.0");
341 xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
346 xmlFreeNode(top_node);
350 static bool_t *boolVal(ODR odr, const char *str)
352 if (*str == '\0' || strchr("0fF", *str))
353 return odr_booldup(odr, 0);
354 return odr_booldup(odr, 1);
357 static Odr_int *intVal(ODR odr, const char *str)
359 return odr_intdup(odr, atoi(str));
362 static void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
364 int *error_code, const char **addinfo)
366 const char *type = yaz_xml_get_prop((xmlNodePtr) ptr, "type");
370 *addinfo = "no operator type";
373 *op = (Z_Operator*) odr_malloc(odr, sizeof(Z_Operator));
374 if (!strcmp(type, "and"))
376 (*op)->which = Z_Operator_and;
377 (*op)->u.op_and = odr_nullval();
379 else if (!strcmp(type, "or"))
381 (*op)->which = Z_Operator_or;
382 (*op)->u.op_or = odr_nullval();
384 else if (!strcmp(type, "not"))
386 (*op)->which = Z_Operator_and_not;
387 (*op)->u.and_not = odr_nullval();
389 else if (!strcmp(type, "prox"))
391 struct _xmlAttr *attr;
392 Z_ProximityOperator *pop = (Z_ProximityOperator *)
393 odr_malloc(odr, sizeof(*pop));
394 (*op)->which = Z_Operator_prox;
398 pop->ordered = odr_booldup(odr, 1);
400 odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
401 pop->which = Z_ProximityOperator_known;
402 pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
403 pop->distance = odr_intdup(odr, 1);
405 for (attr = ptr->properties; attr; attr = attr->next)
407 const char *value = (const char *) attr->children->content;
408 if (!xmlStrcmp(attr->name, BAD_CAST "type"))
410 else if (!xmlStrcmp(attr->name, BAD_CAST "exclusion"))
411 pop->exclusion = boolVal(odr, value);
412 else if (!xmlStrcmp(attr->name, BAD_CAST "distance"))
413 pop->distance = intVal(odr, value);
414 else if (!xmlStrcmp(attr->name, BAD_CAST "ordered"))
415 pop->ordered = boolVal(odr, value);
416 else if (!xmlStrcmp(attr->name, BAD_CAST "relationType"))
417 pop->relationType = intVal(odr, value);
418 else if (!xmlStrcmp(attr->name, BAD_CAST "knownProximityUnit"))
420 pop->which = Z_ProximityOperator_known;
421 pop->u.known = intVal(odr, value);
423 else if (!xmlStrcmp(attr->name, BAD_CAST "privateProximityUnit"))
425 pop->which = Z_ProximityOperator_private;
426 pop->u.known = intVal(odr, value);
431 *addinfo = "bad proximity attribute";
439 *addinfo = "bad operator type";
443 static void yaz_xml2query_attribute_element(const xmlNode *ptr,
444 Z_AttributeElement **elem, ODR odr,
446 const char **addinfo)
453 struct _xmlAttr *attr;
454 for (attr = ptr->properties; attr; attr = attr->next)
456 if (!xmlStrcmp(attr->name, BAD_CAST "set") &&
457 attr->children && attr->children->type == XML_TEXT_NODE)
458 set = attr->children->content;
459 else if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
460 attr->children && attr->children->type == XML_TEXT_NODE)
461 type = attr->children->content;
462 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
463 attr->children && attr->children->type == XML_TEXT_NODE)
465 value = attr->children->content;
471 *addinfo = "bad attribute for attr content";
478 *addinfo = "missing type attribute for att content";
484 *addinfo = "missing value attribute for att content";
488 *elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(**elem));
490 (*elem)->attributeSet = yaz_string_to_oid_odr(yaz_oid_std(),
495 (*elem)->attributeSet = 0;
496 (*elem)->attributeType = intVal(odr, (const char *) type);
498 /* looks like a number ? */
499 for (i = 0; value[i] && value[i] >= '0' && value[i] <= '9'; i++)
501 if (num_values > 1 || value[i])
502 { /* multiple values or string, so turn to complex attribute */
503 (*elem)->which = Z_AttributeValue_complex;
504 (*elem)->value.complex =
505 (Z_ComplexAttribute*) odr_malloc(odr, sizeof(Z_ComplexAttribute));
506 (*elem)->value.complex->num_list = num_values;
507 (*elem)->value.complex->list = (Z_StringOrNumeric **)
508 odr_malloc(odr, sizeof(Z_StringOrNumeric*) * num_values);
510 /* second pass over attr values */
512 for (attr = ptr->properties; attr; attr = attr->next)
514 if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
515 attr->children && attr->children->type == XML_TEXT_NODE)
517 const char *val = (const char *) attr->children->content;
518 assert (i < num_values);
519 (*elem)->value.complex->list[i] = (Z_StringOrNumeric *)
520 odr_malloc(odr, sizeof(Z_StringOrNumeric));
521 (*elem)->value.complex->list[i]->which =
522 Z_StringOrNumeric_string;
523 (*elem)->value.complex->list[i]->u.string =
524 odr_strdup(odr, val);
528 (*elem)->value.complex->num_semanticAction = 0;
529 (*elem)->value.complex->semanticAction = 0;
532 { /* good'ld numeric value */
533 (*elem)->which = Z_AttributeValue_numeric;
534 (*elem)->value.numeric = intVal(odr, (const char *) value);
538 static char *strVal(const xmlNode *ptr_cdata, ODR odr)
540 return nmem_text_node_cdata(ptr_cdata, odr_getmem(odr));
543 static void yaz_xml2query_term(const xmlNode *ptr, Z_Term **term, ODR odr,
544 int *error_code, const char **addinfo)
547 struct _xmlAttr *attr;
548 char *cdata = strVal(ptr->children, odr);
550 for (attr = ptr->properties; attr; attr = attr->next)
552 if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
553 attr->children && attr->children->type == XML_TEXT_NODE)
554 type = attr->children->content;
558 *addinfo = "bad attribute for attr content";
562 *term = (Z_Term *) odr_malloc(odr, sizeof(Z_Term));
564 if (!type || !xmlStrcmp(type, BAD_CAST "general"))
566 (*term)->which = Z_Term_general;
568 odr_create_Odr_oct(odr, cdata, strlen(cdata));
570 else if (!xmlStrcmp(type, BAD_CAST "numeric"))
572 (*term)->which = Z_Term_numeric;
573 (*term)->u.numeric = intVal(odr, cdata);
575 else if (!xmlStrcmp(type, BAD_CAST "string"))
577 (*term)->which = Z_Term_characterString;
578 (*term)->u.characterString = cdata;
580 else if (!xmlStrcmp(type, BAD_CAST "oid"))
583 *addinfo = "unhandled term type: oid";
585 else if (!xmlStrcmp(type, BAD_CAST "dateTime"))
588 *addinfo = "unhandled term type: dateTime";
590 else if (!xmlStrcmp(type, BAD_CAST "integerAndUnit"))
593 *addinfo = "unhandled term type: integerAndUnit";
595 else if (!xmlStrcmp(type, BAD_CAST "null"))
597 (*term)->which = Z_Term_null;
598 (*term)->u.null = odr_nullval();
603 *addinfo = "unhandled term type";
607 static void yaz_xml2query_apt(const xmlNode *ptr_apt,
608 Z_AttributesPlusTerm **zapt, ODR odr,
609 int *error_code, const char **addinfo)
611 const xmlNode *ptr = ptr_apt->children;
614 *zapt = (Z_AttributesPlusTerm *)
615 odr_malloc(odr, sizeof(Z_AttributesPlusTerm));
617 /* deal with attributes */
618 (*zapt)->attributes = (Z_AttributeList*)
619 odr_malloc(odr, sizeof(Z_AttributeList));
621 /* how many attributes? */
622 for (; ptr; ptr = ptr->next)
623 if (ptr->type == XML_ELEMENT_NODE)
625 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
631 /* allocate and parse for real */
632 (*zapt)->attributes->num_attributes = num_attr;
633 (*zapt)->attributes->attributes = (Z_AttributeElement **)
634 odr_malloc(odr, sizeof(Z_AttributeElement*) * num_attr);
637 ptr = ptr_apt->children;
638 for (; ptr; ptr = ptr->next)
639 if (ptr->type == XML_ELEMENT_NODE)
641 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
643 yaz_xml2query_attribute_element(
644 ptr, &(*zapt)->attributes->attributes[i], odr,
645 error_code, addinfo);
651 if (check_diagnostic(ptr, odr, error_code, addinfo))
654 if (ptr && ptr->type == XML_ELEMENT_NODE)
656 if (!xmlStrcmp(ptr->name, BAD_CAST "term"))
659 yaz_xml2query_term(ptr, &(*zapt)->term, odr, error_code, addinfo);
664 *addinfo = "bad element in apt content";
670 *addinfo = "missing term node in apt content";
674 static void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
675 ODR odr, int *error_code, const char **addinfo)
679 *rset = strVal(ptr->children, odr);
684 *addinfo = "missing rset content";
688 static void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
690 int *error_code, const char **addinfo)
692 while (ptr && ptr->type != XML_ELEMENT_NODE)
695 if (!ptr || ptr->type != XML_ELEMENT_NODE)
698 *addinfo = "missing rpn operator, rset, apt node";
701 if (check_diagnostic(ptr, odr, error_code, addinfo))
704 *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
705 if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
707 Z_Complex *zc = (Z_Complex *) odr_malloc(odr, sizeof(Z_Complex));
709 (*zs)->which = Z_RPNStructure_complex;
710 (*zs)->u.complex = zc;
712 yaz_xml2query_operator(ptr, &zc->roperator, odr, error_code, addinfo);
715 while (ptr && ptr->type != XML_ELEMENT_NODE)
717 yaz_xml2query_rpnstructure(ptr, &zc->s1, odr, error_code, addinfo);
720 while (ptr && ptr->type != XML_ELEMENT_NODE)
722 yaz_xml2query_rpnstructure(ptr, &zc->s2, odr, error_code, addinfo);
726 Z_Operand *s = (Z_Operand *) odr_malloc(odr, sizeof(Z_Operand));
727 (*zs)->which = Z_RPNStructure_simple;
729 if (!xmlStrcmp(ptr->name, BAD_CAST "apt"))
731 s->which = Z_Operand_APT;
732 yaz_xml2query_apt(ptr, &s->u.attributesPlusTerm,
733 odr, error_code, addinfo);
735 else if (!xmlStrcmp(ptr->name, BAD_CAST "rset"))
737 s->which = Z_Operand_resultSetId;
738 yaz_xml2query_rset(ptr, &s->u.resultSetId,
739 odr, error_code, addinfo);
744 *addinfo = "bad element: expected binary, apt or rset";
749 static void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
750 int *error_code, const char **addinfo)
752 const char *set = yaz_xml_get_prop((xmlNodePtr) ptr, "set");
754 *query = (Z_RPNQuery*) odr_malloc(odr, sizeof(Z_RPNQuery));
757 (*query)->attributeSetId =
758 yaz_string_to_oid_odr(yaz_oid_std(),
759 CLASS_ATTSET, set, odr);
762 (*query)->attributeSetId = 0;
763 yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,
764 odr, error_code, addinfo);
767 static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
768 int *error_code, const char **addinfo)
770 if (check_diagnostic(ptr, odr, error_code, addinfo))
772 if (ptr && ptr->type == XML_ELEMENT_NODE &&
773 !xmlStrcmp(ptr->name, BAD_CAST "query"))
777 while (ptr && ptr->type != XML_ELEMENT_NODE)
779 if (!ptr || ptr->type != XML_ELEMENT_NODE)
782 *addinfo = "missing query content";
785 type = (const char *) ptr->name;
787 *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
788 if (!type || !strcmp(type, "rpn"))
790 (*query)->which = Z_Query_type_1;
791 yaz_xml2query_rpn(ptr, &(*query)->u.type_1, odr,
792 error_code, addinfo);
794 else if (!strcmp(type, "ccl"))
797 *addinfo = "ccl not supported yet";
799 else if (!strcmp(type, "z39.58"))
802 *addinfo = "z39.58 not supported yet";
804 else if (!strcmp(type, "cql"))
807 *addinfo = "cql not supported yet";
812 *addinfo = "unsupported query type";
818 *addinfo = "missing query element";
822 void yaz_xml2query(const xmlNode *xmlnodep, Z_Query **query, ODR odr,
823 int *error_code, const char **addinfo)
825 yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
834 * c-file-style: "Stroustrup"
835 * indent-tabs-mode: nil
837 * vim: shiftwidth=4 tabstop=8 expandtab