1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
12 #include <yaz/wrbuf.h>
13 #include <yaz/querytowrbuf.h>
14 #include <yaz/xmlquery.h>
15 #include <yaz/pquery.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
31 enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml,
32 const char *expect_pqf)
34 YAZ_PQF_Parser parser = yaz_pqf_create();
35 ODR odr = odr_createmem(ODR_ENCODE);
37 enum pqf2xml_status status = XML_NO_ERROR;
43 rpn = yaz_pqf_parse(parser, odr, pqf);
45 yaz_pqf_destroy(parser);
54 yaz_rpnquery2xml(rpn, &doc);
57 status = QUERY2XML_FAILED;
63 xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
65 if (len_out == (int) strlen(expect_xml)
66 && memcmp(buf_out, expect_xml, len_out) == 0)
70 const char *addinfo = 0;
71 const xmlNode *root_element = xmlDocGetRootElement(doc);
72 ODR odr2 = odr_createmem(ODR_ENCODE);
74 yaz_xml2query(root_element, &query2, odr2,
75 &error_code, &addinfo);
76 if (error_code || !query2)
77 status = XML_NO_MATCH;
80 WRBUF w = wrbuf_alloc();
81 yaz_query_to_wrbuf(w, query2);
82 if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
86 status = XML_NO_MATCH;
87 printf("Result: %s\n", wrbuf_cstr(w));
95 printf("%.*s\n", len_out, buf_out);
96 status = XML_NO_MATCH;
102 status = QUERY2XML_FAILED;
109 static void tst(void)
111 YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED);
113 YAZ_CHECK_EQ(pqf2xml_text(
114 "@attr 1=4 computer",
115 "<?xml version=\"1.0\"?>\n"
116 "<query><rpn set=\"Bib-1\">"
117 "<apt><attr type=\"1\" value=\"4\"/>"
118 "<term type=\"general\">computer</term></apt>"
120 "RPN @attrset Bib-1 @attr 1=4 computer"
123 YAZ_CHECK_EQ(pqf2xml_text(
124 "@attr 2=1 @attr 1=title computer",
125 "<?xml version=\"1.0\"?>\n"
126 "<query><rpn set=\"Bib-1\">"
127 "<apt><attr type=\"1\" value=\"title\"/>"
128 "<attr type=\"2\" value=\"1\"/>"
129 "<term type=\"general\">computer</term></apt>"
131 "RPN @attrset Bib-1 @attr 1=title @attr 2=1 computer"
134 YAZ_CHECK_EQ(pqf2xml_text(
135 "@attr 2=1 @attr exp1 1=1 computer",
136 "<?xml version=\"1.0\"?>\n"
137 "<query><rpn set=\"Bib-1\">"
138 "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
139 "<attr type=\"2\" value=\"1\"/>"
140 "<term type=\"general\">computer</term></apt>"
142 "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
145 YAZ_CHECK_EQ(pqf2xml_text(
147 "<?xml version=\"1.0\"?>\n"
148 "<query><rpn set=\"Bib-1\">"
149 "<operator type=\"and\">"
150 "<apt><term type=\"general\">a</term></apt>"
151 "<apt><term type=\"general\">b</term></apt>"
152 "</operator></rpn></query>\n",
153 "RPN @attrset Bib-1 @and a b"
156 YAZ_CHECK_EQ(pqf2xml_text(
158 "<?xml version=\"1.0\"?>\n"
159 "<query><rpn set=\"Bib-1\">"
160 "<operator type=\"or\">"
161 "<operator type=\"and\">"
162 "<apt><term type=\"general\">a</term></apt>"
163 "<apt><term type=\"general\">b</term></apt></operator>"
164 "<apt><term type=\"general\">c</term></apt>"
165 "</operator></rpn></query>\n",
166 "RPN @attrset Bib-1 @or @and a b c"
169 YAZ_CHECK_EQ(pqf2xml_text(
171 "<?xml version=\"1.0\"?>\n"
172 "<query><rpn set=\"Bib-1\">"
173 "<rset>abe</rset></rpn></query>\n",
174 "RPN @attrset Bib-1 @set abe"
177 YAZ_CHECK_EQ(pqf2xml_text(
178 /* exclusion, distance, ordered, relationtype,
179 knownunit, proxunit */
180 "@prox 0 3 1 2 k 2 a b",
181 "<?xml version=\"1.0\"?>\n"
182 "<query><rpn set=\"Bib-1\">"
183 "<operator type=\"prox\" exclusion=\"false\" "
186 "relationType=\"2\" "
187 "knownProximityUnit=\"2\">"
188 "<apt><term type=\"general\">a</term></apt>"
189 "<apt><term type=\"general\">b</term></apt>"
190 "</operator></rpn></query>\n",
191 "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
194 YAZ_CHECK_EQ(pqf2xml_text(
196 "<?xml version=\"1.0\"?>\n"
197 "<query><rpn set=\"Bib-1\">"
199 "<term type=\"numeric\">32</term></apt>"
201 "RPN @attrset Bib-1 @term numeric 32"
204 YAZ_CHECK_EQ(pqf2xml_text(
205 "@term string computer",
206 "<?xml version=\"1.0\"?>\n"
207 "<query><rpn set=\"Bib-1\">"
209 "<term type=\"string\">computer</term></apt>"
211 "RPN @attrset Bib-1 @term string computer"
214 YAZ_CHECK_EQ(pqf2xml_text(
216 "<?xml version=\"1.0\"?>\n"
217 "<query><rpn set=\"Bib-1\">"
219 "<term type=\"null\"/></apt>"
221 "RPN @attrset Bib-1 @term null x"
224 YAZ_CHECK_EQ(pqf2xml_text(
225 "@attrset gils @attr 4=2 x",
226 "<?xml version=\"1.0\"?>\n"
227 "<query><rpn set=\"GILS\">"
229 "<attr type=\"4\" value=\"2\"/>"
230 "<term type=\"general\">x</term></apt>"
232 "RPN @attrset GILS @attr 4=2 x"
237 int main (int argc, char **argv)
239 YAZ_CHECK_INIT(argc, argv);
247 * c-file-style: "Stroustrup"
248 * indent-tabs-mode: nil
250 * vim: shiftwidth=4 tabstop=8 expandtab