2 * Copyright (C) 1995-2006, Index Data ApS
5 * $Id: xmlquery.c,v 1.9 2006-05-02 20:47:45 adam Exp $
9 \brief Query / XML conversions
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
20 #include <yaz/logrpn.h>
21 #include <yaz/xmlquery.h>
23 void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
27 const char *setname = 0;
29 if (element->attributeSet)
32 attrset = oid_getentbyoid (element->attributeSet);
33 setname = attrset->desc;
36 if (element->which == Z_AttributeValue_numeric)
38 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
41 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
43 sprintf(formstr, "%d", *element->attributeType);
44 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
46 sprintf(formstr, "%d", *element->value.numeric);
47 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
49 else if (element->which == Z_AttributeValue_complex)
52 for (i = 0; i<element->value.complex->num_list; i++)
54 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
57 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
59 sprintf(formstr, "%d", *element->attributeType);
60 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
62 if (element->value.complex->list[i]->which ==
63 Z_StringOrNumeric_string)
65 xmlNewProp(node, BAD_CAST "value", BAD_CAST
66 element->value.complex->list[i]->u.string);
68 else if (element->value.complex->list[i]->which ==
69 Z_StringOrNumeric_numeric)
71 sprintf(formstr, "%d",
72 *element->value.complex->list[i]->u.numeric);
73 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
80 xmlNodePtr yaz_query2xml_term(const Z_Term *term,
84 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
92 t = xmlNewTextLen(BAD_CAST term->u.general->buf, term->u.general->len);
96 sprintf(formstr, "%d", *term->u.numeric);
97 t = xmlNewText(BAD_CAST formstr);
99 case Z_Term_characterString:
101 t = xmlNewText(BAD_CAST term->u.characterString);
106 case Z_Term_dateTime:
109 case Z_Term_external:
112 case Z_Term_integerAndUnit:
113 type ="integerAndUnit";
121 if (t) /* got a term node ? */
122 xmlAddChild(node, t);
124 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
128 xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
131 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
132 int num_attributes = zapt->attributes->num_attributes;
134 for (i = 0; i<num_attributes; i++)
135 yaz_query2xml_attribute_element(zapt->attributes->attributes[i], node);
136 yaz_query2xml_term(zapt->term, node);
142 void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
144 const char *type = 0;
153 case Z_Operator_and_not:
156 case Z_Operator_prox:
162 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
164 if (op->which == Z_Operator_prox)
168 if (op->u.prox->exclusion)
170 if (*op->u.prox->exclusion)
171 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "true");
173 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "false");
175 sprintf(formstr, "%d", *op->u.prox->distance);
176 xmlNewProp(node, BAD_CAST "distance", BAD_CAST formstr);
178 if (*op->u.prox->ordered)
179 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
181 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
183 sprintf(formstr, "%d", *op->u.prox->relationType);
184 xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
186 switch(op->u.prox->which)
188 case Z_ProximityOperator_known:
189 sprintf(formstr, "%d", *op->u.prox->u.known);
190 xmlNewProp(node, BAD_CAST "knownProximityUnit",
193 case Z_ProximityOperator_private:
195 xmlNewProp(node, BAD_CAST "privateProximityUnit",
202 xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
205 if (zs->which == Z_RPNStructure_complex)
207 Z_Complex *zc = zs->u.complex;
209 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
211 yaz_query2xml_operator(zc->roperator, node);
212 yaz_query2xml_rpnstructure(zc->s1, node);
213 yaz_query2xml_rpnstructure(zc->s2, node);
216 else if (zs->which == Z_RPNStructure_simple)
218 if (zs->u.simple->which == Z_Operand_APT)
219 return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
221 else if (zs->u.simple->which == Z_Operand_resultSetId)
222 return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset",
223 BAD_CAST zs->u.simple->u.resultSetId);
228 xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
230 oident *attrset = oid_getentbyoid (rpn->attributeSetId);
231 if (attrset && attrset->value)
232 xmlNewProp(parent, BAD_CAST "set", BAD_CAST attrset->desc);
233 return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
236 xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
241 xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
246 xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
251 void yaz_rpnquery2xml(const Z_RPNQuery *rpn, void *docp_void)
255 query.which = Z_Query_type_1;
256 query.u.type_1 = (Z_RPNQuery *) rpn;
257 yaz_query2xml(&query, docp_void);
260 void yaz_query2xml(const Z_Query *q, void *docp_void)
262 xmlDocPtr *docp = (xmlDocPtr *) docp_void;
263 xmlNodePtr top_node, q_node = 0, child_node = 0;
268 top_node = xmlNewNode(0, BAD_CAST "query");
273 case Z_Query_type_101:
274 q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
275 child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
278 q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
279 child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
281 case Z_Query_type_100:
282 q_node = xmlNewChild(top_node, 0, BAD_CAST "z39.58", 0);
283 child_node = yaz_query2xml_z3958(q->u.type_100, q_node);
285 case Z_Query_type_104:
286 if (q->u.type_104->which == Z_External_CQL)
288 q_node = xmlNewChild(top_node, 0, BAD_CAST "cql", 0);
289 child_node = yaz_query2xml_cql(q->u.type_104->u.cql, q_node);
292 if (child_node && q_node)
294 *docp = xmlNewDoc(BAD_CAST "1.0");
295 xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
300 xmlFreeNode(top_node);
304 bool_t *boolVal(ODR odr, const char *str)
306 if (*str == '\0' || strchr("0fF", *str))
307 return odr_intdup(odr, 0);
308 return odr_intdup(odr, 1);
311 int *intVal(ODR odr, const char *str)
313 return odr_intdup(odr, atoi(str));
316 void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
317 ODR odr, int *error_code, const char **addinfo)
319 const char *type = (const char *)
320 xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
324 *addinfo = "no operator type";
327 *op = (Z_Operator*) odr_malloc(odr, sizeof(Z_Operator));
328 if (!strcmp(type, "and"))
330 (*op)->which = Z_Operator_and;
331 (*op)->u.op_and = odr_nullval();
333 else if (!strcmp(type, "or"))
335 (*op)->which = Z_Operator_or;
336 (*op)->u.op_or = odr_nullval();
338 else if (!strcmp(type, "not"))
340 (*op)->which = Z_Operator_and_not;
341 (*op)->u.and_not = odr_nullval();
343 else if (!strcmp(type, "prox"))
346 Z_ProximityOperator *pop = (Z_ProximityOperator *)
347 odr_malloc(odr, sizeof(Z_ProximityOperator));
349 (*op)->which = Z_Operator_prox;
352 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
353 BAD_CAST "exclusion");
355 pop->exclusion = boolVal(odr, atval);
359 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
360 BAD_CAST "distance");
362 pop->distance = intVal(odr, atval);
364 pop->distance = odr_intdup(odr, 1);
366 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
369 pop->ordered = boolVal(odr, atval);
371 pop->ordered = odr_intdup(odr, 1);
373 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
374 BAD_CAST "relationType");
376 pop->relationType = intVal(odr, atval);
379 odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
381 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
382 BAD_CAST "knownProximityUnit");
385 pop->which = Z_ProximityOperator_known;
386 pop->u.known = intVal(odr, atval);
390 pop->which = Z_ProximityOperator_known;
391 pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
394 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
395 BAD_CAST "privateProximityUnit");
398 pop->which = Z_ProximityOperator_private;
399 pop->u.zprivate = intVal(odr, atval);
405 *addinfo = "bad operator type";
409 void yaz_xml2query_attribute_element(const xmlNode *ptr,
410 Z_AttributeElement **elem, ODR odr,
411 int *error_code, const char **addinfo)
418 struct _xmlAttr *attr;
419 for (attr = ptr->properties; attr; attr = attr->next)
421 if (!xmlStrcmp(attr->name, BAD_CAST "set") &&
422 attr->children && attr->children->type == XML_TEXT_NODE)
423 set = attr->children->content;
424 else if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
425 attr->children && attr->children->type == XML_TEXT_NODE)
426 type = attr->children->content;
427 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
428 attr->children && attr->children->type == XML_TEXT_NODE)
430 value = attr->children->content;
436 *addinfo = "bad attribute for attr content";
443 *addinfo = "missing type attribute for att content";
449 *addinfo = "missing value attribute for att content";
453 *elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(**elem));
455 (*elem)->attributeSet = yaz_str_to_z3950oid(odr, CLASS_ATTSET,
458 (*elem)->attributeSet = 0;
459 (*elem)->attributeType = intVal(odr, (const char *) type);
461 /* looks like a number ? */
462 for (i = 0; value[i] && value[i] >= '0' && value[i] <= '9'; i++)
464 if (num_values > 1 || value[i])
465 { /* multiple values or string, so turn to complex attribute */
466 (*elem)->which = Z_AttributeValue_complex;
467 (*elem)->value.complex =
468 (Z_ComplexAttribute*) odr_malloc(odr, sizeof(Z_ComplexAttribute));
469 (*elem)->value.complex->num_list = num_values;
470 (*elem)->value.complex->list = (Z_StringOrNumeric **)
471 odr_malloc(odr, sizeof(Z_StringOrNumeric*) * num_values);
473 /* second pass over attr values */
475 for (attr = ptr->properties; attr; attr = attr->next)
477 if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
478 attr->children && attr->children->type == XML_TEXT_NODE)
480 const char *val = (const char *) attr->children->content;
481 assert (i < num_values);
482 (*elem)->value.complex->list[i] = (Z_StringOrNumeric *)
483 odr_malloc(odr, sizeof(Z_StringOrNumeric));
484 (*elem)->value.complex->list[i]->which =
485 Z_StringOrNumeric_string;
486 (*elem)->value.complex->list[i]->u.string =
487 odr_strdup(odr, val);
491 (*elem)->value.complex->num_semanticAction = 0;
492 (*elem)->value.complex->semanticAction = 0;
495 { /* good'ld numeric value */
496 (*elem)->which = Z_AttributeValue_numeric;
497 (*elem)->value.numeric = intVal(odr, (const char *) value);
501 char *strVal(const xmlNode *ptr_cdata, ODR odr)
503 return nmem_text_node_cdata(ptr_cdata, odr->mem);
506 void yaz_xml2query_term(const xmlNode *ptr,
507 Z_Term **term, ODR odr,
508 int *error_code, const char **addinfo)
511 struct _xmlAttr *attr;
512 char *cdata = strVal(ptr->children, odr);
514 for (attr = ptr->properties; attr; attr = attr->next)
516 if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
517 attr->children && attr->children->type == XML_TEXT_NODE)
518 type = attr->children->content;
522 *addinfo = "bad attribute for attr content";
526 *term = (Z_Term *) odr_malloc(odr, sizeof(Z_Term));
528 if (!type || !xmlStrcmp(type, BAD_CAST "general"))
530 (*term)->which = Z_Term_general;
532 odr_create_Odr_oct(odr, (unsigned char *)cdata, strlen(cdata));
534 else if (!xmlStrcmp(type, BAD_CAST "numeric"))
536 (*term)->which = Z_Term_numeric;
537 (*term)->u.numeric = intVal(odr, cdata);
539 else if (!xmlStrcmp(type, BAD_CAST "string"))
541 (*term)->which = Z_Term_characterString;
542 (*term)->u.characterString = cdata;
544 else if (!xmlStrcmp(type, BAD_CAST "oid"))
547 *addinfo = "unhandled term type: oid";
549 else if (!xmlStrcmp(type, BAD_CAST "dateTime"))
552 *addinfo = "unhandled term type: dateTime";
554 else if (!xmlStrcmp(type, BAD_CAST "integerAndUnit"))
557 *addinfo = "unhandled term type: integerAndUnit";
559 else if (!xmlStrcmp(type, BAD_CAST "null"))
561 (*term)->which = Z_Term_null;
562 (*term)->u.null = odr_nullval();
567 *addinfo = "unhandled term type";
571 void yaz_xml2query_apt(const xmlNode *ptr_apt,
572 Z_AttributesPlusTerm **zapt, ODR odr,
573 int *error_code, const char **addinfo)
575 const xmlNode *ptr = ptr_apt->children;
578 *zapt = (Z_AttributesPlusTerm *)
579 odr_malloc(odr, sizeof(Z_AttributesPlusTerm));
581 /* deal with attributes */
582 (*zapt)->attributes = (Z_AttributeList*)
583 odr_malloc(odr, sizeof(Z_AttributeList));
585 /* how many attributes? */
586 for (; ptr; ptr = ptr->next)
587 if (ptr->type == XML_ELEMENT_NODE)
589 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
595 /* allocate and parse for real */
596 (*zapt)->attributes->num_attributes = num_attr;
597 (*zapt)->attributes->attributes = (Z_AttributeElement **)
598 odr_malloc(odr, sizeof(Z_AttributeElement*) * num_attr);
601 ptr = ptr_apt->children;
602 for (; ptr; ptr = ptr->next)
603 if (ptr->type == XML_ELEMENT_NODE)
605 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
607 yaz_xml2query_attribute_element(
608 ptr, &(*zapt)->attributes->attributes[i], odr,
609 error_code, addinfo);
615 if (ptr && ptr->type == XML_ELEMENT_NODE)
617 if (!xmlStrcmp(ptr->name, BAD_CAST "term"))
620 yaz_xml2query_term(ptr, &(*zapt)->term, odr, error_code, addinfo);
625 *addinfo = "bad element in apt content";
631 *addinfo = "missing term node in apt content";
635 void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
636 ODR odr, int *error_code, const char **addinfo)
640 *rset = strVal(ptr->children, odr);
645 *addinfo = "missing rset content";
649 void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
650 ODR odr, int *error_code, const char **addinfo)
652 while (ptr && ptr->type != XML_ELEMENT_NODE)
655 if (!ptr || ptr->type != XML_ELEMENT_NODE)
658 *addinfo = "missing rpn operator, rset, apt node";
661 *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
662 if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
664 Z_Complex *zc = odr_malloc(odr, sizeof(Z_Complex));
666 (*zs)->which = Z_RPNStructure_complex;
667 (*zs)->u.complex = zc;
669 yaz_xml2query_operator(ptr, &zc->roperator, odr, error_code, addinfo);
672 while (ptr && ptr->type != XML_ELEMENT_NODE)
674 yaz_xml2query_rpnstructure(ptr, &zc->s1, odr, error_code, addinfo);
677 while (ptr && ptr->type != XML_ELEMENT_NODE)
679 yaz_xml2query_rpnstructure(ptr, &zc->s2, odr, error_code, addinfo);
683 Z_Operand *s = (Z_Operand *) odr_malloc(odr, sizeof(Z_Operand));
684 (*zs)->which = Z_RPNStructure_simple;
686 if (!xmlStrcmp(ptr->name, BAD_CAST "apt"))
688 s->which = Z_Operand_APT;
689 yaz_xml2query_apt(ptr, &s->u.attributesPlusTerm,
690 odr, error_code, addinfo);
692 else if (!xmlStrcmp(ptr->name, BAD_CAST "rset"))
694 s->which = Z_Operand_resultSetId;
695 yaz_xml2query_rset(ptr, &s->u.resultSetId,
696 odr, error_code, addinfo);
701 *addinfo = "bad element: expected binary, apt or rset";
706 void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
707 int *error_code, const char **addinfo)
709 const char *set = (const char *)
710 xmlGetProp((xmlNodePtr) ptr, BAD_CAST "set");
712 *query = (Z_RPNQuery*) odr_malloc(odr, sizeof(Z_RPNQuery));
714 (*query)->attributeSetId = yaz_str_to_z3950oid(odr, CLASS_ATTSET, set);
716 (*query)->attributeSetId = 0;
717 yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,
718 odr, error_code, addinfo);
721 static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
722 int *error_code, const char **addinfo)
724 if (ptr && ptr->type == XML_ELEMENT_NODE &&
725 !xmlStrcmp(ptr->name, BAD_CAST "query"))
729 while (ptr && ptr->type != XML_ELEMENT_NODE)
731 if (!ptr || ptr->type != XML_ELEMENT_NODE)
734 *addinfo = "missing query content";
737 type = (const char *) ptr->name;
739 *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
740 if (!type || !strcmp(type, "rpn"))
742 (*query)->which = Z_Query_type_1;
743 yaz_xml2query_rpn(ptr, &(*query)->u.type_1, odr,
744 error_code, addinfo);
746 else if (!strcmp(type, "ccl"))
749 *addinfo = "ccl not supported yet";
751 else if (!strcmp(type, "z39.58"))
754 *addinfo = "z39.58 not supported yet";
756 else if (!strcmp(type, "cql"))
759 *addinfo = "cql not supported yet";
764 *addinfo = "unsupported query type";
770 *addinfo = "missing query element";
774 void yaz_xml2query(const void *xmlnodep, Z_Query **query, ODR odr,
775 int *error_code, const char **addinfo)
777 yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
786 * indent-tabs-mode: nil
788 * vim: shiftwidth=4 tabstop=8 expandtab