File: yaz-xmlquery.c

package info (click to toggle)
yaz 2.1.18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,988 kB
  • ctags: 11,045
  • sloc: xml: 109,719; ansic: 41,566; sh: 10,625; makefile: 1,115; tcl: 380; yacc: 288
file content (198 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download
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
/*
 * Copyright (C) 1995-2005, Index Data ApS
 * See the file LICENSE for details.
 *
 * $Id: yaz-xmlquery.c,v 1.2 2006/03/06 08:11:41 adam Exp $
 */

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

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

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

static char *prog = "yaz-xmlquery";

#if 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
        {
            char *buf_out = 0;
            int len_out = 0;

            xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);

            if (!len_out || !buf_out)
	    {
		fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
			prog, pqf);
		exit(4);
	    }
	    else
		fwrite(buf_out, len_out, 1, stdout);
            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_buf(w));
	    wrbuf_free(w, 1);
	}
	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 = xmalloc(sz+1);
    xmlstr[sz] = '\0';
    fread(xmlstr, sz, 1, f);
    fclose(f);
    
    xmlquerytopqf(xmlstr);
    xfree(xmlstr);
}
#endif

void usage()
{
    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 HAVE_XML2
    char *arg;
    int r;
    int active = 0;

    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;
}