Update source headers for 2008. Omit CVS ID keyword subst.
[yaz-moved-to-github.git] / test / tstxmlquery.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/wrbuf.h>
10 #include <yaz/querytowrbuf.h>
11 #include <yaz/xmlquery.h>
12 #include <yaz/pquery.h>
13 #include <yaz/test.h>
14
15 #if YAZ_HAVE_XML2
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
18 #endif
19
20 enum pqf2xml_status {
21     PQF_FAILED,
22     QUERY2XML_FAILED,
23     XML_NO_MATCH,
24     XML_MATCH,
25     XML_NO_ERROR
26 };
27
28 enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml,
29                                  const char *expect_pqf)
30 {
31     YAZ_PQF_Parser parser = yaz_pqf_create();
32     ODR odr = odr_createmem(ODR_ENCODE);
33     Z_RPNQuery *rpn;
34     enum pqf2xml_status status = XML_NO_ERROR;
35
36     YAZ_CHECK(parser);
37
38     YAZ_CHECK(odr);
39
40     rpn = yaz_pqf_parse(parser, odr, pqf);
41
42     yaz_pqf_destroy(parser);
43
44     if (!rpn)
45         status = PQF_FAILED;
46     else
47     {
48 #if YAZ_HAVE_XML2
49         xmlDocPtr doc = 0;
50
51         yaz_rpnquery2xml(rpn, &doc);
52         
53         if (!doc)
54             status = QUERY2XML_FAILED;
55         else
56         {
57             char *buf_out;
58             int len_out;
59
60             xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
61             
62             if (len_out == strlen(expect_xml)
63                 && memcmp(buf_out, expect_xml, len_out) == 0)
64             {
65                 Z_Query *query2 = 0;
66                 int error_code = 0;
67                 const char *addinfo = 0;
68                 const xmlNode *root_element = xmlDocGetRootElement(doc);
69                 ODR odr2 = odr_createmem(ODR_ENCODE);
70                 
71                 yaz_xml2query(root_element, &query2, odr2,
72                               &error_code, &addinfo);
73                 if (error_code || !query2)
74                     status = XML_NO_MATCH;
75                 else
76                 {
77                     WRBUF w = wrbuf_alloc();
78                     yaz_query_to_wrbuf(w, query2);
79                     if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
80                         status = XML_MATCH;
81                     else
82                     {
83                         status = XML_NO_MATCH;
84                         printf("Result: %s\n", wrbuf_cstr(w));
85                     }
86                     wrbuf_destroy(w);
87                 }
88                 odr_destroy(odr2);
89             }
90             else
91             {
92                 printf("%.*s\n", len_out, buf_out);
93                 status = XML_NO_MATCH;
94             }
95             xmlFreeDoc(doc);
96         }
97 #else
98         status = QUERY2XML_FAILED;
99 #endif
100     }
101     odr_destroy(odr);
102     return status;
103 }
104
105 static void tst(void)
106 {
107     YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED);
108 #if YAZ_HAVE_XML2
109     YAZ_CHECK_EQ(pqf2xml_text(
110                      "@attr 1=4 computer", 
111                      "<?xml version=\"1.0\"?>\n"
112                      "<query><rpn set=\"Bib-1\">"
113                      "<apt><attr type=\"1\" value=\"4\"/>"
114                      "<term type=\"general\">computer</term></apt>"
115                      "</rpn></query>\n",
116                      "RPN @attrset Bib-1 @attr 1=4 computer"
117                      ), XML_MATCH);
118     
119     YAZ_CHECK_EQ(pqf2xml_text(
120                      "@attr 2=1 @attr 1=title computer",
121                      "<?xml version=\"1.0\"?>\n"
122                      "<query><rpn set=\"Bib-1\">"
123                      "<apt><attr type=\"1\" value=\"title\"/>"
124                      "<attr type=\"2\" value=\"1\"/>"
125                      "<term type=\"general\">computer</term></apt>"
126                      "</rpn></query>\n",
127                      "RPN @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer"
128                      ), XML_MATCH);
129
130     YAZ_CHECK_EQ(pqf2xml_text(
131                      "@attr 2=1 @attr exp1 1=1 computer",
132                      "<?xml version=\"1.0\"?>\n"
133                      "<query><rpn set=\"Bib-1\">"
134                      "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
135                      "<attr type=\"2\" value=\"1\"/>"
136                      "<term type=\"general\">computer</term></apt>"
137                      "</rpn></query>\n",
138                      "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
139                      ), XML_MATCH);
140     
141     YAZ_CHECK_EQ(pqf2xml_text(
142                      "@and a b", 
143                      "<?xml version=\"1.0\"?>\n"
144                      "<query><rpn set=\"Bib-1\">"
145                      "<operator type=\"and\">"
146                      "<apt><term type=\"general\">a</term></apt>"
147                      "<apt><term type=\"general\">b</term></apt>"
148                      "</operator></rpn></query>\n",
149                      "RPN @attrset Bib-1 @and a b"
150                      ), XML_MATCH);
151     
152     YAZ_CHECK_EQ(pqf2xml_text(
153                      "@or @and a b c", 
154                      "<?xml version=\"1.0\"?>\n"
155                      "<query><rpn set=\"Bib-1\">"
156                      "<operator type=\"or\">"
157                      "<operator type=\"and\">"
158                      "<apt><term type=\"general\">a</term></apt>"
159                      "<apt><term type=\"general\">b</term></apt></operator>"
160                      "<apt><term type=\"general\">c</term></apt>"
161                      "</operator></rpn></query>\n",
162                      "RPN @attrset Bib-1 @or @and a b c"
163                      ), XML_MATCH);
164
165     YAZ_CHECK_EQ(pqf2xml_text(
166                      "@set abe", 
167                      "<?xml version=\"1.0\"?>\n"
168                      "<query><rpn set=\"Bib-1\">"
169                      "<rset>abe</rset></rpn></query>\n",
170                      "RPN @attrset Bib-1 @set abe"
171                      ), XML_MATCH);
172
173     YAZ_CHECK_EQ(pqf2xml_text(
174                      /* exclusion, distance, ordered, relationtype, 
175                         knownunit, proxunit */
176                      "@prox 0 3 1 2 k 2           a b", 
177                      "<?xml version=\"1.0\"?>\n"
178                      "<query><rpn set=\"Bib-1\">"
179                      "<operator type=\"prox\" exclusion=\"false\" "
180                      "distance=\"3\" "
181                      "ordered=\"true\" "
182                      "relationType=\"2\" "
183                      "knownProximityUnit=\"2\">"
184                      "<apt><term type=\"general\">a</term></apt>"
185                      "<apt><term type=\"general\">b</term></apt>"
186                      "</operator></rpn></query>\n",
187                      "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
188                      ), XML_MATCH);
189
190     YAZ_CHECK_EQ(pqf2xml_text(
191                      "@term numeric 32", 
192                      "<?xml version=\"1.0\"?>\n"
193                      "<query><rpn set=\"Bib-1\">"
194                      "<apt>"
195                      "<term type=\"numeric\">32</term></apt>"
196                      "</rpn></query>\n",
197                      "RPN @attrset Bib-1 @term numeric 32"
198                      ), XML_MATCH);
199     
200     YAZ_CHECK_EQ(pqf2xml_text(
201                      "@term string computer", 
202                      "<?xml version=\"1.0\"?>\n"
203                      "<query><rpn set=\"Bib-1\">"
204                      "<apt>"
205                      "<term type=\"string\">computer</term></apt>"
206                      "</rpn></query>\n",
207                      "RPN @attrset Bib-1 @term string computer"
208                      ), XML_MATCH);
209     
210     YAZ_CHECK_EQ(pqf2xml_text(
211                      "@term null void", 
212                      "<?xml version=\"1.0\"?>\n"
213                      "<query><rpn set=\"Bib-1\">"
214                      "<apt>"
215                      "<term type=\"null\"/></apt>"
216                      "</rpn></query>\n",
217                      "RPN @attrset Bib-1 @term null x"
218                      ), XML_MATCH);
219
220     YAZ_CHECK_EQ(pqf2xml_text(
221                      "@attrset gils @attr 4=2 x", 
222                      "<?xml version=\"1.0\"?>\n"
223                      "<query><rpn set=\"GILS\">"
224                      "<apt>"
225                      "<attr type=\"4\" value=\"2\"/>"
226                      "<term type=\"general\">x</term></apt>"
227                      "</rpn></query>\n",
228                      "RPN @attrset GILS @attr 4=2 x"
229                      ), XML_MATCH);
230 #endif
231 }
232
233 int main (int argc, char **argv)
234 {
235     YAZ_CHECK_INIT(argc, argv);
236     tst();
237     YAZ_CHECK_TERM;
238 }
239
240 /*
241  * Local variables:
242  * c-basic-offset: 4
243  * indent-tabs-mode: nil
244  * End:
245  * vim: shiftwidth=4 tabstop=8 expandtab
246  */
247