2 * Copyright (C) 1995-2007, Index Data ApS
5 * $Id: xmlquery.c,v 1.13 2007-04-12 13:52:57 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>
22 #include <yaz/nmem_xml.h>
24 void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
28 const char *setname = 0;
29 char oid_name_str[OID_STR_MAX];
31 if (element->attributeSet)
33 setname = yaz_oid_to_string_buf(element->attributeSet,
37 if (element->which == Z_AttributeValue_numeric)
39 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
42 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
44 sprintf(formstr, "%d", *element->attributeType);
45 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
47 sprintf(formstr, "%d", *element->value.numeric);
48 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
50 else if (element->which == Z_AttributeValue_complex)
53 for (i = 0; i<element->value.complex->num_list; i++)
55 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
58 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
60 sprintf(formstr, "%d", *element->attributeType);
61 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
63 if (element->value.complex->list[i]->which ==
64 Z_StringOrNumeric_string)
66 xmlNewProp(node, BAD_CAST "value", BAD_CAST
67 element->value.complex->list[i]->u.string);
69 else if (element->value.complex->list[i]->which ==
70 Z_StringOrNumeric_numeric)
72 sprintf(formstr, "%d",
73 *element->value.complex->list[i]->u.numeric);
74 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
81 xmlNodePtr yaz_query2xml_term(const Z_Term *term,
85 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
93 t = xmlNewTextLen(BAD_CAST term->u.general->buf, term->u.general->len);
97 sprintf(formstr, "%d", *term->u.numeric);
98 t = xmlNewText(BAD_CAST formstr);
100 case Z_Term_characterString:
102 t = xmlNewText(BAD_CAST term->u.characterString);
107 case Z_Term_dateTime:
110 case Z_Term_external:
113 case Z_Term_integerAndUnit:
114 type ="integerAndUnit";
122 if (t) /* got a term node ? */
123 xmlAddChild(node, t);
125 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
129 xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
132 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
133 int num_attributes = zapt->attributes->num_attributes;
135 for (i = 0; i<num_attributes; i++)
136 yaz_query2xml_attribute_element(zapt->attributes->attributes[i], node);
137 yaz_query2xml_term(zapt->term, node);
143 void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
145 const char *type = 0;
154 case Z_Operator_and_not:
157 case Z_Operator_prox:
163 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
165 if (op->which == Z_Operator_prox)
169 if (op->u.prox->exclusion)
171 if (*op->u.prox->exclusion)
172 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "true");
174 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "false");
176 sprintf(formstr, "%d", *op->u.prox->distance);
177 xmlNewProp(node, BAD_CAST "distance", BAD_CAST formstr);
179 if (*op->u.prox->ordered)
180 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
182 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
184 sprintf(formstr, "%d", *op->u.prox->relationType);
185 xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
187 switch(op->u.prox->which)
189 case Z_ProximityOperator_known:
190 sprintf(formstr, "%d", *op->u.prox->u.known);
191 xmlNewProp(node, BAD_CAST "knownProximityUnit",
194 case Z_ProximityOperator_private:
196 xmlNewProp(node, BAD_CAST "privateProximityUnit",
203 xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
206 if (zs->which == Z_RPNStructure_complex)
208 Z_Complex *zc = zs->u.complex;
210 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
212 yaz_query2xml_operator(zc->roperator, node);
213 yaz_query2xml_rpnstructure(zc->s1, node);
214 yaz_query2xml_rpnstructure(zc->s2, node);
217 else if (zs->which == Z_RPNStructure_simple)
219 if (zs->u.simple->which == Z_Operand_APT)
220 return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
222 else if (zs->u.simple->which == Z_Operand_resultSetId)
223 return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset",
224 BAD_CAST zs->u.simple->u.resultSetId);
229 xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
231 if (rpn->attributeSetId)
233 char oid_name_str[OID_STR_MAX];
234 const char *setname = yaz_oid_to_string_buf(rpn->attributeSetId,
237 xmlNewProp(parent, BAD_CAST "set", BAD_CAST setname);
239 return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
242 xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
247 xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
252 xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
257 void yaz_rpnquery2xml(const Z_RPNQuery *rpn, xmlDocPtr *docp)
261 query.which = Z_Query_type_1;
262 query.u.type_1 = (Z_RPNQuery *) rpn;
263 yaz_query2xml(&query, docp);
266 void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
268 xmlNodePtr top_node, q_node = 0, child_node = 0;
273 top_node = xmlNewNode(0, BAD_CAST "query");
278 case Z_Query_type_101:
279 q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
280 child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
283 q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
284 child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
286 case Z_Query_type_100:
287 q_node = xmlNewChild(top_node, 0, BAD_CAST "z39.58", 0);
288 child_node = yaz_query2xml_z3958(q->u.type_100, q_node);
290 case Z_Query_type_104:
291 if (q->u.type_104->which == Z_External_CQL)
293 q_node = xmlNewChild(top_node, 0, BAD_CAST "cql", 0);
294 child_node = yaz_query2xml_cql(q->u.type_104->u.cql, q_node);
297 if (child_node && q_node)
299 *docp = xmlNewDoc(BAD_CAST "1.0");
300 xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
305 xmlFreeNode(top_node);
309 bool_t *boolVal(ODR odr, const char *str)
311 if (*str == '\0' || strchr("0fF", *str))
312 return odr_intdup(odr, 0);
313 return odr_intdup(odr, 1);
316 int *intVal(ODR odr, const char *str)
318 return odr_intdup(odr, atoi(str));
321 void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
322 ODR odr, int *error_code, const char **addinfo)
324 const char *type = (const char *)
325 xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
329 *addinfo = "no operator type";
332 *op = (Z_Operator*) odr_malloc(odr, sizeof(Z_Operator));
333 if (!strcmp(type, "and"))
335 (*op)->which = Z_Operator_and;
336 (*op)->u.op_and = odr_nullval();
338 else if (!strcmp(type, "or"))
340 (*op)->which = Z_Operator_or;
341 (*op)->u.op_or = odr_nullval();
343 else if (!strcmp(type, "not"))
345 (*op)->which = Z_Operator_and_not;
346 (*op)->u.and_not = odr_nullval();
348 else if (!strcmp(type, "prox"))
351 Z_ProximityOperator *pop = (Z_ProximityOperator *)
352 odr_malloc(odr, sizeof(Z_ProximityOperator));
354 (*op)->which = Z_Operator_prox;
357 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
358 BAD_CAST "exclusion");
360 pop->exclusion = boolVal(odr, atval);
364 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
365 BAD_CAST "distance");
367 pop->distance = intVal(odr, atval);
369 pop->distance = odr_intdup(odr, 1);
371 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
374 pop->ordered = boolVal(odr, atval);
376 pop->ordered = odr_intdup(odr, 1);
378 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
379 BAD_CAST "relationType");
381 pop->relationType = intVal(odr, atval);
384 odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
386 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
387 BAD_CAST "knownProximityUnit");
390 pop->which = Z_ProximityOperator_known;
391 pop->u.known = intVal(odr, atval);
395 pop->which = Z_ProximityOperator_known;
396 pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
399 atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
400 BAD_CAST "privateProximityUnit");
403 pop->which = Z_ProximityOperator_private;
404 pop->u.zprivate = intVal(odr, atval);
410 *addinfo = "bad operator type";
414 void yaz_xml2query_attribute_element(const xmlNode *ptr,
415 Z_AttributeElement **elem, ODR odr,
416 int *error_code, const char **addinfo)
423 struct _xmlAttr *attr;
424 for (attr = ptr->properties; attr; attr = attr->next)
426 if (!xmlStrcmp(attr->name, BAD_CAST "set") &&
427 attr->children && attr->children->type == XML_TEXT_NODE)
428 set = attr->children->content;
429 else if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
430 attr->children && attr->children->type == XML_TEXT_NODE)
431 type = attr->children->content;
432 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
433 attr->children && attr->children->type == XML_TEXT_NODE)
435 value = attr->children->content;
441 *addinfo = "bad attribute for attr content";
448 *addinfo = "missing type attribute for att content";
454 *addinfo = "missing value attribute for att content";
458 *elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(**elem));
460 (*elem)->attributeSet = yaz_string_to_oid_odr(yaz_oid_std(),
465 (*elem)->attributeSet = 0;
466 (*elem)->attributeType = intVal(odr, (const char *) type);
468 /* looks like a number ? */
469 for (i = 0; value[i] && value[i] >= '0' && value[i] <= '9'; i++)
471 if (num_values > 1 || value[i])
472 { /* multiple values or string, so turn to complex attribute */
473 (*elem)->which = Z_AttributeValue_complex;
474 (*elem)->value.complex =
475 (Z_ComplexAttribute*) odr_malloc(odr, sizeof(Z_ComplexAttribute));
476 (*elem)->value.complex->num_list = num_values;
477 (*elem)->value.complex->list = (Z_StringOrNumeric **)
478 odr_malloc(odr, sizeof(Z_StringOrNumeric*) * num_values);
480 /* second pass over attr values */
482 for (attr = ptr->properties; attr; attr = attr->next)
484 if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
485 attr->children && attr->children->type == XML_TEXT_NODE)
487 const char *val = (const char *) attr->children->content;
488 assert (i < num_values);
489 (*elem)->value.complex->list[i] = (Z_StringOrNumeric *)
490 odr_malloc(odr, sizeof(Z_StringOrNumeric));
491 (*elem)->value.complex->list[i]->which =
492 Z_StringOrNumeric_string;
493 (*elem)->value.complex->list[i]->u.string =
494 odr_strdup(odr, val);
498 (*elem)->value.complex->num_semanticAction = 0;
499 (*elem)->value.complex->semanticAction = 0;
502 { /* good'ld numeric value */
503 (*elem)->which = Z_AttributeValue_numeric;
504 (*elem)->value.numeric = intVal(odr, (const char *) value);
508 char *strVal(const xmlNode *ptr_cdata, ODR odr)
510 return nmem_text_node_cdata(ptr_cdata, odr_getmem(odr));
513 void yaz_xml2query_term(const xmlNode *ptr,
514 Z_Term **term, ODR odr,
515 int *error_code, const char **addinfo)
518 struct _xmlAttr *attr;
519 char *cdata = strVal(ptr->children, odr);
521 for (attr = ptr->properties; attr; attr = attr->next)
523 if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
524 attr->children && attr->children->type == XML_TEXT_NODE)
525 type = attr->children->content;
529 *addinfo = "bad attribute for attr content";
533 *term = (Z_Term *) odr_malloc(odr, sizeof(Z_Term));
535 if (!type || !xmlStrcmp(type, BAD_CAST "general"))
537 (*term)->which = Z_Term_general;
539 odr_create_Odr_oct(odr, (unsigned char *)cdata, strlen(cdata));
541 else if (!xmlStrcmp(type, BAD_CAST "numeric"))
543 (*term)->which = Z_Term_numeric;
544 (*term)->u.numeric = intVal(odr, cdata);
546 else if (!xmlStrcmp(type, BAD_CAST "string"))
548 (*term)->which = Z_Term_characterString;
549 (*term)->u.characterString = cdata;
551 else if (!xmlStrcmp(type, BAD_CAST "oid"))
554 *addinfo = "unhandled term type: oid";
556 else if (!xmlStrcmp(type, BAD_CAST "dateTime"))
559 *addinfo = "unhandled term type: dateTime";
561 else if (!xmlStrcmp(type, BAD_CAST "integerAndUnit"))
564 *addinfo = "unhandled term type: integerAndUnit";
566 else if (!xmlStrcmp(type, BAD_CAST "null"))
568 (*term)->which = Z_Term_null;
569 (*term)->u.null = odr_nullval();
574 *addinfo = "unhandled term type";
578 void yaz_xml2query_apt(const xmlNode *ptr_apt,
579 Z_AttributesPlusTerm **zapt, ODR odr,
580 int *error_code, const char **addinfo)
582 const xmlNode *ptr = ptr_apt->children;
585 *zapt = (Z_AttributesPlusTerm *)
586 odr_malloc(odr, sizeof(Z_AttributesPlusTerm));
588 /* deal with attributes */
589 (*zapt)->attributes = (Z_AttributeList*)
590 odr_malloc(odr, sizeof(Z_AttributeList));
592 /* how many attributes? */
593 for (; ptr; ptr = ptr->next)
594 if (ptr->type == XML_ELEMENT_NODE)
596 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
602 /* allocate and parse for real */
603 (*zapt)->attributes->num_attributes = num_attr;
604 (*zapt)->attributes->attributes = (Z_AttributeElement **)
605 odr_malloc(odr, sizeof(Z_AttributeElement*) * num_attr);
608 ptr = ptr_apt->children;
609 for (; ptr; ptr = ptr->next)
610 if (ptr->type == XML_ELEMENT_NODE)
612 if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
614 yaz_xml2query_attribute_element(
615 ptr, &(*zapt)->attributes->attributes[i], odr,
616 error_code, addinfo);
622 if (ptr && ptr->type == XML_ELEMENT_NODE)
624 if (!xmlStrcmp(ptr->name, BAD_CAST "term"))
627 yaz_xml2query_term(ptr, &(*zapt)->term, odr, error_code, addinfo);
632 *addinfo = "bad element in apt content";
638 *addinfo = "missing term node in apt content";
642 void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
643 ODR odr, int *error_code, const char **addinfo)
647 *rset = strVal(ptr->children, odr);
652 *addinfo = "missing rset content";
656 void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
657 ODR odr, int *error_code, const char **addinfo)
659 while (ptr && ptr->type != XML_ELEMENT_NODE)
662 if (!ptr || ptr->type != XML_ELEMENT_NODE)
665 *addinfo = "missing rpn operator, rset, apt node";
668 *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
669 if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
671 Z_Complex *zc = odr_malloc(odr, sizeof(Z_Complex));
673 (*zs)->which = Z_RPNStructure_complex;
674 (*zs)->u.complex = zc;
676 yaz_xml2query_operator(ptr, &zc->roperator, odr, error_code, addinfo);
679 while (ptr && ptr->type != XML_ELEMENT_NODE)
681 yaz_xml2query_rpnstructure(ptr, &zc->s1, odr, error_code, addinfo);
684 while (ptr && ptr->type != XML_ELEMENT_NODE)
686 yaz_xml2query_rpnstructure(ptr, &zc->s2, odr, error_code, addinfo);
690 Z_Operand *s = (Z_Operand *) odr_malloc(odr, sizeof(Z_Operand));
691 (*zs)->which = Z_RPNStructure_simple;
693 if (!xmlStrcmp(ptr->name, BAD_CAST "apt"))
695 s->which = Z_Operand_APT;
696 yaz_xml2query_apt(ptr, &s->u.attributesPlusTerm,
697 odr, error_code, addinfo);
699 else if (!xmlStrcmp(ptr->name, BAD_CAST "rset"))
701 s->which = Z_Operand_resultSetId;
702 yaz_xml2query_rset(ptr, &s->u.resultSetId,
703 odr, error_code, addinfo);
708 *addinfo = "bad element: expected binary, apt or rset";
713 void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
714 int *error_code, const char **addinfo)
716 const char *set = (const char *)
717 xmlGetProp((xmlNodePtr) ptr, BAD_CAST "set");
719 *query = (Z_RPNQuery*) odr_malloc(odr, sizeof(Z_RPNQuery));
721 (*query)->attributeSetId = yaz_string_to_oid_odr(yaz_oid_std(),
722 CLASS_ATTSET, set, odr);
724 (*query)->attributeSetId = 0;
725 yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,
726 odr, error_code, addinfo);
729 static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
730 int *error_code, const char **addinfo)
732 if (ptr && ptr->type == XML_ELEMENT_NODE &&
733 !xmlStrcmp(ptr->name, BAD_CAST "query"))
737 while (ptr && ptr->type != XML_ELEMENT_NODE)
739 if (!ptr || ptr->type != XML_ELEMENT_NODE)
742 *addinfo = "missing query content";
745 type = (const char *) ptr->name;
747 *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
748 if (!type || !strcmp(type, "rpn"))
750 (*query)->which = Z_Query_type_1;
751 yaz_xml2query_rpn(ptr, &(*query)->u.type_1, odr,
752 error_code, addinfo);
754 else if (!strcmp(type, "ccl"))
757 *addinfo = "ccl not supported yet";
759 else if (!strcmp(type, "z39.58"))
762 *addinfo = "z39.58 not supported yet";
764 else if (!strcmp(type, "cql"))
767 *addinfo = "cql not supported yet";
772 *addinfo = "unsupported query type";
778 *addinfo = "missing query element";
782 void yaz_xml2query(const void *xmlnodep, Z_Query **query, ODR odr,
783 int *error_code, const char **addinfo)
785 yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
794 * indent-tabs-mode: nil
796 * vim: shiftwidth=4 tabstop=8 expandtab