1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
10 #include <yaz/querytowrbuf.h>
11 #include <yaz/xmlquery.h>
12 #include <yaz/pquery.h>
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
28 enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml,
29 const char *expect_pqf)
31 YAZ_PQF_Parser parser = yaz_pqf_create();
32 ODR odr = odr_createmem(ODR_ENCODE);
34 enum pqf2xml_status status = XML_NO_ERROR;
40 rpn = yaz_pqf_parse(parser, odr, pqf);
42 yaz_pqf_destroy(parser);
51 yaz_rpnquery2xml(rpn, &doc);
54 status = QUERY2XML_FAILED;
60 xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
62 if (len_out == strlen(expect_xml)
63 && memcmp(buf_out, expect_xml, len_out) == 0)
67 const char *addinfo = 0;
68 const xmlNode *root_element = xmlDocGetRootElement(doc);
69 ODR odr2 = odr_createmem(ODR_ENCODE);
71 yaz_xml2query(root_element, &query2, odr2,
72 &error_code, &addinfo);
73 if (error_code || !query2)
74 status = XML_NO_MATCH;
77 WRBUF w = wrbuf_alloc();
78 yaz_query_to_wrbuf(w, query2);
79 if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
83 status = XML_NO_MATCH;
84 printf("Result: %s\n", wrbuf_cstr(w));
92 printf("%.*s\n", len_out, buf_out);
93 status = XML_NO_MATCH;
98 status = QUERY2XML_FAILED;
105 static void tst(void)
107 YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED);
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>"
116 "RPN @attrset Bib-1 @attr 1=4 computer"
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>"
127 "RPN @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer"
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>"
138 "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
141 YAZ_CHECK_EQ(pqf2xml_text(
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"
152 YAZ_CHECK_EQ(pqf2xml_text(
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"
165 YAZ_CHECK_EQ(pqf2xml_text(
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"
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\" "
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"
190 YAZ_CHECK_EQ(pqf2xml_text(
192 "<?xml version=\"1.0\"?>\n"
193 "<query><rpn set=\"Bib-1\">"
195 "<term type=\"numeric\">32</term></apt>"
197 "RPN @attrset Bib-1 @term numeric 32"
200 YAZ_CHECK_EQ(pqf2xml_text(
201 "@term string computer",
202 "<?xml version=\"1.0\"?>\n"
203 "<query><rpn set=\"Bib-1\">"
205 "<term type=\"string\">computer</term></apt>"
207 "RPN @attrset Bib-1 @term string computer"
210 YAZ_CHECK_EQ(pqf2xml_text(
212 "<?xml version=\"1.0\"?>\n"
213 "<query><rpn set=\"Bib-1\">"
215 "<term type=\"null\"/></apt>"
217 "RPN @attrset Bib-1 @term null x"
220 YAZ_CHECK_EQ(pqf2xml_text(
221 "@attrset gils @attr 4=2 x",
222 "<?xml version=\"1.0\"?>\n"
223 "<query><rpn set=\"GILS\">"
225 "<attr type=\"4\" value=\"2\"/>"
226 "<term type=\"general\">x</term></apt>"
228 "RPN @attrset GILS @attr 4=2 x"
233 int main (int argc, char **argv)
235 YAZ_CHECK_INIT(argc, argv);
243 * indent-tabs-mode: nil
245 * vim: shiftwidth=4 tabstop=8 expandtab