1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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;
99 status = QUERY2XML_FAILED;
106 static void tst(void)
108 YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED);
110 YAZ_CHECK_EQ(pqf2xml_text(
111 "@attr 1=4 computer",
112 "<?xml version=\"1.0\"?>\n"
113 "<query><rpn set=\"Bib-1\">"
114 "<apt><attr type=\"1\" value=\"4\"/>"
115 "<term type=\"general\">computer</term></apt>"
117 "RPN @attrset Bib-1 @attr 1=4 computer"
120 YAZ_CHECK_EQ(pqf2xml_text(
121 "@attr 2=1 @attr 1=title computer",
122 "<?xml version=\"1.0\"?>\n"
123 "<query><rpn set=\"Bib-1\">"
124 "<apt><attr type=\"1\" value=\"title\"/>"
125 "<attr type=\"2\" value=\"1\"/>"
126 "<term type=\"general\">computer</term></apt>"
128 "RPN @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer"
131 YAZ_CHECK_EQ(pqf2xml_text(
132 "@attr 2=1 @attr exp1 1=1 computer",
133 "<?xml version=\"1.0\"?>\n"
134 "<query><rpn set=\"Bib-1\">"
135 "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
136 "<attr type=\"2\" value=\"1\"/>"
137 "<term type=\"general\">computer</term></apt>"
139 "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
142 YAZ_CHECK_EQ(pqf2xml_text(
144 "<?xml version=\"1.0\"?>\n"
145 "<query><rpn set=\"Bib-1\">"
146 "<operator type=\"and\">"
147 "<apt><term type=\"general\">a</term></apt>"
148 "<apt><term type=\"general\">b</term></apt>"
149 "</operator></rpn></query>\n",
150 "RPN @attrset Bib-1 @and a b"
153 YAZ_CHECK_EQ(pqf2xml_text(
155 "<?xml version=\"1.0\"?>\n"
156 "<query><rpn set=\"Bib-1\">"
157 "<operator type=\"or\">"
158 "<operator type=\"and\">"
159 "<apt><term type=\"general\">a</term></apt>"
160 "<apt><term type=\"general\">b</term></apt></operator>"
161 "<apt><term type=\"general\">c</term></apt>"
162 "</operator></rpn></query>\n",
163 "RPN @attrset Bib-1 @or @and a b c"
166 YAZ_CHECK_EQ(pqf2xml_text(
168 "<?xml version=\"1.0\"?>\n"
169 "<query><rpn set=\"Bib-1\">"
170 "<rset>abe</rset></rpn></query>\n",
171 "RPN @attrset Bib-1 @set abe"
174 YAZ_CHECK_EQ(pqf2xml_text(
175 /* exclusion, distance, ordered, relationtype,
176 knownunit, proxunit */
177 "@prox 0 3 1 2 k 2 a b",
178 "<?xml version=\"1.0\"?>\n"
179 "<query><rpn set=\"Bib-1\">"
180 "<operator type=\"prox\" exclusion=\"false\" "
183 "relationType=\"2\" "
184 "knownProximityUnit=\"2\">"
185 "<apt><term type=\"general\">a</term></apt>"
186 "<apt><term type=\"general\">b</term></apt>"
187 "</operator></rpn></query>\n",
188 "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
191 YAZ_CHECK_EQ(pqf2xml_text(
193 "<?xml version=\"1.0\"?>\n"
194 "<query><rpn set=\"Bib-1\">"
196 "<term type=\"numeric\">32</term></apt>"
198 "RPN @attrset Bib-1 @term numeric 32"
201 YAZ_CHECK_EQ(pqf2xml_text(
202 "@term string computer",
203 "<?xml version=\"1.0\"?>\n"
204 "<query><rpn set=\"Bib-1\">"
206 "<term type=\"string\">computer</term></apt>"
208 "RPN @attrset Bib-1 @term string computer"
211 YAZ_CHECK_EQ(pqf2xml_text(
213 "<?xml version=\"1.0\"?>\n"
214 "<query><rpn set=\"Bib-1\">"
216 "<term type=\"null\"/></apt>"
218 "RPN @attrset Bib-1 @term null x"
221 YAZ_CHECK_EQ(pqf2xml_text(
222 "@attrset gils @attr 4=2 x",
223 "<?xml version=\"1.0\"?>\n"
224 "<query><rpn set=\"GILS\">"
226 "<attr type=\"4\" value=\"2\"/>"
227 "<term type=\"general\">x</term></apt>"
229 "RPN @attrset GILS @attr 4=2 x"
234 int main (int argc, char **argv)
236 YAZ_CHECK_INIT(argc, argv);
244 * c-file-style: "Stroustrup"
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab