1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 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
35 #include <idzebra/recgrs.h>
38 #include <yaz/xmalloc.h>
42 #define XML_CHUNK 1024
45 data1_node *d1_stack[256];
52 static void report_xml_error(XML_Parser parser)
54 zint line = XML_GetCurrentLineNumber(parser);
55 zint col = XML_GetCurrentColumnNumber(parser);
56 yaz_log (YLOG_WARN, ZINT_FORMAT ":" ZINT_FORMAT ":XML error: %s",
57 line, col, XML_ErrorString(XML_GetErrorCode(parser)));
60 static void cb_start (void *user, const char *el, const char **attr)
62 struct user_info *ui = (struct user_info*) user;
64 data1_set_root (ui->dh, ui->d1_stack[0], ui->nmem, el);
65 ui->d1_stack[ui->level] = data1_mk_tag (ui->dh, ui->nmem, el, attr,
66 ui->d1_stack[ui->level-1]);
68 yaz_log (ui->loglevel, "cb_start %s", el);
71 static void cb_end (void *user, const char *el)
73 struct user_info *ui = (struct user_info*) user;
76 yaz_log (ui->loglevel, "cb_end %s", el);
79 static void cb_chardata (void *user, const char *s, int len)
81 struct user_info *ui = (struct user_info*) user;
83 yaz_log (ui->loglevel, "cb_chardata %.*s", len, s);
85 ui->d1_stack[ui->level] = data1_mk_text_n (ui->dh, ui->nmem, s, len,
86 ui->d1_stack[ui->level -1]);
89 static void cb_decl (void *user, const char *version, const char *encoding,
92 struct user_info *ui = (struct user_info*) user;
93 const char *attr_list[7];
95 attr_list[0] = "version";
96 attr_list[1] = version;
98 attr_list[2] = "encoding";
99 attr_list[3] = "UTF-8"; /* internally it's always UTF-8 */
101 attr_list[4] = "standalone";
102 attr_list[5] = standalone ? "yes" : "no";
106 data1_mk_preprocess (ui->dh, ui->nmem, "xml", attr_list,
107 ui->d1_stack[ui->level-1]);
109 yaz_log (YLOG_LOG, "decl version=%s encoding=%s",
110 version ? version : "null",
111 encoding ? encoding : "null");
115 static void cb_processing (void *user, const char *target,
118 struct user_info *ui = (struct user_info*) user;
120 data1_mk_preprocess (ui->dh, ui->nmem, target, 0,
121 ui->d1_stack[ui->level-1]);
122 data1_mk_text_nf (ui->dh, ui->nmem, data, strlen(data), res);
124 yaz_log (ui->loglevel, "decl processing target=%s data=%s",
125 target ? target : "null",
126 data ? data : "null");
129 static void cb_comment (void *user, const char *data)
131 struct user_info *ui = (struct user_info*) user;
132 yaz_log (ui->loglevel, "decl comment data=%s", data ? data : "null");
133 data1_mk_comment (ui->dh, ui->nmem, data, ui->d1_stack[ui->level-1]);
136 static void cb_doctype_start (void *userData, const char *doctypeName,
137 const char *sysid, const char *pubid,
138 int has_internal_subset)
140 struct user_info *ui = (struct user_info*) userData;
141 yaz_log (ui->loglevel, "doctype start doctype=%s sysid=%s pubid=%s",
142 doctypeName, sysid, pubid);
145 static void cb_doctype_end (void *userData)
147 struct user_info *ui = (struct user_info*) userData;
148 yaz_log (ui->loglevel, "doctype end");
152 static void cb_entity_decl (void *userData, const char *entityName,
153 int is_parameter_entity,
154 const char *value, int value_length,
155 const char *base, const char *systemId,
156 const char *publicId, const char *notationName)
158 struct user_info *ui = (struct user_info*) userData;
159 yaz_log (ui->loglevel,
160 "entity decl %s is_para_entry=%d value=%.*s base=%s systemId=%s"
161 " publicId=%s notationName=%s",
162 entityName, is_parameter_entity, value_length, value,
163 base, systemId, publicId, notationName);
167 static int cb_external_entity(XML_Parser pparser,
170 const char *systemId,
171 const char *publicId)
173 struct user_info *ui = (struct user_info*) XML_GetUserData(pparser);
178 yaz_log (ui->loglevel,
179 "external entity context=%s base=%s systemid=%s publicid=%s",
180 context, base, systemId, publicId);
184 if (!(inf = fopen (systemId, "rb")))
186 yaz_log (YLOG_WARN|YLOG_ERRNO, "fopen %s", systemId);
190 parser = XML_ExternalEntityParserCreate (pparser, "", 0);
194 void *buf = XML_GetBuffer (parser, XML_CHUNK);
197 yaz_log (YLOG_WARN, "XML_GetBuffer fail");
200 r = fread (buf, 1, XML_CHUNK, inf);
205 yaz_log (YLOG_WARN|YLOG_ERRNO, "fread %s", systemId);
210 if (!XML_ParseBuffer (parser, r, done))
213 report_xml_error(parser);
217 XML_ParserFree (parser);
223 static int cb_encoding_convert (void *data, const char *s)
225 iconv_t t = (iconv_t) data;
228 char outbuf_[2], *outbuf = outbuf_;
230 char *inbuf = (char *) s;
234 yaz_log(YLOG_LOG, "------------------------- cb_encoding_convert --- ");
236 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
237 if (ret == (size_t) (-1) && errno != E2BIG)
239 iconv (t, 0, 0, 0, 0);
244 memcpy (&code, outbuf_, sizeof(short));
248 static void cb_encoding_release (void *data)
250 iconv_t t = (iconv_t) data;
254 static int cb_encoding_handler (void *userData, const char *name,
259 struct user_info *ui = (struct user_info*) userData;
261 iconv_t t = iconv_open ("UNICODE", name);
262 if (t == (iconv_t) (-1))
265 info->data = 0; /* signal that multibyte is not in use */
266 yaz_log (ui->loglevel, "Encoding handler of %s", name);
267 for (i = 0; i<256; i++)
272 char *inbuf = inbuf_;
273 char *outbuf = outbuf_;
278 iconv (t, 0, 0, 0, 0); /* reset iconv */
280 ret = iconv(t, &inbuf, &inleft, &outbuf, &outleft);
281 if (ret == (size_t) (-1))
285 yaz_log (ui->loglevel, "Encoding %d: invalid sequence", i);
286 info->map[i] = -1; /* invalid sequence */
289 { /* multi byte input */
306 assert (i >= 0 && i<255);
309 for (k = 0; k<len; k++)
311 sprintf (sbuf+strlen(sbuf), "%d ", inbuf_[k]&255);
313 ret = iconv (t, &inbuf, &inleft, &outbuf, &outleft);
314 if (ret == (size_t) (-1))
316 if (errno == EILSEQ || errno == E2BIG)
322 else if (errno == EINVAL)
328 else if (outleft == 0)
331 info->data = t; /* signal that multibyte is in use */
339 if (info->map[i] < -1)
340 yaz_log (ui->loglevel, "Encoding %d: multibyte input %d",
343 yaz_log (ui->loglevel, "Encoding %d: multibyte input failed",
348 info->map[i] = -1; /* no room for output */
350 yaz_log (YLOG_WARN, "Encoding %d: no room for output",
354 else if (outleft == 0)
357 memcpy (&code, outbuf_, sizeof(short));
362 { /* should never happen */
364 yaz_log (YLOG_DEBUG, "Encoding %d: bad state", i);
368 { /* at least one multi byte */
369 info->convert = cb_encoding_convert;
370 info->release = cb_encoding_release;
374 /* no multi byte - we no longer need iconv handler */
386 static void cb_ns_start(void *userData, const char *prefix, const char *uri)
388 struct user_info *ui = (struct user_info*) userData;
390 yaz_log(ui->loglevel, "cb_ns_start %s %s", prefix, uri);
393 static void cb_ns_end(void *userData, const char *prefix)
395 struct user_info *ui = (struct user_info*) userData;
397 yaz_log(ui->loglevel, "cb_ns_end %s", prefix);
400 data1_node *zebra_read_xml(data1_handle dh,
401 struct ZebraRecStream *stream,
405 struct user_info uinfo;
407 data1_node *first_node;
410 uinfo.loglevel = YLOG_DEBUG;
414 uinfo.d1_stack[0] = data1_mk_node2 (dh, m, DATA1N_root, 0);
415 uinfo.d1_stack[1] = 0; /* indicate no children (see end of routine) */
417 parser = XML_ParserCreate (0 /* encoding */);
419 XML_SetElementHandler (parser, cb_start, cb_end);
420 XML_SetCharacterDataHandler (parser, cb_chardata);
421 XML_SetXmlDeclHandler (parser, cb_decl);
422 XML_SetProcessingInstructionHandler (parser, cb_processing);
423 XML_SetUserData (parser, &uinfo);
424 XML_SetCommentHandler (parser, cb_comment);
425 XML_SetDoctypeDeclHandler (parser, cb_doctype_start, cb_doctype_end);
426 XML_SetEntityDeclHandler (parser, cb_entity_decl);
427 XML_SetExternalEntityRefHandler (parser, cb_external_entity);
428 XML_SetNamespaceDeclHandler(parser, cb_ns_start, cb_ns_end);
430 XML_SetUnknownEncodingHandler (parser, cb_encoding_handler, &uinfo);
435 void *buf = XML_GetBuffer (parser, XML_CHUNK);
439 yaz_log (YLOG_WARN, "XML_GetBuffer fail");
442 r = stream->readf(stream, buf, XML_CHUNK);
446 yaz_log (YLOG_WARN, "XML read fail");
453 if (no_read && !XML_ParseBuffer (parser, r, done))
456 report_xml_error(parser);
459 XML_ParserFree (parser);
462 if (!uinfo.d1_stack[1] || !done)
464 /* insert XML header if not present .. */
465 first_node = uinfo.d1_stack[0]->child;
466 if (first_node->which != DATA1N_preprocess ||
467 strcmp(first_node->u.preprocess.target, "xml"))
469 const char *attr_list[5];
471 attr_list[0] = "version";
472 attr_list[1] = "1.0";
474 attr_list[2] = "encoding";
475 attr_list[3] = "UTF-8"; /* encoding */
479 data1_insert_preprocess (uinfo.dh, uinfo.nmem, "xml", attr_list,
482 return uinfo.d1_stack[0];
486 XML_Expat_Version expat_version;
489 static data1_node *grs_read_xml(struct grs_read_info *p)
491 return zebra_read_xml(p->dh, p->stream, p->mem);
494 static void *filter_init(Res res, RecType recType)
496 struct xml_info *p = (struct xml_info *) xmalloc (sizeof(*p));
498 p->expat_version = XML_ExpatVersionInfo();
503 static void filter_destroy(void *clientData)
505 struct xml_info *p = (struct xml_info *) clientData;
510 static int filter_extract(void *clientData, struct recExtractCtrl *ctrl)
512 return zebra_grs_extract(clientData, ctrl, grs_read_xml);
515 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *ctrl)
517 return zebra_grs_retrieve(clientData, ctrl, grs_read_xml);
520 static struct recType filter_type = {
531 #if IDZEBRA_STATIC_GRS_XML
532 idzebra_filter_grs_xml
547 * c-file-style: "Stroustrup"
548 * indent-tabs-mode: nil
550 * vim: shiftwidth=4 tabstop=8 expandtab