Update headers and omit CVS Ids.
[yaz-moved-to-github.git] / util / yaz-xmlquery.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/options.h>
10 #include <yaz/querytowrbuf.h>
11 #include <yaz/xmlquery.h>
12 #include <yaz/pquery.h>
13 #include <yaz/test.h>
14
15 #if YAZ_HAVE_XML2
16 #include <libxml/parser.h>
17 #endif
18
19 static char *prog = "yaz-xmlquery";
20
21 #if YAZ_HAVE_XML2
22 void pqftoxmlquery(const char *pqf)
23 {
24     YAZ_PQF_Parser parser = yaz_pqf_create();
25     ODR odr = odr_createmem(ODR_ENCODE);
26     Z_RPNQuery *rpn;
27
28     if (!parser)
29     {
30         fprintf(stderr, "%s: cannot create parser\n", prog);
31         exit(1);
32     }
33
34     if (!odr)
35     {
36         fprintf(stderr, "%s: cannot create parser\n", prog);
37         exit(1);
38     }
39
40     rpn = yaz_pqf_parse(parser, odr, pqf);
41
42     yaz_pqf_destroy(parser);
43
44     if (!rpn)
45     {
46         fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
47         exit(2);
48     }
49     else
50     {
51         xmlDocPtr doc = 0;
52         
53         yaz_rpnquery2xml(rpn, &doc);
54         
55         if (!doc)
56         {
57             fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
58                     prog, pqf);
59             exit(3);
60         }
61         else
62         {
63             xmlChar *buf_out = 0;
64             int len_out = 0;
65
66             xmlDocDumpMemory(doc, &buf_out, &len_out);
67
68             if (!len_out || !buf_out)
69             {
70                 fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
71                         prog, pqf);
72                 exit(4);
73             }
74             else
75                 fwrite(buf_out, len_out, 1, stdout);
76             xmlFreeDoc(doc);
77         }
78     }    
79     odr_destroy(odr);
80 }
81
82
83 void xmlquerytopqf(const char *xmlstr)
84 {
85     xmlDocPtr doc;
86
87     doc = xmlParseMemory(xmlstr, strlen(xmlstr));
88     if (!doc)
89     {
90         fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
91         exit(1);
92     }
93     else
94     {
95         int error_code = 0;
96         const char *addinfo = 0;
97         Z_Query *query = 0;
98         ODR odr = odr_createmem(ODR_ENCODE);
99
100         const xmlNode *root_element = xmlDocGetRootElement(doc);
101         yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
102         if (error_code)
103         {
104             fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
105                     prog, error_code, addinfo);
106             exit(1);
107         }
108         else if (!query)
109         {
110             fprintf(stderr, "%s: yaz_xml2query no query result\n",
111                     prog);
112             exit(1);
113         }
114         else
115         {
116             WRBUF w = wrbuf_alloc();
117             yaz_query_to_wrbuf(w, query);
118             printf("%s\n", wrbuf_cstr(w));
119             wrbuf_destroy(w);
120         }
121         odr_destroy(odr);
122         xmlFreeDoc(doc);
123     }
124 }
125
126 void xmlfiletopqf(const char *xmlfile)
127 {
128     long sz;
129     char *xmlstr;
130     FILE *f = fopen(xmlfile, "rb");
131     if (!f)
132     {
133         fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
134         exit(1);
135     }
136     fseek(f, 0, SEEK_END);
137     sz = ftell(f);
138     if (sz <= 0 || sz >= 1<<18)
139     {
140         fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
141         exit(1);
142     }
143     rewind(f);
144     xmlstr = (char *) xmalloc(sz+1);
145     xmlstr[sz] = '\0';
146     fread(xmlstr, sz, 1, f);
147     fclose(f);
148     
149     xmlquerytopqf(xmlstr);
150     xfree(xmlstr);
151 }
152 #endif
153
154 void usage(void)
155 {
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");
159     exit(1);
160 }
161
162 int main (int argc, char **argv)
163 {
164 #if YAZ_HAVE_XML2
165     char *arg;
166     int r;
167     int active = 0;
168
169     while ((r = options("-p:x:", argv, argc, &arg)) != -2)
170     {
171         switch(r)
172         {
173         case 'p':
174             pqftoxmlquery(arg);
175             active = 1;
176             break;
177         case 'x':
178             xmlfiletopqf(arg);
179             active = 1;
180             break;
181         case 0:
182             break;
183         }
184     }
185     if (!active)
186     {
187         fprintf(stderr, "%s: nothing to do\n", prog);
188         usage();
189     }
190 #else
191     fprintf(stderr, "%s: XML support not enabled.\n", prog);
192     exit(1);
193 #endif
194     return 0;
195 }
196