1 /* $Id: xmlread.c,v 1.12 2004-07-26 12:26:25 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
37 #include <yaz/xmalloc.h>
43 #define XML_CHUNK 1024
46 data1_node *d1_stack[256];
53 static void cb_start (void *user, const char *el, const char **attr)
55 struct user_info *ui = (struct user_info*) user;
57 data1_set_root (ui->dh, ui->d1_stack[0], ui->nmem, el);
58 ui->d1_stack[ui->level] = data1_mk_tag (ui->dh, ui->nmem, el, attr,
59 ui->d1_stack[ui->level-1]);
61 yaz_log (ui->loglevel, "cb_start %s", el);
64 static void cb_end (void *user, const char *el)
66 struct user_info *ui = (struct user_info*) user;
69 yaz_log (ui->loglevel, "cb_end %s", el);
72 static void cb_chardata (void *user, const char *s, int len)
74 struct user_info *ui = (struct user_info*) user;
76 yaz_log (ui->loglevel, "cb_chardata %.*s", len, s);
78 ui->d1_stack[ui->level] = data1_mk_text_n (ui->dh, ui->nmem, s, len,
79 ui->d1_stack[ui->level -1]);
82 static void cb_decl (void *user, const char *version, const char *encoding,
85 struct user_info *ui = (struct user_info*) user;
86 const char *attr_list[7];
88 attr_list[0] = "version";
89 attr_list[1] = version;
91 attr_list[2] = "encoding";
92 attr_list[3] = "UTF-8"; /* internally it's always UTF-8 */
94 attr_list[4] = "standalone";
95 attr_list[5] = standalone ? "yes" : "no";
99 data1_mk_preprocess (ui->dh, ui->nmem, "xml", attr_list,
100 ui->d1_stack[ui->level-1]);
101 yaz_log (LOG_LOG, "decl version=%s encoding=%s",
102 version ? version : "null",
103 encoding ? encoding : "null");
106 static void cb_processing (void *user, const char *target,
109 struct user_info *ui = (struct user_info*) user;
111 data1_mk_preprocess (ui->dh, ui->nmem, target, 0,
112 ui->d1_stack[ui->level-1]);
113 data1_mk_text_nf (ui->dh, ui->nmem, data, strlen(data), res);
115 yaz_log (ui->loglevel, "decl processing target=%s data=%s",
116 target ? target : "null",
117 data ? data : "null");
120 static void cb_comment (void *user, const char *data)
122 struct user_info *ui = (struct user_info*) user;
123 yaz_log (ui->loglevel, "decl comment data=%s", data ? data : "null");
124 data1_mk_comment (ui->dh, ui->nmem, data, ui->d1_stack[ui->level-1]);
127 static void cb_doctype_start (void *userData, const char *doctypeName,
128 const char *sysid, const char *pubid,
129 int has_internal_subset)
131 struct user_info *ui = (struct user_info*) userData;
132 yaz_log (ui->loglevel, "doctype start doctype=%s sysid=%s pubid=%s",
133 doctypeName, sysid, pubid);
136 static void cb_doctype_end (void *userData)
138 struct user_info *ui = (struct user_info*) userData;
139 yaz_log (ui->loglevel, "doctype end");
143 static void cb_entity_decl (void *userData, const char *entityName,
144 int is_parameter_entity,
145 const char *value, int value_length,
146 const char *base, const char *systemId,
147 const char *publicId, const char *notationName)
149 struct user_info *ui = (struct user_info*) userData;
150 yaz_log (ui->loglevel,
151 "entity decl %s is_para_entry=%d value=%.*s base=%s systemId=%s"
152 " publicId=%s notationName=%s",
153 entityName, is_parameter_entity, value_length, value,
154 base, systemId, publicId, notationName);
158 static int cb_external_entity (XML_Parser pparser,
161 const char *systemId,
162 const char *publicId)
164 struct user_info *ui = (struct user_info*) XML_GetUserData(pparser);
169 yaz_log (ui->loglevel,
170 "external entity context=%s base=%s systemid=%s publicid=%s",
171 context, base, systemId, publicId);
175 if (!(inf = fopen (systemId, "rb")))
177 yaz_log (LOG_WARN|LOG_ERRNO, "fopen %s", systemId);
181 parser = XML_ExternalEntityParserCreate (pparser, "", 0);
185 void *buf = XML_GetBuffer (parser, XML_CHUNK);
188 yaz_log (LOG_WARN, "XML_GetBuffer fail");
191 r = fread (buf, 1, XML_CHUNK, inf);
196 yaz_log (LOG_WARN|LOG_ERRNO, "fread %s", systemId);
201 if (!XML_ParseBuffer (parser, r, done))
204 yaz_log (LOG_WARN, "%s:%d:%d:XML error: %s",
206 XML_GetCurrentLineNumber(parser),
207 XML_GetCurrentColumnNumber(parser),
208 XML_ErrorString(XML_GetErrorCode(parser)));
212 XML_ParserFree (parser);
218 static int cb_encoding_convert (void *data, const char *s)
220 iconv_t t = (iconv_t) data;
223 char outbuf_[2], *outbuf = outbuf_;
225 char *inbuf = (char *) s;
229 yaz_log(LOG_LOG, "------------------------- cb_encoding_convert --- ");
231 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
232 if (ret == (size_t) (-1) && errno != E2BIG)
234 iconv (t, 0, 0, 0, 0);
239 memcpy (&code, outbuf_, sizeof(short));
243 static void cb_encoding_release (void *data)
245 iconv_t t = (iconv_t) data;
249 static int cb_encoding_handler (void *userData, const char *name,
254 struct user_info *ui = (struct user_info*) userData;
256 iconv_t t = iconv_open ("UNICODE", name);
257 if (t == (iconv_t) (-1))
260 info->data = 0; /* signal that multibyte is not in use */
261 yaz_log (ui->loglevel, "Encoding handler of %s", name);
262 for (i = 0; i<256; i++)
267 char *inbuf = inbuf_;
268 char *outbuf = outbuf_;
273 iconv (t, 0, 0, 0, 0); /* reset iconv */
275 ret = iconv(t, &inbuf, &inleft, &outbuf, &outleft);
276 if (ret == (size_t) (-1))
280 yaz_log (ui->loglevel, "Encoding %d: invalid sequence", i);
281 info->map[i] = -1; /* invalid sequence */
284 { /* multi byte input */
301 assert (i >= 0 && i<255);
304 for (k = 0; k<len; k++)
306 sprintf (sbuf+strlen(sbuf), "%d ", inbuf_[k]&255);
308 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
309 if (ret == (size_t) (-1))
311 if (errno == EILSEQ || errno == E2BIG)
317 else if (errno == EINVAL)
323 else if (outleft == 0)
326 info->data = t; /* signal that multibyte is in use */
334 if (info->map[i] < -1)
335 yaz_log (ui->loglevel, "Encoding %d: multibyte input %d",
338 yaz_log (ui->loglevel, "Encoding %d: multibyte input failed",
343 info->map[i] = -1; /* no room for output */
345 yaz_log (LOG_WARN, "Encoding %d: no room for output",
349 else if (outleft == 0)
352 memcpy (&code, outbuf_, sizeof(short));
357 { /* should never happen */
359 yaz_log (LOG_DEBUG, "Encoding %d: bad state", i);
363 { /* at least one multi byte */
364 info->convert = cb_encoding_convert;
365 info->release = cb_encoding_release;
369 /* no multi byte - we no longer need iconv handler */
381 static void cb_ns_start(void *userData, const char *prefix, const char *uri)
383 struct user_info *ui = (struct user_info*) userData;
385 yaz_log(ui->loglevel, "cb_ns_start %s %s", prefix, uri);
388 static void cb_ns_end(void *userData, const char *prefix)
390 struct user_info *ui = (struct user_info*) userData;
392 yaz_log(ui->loglevel, "cb_ns_end %s", prefix);
394 data1_node *zebra_read_xml (data1_handle dh,
395 int (*rf)(void *, char *, size_t), void *fh,
399 struct user_info uinfo;
401 data1_node *first_node;
403 uinfo.loglevel = LOG_DEBUG;
407 uinfo.d1_stack[0] = data1_mk_node2 (dh, m, DATA1N_root, 0);
408 uinfo.d1_stack[1] = 0; /* indicate no children (see end of routine) */
410 parser = XML_ParserCreate (0 /* encoding */);
412 XML_SetElementHandler (parser, cb_start, cb_end);
413 XML_SetCharacterDataHandler (parser, cb_chardata);
414 XML_SetXmlDeclHandler (parser, cb_decl);
415 XML_SetProcessingInstructionHandler (parser, cb_processing);
416 XML_SetUserData (parser, &uinfo);
417 XML_SetCommentHandler (parser, cb_comment);
418 XML_SetDoctypeDeclHandler (parser, cb_doctype_start, cb_doctype_end);
419 XML_SetEntityDeclHandler (parser, cb_entity_decl);
420 XML_SetExternalEntityRefHandler (parser, cb_external_entity);
421 XML_SetNamespaceDeclHandler(parser, cb_ns_start, cb_ns_end);
423 XML_SetUnknownEncodingHandler (parser, cb_encoding_handler, &uinfo);
428 void *buf = XML_GetBuffer (parser, XML_CHUNK);
432 yaz_log (LOG_WARN, "XML_GetBuffer fail");
435 r = (*rf)(fh, buf, XML_CHUNK);
439 yaz_log (LOG_WARN, "XML read fail");
444 if (!XML_ParseBuffer (parser, r, done))
447 yaz_log (LOG_WARN, "%d:%d:XML error: %s",
448 XML_GetCurrentLineNumber(parser),
449 XML_GetCurrentColumnNumber(parser),
450 XML_ErrorString(XML_GetErrorCode(parser)));
453 XML_ParserFree (parser);
454 if (!uinfo.d1_stack[1] || !done)
456 /* insert XML header if not present .. */
457 first_node = uinfo.d1_stack[0]->child;
458 if (first_node->which != DATA1N_preprocess ||
459 strcmp(first_node->u.preprocess.target, "xml"))
461 const char *attr_list[5];
463 attr_list[0] = "version";
464 attr_list[1] = "1.0";
466 attr_list[2] = "encoding";
467 attr_list[3] = "UTF-8"; /* encoding */
471 data1_insert_preprocess (uinfo.dh, uinfo.nmem, "xml", attr_list,
474 return uinfo.d1_stack[0];
478 XML_Expat_Version expat_version;
481 static void *grs_init_xml(void)
483 struct xml_info *p = (struct xml_info *) xmalloc (sizeof(*p));
485 p->expat_version = XML_ExpatVersionInfo();
490 static data1_node *grs_read_xml (struct grs_read_info *p)
492 return zebra_read_xml (p->dh, p->readf, p->fh, p->mem);
495 static void grs_destroy_xml(void *clientData)
497 struct xml_info *p = (struct xml_info *) clientData;
502 static struct recTypeGrs xml_type = {
509 RecTypeGrs recTypeGrs_xml = &xml_type;