1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
9 #include <yaz/options.h>
10 #include <yaz/querytowrbuf.h>
11 #include <yaz/xmlquery.h>
12 #include <yaz/pquery.h>
16 #include <libxml/parser.h>
19 static char *prog = "yaz-xmlquery";
22 void pqftoxmlquery(const char *pqf)
24 YAZ_PQF_Parser parser = yaz_pqf_create();
25 ODR odr = odr_createmem(ODR_ENCODE);
30 fprintf(stderr, "%s: cannot create parser\n", prog);
36 fprintf(stderr, "%s: cannot create parser\n", prog);
40 rpn = yaz_pqf_parse(parser, odr, pqf);
42 yaz_pqf_destroy(parser);
46 fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
53 yaz_rpnquery2xml(rpn, &doc);
57 fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
66 xmlDocDumpMemory(doc, &buf_out, &len_out);
68 if (!len_out || !buf_out)
70 fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
76 if (fwrite(buf_out, len_out, 1, stdout) != 1)
78 fprintf(stderr, "%s: write failed\n", prog);
89 void xmlquerytopqf(const char *xmlstr)
93 doc = xmlParseMemory(xmlstr, strlen(xmlstr));
96 fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
102 const char *addinfo = 0;
104 ODR odr = odr_createmem(ODR_ENCODE);
106 const xmlNode *root_element = xmlDocGetRootElement(doc);
107 yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
110 fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
111 prog, error_code, addinfo);
116 fprintf(stderr, "%s: yaz_xml2query no query result\n",
122 WRBUF w = wrbuf_alloc();
123 yaz_query_to_wrbuf(w, query);
124 printf("%s\n", wrbuf_cstr(w));
132 void xmlfiletopqf(const char *xmlfile)
136 FILE *f = fopen(xmlfile, "rb");
139 fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
142 fseek(f, 0, SEEK_END);
144 if (sz <= 0 || sz >= 1<<18)
146 fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
150 xmlstr = (char *) xmalloc(sz+1);
152 if (fread(xmlstr, sz, 1, f) != 1)
154 fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
159 fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
163 xmlquerytopqf(xmlstr);
170 fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
171 fprintf(stderr, " -p pqf reads pqf. write xml to stdout\n");
172 fprintf(stderr, " -x xmlfile reads XML from file. write pqf to stdout\n");
176 int main (int argc, char **argv)
183 while ((r = options("-p:x:", argv, argc, &arg)) != -2)
201 fprintf(stderr, "%s: nothing to do\n", prog);
205 fprintf(stderr, "%s: XML support not enabled.\n", prog);