1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
12 #include <yaz/options.h>
13 #include <yaz/querytowrbuf.h>
14 #include <yaz/xmlquery.h>
15 #include <yaz/pquery.h>
19 #include <libxml/parser.h>
22 static char *prog = "yaz-xmlquery";
25 void pqftoxmlquery(const char *pqf)
27 YAZ_PQF_Parser parser = yaz_pqf_create();
28 ODR odr = odr_createmem(ODR_ENCODE);
33 fprintf(stderr, "%s: cannot create parser\n", prog);
39 fprintf(stderr, "%s: cannot create parser\n", prog);
43 rpn = yaz_pqf_parse(parser, odr, pqf);
45 yaz_pqf_destroy(parser);
49 fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
56 yaz_rpnquery2xml(rpn, &doc);
60 fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
69 xmlDocDumpMemory(doc, &buf_out, &len_out);
71 if (!len_out || !buf_out)
73 fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
79 if (fwrite(buf_out, len_out, 1, stdout) != 1)
81 fprintf(stderr, "%s: write failed\n", prog);
92 void xmlquerytopqf(const char *xmlstr)
96 doc = xmlParseMemory(xmlstr, strlen(xmlstr));
99 fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
105 const char *addinfo = 0;
107 ODR odr = odr_createmem(ODR_ENCODE);
109 const xmlNode *root_element = xmlDocGetRootElement(doc);
110 yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
113 fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
114 prog, error_code, addinfo);
119 fprintf(stderr, "%s: yaz_xml2query no query result\n",
125 WRBUF w = wrbuf_alloc();
126 yaz_query_to_wrbuf(w, query);
127 printf("%s\n", wrbuf_cstr(w));
135 void xmlfiletopqf(const char *xmlfile)
139 FILE *f = fopen(xmlfile, "rb");
142 fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
145 fseek(f, 0, SEEK_END);
147 if (sz <= 0 || sz >= 1<<18)
149 fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
153 xmlstr = (char *) xmalloc(sz+1);
155 if (fread(xmlstr, sz, 1, f) != 1)
157 fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
162 fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
166 xmlquerytopqf(xmlstr);
173 fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
174 fprintf(stderr, " -p pqf reads pqf. write xml to stdout\n");
175 fprintf(stderr, " -x xmlfile reads XML from file. write pqf to stdout\n");
179 int main (int argc, char **argv)
186 while ((r = options("-p:x:", argv, argc, &arg)) != -2)
204 fprintf(stderr, "%s: nothing to do\n", prog);
208 fprintf(stderr, "%s: XML support not enabled.\n", prog);