2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaz-xmlquery.c,v 1.2 2006-03-06 08:11:41 adam Exp $
11 #include <yaz/options.h>
12 #include <yaz/querytowrbuf.h>
13 #include <yaz/xmlquery.h>
14 #include <yaz/pquery.h>
18 #include <libxml/parser.h>
21 static char *prog = "yaz-xmlquery";
24 void pqftoxmlquery(const char *pqf)
26 YAZ_PQF_Parser parser = yaz_pqf_create();
27 ODR odr = odr_createmem(ODR_ENCODE);
32 fprintf(stderr, "%s: cannot create parser\n", prog);
38 fprintf(stderr, "%s: cannot create parser\n", prog);
42 rpn = yaz_pqf_parse(parser, odr, pqf);
44 yaz_pqf_destroy(parser);
48 fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
55 yaz_rpnquery2xml(rpn, &doc);
59 fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
68 xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
70 if (!len_out || !buf_out)
72 fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
77 fwrite(buf_out, len_out, 1, stdout);
85 void xmlquerytopqf(const char *xmlstr)
89 doc = xmlParseMemory(xmlstr, strlen(xmlstr));
92 fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
98 const char *addinfo = 0;
100 ODR odr = odr_createmem(ODR_ENCODE);
102 const xmlNode *root_element = xmlDocGetRootElement(doc);
103 yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
106 fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
107 prog, error_code, addinfo);
112 fprintf(stderr, "%s: yaz_xml2query no query result\n",
118 WRBUF w = wrbuf_alloc();
119 yaz_query_to_wrbuf(w, query);
120 printf("%s\n", wrbuf_buf(w));
128 void xmlfiletopqf(const char *xmlfile)
132 FILE *f = fopen(xmlfile, "rb");
135 fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
138 fseek(f, 0, SEEK_END);
140 if (sz <= 0 || sz >= 1<<18)
142 fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
146 xmlstr = xmalloc(sz+1);
148 fread(xmlstr, sz, 1, f);
151 xmlquerytopqf(xmlstr);
158 fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
159 fprintf(stderr, " -p pqf reads pqf. write xml to stdout\n");
160 fprintf(stderr, " -x xmlfile reads XML from file. write pqf to stdout\n");
164 int main (int argc, char **argv)
171 while ((r = options("-p:x:", argv, argc, &arg)) != -2)
189 fprintf(stderr, "%s: nothing to do\n", prog);
193 fprintf(stderr, "%s: XML support not enabled.\n", prog);