Towards 2.1.40.
[yaz-moved-to-github.git] / src / xmlquery.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * All rights reserved.
4  *
5  * $Id: xmlquery.c,v 1.11 2006-10-27 12:19:15 adam Exp $
6  */
7
8 /** \file xmlquery.c
9     \brief Query / XML conversions
10 */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.h>
15
16 #if YAZ_HAVE_XML2
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19
20 #include <yaz/logrpn.h>
21 #include <yaz/xmlquery.h>
22 #include <yaz/nmem_xml.h>
23
24 void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
25                                      xmlNodePtr parent)
26 {
27     char formstr[30];
28     const char *setname = 0;
29     
30     if (element->attributeSet)
31     {
32         oident *attrset;
33         attrset = oid_getentbyoid (element->attributeSet);
34         setname = attrset->desc;
35     }
36
37     if (element->which == Z_AttributeValue_numeric)
38     {
39         xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
40
41         if (setname)
42             xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
43
44         sprintf(formstr, "%d", *element->attributeType);
45         xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
46
47         sprintf(formstr, "%d", *element->value.numeric);
48         xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
49     }
50     else if (element->which == Z_AttributeValue_complex)
51     {
52         int i;
53         for (i = 0; i<element->value.complex->num_list; i++)
54         {
55             xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
56             
57             if (setname)
58                 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
59             
60             sprintf(formstr, "%d", *element->attributeType);
61             xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
62             
63             if (element->value.complex->list[i]->which ==
64                 Z_StringOrNumeric_string)
65             {
66                 xmlNewProp(node, BAD_CAST "value", BAD_CAST 
67                            element->value.complex->list[i]->u.string);
68             }
69             else if (element->value.complex->list[i]->which ==
70                      Z_StringOrNumeric_numeric)
71             {
72                 sprintf(formstr, "%d",
73                         *element->value.complex->list[i]->u.numeric);
74                 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
75             }
76         }
77     }
78 }
79
80
81 xmlNodePtr yaz_query2xml_term(const Z_Term *term,
82                               xmlNodePtr parent)
83 {
84     xmlNodePtr t = 0;
85     xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
86     char formstr[20];
87     const char *type = 0;
88
89     switch (term->which)
90     {
91     case Z_Term_general:
92         type = "general";
93         t = xmlNewTextLen(BAD_CAST term->u.general->buf, term->u.general->len);
94         break;
95     case Z_Term_numeric:
96         type = "numeric";
97         sprintf(formstr, "%d", *term->u.numeric);
98         t = xmlNewText(BAD_CAST formstr);       
99         break;
100     case Z_Term_characterString:
101         type = "string";
102         t = xmlNewText(BAD_CAST term->u.characterString);
103         break;
104     case Z_Term_oid:
105         type = "oid";
106         break;
107     case Z_Term_dateTime:
108         type = "dateTime";
109         break;
110     case Z_Term_external:
111         type = "external";
112         break;
113     case Z_Term_integerAndUnit:
114         type ="integerAndUnit";
115         break;
116     case Z_Term_null:
117         type = "null";
118         break;
119     default:
120         break;
121     }
122     if (t) /* got a term node ? */
123         xmlAddChild(node, t);
124     if (type)
125         xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
126     return node;
127 }
128
129 xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
130                              xmlNodePtr parent)
131 {
132     xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
133     int num_attributes = zapt->attributes->num_attributes;
134     int i;
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);
138
139     return node;
140 }
141
142
143 void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
144 {
145     const char *type = 0;
146     switch(op->which)
147     {
148     case Z_Operator_and:
149         type = "and";
150         break;
151     case Z_Operator_or:
152         type = "or";
153         break;
154     case Z_Operator_and_not:
155         type = "not";
156         break;
157     case Z_Operator_prox:
158         type = "prox";
159         break;
160     default:
161         return;
162     }
163     xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
164     
165     if (op->which == Z_Operator_prox)
166     {
167         char formstr[30];
168         
169         if (op->u.prox->exclusion)
170         {
171             if (*op->u.prox->exclusion)
172                 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "true");
173             else
174                 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "false");
175         }
176         sprintf(formstr, "%d", *op->u.prox->distance);
177         xmlNewProp(node, BAD_CAST "distance", BAD_CAST formstr);
178
179         if (*op->u.prox->ordered)
180             xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
181         else 
182             xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
183        
184         sprintf(formstr, "%d", *op->u.prox->relationType);
185         xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
186         
187         switch(op->u.prox->which)
188         {
189         case Z_ProximityOperator_known:
190             sprintf(formstr, "%d", *op->u.prox->u.known);
191             xmlNewProp(node, BAD_CAST "knownProximityUnit",
192                        BAD_CAST formstr);
193             break;
194         case Z_ProximityOperator_private:
195         default:
196             xmlNewProp(node, BAD_CAST "privateProximityUnit",
197                        BAD_CAST "private");
198             break;
199         }
200     }
201 }
202
203 xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
204                                       xmlNodePtr parent)
205 {
206     if (zs->which == Z_RPNStructure_complex)
207     {
208         Z_Complex *zc = zs->u.complex;
209
210         xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
211         if (zc->roperator)
212             yaz_query2xml_operator(zc->roperator, node);
213         yaz_query2xml_rpnstructure(zc->s1, node);
214         yaz_query2xml_rpnstructure(zc->s2, node);
215         return node;
216     }
217     else if (zs->which == Z_RPNStructure_simple)
218     {
219         if (zs->u.simple->which == Z_Operand_APT)
220             return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
221                                      parent);
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);
225     }
226     return 0;
227 }
228
229 xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
230 {
231     oident *attrset = oid_getentbyoid (rpn->attributeSetId);
232     if (attrset && attrset->value)
233         xmlNewProp(parent, BAD_CAST "set", BAD_CAST attrset->desc);
234     return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
235 }
236
237 xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
238 {
239     return 0;
240 }
241
242 xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
243 {
244     return 0;
245 }
246
247 xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
248 {
249     return 0;
250 }
251
252 void yaz_rpnquery2xml(const Z_RPNQuery *rpn, xmlDocPtr *docp)
253 {
254     Z_Query query;
255
256     query.which = Z_Query_type_1;
257     query.u.type_1 = (Z_RPNQuery *) rpn;
258     yaz_query2xml(&query, docp);
259 }
260
261 void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
262 {
263     xmlNodePtr top_node, q_node = 0, child_node = 0;
264
265     assert(q);
266     assert(docp);
267
268     top_node = xmlNewNode(0, BAD_CAST "query");
269
270     switch (q->which)
271     {
272     case Z_Query_type_1: 
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);
276         break;
277     case Z_Query_type_2:
278         q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
279         child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
280         break;
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);
284         break;
285     case Z_Query_type_104:
286         if (q->u.type_104->which == Z_External_CQL)
287         {
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);
290         }
291     }
292     if (child_node && q_node)
293     {
294         *docp = xmlNewDoc(BAD_CAST "1.0");
295         xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
296     }
297     else
298     {
299         *docp = 0;
300         xmlFreeNode(top_node);
301     }
302 }
303
304 bool_t *boolVal(ODR odr, const char *str)
305 {
306     if (*str == '\0' || strchr("0fF", *str))
307         return odr_intdup(odr, 0);
308     return odr_intdup(odr, 1);
309 }
310
311 int *intVal(ODR odr, const char *str)
312 {
313     return odr_intdup(odr, atoi(str));
314 }
315
316 void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
317                             ODR odr, int *error_code, const char **addinfo)
318 {
319     const char *type = (const char *)
320         xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
321     if (!type)
322     {
323         *error_code = 1;
324         *addinfo = "no operator type";
325         return;
326     }
327     *op = (Z_Operator*) odr_malloc(odr, sizeof(Z_Operator));
328     if (!strcmp(type, "and"))
329     {
330         (*op)->which = Z_Operator_and;
331         (*op)->u.op_and = odr_nullval();
332     }
333     else if (!strcmp(type, "or"))
334     {
335         (*op)->which = Z_Operator_or;
336         (*op)->u.op_or = odr_nullval();
337     }
338     else if (!strcmp(type, "not"))
339     {
340         (*op)->which = Z_Operator_and_not;
341         (*op)->u.and_not = odr_nullval();
342     }
343     else if (!strcmp(type, "prox"))
344     {
345         const char *atval;
346         Z_ProximityOperator *pop = (Z_ProximityOperator *) 
347             odr_malloc(odr, sizeof(Z_ProximityOperator));
348
349         (*op)->which = Z_Operator_prox;
350         (*op)->u.prox = pop;
351
352         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
353                                           BAD_CAST "exclusion");
354         if (atval)
355             pop->exclusion = boolVal(odr, atval);
356         else
357             pop->exclusion = 0;
358
359         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
360                                           BAD_CAST "distance");
361         if (atval)
362             pop->distance = intVal(odr, atval);
363         else
364             pop->distance = odr_intdup(odr, 1);
365
366         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
367                                           BAD_CAST "ordered");
368         if (atval)
369             pop->ordered = boolVal(odr, atval);
370         else
371             pop->ordered = odr_intdup(odr, 1);
372
373         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
374                                           BAD_CAST "relationType");
375         if (atval)
376             pop->relationType = intVal(odr, atval);
377         else
378             pop->relationType =
379                 odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
380
381         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
382                                           BAD_CAST "knownProximityUnit");
383         if (atval)
384         {
385             pop->which = Z_ProximityOperator_known;            
386             pop->u.known = intVal(odr, atval);
387         }
388         else
389         {
390             pop->which = Z_ProximityOperator_known;
391             pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
392         }
393
394         atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
395                                           BAD_CAST "privateProximityUnit");
396         if (atval)
397         {
398             pop->which = Z_ProximityOperator_private;
399             pop->u.zprivate = intVal(odr, atval);
400         }
401     }
402     else
403     {
404         *error_code = 1;
405         *addinfo = "bad operator type";
406     }
407 }
408
409 void yaz_xml2query_attribute_element(const xmlNode *ptr, 
410                                      Z_AttributeElement **elem, ODR odr,
411                                      int *error_code, const char **addinfo)
412 {
413     int i;
414     xmlChar *set = 0;
415     xmlChar *type = 0;
416     xmlChar *value = 0;
417     int num_values = 0;
418     struct _xmlAttr *attr;
419     for (attr = ptr->properties; attr; attr = attr->next)
420     {
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)
429         {
430             value = attr->children->content;
431             num_values++;
432         }
433         else
434         {
435             *error_code = 1;
436             *addinfo = "bad attribute for attr content";
437             return;
438         }
439     }
440     if (!type)
441     {
442         *error_code = 1;
443         *addinfo = "missing type attribute for att content";
444         return;
445     }
446     if (!value)
447     {
448         *error_code = 1;
449         *addinfo = "missing value attribute for att content";
450         return;
451     }
452         
453     *elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(**elem));
454     if (set)
455         (*elem)->attributeSet = yaz_str_to_z3950oid(odr, CLASS_ATTSET,
456                                                     (const char *)set);
457     else
458         (*elem)->attributeSet = 0;
459     (*elem)->attributeType = intVal(odr, (const char *) type);
460
461     /* looks like a number ? */
462     for (i = 0; value[i] && value[i] >= '0' && value[i] <= '9'; i++)
463         ;
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);
472
473         /* second pass over attr values */
474         i = 0;
475         for (attr = ptr->properties; attr; attr = attr->next)
476         {
477             if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
478                 attr->children && attr->children->type == XML_TEXT_NODE)
479             {
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);
488                 i++;
489             }
490         }
491         (*elem)->value.complex->num_semanticAction = 0;
492         (*elem)->value.complex->semanticAction = 0;        
493     }
494     else
495     {   /* good'ld numeric value */
496         (*elem)->which = Z_AttributeValue_numeric;
497         (*elem)->value.numeric = intVal(odr, (const char *) value);
498     }
499 }
500
501 char *strVal(const xmlNode *ptr_cdata, ODR odr)
502 {
503     return nmem_text_node_cdata(ptr_cdata, odr->mem);
504 }
505
506 void yaz_xml2query_term(const xmlNode *ptr,
507                        Z_Term **term, ODR odr,
508                        int *error_code, const char **addinfo)
509 {
510     xmlChar *type = 0;
511     struct _xmlAttr *attr;
512     char *cdata = strVal(ptr->children, odr);
513
514     for (attr = ptr->properties; attr; attr = attr->next)
515     {
516         if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
517             attr->children && attr->children->type == XML_TEXT_NODE)
518             type = attr->children->content;
519         else
520         {
521             *error_code = 1;
522             *addinfo = "bad attribute for attr content";
523             return;
524         }
525     }
526     *term = (Z_Term *) odr_malloc(odr, sizeof(Z_Term));
527
528     if (!type || !xmlStrcmp(type, BAD_CAST "general"))
529     {
530         (*term)->which = Z_Term_general;
531         (*term)->u.general =
532             odr_create_Odr_oct(odr, (unsigned char *)cdata, strlen(cdata));
533     }
534     else if (!xmlStrcmp(type, BAD_CAST "numeric"))
535     {
536         (*term)->which = Z_Term_numeric;
537         (*term)->u.numeric = intVal(odr, cdata);
538     }
539     else if (!xmlStrcmp(type, BAD_CAST "string"))
540     {
541         (*term)->which = Z_Term_characterString;
542         (*term)->u.characterString = cdata;
543     }
544     else if (!xmlStrcmp(type, BAD_CAST "oid"))
545     {
546         *error_code = 1;
547         *addinfo = "unhandled term type: oid";
548     }
549     else if (!xmlStrcmp(type, BAD_CAST "dateTime"))
550     {
551         *error_code = 1;
552         *addinfo = "unhandled term type: dateTime";
553     }
554     else if (!xmlStrcmp(type, BAD_CAST "integerAndUnit"))
555     {
556         *error_code = 1;
557         *addinfo = "unhandled term type: integerAndUnit";
558     }
559     else if (!xmlStrcmp(type, BAD_CAST "null"))
560     {
561         (*term)->which = Z_Term_null;
562         (*term)->u.null = odr_nullval();
563     }
564     else
565     {
566         *error_code = 1;
567         *addinfo = "unhandled term type";
568     }
569 }
570
571 void yaz_xml2query_apt(const xmlNode *ptr_apt,
572                        Z_AttributesPlusTerm **zapt, ODR odr,
573                        int *error_code, const char **addinfo)
574 {
575     const xmlNode *ptr = ptr_apt->children;
576     int i, num_attr = 0;
577
578     *zapt = (Z_AttributesPlusTerm *)
579         odr_malloc(odr, sizeof(Z_AttributesPlusTerm));
580
581     /* deal with attributes */
582     (*zapt)->attributes = (Z_AttributeList*)
583         odr_malloc(odr, sizeof(Z_AttributeList));
584
585     /* how many attributes? */
586     for (; ptr; ptr = ptr->next)
587         if (ptr->type == XML_ELEMENT_NODE)
588         {
589             if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
590                 num_attr++;
591             else
592                 break;
593         }
594
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);
599
600     i = 0;    
601     ptr = ptr_apt->children;
602     for (; ptr; ptr = ptr->next)
603         if (ptr->type == XML_ELEMENT_NODE)
604         {
605             if (!xmlStrcmp(ptr->name, BAD_CAST "attr"))
606             {
607                 yaz_xml2query_attribute_element(
608                     ptr,  &(*zapt)->attributes->attributes[i], odr,
609                     error_code, addinfo);
610                 i++;
611             }
612             else
613                 break;
614         }
615     if (ptr && ptr->type == XML_ELEMENT_NODE)
616     {
617         if (!xmlStrcmp(ptr->name, BAD_CAST "term"))
618         {        
619             /* deal with term */
620             yaz_xml2query_term(ptr, &(*zapt)->term, odr, error_code, addinfo);
621         }
622         else
623         {
624             *error_code = 1;
625             *addinfo = "bad element in apt content";
626         }
627     }
628     else
629     {
630         *error_code = 1;
631         *addinfo = "missing term node in apt content";
632     }
633 }
634
635 void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
636                         ODR odr, int *error_code, const char **addinfo)
637 {
638     if (ptr->children)
639     {
640         *rset = strVal(ptr->children, odr);
641     }
642     else
643     {
644         *error_code = 1;
645         *addinfo = "missing rset content";
646     }
647 }
648
649 void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
650                                 ODR odr, int *error_code, const char **addinfo)
651 {
652     while (ptr && ptr->type != XML_ELEMENT_NODE)
653         ptr = ptr->next;
654     
655     if (!ptr || ptr->type != XML_ELEMENT_NODE)
656     {
657         *error_code = 1;
658         *addinfo = "missing rpn operator, rset, apt node";
659         return;
660     }
661     *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
662     if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
663     {
664         Z_Complex *zc = odr_malloc(odr, sizeof(Z_Complex));
665         
666         (*zs)->which = Z_RPNStructure_complex;
667         (*zs)->u.complex = zc;
668         
669         yaz_xml2query_operator(ptr, &zc->roperator, odr, error_code, addinfo);
670
671         ptr = ptr->children;
672         while (ptr && ptr->type != XML_ELEMENT_NODE)
673             ptr = ptr->next;
674         yaz_xml2query_rpnstructure(ptr, &zc->s1, odr, error_code, addinfo);
675         if (ptr)
676             ptr = ptr->next;
677         while (ptr && ptr->type != XML_ELEMENT_NODE)
678             ptr = ptr->next;
679         yaz_xml2query_rpnstructure(ptr, &zc->s2, odr, error_code, addinfo);
680     }
681     else 
682     {
683         Z_Operand *s = (Z_Operand *) odr_malloc(odr, sizeof(Z_Operand));
684         (*zs)->which = Z_RPNStructure_simple;
685         (*zs)->u.simple = s;
686         if (!xmlStrcmp(ptr->name, BAD_CAST "apt"))
687         {
688             s->which = Z_Operand_APT;
689             yaz_xml2query_apt(ptr, &s->u.attributesPlusTerm,
690                               odr, error_code, addinfo);
691         }
692         else if (!xmlStrcmp(ptr->name, BAD_CAST "rset"))
693         {
694             s->which = Z_Operand_resultSetId; 
695             yaz_xml2query_rset(ptr, &s->u.resultSetId,
696                                odr, error_code, addinfo);
697         }
698         else
699         {
700             *error_code = 1;
701             *addinfo = "bad element: expected binary, apt or rset";
702         }        
703     }
704 }
705
706 void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
707                    int *error_code, const char **addinfo)
708 {
709     const char *set = (const char *)
710         xmlGetProp((xmlNodePtr) ptr, BAD_CAST "set");
711
712     *query = (Z_RPNQuery*) odr_malloc(odr, sizeof(Z_RPNQuery));
713     if (set)
714         (*query)->attributeSetId = yaz_str_to_z3950oid(odr, CLASS_ATTSET, set);
715     else
716         (*query)->attributeSetId = 0;
717     yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,
718                                odr, error_code, addinfo);
719 }
720
721 static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
722                            int *error_code, const char **addinfo)
723 {
724     if (ptr && ptr->type == XML_ELEMENT_NODE && 
725         !xmlStrcmp(ptr->name, BAD_CAST "query"))
726     {
727         const char *type;
728         ptr = ptr->children;
729         while (ptr && ptr->type != XML_ELEMENT_NODE)
730             ptr = ptr->next;
731         if (!ptr || ptr->type != XML_ELEMENT_NODE)
732         {
733             *error_code = 1;
734             *addinfo = "missing query content";
735             return;
736         }
737         type = (const char *) ptr->name;
738
739         *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
740         if (!type || !strcmp(type, "rpn"))
741         {
742             (*query)->which = Z_Query_type_1;
743             yaz_xml2query_rpn(ptr, &(*query)->u.type_1, odr,
744                               error_code, addinfo);
745         }
746         else if (!strcmp(type, "ccl"))
747         {
748             *error_code = 1;
749             *addinfo = "ccl not supported yet";
750         }
751         else if (!strcmp(type, "z39.58"))
752         {
753             *error_code = 1;
754             *addinfo = "z39.58 not supported yet";
755         }
756         else if (!strcmp(type, "cql"))
757         {
758             *error_code = 1;
759             *addinfo = "cql not supported yet";
760         }
761         else
762         {
763             *error_code = 1;
764             *addinfo = "unsupported query type";
765         }
766     }
767     else
768     {
769         *error_code = 1;
770         *addinfo = "missing query element";
771     }
772 }
773
774 void yaz_xml2query(const void *xmlnodep, Z_Query **query, ODR odr,
775                    int *error_code, const char **addinfo)
776 {
777     yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
778 }
779
780 /* YAZ_HAVE_XML2 */
781 #endif
782
783 /*
784  * Local variables:
785  * c-basic-offset: 4
786  * indent-tabs-mode: nil
787  * End:
788  * vim: shiftwidth=4 tabstop=8 expandtab
789  */