1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <idzebra/recgrs.h>
35 #include <yaz/xmalloc.h>
39 #define XML_CHUNK 1024
42 data1_node *d1_stack[256];
49 static void cb_start (void *user, const char *el, const char **attr)
51 struct user_info *ui = (struct user_info*) user;
53 data1_set_root (ui->dh, ui->d1_stack[0], ui->nmem, el);
54 ui->d1_stack[ui->level] = data1_mk_tag (ui->dh, ui->nmem, el, attr,
55 ui->d1_stack[ui->level-1]);
57 yaz_log (ui->loglevel, "cb_start %s", el);
60 static void cb_end (void *user, const char *el)
62 struct user_info *ui = (struct user_info*) user;
65 yaz_log (ui->loglevel, "cb_end %s", el);
68 static void cb_chardata (void *user, const char *s, int len)
70 struct user_info *ui = (struct user_info*) user;
72 yaz_log (ui->loglevel, "cb_chardata %.*s", len, s);
74 ui->d1_stack[ui->level] = data1_mk_text_n (ui->dh, ui->nmem, s, len,
75 ui->d1_stack[ui->level -1]);
78 static void cb_decl (void *user, const char *version, const char *encoding,
81 struct user_info *ui = (struct user_info*) user;
82 const char *attr_list[7];
84 attr_list[0] = "version";
85 attr_list[1] = version;
87 attr_list[2] = "encoding";
88 attr_list[3] = "UTF-8"; /* internally it's always UTF-8 */
90 attr_list[4] = "standalone";
91 attr_list[5] = standalone ? "yes" : "no";
95 data1_mk_preprocess (ui->dh, ui->nmem, "xml", attr_list,
96 ui->d1_stack[ui->level-1]);
98 yaz_log (YLOG_LOG, "decl version=%s encoding=%s",
99 version ? version : "null",
100 encoding ? encoding : "null");
104 static void cb_processing (void *user, const char *target,
107 struct user_info *ui = (struct user_info*) user;
109 data1_mk_preprocess (ui->dh, ui->nmem, target, 0,
110 ui->d1_stack[ui->level-1]);
111 data1_mk_text_nf (ui->dh, ui->nmem, data, strlen(data), res);
113 yaz_log (ui->loglevel, "decl processing target=%s data=%s",
114 target ? target : "null",
115 data ? data : "null");
118 static void cb_comment (void *user, const char *data)
120 struct user_info *ui = (struct user_info*) user;
121 yaz_log (ui->loglevel, "decl comment data=%s", data ? data : "null");
122 data1_mk_comment (ui->dh, ui->nmem, data, ui->d1_stack[ui->level-1]);
125 static void cb_doctype_start (void *userData, const char *doctypeName,
126 const char *sysid, const char *pubid,
127 int has_internal_subset)
129 struct user_info *ui = (struct user_info*) userData;
130 yaz_log (ui->loglevel, "doctype start doctype=%s sysid=%s pubid=%s",
131 doctypeName, sysid, pubid);
134 static void cb_doctype_end (void *userData)
136 struct user_info *ui = (struct user_info*) userData;
137 yaz_log (ui->loglevel, "doctype end");
141 static void cb_entity_decl (void *userData, const char *entityName,
142 int is_parameter_entity,
143 const char *value, int value_length,
144 const char *base, const char *systemId,
145 const char *publicId, const char *notationName)
147 struct user_info *ui = (struct user_info*) userData;
148 yaz_log (ui->loglevel,
149 "entity decl %s is_para_entry=%d value=%.*s base=%s systemId=%s"
150 " publicId=%s notationName=%s",
151 entityName, is_parameter_entity, value_length, value,
152 base, systemId, publicId, notationName);
156 static int cb_external_entity(XML_Parser pparser,
159 const char *systemId,
160 const char *publicId)
162 struct user_info *ui = (struct user_info*) XML_GetUserData(pparser);
167 yaz_log (ui->loglevel,
168 "external entity context=%s base=%s systemid=%s publicid=%s",
169 context, base, systemId, publicId);
173 if (!(inf = fopen (systemId, "rb")))
175 yaz_log (YLOG_WARN|YLOG_ERRNO, "fopen %s", systemId);
179 parser = XML_ExternalEntityParserCreate (pparser, "", 0);
183 void *buf = XML_GetBuffer (parser, XML_CHUNK);
186 yaz_log (YLOG_WARN, "XML_GetBuffer fail");
189 r = fread (buf, 1, XML_CHUNK, inf);
194 yaz_log (YLOG_WARN|YLOG_ERRNO, "fread %s", systemId);
199 if (!XML_ParseBuffer (parser, r, done))
202 yaz_log (YLOG_WARN, "%s:%d:%d:XML error: %s",
204 XML_GetCurrentLineNumber(parser),
205 XML_GetCurrentColumnNumber(parser),
206 XML_ErrorString(XML_GetErrorCode(parser)));
210 XML_ParserFree (parser);
216 static int cb_encoding_convert (void *data, const char *s)
218 iconv_t t = (iconv_t) data;
221 char outbuf_[2], *outbuf = outbuf_;
223 char *inbuf = (char *) s;
227 yaz_log(YLOG_LOG, "------------------------- cb_encoding_convert --- ");
229 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
230 if (ret == (size_t) (-1) && errno != E2BIG)
232 iconv (t, 0, 0, 0, 0);
237 memcpy (&code, outbuf_, sizeof(short));
241 static void cb_encoding_release (void *data)
243 iconv_t t = (iconv_t) data;
247 static int cb_encoding_handler (void *userData, const char *name,
252 struct user_info *ui = (struct user_info*) userData;
254 iconv_t t = iconv_open ("UNICODE", name);
255 if (t == (iconv_t) (-1))
258 info->data = 0; /* signal that multibyte is not in use */
259 yaz_log (ui->loglevel, "Encoding handler of %s", name);
260 for (i = 0; i<256; i++)
265 char *inbuf = inbuf_;
266 char *outbuf = outbuf_;
271 iconv (t, 0, 0, 0, 0); /* reset iconv */
273 ret = iconv(t, &inbuf, &inleft, &outbuf, &outleft);
274 if (ret == (size_t) (-1))
278 yaz_log (ui->loglevel, "Encoding %d: invalid sequence", i);
279 info->map[i] = -1; /* invalid sequence */
282 { /* multi byte input */
299 assert (i >= 0 && i<255);
302 for (k = 0; k<len; k++)
304 sprintf (sbuf+strlen(sbuf), "%d ", inbuf_[k]&255);
306 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
307 if (ret == (size_t) (-1))
309 if (errno == EILSEQ || errno == E2BIG)
315 else if (errno == EINVAL)
321 else if (outleft == 0)
324 info->data = t; /* signal that multibyte is in use */
332 if (info->map[i] < -1)
333 yaz_log (ui->loglevel, "Encoding %d: multibyte input %d",
336 yaz_log (ui->loglevel, "Encoding %d: multibyte input failed",
341 info->map[i] = -1; /* no room for output */
343 yaz_log (YLOG_WARN, "Encoding %d: no room for output",
347 else if (outleft == 0)
350 memcpy (&code, outbuf_, sizeof(short));
355 { /* should never happen */
357 yaz_log (YLOG_DEBUG, "Encoding %d: bad state", i);
361 { /* at least one multi byte */
362 info->convert = cb_encoding_convert;
363 info->release = cb_encoding_release;
367 /* no multi byte - we no longer need iconv handler */
379 static void cb_ns_start(void *userData, const char *prefix, const char *uri)
381 struct user_info *ui = (struct user_info*) userData;
383 yaz_log(ui->loglevel, "cb_ns_start %s %s", prefix, uri);
386 static void cb_ns_end(void *userData, const char *prefix)
388 struct user_info *ui = (struct user_info*) userData;
390 yaz_log(ui->loglevel, "cb_ns_end %s", prefix);
393 data1_node *zebra_read_xml(data1_handle dh,
394 struct ZebraRecStream *stream,
398 struct user_info uinfo;
400 data1_node *first_node;
403 uinfo.loglevel = YLOG_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 (YLOG_WARN, "XML_GetBuffer fail");
435 r = stream->readf(stream, buf, XML_CHUNK);
439 yaz_log (YLOG_WARN, "XML read fail");
446 if (no_read && !XML_ParseBuffer (parser, r, done))
449 yaz_log (YLOG_WARN, "%d:%d:XML error: %s",
450 XML_GetCurrentLineNumber(parser),
451 XML_GetCurrentColumnNumber(parser),
452 XML_ErrorString(XML_GetErrorCode(parser)));
455 XML_ParserFree (parser);
458 if (!uinfo.d1_stack[1] || !done)
460 /* insert XML header if not present .. */
461 first_node = uinfo.d1_stack[0]->child;
462 if (first_node->which != DATA1N_preprocess ||
463 strcmp(first_node->u.preprocess.target, "xml"))
465 const char *attr_list[5];
467 attr_list[0] = "version";
468 attr_list[1] = "1.0";
470 attr_list[2] = "encoding";
471 attr_list[3] = "UTF-8"; /* encoding */
475 data1_insert_preprocess (uinfo.dh, uinfo.nmem, "xml", attr_list,
478 return uinfo.d1_stack[0];
482 XML_Expat_Version expat_version;
485 static data1_node *grs_read_xml(struct grs_read_info *p)
487 return zebra_read_xml(p->dh, p->stream, p->mem);
490 static void *filter_init(Res res, RecType recType)
492 struct xml_info *p = (struct xml_info *) xmalloc (sizeof(*p));
494 p->expat_version = XML_ExpatVersionInfo();
499 static void filter_destroy(void *clientData)
501 struct xml_info *p = (struct xml_info *) clientData;
506 static int filter_extract(void *clientData, struct recExtractCtrl *ctrl)
508 return zebra_grs_extract(clientData, ctrl, grs_read_xml);
511 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *ctrl)
513 return zebra_grs_retrieve(clientData, ctrl, grs_read_xml);
516 static struct recType filter_type = {
527 #ifdef IDZEBRA_STATIC_GRS_XML
528 idzebra_filter_grs_xml
543 * indent-tabs-mode: nil
545 * vim: shiftwidth=4 tabstop=8 expandtab