File: yaz-xmlquery.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (214 lines) | stat: -rw-r--r-- 4,072 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <yaz/options.h>
#include <yaz/querytowrbuf.h>
#include <yaz/xmlquery.h>
#include <yaz/pquery.h>
#include <yaz/backtrace.h>

#if YAZ_HAVE_XML2
#include <libxml/parser.h>
#endif

static char *prog = "yaz-xmlquery";

#if YAZ_HAVE_XML2
void pqftoxmlquery(const char *pqf)
{
    YAZ_PQF_Parser parser = yaz_pqf_create();
    ODR odr = odr_createmem(ODR_ENCODE);
    Z_RPNQuery *rpn;

    if (!parser)
    {
	fprintf(stderr, "%s: cannot create parser\n", prog);
	exit(1);
    }

    if (!odr)
    {
	fprintf(stderr, "%s: cannot create parser\n", prog);
	exit(1);
    }

    rpn = yaz_pqf_parse(parser, odr, pqf);

    yaz_pqf_destroy(parser);

    if (!rpn)
    {
	fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
	exit(2);
    }
    else
    {
	xmlDocPtr doc = 0;

        yaz_rpnquery2xml(rpn, &doc);

        if (!doc)
	{
	    fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
		    prog, pqf);
	    exit(3);
	}
        else
        {
            xmlChar *buf_out = 0;
            int len_out = 0;

            xmlDocDumpMemory(doc, &buf_out, &len_out);

            if (!len_out || !buf_out)
	    {
		fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
			prog, pqf);
		exit(4);
	    }
	    else
	    {
		if (fwrite(buf_out, len_out, 1, stdout) != 1)
		{
		    fprintf(stderr, "%s: write failed\n", prog);
		    exit(5);
		}
	    }
            xmlFreeDoc(doc);
	}
    }
    odr_destroy(odr);
}


void xmlquerytopqf(const char *xmlstr)
{
    xmlDocPtr doc;

    doc = xmlParseMemory(xmlstr, strlen(xmlstr));
    if (!doc)
    {
	fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
	exit(1);
    }
    else
    {
	int error_code = 0;
	const char *addinfo = 0;
	Z_Query *query = 0;
	ODR odr = odr_createmem(ODR_ENCODE);

	const xmlNode *root_element = xmlDocGetRootElement(doc);
	yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
	if (error_code)
	{
	    fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
		    prog, error_code, addinfo);
	    exit(1);
	}
	else if (!query)
	{
	    fprintf(stderr, "%s: yaz_xml2query no query result\n",
		    prog);
	    exit(1);
	}
	else
	{
	    WRBUF w = wrbuf_alloc();
	    yaz_query_to_wrbuf(w, query);
	    printf("%s\n", wrbuf_cstr(w));
	    wrbuf_destroy(w);
	}
	odr_destroy(odr);
	xmlFreeDoc(doc);
    }
}

void xmlfiletopqf(const char *xmlfile)
{
    long sz;
    char *xmlstr;
    FILE *f = fopen(xmlfile, "rb");
    if (!f)
    {
	fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
	exit(1);
    }
    fseek(f, 0, SEEK_END);
    sz = ftell(f);
    if (sz <= 0 || sz >= 1<<18)
    {
	fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
	exit(1);
    }
    rewind(f);
    xmlstr = (char *) xmalloc(sz+1);
    xmlstr[sz] = '\0';
    if (fread(xmlstr, sz, 1, f) != 1)
    {
	fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
	exit(1);
    }
    if (fclose(f))
    {
	fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
	exit(1);
    }

    xmlquerytopqf(xmlstr);
    xfree(xmlstr);
}
#endif

void usage(void)
{
    fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
    fprintf(stderr, " -p pqf      reads pqf. write xml to stdout\n");
    fprintf(stderr, " -x xmlfile  reads XML from file. write pqf to stdout\n");
    exit(1);
}

int main (int argc, char **argv)
{
#if YAZ_HAVE_XML2
    char *arg;
    int r;
    int active = 0;

    yaz_enable_panic_backtrace(*argv);
    while ((r = options("-p:x:", argv, argc, &arg)) != -2)
    {
	switch(r)
	{
	case 'p':
	    pqftoxmlquery(arg);
	    active = 1;
	    break;
	case 'x':
	    xmlfiletopqf(arg);
	    active = 1;
	    break;
	case 0:
	    break;
	}
    }
    if (!active)
    {
	fprintf(stderr, "%s: nothing to do\n", prog);
	usage();
    }
#else
    fprintf(stderr, "%s: XML support not enabled.\n", prog);
    exit(1);
#endif
    return 0;
}