1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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 */
56 const char *input_charset;
57 const char *output_charset;
58 int input_format_mode;
59 int output_format_mode;
60 const char *leader_spec;
63 /** \brief tranformation info (rule info) */
64 struct yaz_record_conv_rule {
65 struct yaz_record_conv_type *type;
67 struct yaz_record_conv_rule *next;
70 /** \brief reset rules+configuration */
71 static void yaz_record_conv_reset(yaz_record_conv_t p)
74 struct yaz_record_conv_rule *r;
75 for (r = p->rules; r; r = r->next)
77 r->type->destroy(r->info);
79 wrbuf_rewind(p->wr_error);
84 p->rules_p = &p->rules;
87 void yaz_record_conv_destroy(yaz_record_conv_t p)
91 yaz_record_conv_reset(p);
92 nmem_destroy(p->nmem);
93 wrbuf_destroy(p->wr_error);
104 const char **xsl_parms;
107 static void *construct_xslt(const xmlNode *ptr,
108 const char *path, WRBUF wr_error)
110 struct _xmlAttr *attr;
111 const char *stylesheet = 0;
112 struct xslt_info *info = 0;
117 if (strcmp((const char *) ptr->name, "xslt"))
120 for (attr = ptr->properties; attr; attr = attr->next)
122 if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
123 attr->children && attr->children->type == XML_TEXT_NODE)
124 stylesheet = (const char *) attr->children->content;
127 wrbuf_printf(wr_error, "Bad attribute '%s'"
128 "Expected stylesheet.", attr->name);
132 nmem = nmem_create();
133 info = nmem_malloc(nmem, sizeof(*info));
135 info->xsl_parms = nmem_malloc(
136 nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms));
138 for (ptr = ptr->children; ptr; ptr = ptr->next)
140 const char *name = 0;
141 const char *value = 0;
143 if (ptr->type != XML_ELEMENT_NODE)
145 if (strcmp((const char *) ptr->name, "param"))
147 wrbuf_printf(wr_error, "Bad element '%s'"
148 "Expected param.", ptr->name);
152 for (attr = ptr->properties; attr; attr = attr->next)
154 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
155 attr->children && attr->children->type == XML_TEXT_NODE)
156 name = (const char *) attr->children->content;
157 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
158 attr->children && attr->children->type == XML_TEXT_NODE)
159 value = (const char *) attr->children->content;
162 wrbuf_printf(wr_error, "Bad attribute '%s'"
163 "Expected name or value.", attr->name);
170 wrbuf_printf(wr_error, "Missing attributes name or value");
174 if (no_parms >= max_parms)
176 wrbuf_printf(wr_error, "Too many parameters given");
181 qvalue = nmem_malloc(nmem, strlen(value) + 3);
182 strcpy(qvalue, "\'");
183 strcat(qvalue, value);
184 strcat(qvalue, "\'");
186 info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name);
187 info->xsl_parms[2 * no_parms + 1] = qvalue;
191 info->xsl_parms[2 * no_parms] = '\0';
195 wrbuf_printf(wr_error, "Element <xslt>: "
196 "attribute 'stylesheet' expected");
203 xsltStylesheetPtr xsp;
204 if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath))
206 wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:"
207 " could not locate stylesheet '%s'",
208 stylesheet, stylesheet);
210 wrbuf_printf(wr_error, " with path '%s'", path);
215 info->xsp_doc = xmlParseFile(fullpath);
218 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
219 " xml parse failed: %s", stylesheet, fullpath);
221 wrbuf_printf(wr_error, " with path '%s'", path);
225 /* need to copy this before passing it to the processor. It will
226 be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
227 xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1));
230 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
231 " xslt parse failed: %s", stylesheet, fullpath);
233 wrbuf_printf(wr_error, " with path '%s'", path);
234 wrbuf_printf(wr_error, " ("
239 "EXSLT not supported"
242 xmlFreeDoc(info->xsp_doc);
243 nmem_destroy(info->nmem);
248 xsltFreeStylesheet(xsp);
255 static int convert_xslt(void *vinfo, WRBUF record, WRBUF wr_error)
258 struct xslt_info *info = vinfo;
260 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
264 wrbuf_printf(wr_error, "xmlParseMemory failed");
269 xmlDocPtr xsp_doc = xmlCopyDoc(info->xsp_doc, 1);
270 xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
271 xmlDocPtr res = xsltApplyStylesheet(xsp, doc, info->xsl_parms);
274 xmlChar *out_buf = 0;
277 #if HAVE_XSLTSAVERESULTTOSTRING
278 xsltSaveResultToString(&out_buf, &out_len, res, xsp);
280 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
284 wrbuf_printf(wr_error,
285 "xsltSaveResultToString failed");
290 wrbuf_rewind(record);
291 wrbuf_write(record, (const char *) out_buf, out_len);
299 wrbuf_printf(wr_error, "xsltApplyStylesheet failed");
303 xsltFreeStylesheet(xsp); /* frees xsp_doc too */
308 static void destroy_xslt(void *vinfo)
310 struct xslt_info *info = vinfo;
314 xmlFreeDoc(info->xsp_doc);
315 nmem_destroy(info->nmem);
323 static void *construct_marc(const xmlNode *ptr,
324 const char *path, WRBUF wr_error)
326 NMEM nmem = nmem_create();
327 struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
328 struct _xmlAttr *attr;
329 const char *input_format = 0;
330 const char *output_format = 0;
332 if (strcmp((const char *) ptr->name, "marc"))
339 info->input_charset = 0;
340 info->output_charset = 0;
341 info->input_format_mode = 0;
342 info->output_format_mode = 0;
343 info->leader_spec = 0;
345 for (attr = ptr->properties; attr; attr = attr->next)
347 if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
348 attr->children && attr->children->type == XML_TEXT_NODE)
349 info->input_charset = (const char *) attr->children->content;
350 else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
351 attr->children && attr->children->type == XML_TEXT_NODE)
352 info->output_charset = (const char *) attr->children->content;
353 else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
354 attr->children && attr->children->type == XML_TEXT_NODE)
355 input_format = (const char *) attr->children->content;
356 else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
357 attr->children && attr->children->type == XML_TEXT_NODE)
358 output_format = (const char *) attr->children->content;
359 else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
360 attr->children && attr->children->type == XML_TEXT_NODE)
362 nmem_strdup(info->nmem,(const char *) attr->children->content);
365 wrbuf_printf(wr_error, "Element <marc>: expected attributes"
366 "'inputformat', 'inputcharset', 'outputformat' or"
367 " 'outputcharset', got attribute '%s'",
369 nmem_destroy(info->nmem);
375 wrbuf_printf(wr_error, "Element <marc>: "
376 "attribute 'inputformat' required");
377 nmem_destroy(info->nmem);
380 else if (!strcmp(input_format, "marc"))
382 info->input_format_mode = YAZ_MARC_ISO2709;
384 else if (!strcmp(input_format, "xml"))
386 info->input_format_mode = YAZ_MARC_MARCXML;
387 /** Libxml2 generates UTF-8 encoding by default .
388 So we convert from UTF-8 to outputcharset (if defined)
390 if (!info->input_charset && info->output_charset)
391 info->input_charset = "utf-8";
395 wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
396 " Unsupported input format"
397 " defined by attribute value",
399 nmem_destroy(info->nmem);
405 wrbuf_printf(wr_error,
406 "Element <marc>: attribute 'outputformat' required");
407 nmem_destroy(info->nmem);
410 else if (!strcmp(output_format, "line"))
412 info->output_format_mode = YAZ_MARC_LINE;
414 else if (!strcmp(output_format, "marcxml"))
416 info->output_format_mode = YAZ_MARC_MARCXML;
417 if (info->input_charset && !info->output_charset)
418 info->output_charset = "utf-8";
420 else if (!strcmp(output_format, "turbomarc"))
422 info->output_format_mode = YAZ_MARC_TURBOMARC;
423 if (info->input_charset && !info->output_charset)
424 info->output_charset = "utf-8";
426 else if (!strcmp(output_format, "marc"))
428 info->output_format_mode = YAZ_MARC_ISO2709;
430 else if (!strcmp(output_format, "marcxchange"))
432 info->output_format_mode = YAZ_MARC_XCHANGE;
433 if (info->input_charset && !info->output_charset)
434 info->output_charset = "utf-8";
438 wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
439 " Unsupported output format"
440 " defined by attribute value",
442 nmem_destroy(info->nmem);
445 if (info->input_charset && info->output_charset)
447 yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
448 info->input_charset);
451 wrbuf_printf(wr_error,
452 "Element <marc inputcharset='%s' outputcharset='%s'>:"
453 " Unsupported character set mapping"
454 " defined by attribute values",
455 info->input_charset, info->output_charset);
456 nmem_destroy(info->nmem);
461 else if (!info->output_charset)
463 wrbuf_printf(wr_error, "Element <marc>: "
464 "attribute 'outputcharset' missing");
465 nmem_destroy(info->nmem);
468 else if (!info->input_charset)
470 wrbuf_printf(wr_error, "Element <marc>: "
471 "attribute 'inputcharset' missing");
472 nmem_destroy(info->nmem);
475 info->input_charset = nmem_strdup(info->nmem, info->input_charset);
476 info->output_charset = nmem_strdup(info->nmem, info->output_charset);
480 static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
482 struct marc_info *mi = info;
485 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, mi->input_charset);
486 yaz_marc_t mt = yaz_marc_create();
488 yaz_marc_xml(mt, mi->output_format_mode);
490 yaz_marc_leader_spec(mt, mi->leader_spec);
493 yaz_marc_iconv(mt, cd);
494 if (mi->input_format_mode == YAZ_MARC_ISO2709)
496 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
503 else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
504 mi->input_format_mode == YAZ_MARC_TURBOMARC)
506 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
510 wrbuf_printf(wr_error, "xmlParseMemory failed");
515 ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
517 wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
523 wrbuf_printf(wr_error, "unsupported input format");
528 wrbuf_rewind(record);
529 ret = yaz_marc_write_mode(mt, record);
531 wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
535 yaz_marc_destroy(mt);
539 static void destroy_marc(void *info)
541 struct marc_info *mi = info;
543 nmem_destroy(mi->nmem);
546 int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr,
547 struct yaz_record_conv_type *types)
549 struct yaz_record_conv_type bt[2];
552 bt[0].construct = construct_marc;
553 bt[0].convert = convert_marc;
554 bt[0].destroy = destroy_marc;
560 bt[1].construct = construct_xslt;
561 bt[1].convert = convert_xslt;
562 bt[1].destroy = destroy_xslt;
567 yaz_record_conv_reset(p);
569 /* parsing element children */
570 for (ptr = ptr->children; ptr; ptr = ptr->next)
572 struct yaz_record_conv_type *t;
573 struct yaz_record_conv_rule *r;
575 if (ptr->type != XML_ELEMENT_NODE)
577 for (t = &bt[0]; t; t = t->next)
579 wrbuf_rewind(p->wr_error);
580 info = t->construct(ptr, p->path, p->wr_error);
582 if (info || wrbuf_len(p->wr_error))
584 /* info== 0 and no error reported , ie not handled by it */
588 if (wrbuf_len(p->wr_error) == 0)
589 wrbuf_printf(p->wr_error, "Element <backend>: expected "
590 "<marc> or <xslt> element, got <%s>"
594 r = (struct yaz_record_conv_rule *) nmem_malloc(p->nmem, sizeof(*r));
597 r->type = nmem_malloc(p->nmem, sizeof(*t));
598 memcpy(r->type, t, sizeof(*t));
600 p->rules_p = &r->next;
605 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
607 return yaz_record_conv_configure_t(p, ptr, 0);
610 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
611 struct yaz_record_conv_rule *r,
612 const char *input_record_buf,
613 size_t input_record_len,
617 WRBUF record = output_record; /* pointer transfer */
618 wrbuf_rewind(p->wr_error);
620 wrbuf_write(record, input_record_buf, input_record_len);
621 for (; ret == 0 && r; r = r->next)
622 ret = r->type->convert(r->info, record, p->wr_error);
626 int yaz_record_conv_opac_record(yaz_record_conv_t p,
627 Z_OPACRecord *input_record,
631 struct yaz_record_conv_rule *r = p->rules;
632 if (!r || r->type->construct != construct_marc)
634 wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
635 ret = -1; /* no marc rule so we can't do OPAC */
639 struct marc_info *mi = r->info;
641 WRBUF res = wrbuf_alloc();
642 yaz_marc_t mt = yaz_marc_create();
643 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset,
646 wrbuf_rewind(p->wr_error);
647 yaz_marc_xml(mt, mi->output_format_mode);
649 yaz_marc_iconv(mt, cd);
651 yaz_opac_decode_wrbuf(mt, input_record, res);
654 ret = yaz_record_conv_record_rule(p,
656 wrbuf_buf(res), wrbuf_len(res),
659 yaz_marc_destroy(mt);
667 int yaz_record_conv_record(yaz_record_conv_t p,
668 const char *input_record_buf,
669 size_t input_record_len,
672 return yaz_record_conv_record_rule(p, p->rules,
674 input_record_len, output_record);
677 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
679 return wrbuf_cstr(p->wr_error);
682 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
687 p->path = xstrdup(path);
690 yaz_record_conv_t yaz_record_conv_create()
692 yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
693 p->nmem = nmem_create();
694 p->wr_error = wrbuf_alloc();
709 * c-file-style: "Stroustrup"
710 * indent-tabs-mode: nil
712 * vim: shiftwidth=4 tabstop=8 expandtab