1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 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",
75 fwrite(buf_out, len_out, 1, stdout);
83 void xmlquerytopqf(const char *xmlstr)
87 doc = xmlParseMemory(xmlstr, strlen(xmlstr));
90 fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
96 const char *addinfo = 0;
98 ODR odr = odr_createmem(ODR_ENCODE);
100 const xmlNode *root_element = xmlDocGetRootElement(doc);
101 yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
104 fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
105 prog, error_code, addinfo);
110 fprintf(stderr, "%s: yaz_xml2query no query result\n",
116 WRBUF w = wrbuf_alloc();
117 yaz_query_to_wrbuf(w, query);
118 printf("%s\n", wrbuf_cstr(w));
126 void xmlfiletopqf(const char *xmlfile)
130 FILE *f = fopen(xmlfile, "rb");
133 fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
136 fseek(f, 0, SEEK_END);
138 if (sz <= 0 || sz >= 1<<18)
140 fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
144 xmlstr = (char *) xmalloc(sz+1);
146 fread(xmlstr, sz, 1, f);
149 xmlquerytopqf(xmlstr);
156 fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
157 fprintf(stderr, " -p pqf reads pqf. write xml to stdout\n");
158 fprintf(stderr, " -x xmlfile reads XML from file. write pqf to stdout\n");
162 int main (int argc, char **argv)
169 while ((r = options("-p:x:", argv, argc, &arg)) != -2)
187 fprintf(stderr, "%s: nothing to do\n", prog);
191 fprintf(stderr, "%s: XML support not enabled.\n", prog);