1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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>
23 #include <yaz/xml_get.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxml/xinclude.h>
29 #include <libxml/xpath.h>
30 #include <libxml/xpathInternals.h>
32 #include <libxslt/xsltutils.h>
33 #include <libxslt/transform.h>
36 #include <libexslt/exslt.h>
39 /** \brief The internal structure for yaz_record_conv_t */
40 struct yaz_record_conv_struct {
41 /** \brief memory for configuration */
44 /** \brief conversion rules (allocated using NMEM) */
45 struct yaz_record_conv_rule *rules;
47 /** \brief pointer to last conversion rule pointer in chain */
48 struct yaz_record_conv_rule **rules_p;
50 /** \brief string buffer for error messages */
53 /** \brief path for opening files */
59 const char *input_charset;
60 const char *output_charset;
61 int input_format_mode;
62 int output_format_mode;
63 const char *leader_spec;
66 /** \brief tranformation info (rule info) */
67 struct yaz_record_conv_rule {
68 struct yaz_record_conv_type *type;
70 struct yaz_record_conv_rule *next;
73 /** \brief reset rules+configuration */
74 static void yaz_record_conv_reset(yaz_record_conv_t p)
77 struct yaz_record_conv_rule *r;
78 for (r = p->rules; r; r = r->next)
80 r->type->destroy(r->info);
82 wrbuf_rewind(p->wr_error);
87 p->rules_p = &p->rules;
90 void yaz_record_conv_destroy(yaz_record_conv_t p)
94 yaz_record_conv_reset(p);
95 nmem_destroy(p->nmem);
96 wrbuf_destroy(p->wr_error);
107 const char **xsl_parms;
110 static void *construct_xslt(const xmlNode *ptr,
111 const char *path, WRBUF wr_error)
113 struct _xmlAttr *attr;
114 const char *stylesheet = 0;
115 struct xslt_info *info = 0;
120 if (strcmp((const char *) ptr->name, "xslt"))
123 for (attr = ptr->properties; attr; attr = attr->next)
125 if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
126 attr->children && attr->children->type == XML_TEXT_NODE)
127 stylesheet = (const char *) attr->children->content;
130 wrbuf_printf(wr_error, "Bad attribute '%s'"
131 "Expected stylesheet.", attr->name);
135 nmem = nmem_create();
136 info = nmem_malloc(nmem, sizeof(*info));
138 info->xsl_parms = nmem_malloc(
139 nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms));
141 for (ptr = ptr->children; ptr; ptr = ptr->next)
143 const char *name = 0;
144 const char *value = 0;
146 if (ptr->type != XML_ELEMENT_NODE)
148 if (strcmp((const char *) ptr->name, "param"))
150 wrbuf_printf(wr_error, "Bad element '%s'"
151 "Expected param.", ptr->name);
155 for (attr = ptr->properties; attr; attr = attr->next)
157 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
158 attr->children && attr->children->type == XML_TEXT_NODE)
159 name = (const char *) attr->children->content;
160 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
161 attr->children && attr->children->type == XML_TEXT_NODE)
162 value = (const char *) attr->children->content;
165 wrbuf_printf(wr_error, "Bad attribute '%s'"
166 "Expected name or value.", attr->name);
173 wrbuf_printf(wr_error, "Missing attributes name or value");
177 if (no_parms >= max_parms)
179 wrbuf_printf(wr_error, "Too many parameters given");
184 qvalue = nmem_malloc(nmem, strlen(value) + 3);
185 strcpy(qvalue, "\'");
186 strcat(qvalue, value);
187 strcat(qvalue, "\'");
189 info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name);
190 info->xsl_parms[2 * no_parms + 1] = qvalue;
193 info->xsl_parms[2 * no_parms] = 0;
197 wrbuf_printf(wr_error, "Element <xslt>: "
198 "attribute 'stylesheet' expected");
204 xsltStylesheetPtr xsp;
205 if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath))
207 wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:"
208 " could not locate stylesheet '%s'",
209 stylesheet, stylesheet);
211 wrbuf_printf(wr_error, " with path '%s'", path);
216 info->xsp_doc = xmlParseFile(fullpath);
219 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
220 " xml parse failed: %s", stylesheet, fullpath);
222 wrbuf_printf(wr_error, " with path '%s'", path);
226 /* need to copy this before passing it to the processor. It will
227 be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
228 xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1));
231 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
232 " xslt parse failed: %s", stylesheet, fullpath);
234 wrbuf_printf(wr_error, " with path '%s'", path);
235 wrbuf_printf(wr_error, " ("
240 "EXSLT not supported"
243 xmlFreeDoc(info->xsp_doc);
244 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);
327 static void *construct_select(const xmlNode *ptr,
328 const char *path, WRBUF wr_error)
330 if (strcmp((const char *) ptr->name, "select"))
334 NMEM nmem = nmem_create();
335 struct select_info *info = nmem_malloc(nmem, sizeof(*info));
336 const char *attr_str;
337 const char *xpath = 0;
340 info->xpath_expr = 0;
341 attr_str = yaz_xml_get_prop(ptr, "path%s", &xpath);
344 wrbuf_printf(wr_error, "Bad attribute '%s'"
345 "Expected xpath.", attr_str);
350 info->xpath_expr = nmem_strdup(nmem, xpath);
355 static int convert_select(void *vinfo, WRBUF record, WRBUF wr_error)
358 struct select_info *info = vinfo;
360 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
364 wrbuf_printf(wr_error, "xmlParseMemory failed");
369 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
370 if (xpathCtx && info->xpath_expr)
372 xmlXPathObjectPtr xpathObj =
373 xmlXPathEvalExpression((const xmlChar *) info->xpath_expr,
377 xmlNodeSetPtr nodes = xpathObj->nodesetval;
381 if (nodes->nodeNr > 0)
382 wrbuf_rewind(record);
383 for (i = 0; i < nodes->nodeNr; i++)
385 xmlNode *ptr = nodes->nodeTab[i];
386 if (ptr->type == XML_ELEMENT_NODE)
388 for (; ptr; ptr = ptr->next)
389 if (ptr->type == XML_TEXT_NODE)
390 wrbuf_puts(record, (const char *) ptr->content);
393 xmlXPathFreeObject(xpathObj);
395 xmlXPathFreeContext(xpathCtx);
402 static void destroy_select(void *vinfo)
404 struct select_info *info = vinfo;
407 nmem_destroy(info->nmem);
411 static void *construct_solrmarc(const xmlNode *ptr,
412 const char *path, WRBUF wr_error)
414 if (strcmp((const char *) ptr->name, "solrmarc"))
416 return wr_error; /* any non-null ptr will do; we don't use it later*/
419 static int convert_solrmarc(void *info, WRBUF record, WRBUF wr_error)
421 WRBUF w = wrbuf_alloc();
422 const char *buf = wrbuf_buf(record);
423 size_t i, sz = wrbuf_len(record);
424 for (i = 0; i < sz; i++)
427 if (buf[i] == '#' && i < sz - 3 && buf[i+3] == ';'
428 && atoi_n_check(buf+i+1, 2, &ch))
434 wrbuf_rewind(record);
435 wrbuf_write(record, wrbuf_buf(w), wrbuf_len(w));
440 static void destroy_solrmarc(void *info)
444 static void *construct_marc(const xmlNode *ptr,
445 const char *path, WRBUF wr_error)
447 NMEM nmem = nmem_create();
448 struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
449 struct _xmlAttr *attr;
450 const char *input_format = 0;
451 const char *output_format = 0;
453 if (strcmp((const char *) ptr->name, "marc"))
459 info->input_charset = 0;
460 info->output_charset = 0;
461 info->input_format_mode = 0;
462 info->output_format_mode = 0;
463 info->leader_spec = 0;
465 for (attr = ptr->properties; attr; attr = attr->next)
467 if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
468 attr->children && attr->children->type == XML_TEXT_NODE)
469 info->input_charset = (const char *) attr->children->content;
470 else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
471 attr->children && attr->children->type == XML_TEXT_NODE)
472 info->output_charset = (const char *) attr->children->content;
473 else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
474 attr->children && attr->children->type == XML_TEXT_NODE)
475 input_format = (const char *) attr->children->content;
476 else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
477 attr->children && attr->children->type == XML_TEXT_NODE)
478 output_format = (const char *) attr->children->content;
479 else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
480 attr->children && attr->children->type == XML_TEXT_NODE)
482 nmem_strdup(info->nmem,(const char *) attr->children->content);
485 wrbuf_printf(wr_error, "Element <marc>: expected attributes"
486 "'inputformat', 'inputcharset', 'outputformat' or"
487 " 'outputcharset', got attribute '%s'",
489 nmem_destroy(info->nmem);
495 wrbuf_printf(wr_error, "Element <marc>: "
496 "attribute 'inputformat' required");
497 nmem_destroy(info->nmem);
500 else if (!strcmp(input_format, "marc"))
502 info->input_format_mode = YAZ_MARC_ISO2709;
504 else if (!strcmp(input_format, "xml"))
506 info->input_format_mode = YAZ_MARC_MARCXML;
507 /** Libxml2 generates UTF-8 encoding by default .
508 So we convert from UTF-8 to outputcharset (if defined)
510 if (!info->input_charset && info->output_charset)
511 info->input_charset = "utf-8";
513 else if (!strcmp(input_format, "json"))
515 info->input_format_mode = YAZ_MARC_JSON;
519 wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
520 " Unsupported input format"
521 " defined by attribute value",
523 nmem_destroy(info->nmem);
529 wrbuf_printf(wr_error,
530 "Element <marc>: attribute 'outputformat' required");
531 nmem_destroy(info->nmem);
534 else if (!strcmp(output_format, "line"))
536 info->output_format_mode = YAZ_MARC_LINE;
538 else if (!strcmp(output_format, "marcxml"))
540 info->output_format_mode = YAZ_MARC_MARCXML;
541 if (info->input_charset && !info->output_charset)
542 info->output_charset = "utf-8";
544 else if (!strcmp(output_format, "turbomarc"))
546 info->output_format_mode = YAZ_MARC_TURBOMARC;
547 if (info->input_charset && !info->output_charset)
548 info->output_charset = "utf-8";
550 else if (!strcmp(output_format, "marc"))
552 info->output_format_mode = YAZ_MARC_ISO2709;
554 else if (!strcmp(output_format, "marcxchange"))
556 info->output_format_mode = YAZ_MARC_XCHANGE;
557 if (info->input_charset && !info->output_charset)
558 info->output_charset = "utf-8";
560 else if (!strcmp(output_format, "json"))
562 info->output_format_mode = YAZ_MARC_JSON;
563 if (info->input_charset && !info->output_charset)
564 info->output_charset = "utf-8";
568 wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
569 " Unsupported output format"
570 " defined by attribute value",
572 nmem_destroy(info->nmem);
575 if (info->input_charset && info->output_charset)
577 yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
578 info->input_charset);
581 wrbuf_printf(wr_error,
582 "Element <marc inputcharset='%s' outputcharset='%s'>:"
583 " Unsupported character set mapping"
584 " defined by attribute values",
585 info->input_charset, info->output_charset);
586 nmem_destroy(info->nmem);
591 else if (!info->output_charset)
593 wrbuf_printf(wr_error, "Element <marc>: "
594 "attribute 'outputcharset' missing");
595 nmem_destroy(info->nmem);
598 else if (!info->input_charset)
600 wrbuf_printf(wr_error, "Element <marc>: "
601 "attribute 'inputcharset' missing");
602 nmem_destroy(info->nmem);
605 info->input_charset = nmem_strdup(info->nmem, info->input_charset);
606 info->output_charset = nmem_strdup(info->nmem, info->output_charset);
610 static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
612 struct marc_info *mi = info;
613 const char *input_charset = mi->input_charset;
615 yaz_marc_t mt = yaz_marc_create();
617 yaz_marc_xml(mt, mi->output_format_mode);
619 yaz_marc_leader_spec(mt, mi->leader_spec);
621 if (mi->input_format_mode == YAZ_MARC_ISO2709)
623 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
627 if (yaz_marc_check_marc21_coding(input_charset, wrbuf_buf(record),
629 input_charset = "utf-8";
635 else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
636 mi->input_format_mode == YAZ_MARC_TURBOMARC)
638 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
642 wrbuf_printf(wr_error, "xmlParseMemory failed");
647 ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
649 wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
655 wrbuf_printf(wr_error, "unsupported input format");
660 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, input_charset);
663 yaz_marc_iconv(mt, cd);
665 wrbuf_rewind(record);
666 ret = yaz_marc_write_mode(mt, record);
668 wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
672 yaz_marc_destroy(mt);
676 static void destroy_marc(void *info)
678 struct marc_info *mi = info;
680 nmem_destroy(mi->nmem);
683 int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr,
684 struct yaz_record_conv_type *types)
686 struct yaz_record_conv_type bt[4];
690 bt[i].construct = construct_marc;
691 bt[i].convert = convert_marc;
692 bt[i++].destroy = destroy_marc;
694 bt[i-1].next = &bt[i];
695 bt[i].construct = construct_solrmarc;
696 bt[i].convert = convert_solrmarc;
697 bt[i++].destroy = destroy_solrmarc;
699 bt[i-1].next = &bt[i];
700 bt[i].construct = construct_select;
701 bt[i].convert = convert_select;
702 bt[i++].destroy = destroy_select;
706 bt[i-1].next = &bt[i];
707 bt[i].construct = construct_xslt;
708 bt[i].convert = convert_xslt;
709 bt[i++].destroy = destroy_xslt;
712 bt[i-1].next = types;
713 yaz_record_conv_reset(p);
715 /* parsing element children */
716 for (ptr = ptr->children; ptr; ptr = ptr->next)
718 struct yaz_record_conv_type *t;
719 struct yaz_record_conv_rule *r;
721 if (ptr->type != XML_ELEMENT_NODE)
723 for (t = &bt[0]; t; t = t->next)
725 wrbuf_rewind(p->wr_error);
726 info = t->construct(ptr, p->path, p->wr_error);
728 if (info || wrbuf_len(p->wr_error))
730 /* info== 0 and no error reported , ie not handled by it */
734 if (wrbuf_len(p->wr_error) == 0)
735 wrbuf_printf(p->wr_error, "Element <backend>: expected "
736 "<marc> or <xslt> element, got <%s>"
740 r = (struct yaz_record_conv_rule *) nmem_malloc(p->nmem, sizeof(*r));
743 r->type = nmem_malloc(p->nmem, sizeof(*t));
744 memcpy(r->type, t, sizeof(*t));
746 p->rules_p = &r->next;
751 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
753 return yaz_record_conv_configure_t(p, ptr, 0);
756 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
757 struct yaz_record_conv_rule *r,
758 const char *input_record_buf,
759 size_t input_record_len,
763 WRBUF record = output_record; /* pointer transfer */
764 wrbuf_rewind(p->wr_error);
766 wrbuf_write(record, input_record_buf, input_record_len);
767 for (; ret == 0 && r; r = r->next)
768 ret = r->type->convert(r->info, record, p->wr_error);
772 int yaz_record_conv_opac_record(yaz_record_conv_t p,
773 Z_OPACRecord *input_record,
777 struct yaz_record_conv_rule *r = p->rules;
778 if (!r || r->type->construct != construct_marc)
780 wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
781 ret = -1; /* no marc rule so we can't do OPAC */
785 struct marc_info *mi = r->info;
786 const char *input_charset = mi->input_charset;
789 WRBUF res = wrbuf_alloc();
790 yaz_marc_t mt = yaz_marc_create();
792 if (yaz_opac_check_marc21_coding(input_charset, input_record))
793 input_charset = "utf-8";
794 cd = yaz_iconv_open(mi->output_charset, input_charset);
796 wrbuf_rewind(p->wr_error);
797 yaz_marc_xml(mt, mi->output_format_mode);
799 yaz_marc_iconv(mt, cd);
801 yaz_opac_decode_wrbuf(mt, input_record, res);
804 ret = yaz_record_conv_record_rule(p,
806 wrbuf_buf(res), wrbuf_len(res),
809 yaz_marc_destroy(mt);
817 int yaz_record_conv_record(yaz_record_conv_t p,
818 const char *input_record_buf,
819 size_t input_record_len,
822 return yaz_record_conv_record_rule(p, p->rules,
824 input_record_len, output_record);
827 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
829 return wrbuf_cstr(p->wr_error);
832 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
837 p->path = xstrdup(path);
840 yaz_record_conv_t yaz_record_conv_create()
842 yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
843 p->nmem = nmem_create();
844 p->wr_error = wrbuf_alloc();
856 * c-file-style: "Stroustrup"
857 * indent-tabs-mode: nil
859 * vim: shiftwidth=4 tabstop=8 expandtab