2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tstxmlquery.c,v 1.14 2007-01-03 08:42:16 adam Exp $
11 #include <yaz/wrbuf.h>
12 #include <yaz/querytowrbuf.h>
13 #include <yaz/xmlquery.h>
14 #include <yaz/pquery.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
30 enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml,
31 const char *expect_pqf)
33 YAZ_PQF_Parser parser = yaz_pqf_create();
34 ODR odr = odr_createmem(ODR_ENCODE);
36 enum pqf2xml_status status = XML_NO_ERROR;
42 rpn = yaz_pqf_parse(parser, odr, pqf);
44 yaz_pqf_destroy(parser);
53 yaz_rpnquery2xml(rpn, &doc);
56 status = QUERY2XML_FAILED;
62 xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
64 if (len_out == strlen(expect_xml)
65 && memcmp(buf_out, expect_xml, len_out) == 0)
69 const char *addinfo = 0;
70 const xmlNode *root_element = xmlDocGetRootElement(doc);
71 ODR odr2 = odr_createmem(ODR_ENCODE);
73 yaz_xml2query(root_element, &query2, odr2,
74 &error_code, &addinfo);
75 if (error_code || !query2)
76 status = XML_NO_MATCH;
79 WRBUF w = wrbuf_alloc();
80 yaz_query_to_wrbuf(w, query2);
81 if (!expect_pqf || strcmp(expect_pqf, wrbuf_buf(w)) == 0)
85 status = XML_NO_MATCH;
86 printf("Result: %s\n", wrbuf_buf(w));
94 printf("%.*s\n", len_out, buf_out);
95 status = XML_NO_MATCH;
100 status = QUERY2XML_FAILED;
107 static void tst(void)
109 YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED);
111 YAZ_CHECK_EQ(pqf2xml_text(
112 "@attr 1=4 computer",
113 "<?xml version=\"1.0\"?>\n"
114 "<query><rpn set=\"Bib-1\">"
115 "<apt><attr type=\"1\" value=\"4\"/>"
116 "<term type=\"general\">computer</term></apt>"
118 "RPN @attrset Bib-1 @attr 1=4 computer"
121 YAZ_CHECK_EQ(pqf2xml_text(
122 "@attr 2=1 @attr 1=title computer",
123 "<?xml version=\"1.0\"?>\n"
124 "<query><rpn set=\"Bib-1\">"
125 "<apt><attr type=\"1\" value=\"title\"/>"
126 "<attr type=\"2\" value=\"1\"/>"
127 "<term type=\"general\">computer</term></apt>"
129 "RPN @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer"
132 YAZ_CHECK_EQ(pqf2xml_text(
133 "@attr 2=1 @attr exp1 1=1 computer",
134 "<?xml version=\"1.0\"?>\n"
135 "<query><rpn set=\"Bib-1\">"
136 "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
137 "<attr type=\"2\" value=\"1\"/>"
138 "<term type=\"general\">computer</term></apt>"
140 "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
143 YAZ_CHECK_EQ(pqf2xml_text(
145 "<?xml version=\"1.0\"?>\n"
146 "<query><rpn set=\"Bib-1\">"
147 "<operator type=\"and\">"
148 "<apt><term type=\"general\">a</term></apt>"
149 "<apt><term type=\"general\">b</term></apt>"
150 "</operator></rpn></query>\n",
151 "RPN @attrset Bib-1 @and a b"
154 YAZ_CHECK_EQ(pqf2xml_text(
156 "<?xml version=\"1.0\"?>\n"
157 "<query><rpn set=\"Bib-1\">"
158 "<operator type=\"or\">"
159 "<operator type=\"and\">"
160 "<apt><term type=\"general\">a</term></apt>"
161 "<apt><term type=\"general\">b</term></apt></operator>"
162 "<apt><term type=\"general\">c</term></apt>"
163 "</operator></rpn></query>\n",
164 "RPN @attrset Bib-1 @or @and a b c"
167 YAZ_CHECK_EQ(pqf2xml_text(
169 "<?xml version=\"1.0\"?>\n"
170 "<query><rpn set=\"Bib-1\">"
171 "<rset>abe</rset></rpn></query>\n",
172 "RPN @attrset Bib-1 @set abe"
175 YAZ_CHECK_EQ(pqf2xml_text(
176 /* exclusion, distance, ordered, relationtype,
177 knownunit, proxunit */
178 "@prox 0 3 1 2 k 2 a b",
179 "<?xml version=\"1.0\"?>\n"
180 "<query><rpn set=\"Bib-1\">"
181 "<operator type=\"prox\" exclusion=\"false\" "
184 "relationType=\"2\" "
185 "knownProximityUnit=\"2\">"
186 "<apt><term type=\"general\">a</term></apt>"
187 "<apt><term type=\"general\">b</term></apt>"
188 "</operator></rpn></query>\n",
189 "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
192 YAZ_CHECK_EQ(pqf2xml_text(
194 "<?xml version=\"1.0\"?>\n"
195 "<query><rpn set=\"Bib-1\">"
197 "<term type=\"numeric\">32</term></apt>"
199 "RPN @attrset Bib-1 @term numeric 32"
202 YAZ_CHECK_EQ(pqf2xml_text(
203 "@term string computer",
204 "<?xml version=\"1.0\"?>\n"
205 "<query><rpn set=\"Bib-1\">"
207 "<term type=\"string\">computer</term></apt>"
209 "RPN @attrset Bib-1 @term string computer"
212 YAZ_CHECK_EQ(pqf2xml_text(
214 "<?xml version=\"1.0\"?>\n"
215 "<query><rpn set=\"Bib-1\">"
217 "<term type=\"null\"/></apt>"
219 "RPN @attrset Bib-1 @term null x"
222 YAZ_CHECK_EQ(pqf2xml_text(
223 "@attrset gils @attr 4=2 x",
224 "<?xml version=\"1.0\"?>\n"
225 "<query><rpn set=\"GILS\">"
227 "<attr type=\"4\" value=\"2\"/>"
228 "<term type=\"general\">x</term></apt>"
230 "RPN @attrset GILS @attr 4=2 x"
235 int main (int argc, char **argv)
237 YAZ_CHECK_INIT(argc, argv);
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab