1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Record Conversions utility
15 #include <yaz/yaz-iconv.h>
16 #include <yaz/marcdisp.h>
17 #include <yaz/record_conv.h>
18 #include <yaz/wrbuf.h>
19 #include <yaz/xmalloc.h>
21 #include <yaz/tpath.h>
22 #include <yaz/z-opac.h>
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27 #include <libxml/xinclude.h>
29 #include <libxslt/xsltutils.h>
30 #include <libxslt/transform.h>
33 #include <libexslt/exslt.h>
36 /** \brief The internal structure for yaz_record_conv_t */
37 struct yaz_record_conv_struct {
38 /** \brief memory for configuration */
41 /** \brief conversion rules (allocated using NMEM) */
42 struct yaz_record_conv_rule *rules;
44 /** \brief pointer to last conversion rule pointer in chain */
45 struct yaz_record_conv_rule **rules_p;
47 /** \brief string buffer for error messages */
50 /** \brief path for opening files */
54 /** \brief tranformation types (rule types) */
55 enum YAZ_RECORD_CONV_RULE
57 YAZ_RECORD_CONV_RULE_XSLT,
58 YAZ_RECORD_CONV_RULE_MARC
61 /** \brief tranformation info (rule info) */
62 struct yaz_record_conv_rule {
63 enum YAZ_RECORD_CONV_RULE which;
71 const char *input_charset;
72 const char *output_charset;
77 struct yaz_record_conv_rule *next;
80 /** \brief reset rules+configuration */
81 static void yaz_record_conv_reset(yaz_record_conv_t p)
84 struct yaz_record_conv_rule *r;
85 for (r = p->rules; r; r = r->next)
87 if (r->which == YAZ_RECORD_CONV_RULE_MARC)
92 else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
94 xmlFreeDoc(r->u.xslt.xsp_doc);
98 wrbuf_rewind(p->wr_error);
103 p->rules_p = &p->rules;
106 yaz_record_conv_t yaz_record_conv_create()
108 yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
109 p->nmem = nmem_create();
110 p->wr_error = wrbuf_alloc();
117 yaz_record_conv_reset(p);
121 void yaz_record_conv_destroy(yaz_record_conv_t p)
125 yaz_record_conv_reset(p);
126 nmem_destroy(p->nmem);
127 wrbuf_destroy(p->wr_error);
133 /** \brief adds a rule */
134 static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
135 enum YAZ_RECORD_CONV_RULE type)
137 struct yaz_record_conv_rule *r = (struct yaz_record_conv_rule *)
138 nmem_malloc(p->nmem, sizeof(*r));
142 p->rules_p = &r->next;
146 /** \brief parse 'xslt' conversion node */
147 static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
150 struct _xmlAttr *attr;
151 const char *stylesheet = 0;
153 for (attr = ptr->properties; attr; attr = attr->next)
155 if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
156 attr->children && attr->children->type == XML_TEXT_NODE)
157 stylesheet = (const char *) attr->children->content;
160 wrbuf_printf(p->wr_error, "Bad attribute '%s'"
161 "Expected stylesheet.", attr->name);
167 wrbuf_printf(p->wr_error, "Element <xslt>: "
168 "attribute 'stylesheet' expected");
174 xsltStylesheetPtr xsp;
176 if (!yaz_filepath_resolve(stylesheet, p->path, 0, fullpath))
178 wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
179 " could not locate stylesheet '%s'",
180 stylesheet, fullpath);
182 wrbuf_printf(p->wr_error, " with path '%s'", p->path);
186 xsp_doc = xmlParseFile(fullpath);
189 wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
190 " xml parse failed: %s", stylesheet, fullpath);
192 wrbuf_printf(p->wr_error, " with path '%s'", p->path);
195 xsp = xsltParseStylesheetDoc(xsp_doc);
198 wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
199 " xslt parse failed: %s", stylesheet, fullpath);
201 wrbuf_printf(p->wr_error, " with path '%s'", p->path);
202 wrbuf_printf(p->wr_error, " ("
207 "EXSLT not supported"
214 struct yaz_record_conv_rule *r =
215 add_rule(p, YAZ_RECORD_CONV_RULE_XSLT);
216 r->u.xslt.xsp_doc = xmlCopyDoc(xsp_doc, 1);
217 xsltFreeStylesheet(xsp); /* will free xsp_doc */
222 wrbuf_printf(p->wr_error, "xslt unsupported."
223 " YAZ compiled without XSLT support");
228 /** \brief parse 'marc' conversion node */
229 static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
231 struct _xmlAttr *attr;
232 const char *input_charset = 0;
233 const char *output_charset = 0;
234 const char *input_format = 0;
235 const char *output_format = 0;
236 int input_format_mode = 0;
237 int output_format_mode = 0;
238 struct yaz_record_conv_rule *r;
240 for (attr = ptr->properties; attr; attr = attr->next)
242 if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
243 attr->children && attr->children->type == XML_TEXT_NODE)
244 input_charset = (const char *) attr->children->content;
245 else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
246 attr->children && attr->children->type == XML_TEXT_NODE)
247 output_charset = (const char *) attr->children->content;
248 else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
249 attr->children && attr->children->type == XML_TEXT_NODE)
250 input_format = (const char *) attr->children->content;
251 else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
252 attr->children && attr->children->type == XML_TEXT_NODE)
253 output_format = (const char *) attr->children->content;
256 wrbuf_printf(p->wr_error, "Element <marc>: expected attributes"
257 "'inputformat', 'inputcharset', 'outputformat' or"
258 " 'outputcharset', got attribute '%s'",
265 wrbuf_printf(p->wr_error, "Element <marc>: "
266 "attribute 'inputformat' required");
269 else if (!strcmp(input_format, "marc"))
271 input_format_mode = YAZ_MARC_ISO2709;
273 else if (!strcmp(input_format, "xml"))
275 input_format_mode = YAZ_MARC_MARCXML;
276 /** Libxml2 generates UTF-8 encoding by default .
277 So we convert from UTF-8 to outputcharset (if defined)
279 if (!input_charset && output_charset)
280 input_charset = "utf-8";
284 wrbuf_printf(p->wr_error, "Element <marc inputformat='%s'>: "
285 " Unsupported input format"
286 " defined by attribute value",
293 wrbuf_printf(p->wr_error,
294 "Element <marc>: attribute 'outputformat' required");
297 else if (!strcmp(output_format, "line"))
299 output_format_mode = YAZ_MARC_LINE;
301 else if (!strcmp(output_format, "marcxml"))
303 output_format_mode = YAZ_MARC_MARCXML;
304 if (input_charset && !output_charset)
305 output_charset = "utf-8";
307 else if (!strcmp(output_format, "marc"))
309 output_format_mode = YAZ_MARC_ISO2709;
311 else if (!strcmp(output_format, "marcxchange"))
313 output_format_mode = YAZ_MARC_XCHANGE;
314 if (input_charset && !output_charset)
315 output_charset = "utf-8";
319 wrbuf_printf(p->wr_error, "Element <marc outputformat='%s'>: "
320 " Unsupported output format"
321 " defined by attribute value",
325 if (input_charset && output_charset)
327 yaz_iconv_t cd = yaz_iconv_open(output_charset, input_charset);
330 wrbuf_printf(p->wr_error,
331 "Element <marc inputcharset='%s' outputcharset='%s'>:"
332 " Unsupported character set mapping"
333 " defined by attribute values",
334 input_charset, output_charset);
339 else if (input_charset)
341 wrbuf_printf(p->wr_error, "Element <marc>: "
342 "attribute 'outputcharset' missing");
345 else if (output_charset)
347 wrbuf_printf(p->wr_error, "Element <marc>: "
348 "attribute 'inputcharset' missing");
351 r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC);
353 r->u.marc.input_charset = nmem_strdup(p->nmem, input_charset);
354 r->u.marc.output_charset = nmem_strdup(p->nmem, output_charset);
355 r->u.marc.input_format = input_format_mode;
356 r->u.marc.output_format = output_format_mode;
360 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
362 yaz_record_conv_reset(p);
364 /* parsing element children */
365 for (ptr = ptr->children; ptr; ptr = ptr->next)
367 if (ptr->type != XML_ELEMENT_NODE)
369 if (!strcmp((const char *) ptr->name, "xslt"))
371 if (conv_xslt(p, ptr))
374 else if (!strcmp((const char *) ptr->name, "marc"))
376 if (conv_marc(p, ptr))
381 wrbuf_printf(p->wr_error, "Element <backend>: expected "
382 "<marc> or <xslt> element, got <%s>"
390 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
391 struct yaz_record_conv_rule *r,
392 const char *input_record_buf,
393 size_t input_record_len,
394 WRBUF output_record);
396 int yaz_record_conv_opac_record(yaz_record_conv_t p,
397 Z_OPACRecord *input_record,
401 struct yaz_record_conv_rule *r = p->rules;
402 if (!r || r->which != YAZ_RECORD_CONV_RULE_MARC)
403 ret = -1; /* no marc rule so we can't do OPAC */
406 WRBUF res = wrbuf_alloc();
407 yaz_marc_t mt = yaz_marc_create();
408 yaz_iconv_t cd = yaz_iconv_open(r->u.marc.output_charset,
409 r->u.marc.input_charset);
411 wrbuf_rewind(p->wr_error);
412 yaz_marc_xml(mt, r->u.marc.output_format);
414 yaz_marc_iconv(mt, cd);
416 yaz_opac_decode_wrbuf(mt, input_record, res);
419 ret = yaz_record_conv_record_rule(p,
421 wrbuf_buf(res), wrbuf_len(res),
424 yaz_marc_destroy(mt);
432 int yaz_record_conv_record(yaz_record_conv_t p,
433 const char *input_record_buf,
434 size_t input_record_len,
437 return yaz_record_conv_record_rule(p, p->rules,
439 input_record_len, output_record);
442 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
443 struct yaz_record_conv_rule *r,
444 const char *input_record_buf,
445 size_t input_record_len,
449 WRBUF record = output_record; /* pointer transfer */
450 wrbuf_rewind(p->wr_error);
452 wrbuf_write(record, input_record_buf, input_record_len);
453 for (; ret == 0 && r; r = r->next)
455 if (r->which == YAZ_RECORD_CONV_RULE_MARC)
458 yaz_iconv_open(r->u.marc.output_charset,
459 r->u.marc.input_charset);
460 yaz_marc_t mt = yaz_marc_create();
462 yaz_marc_xml(mt, r->u.marc.output_format);
465 yaz_marc_iconv(mt, cd);
466 if (r->u.marc.input_format == YAZ_MARC_ISO2709)
468 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
475 else if (r->u.marc.input_format == YAZ_MARC_MARCXML)
477 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
481 wrbuf_printf(p->wr_error, "xmlParseMemory failed");
486 ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
488 wrbuf_printf(p->wr_error, "yaz_marc_read_xml failed");
494 wrbuf_printf(p->wr_error, "unsupported input format");
499 wrbuf_rewind(record);
500 ret = yaz_marc_write_mode(mt, record);
502 wrbuf_printf(p->wr_error, "yaz_marc_write_mode failed");
506 yaz_marc_destroy(mt);
509 else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
511 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
515 wrbuf_printf(p->wr_error, "xmlParseMemory failed");
520 xmlDocPtr xsp_doc = xmlCopyDoc(r->u.xslt.xsp_doc, 1);
521 xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
522 xmlDocPtr res = xsltApplyStylesheet(xsp, doc, 0);
525 xmlChar *out_buf = 0;
528 #if YAZ_HAVE_XSLTSAVERESULTTOSTRING
529 xsltSaveResultToString(&out_buf, &out_len, res, xsp);
531 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
535 wrbuf_printf(p->wr_error,
536 "xsltSaveResultToString failed");
541 wrbuf_rewind(record);
542 wrbuf_write(record, (const char *) out_buf, out_len);
550 wrbuf_printf(p->wr_error, "xsltApplyStylesheet failed");
554 xsltFreeStylesheet(xsp); /* frees xsp_doc too */
562 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
564 return wrbuf_cstr(p->wr_error);
567 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
572 p->path = xstrdup(path);
579 * c-file-style: "Stroustrup"
580 * indent-tabs-mode: nil
582 * vim: shiftwidth=4 tabstop=8 expandtab