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>
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27 #include <libxml/xinclude.h>
28 #include <libxml/xpath.h>
29 #include <libxml/xpathInternals.h>
31 #include <libxslt/xsltutils.h>
32 #include <libxslt/transform.h>
35 #include <libexslt/exslt.h>
38 /** \brief The internal structure for yaz_record_conv_t */
39 struct yaz_record_conv_struct {
40 /** \brief memory for configuration */
43 /** \brief conversion rules (allocated using NMEM) */
44 struct yaz_record_conv_rule *rules;
46 /** \brief pointer to last conversion rule pointer in chain */
47 struct yaz_record_conv_rule **rules_p;
49 /** \brief string buffer for error messages */
52 /** \brief path for opening files */
58 const char *input_charset;
59 const char *output_charset;
60 int input_format_mode;
61 int output_format_mode;
62 const char *leader_spec;
65 /** \brief tranformation info (rule info) */
66 struct yaz_record_conv_rule {
67 struct yaz_record_conv_type *type;
69 struct yaz_record_conv_rule *next;
72 /** \brief reset rules+configuration */
73 static void yaz_record_conv_reset(yaz_record_conv_t p)
76 struct yaz_record_conv_rule *r;
77 for (r = p->rules; r; r = r->next)
79 r->type->destroy(r->info);
81 wrbuf_rewind(p->wr_error);
86 p->rules_p = &p->rules;
89 void yaz_record_conv_destroy(yaz_record_conv_t p)
93 yaz_record_conv_reset(p);
94 nmem_destroy(p->nmem);
95 wrbuf_destroy(p->wr_error);
106 const char **xsl_parms;
109 static void *construct_xslt(const xmlNode *ptr,
110 const char *path, WRBUF wr_error)
112 struct _xmlAttr *attr;
113 const char *stylesheet = 0;
114 struct xslt_info *info = 0;
119 if (strcmp((const char *) ptr->name, "xslt"))
122 for (attr = ptr->properties; attr; attr = attr->next)
124 if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
125 attr->children && attr->children->type == XML_TEXT_NODE)
126 stylesheet = (const char *) attr->children->content;
129 wrbuf_printf(wr_error, "Bad attribute '%s'"
130 "Expected stylesheet.", attr->name);
134 nmem = nmem_create();
135 info = nmem_malloc(nmem, sizeof(*info));
137 info->xsl_parms = nmem_malloc(
138 nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms));
140 for (ptr = ptr->children; ptr; ptr = ptr->next)
142 const char *name = 0;
143 const char *value = 0;
145 if (ptr->type != XML_ELEMENT_NODE)
147 if (strcmp((const char *) ptr->name, "param"))
149 wrbuf_printf(wr_error, "Bad element '%s'"
150 "Expected param.", ptr->name);
154 for (attr = ptr->properties; attr; attr = attr->next)
156 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
157 attr->children && attr->children->type == XML_TEXT_NODE)
158 name = (const char *) attr->children->content;
159 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
160 attr->children && attr->children->type == XML_TEXT_NODE)
161 value = (const char *) attr->children->content;
164 wrbuf_printf(wr_error, "Bad attribute '%s'"
165 "Expected name or value.", attr->name);
172 wrbuf_printf(wr_error, "Missing attributes name or value");
176 if (no_parms >= max_parms)
178 wrbuf_printf(wr_error, "Too many parameters given");
183 qvalue = nmem_malloc(nmem, strlen(value) + 3);
184 strcpy(qvalue, "\'");
185 strcat(qvalue, value);
186 strcat(qvalue, "\'");
188 info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name);
189 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 struct _xmlAttr *attr;
335 NMEM nmem = nmem_create();
336 struct select_info *info = nmem_malloc(nmem, sizeof(*info));
339 info->xpath_expr = 0;
340 for (attr = ptr->properties; attr; attr = attr->next)
342 if (!xmlStrcmp(attr->name, BAD_CAST "path") &&
343 attr->children && attr->children->type == XML_TEXT_NODE)
345 nmem_strdup(nmem, (const char *) attr->children->content);
348 wrbuf_printf(wr_error, "Bad attribute '%s'"
349 "Expected xpath.", attr->name);
358 static int convert_select(void *vinfo, WRBUF record, WRBUF wr_error)
361 struct select_info *info = vinfo;
363 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
367 wrbuf_printf(wr_error, "xmlParseMemory failed");
372 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
373 if (xpathCtx && info->xpath_expr)
375 xmlXPathObjectPtr xpathObj =
376 xmlXPathEvalExpression((const xmlChar *) info->xpath_expr,
380 xmlNodeSetPtr nodes = xpathObj->nodesetval;
381 wrbuf_rewind(record);
385 for (i = 0; i < nodes->nodeNr; i++)
387 xmlNode *ptr = nodes->nodeTab[i];
388 if (ptr->type == XML_ELEMENT_NODE)
390 for (; ptr; ptr = ptr->next)
391 if (ptr->type == XML_TEXT_NODE)
392 wrbuf_puts(record, (const char *) ptr->content);
395 xmlXPathFreeObject(xpathObj);
397 xmlXPathFreeContext(xpathCtx);
404 static void destroy_select(void *vinfo)
406 struct select_info *info = vinfo;
409 nmem_destroy(info->nmem);
413 static void *construct_solrmarc(const xmlNode *ptr,
414 const char *path, WRBUF wr_error)
416 if (strcmp((const char *) ptr->name, "solrmarc"))
418 return wr_error; /* any non-null ptr will do; we don't use it later*/
421 static int convert_solrmarc(void *info, WRBUF record, WRBUF wr_error)
423 WRBUF w = wrbuf_alloc();
424 const char *buf = wrbuf_buf(record);
425 size_t i, sz = wrbuf_len(record);
426 for (i = 0; i < sz; i++)
429 if (buf[i] == '#' && i < sz - 3 && buf[i+3] == ';'
430 && atoi_n_check(buf+i+1, 2, &ch))
436 wrbuf_rewind(record);
437 wrbuf_write(record, wrbuf_buf(w), wrbuf_len(w));
442 static void destroy_solrmarc(void *info)
446 static void *construct_marc(const xmlNode *ptr,
447 const char *path, WRBUF wr_error)
449 NMEM nmem = nmem_create();
450 struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
451 struct _xmlAttr *attr;
452 const char *input_format = 0;
453 const char *output_format = 0;
455 if (strcmp((const char *) ptr->name, "marc"))
461 info->input_charset = 0;
462 info->output_charset = 0;
463 info->input_format_mode = 0;
464 info->output_format_mode = 0;
465 info->leader_spec = 0;
467 for (attr = ptr->properties; attr; attr = attr->next)
469 if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
470 attr->children && attr->children->type == XML_TEXT_NODE)
471 info->input_charset = (const char *) attr->children->content;
472 else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
473 attr->children && attr->children->type == XML_TEXT_NODE)
474 info->output_charset = (const char *) attr->children->content;
475 else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
476 attr->children && attr->children->type == XML_TEXT_NODE)
477 input_format = (const char *) attr->children->content;
478 else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
479 attr->children && attr->children->type == XML_TEXT_NODE)
480 output_format = (const char *) attr->children->content;
481 else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
482 attr->children && attr->children->type == XML_TEXT_NODE)
484 nmem_strdup(info->nmem,(const char *) attr->children->content);
487 wrbuf_printf(wr_error, "Element <marc>: expected attributes"
488 "'inputformat', 'inputcharset', 'outputformat' or"
489 " 'outputcharset', got attribute '%s'",
491 nmem_destroy(info->nmem);
497 wrbuf_printf(wr_error, "Element <marc>: "
498 "attribute 'inputformat' required");
499 nmem_destroy(info->nmem);
502 else if (!strcmp(input_format, "marc"))
504 info->input_format_mode = YAZ_MARC_ISO2709;
506 else if (!strcmp(input_format, "xml"))
508 info->input_format_mode = YAZ_MARC_MARCXML;
509 /** Libxml2 generates UTF-8 encoding by default .
510 So we convert from UTF-8 to outputcharset (if defined)
512 if (!info->input_charset && info->output_charset)
513 info->input_charset = "utf-8";
515 else if (!strcmp(input_format, "json"))
517 info->input_format_mode = YAZ_MARC_JSON;
521 wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
522 " Unsupported input format"
523 " defined by attribute value",
525 nmem_destroy(info->nmem);
531 wrbuf_printf(wr_error,
532 "Element <marc>: attribute 'outputformat' required");
533 nmem_destroy(info->nmem);
536 else if (!strcmp(output_format, "line"))
538 info->output_format_mode = YAZ_MARC_LINE;
540 else if (!strcmp(output_format, "marcxml"))
542 info->output_format_mode = YAZ_MARC_MARCXML;
543 if (info->input_charset && !info->output_charset)
544 info->output_charset = "utf-8";
546 else if (!strcmp(output_format, "turbomarc"))
548 info->output_format_mode = YAZ_MARC_TURBOMARC;
549 if (info->input_charset && !info->output_charset)
550 info->output_charset = "utf-8";
552 else if (!strcmp(output_format, "marc"))
554 info->output_format_mode = YAZ_MARC_ISO2709;
556 else if (!strcmp(output_format, "marcxchange"))
558 info->output_format_mode = YAZ_MARC_XCHANGE;
559 if (info->input_charset && !info->output_charset)
560 info->output_charset = "utf-8";
562 else if (!strcmp(output_format, "json"))
564 info->output_format_mode = YAZ_MARC_JSON;
565 if (info->input_charset && !info->output_charset)
566 info->output_charset = "utf-8";
570 wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
571 " Unsupported output format"
572 " defined by attribute value",
574 nmem_destroy(info->nmem);
577 if (info->input_charset && info->output_charset)
579 yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
580 info->input_charset);
583 wrbuf_printf(wr_error,
584 "Element <marc inputcharset='%s' outputcharset='%s'>:"
585 " Unsupported character set mapping"
586 " defined by attribute values",
587 info->input_charset, info->output_charset);
588 nmem_destroy(info->nmem);
593 else if (!info->output_charset)
595 wrbuf_printf(wr_error, "Element <marc>: "
596 "attribute 'outputcharset' missing");
597 nmem_destroy(info->nmem);
600 else if (!info->input_charset)
602 wrbuf_printf(wr_error, "Element <marc>: "
603 "attribute 'inputcharset' missing");
604 nmem_destroy(info->nmem);
607 info->input_charset = nmem_strdup(info->nmem, info->input_charset);
608 info->output_charset = nmem_strdup(info->nmem, info->output_charset);
612 static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
614 struct marc_info *mi = info;
615 const char *input_charset = mi->input_charset;
617 yaz_marc_t mt = yaz_marc_create();
619 yaz_marc_xml(mt, mi->output_format_mode);
621 yaz_marc_leader_spec(mt, mi->leader_spec);
623 if (mi->input_format_mode == YAZ_MARC_ISO2709)
625 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
629 if (yaz_marc_check_marc21_coding(input_charset, wrbuf_buf(record),
631 input_charset = "utf-8";
637 else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
638 mi->input_format_mode == YAZ_MARC_TURBOMARC)
640 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
644 wrbuf_printf(wr_error, "xmlParseMemory failed");
649 ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
651 wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
657 wrbuf_printf(wr_error, "unsupported input format");
662 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, input_charset);
665 yaz_marc_iconv(mt, cd);
667 wrbuf_rewind(record);
668 ret = yaz_marc_write_mode(mt, record);
670 wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
674 yaz_marc_destroy(mt);
678 static void destroy_marc(void *info)
680 struct marc_info *mi = info;
682 nmem_destroy(mi->nmem);
685 int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr,
686 struct yaz_record_conv_type *types)
688 struct yaz_record_conv_type bt[4];
692 bt[i].construct = construct_marc;
693 bt[i].convert = convert_marc;
694 bt[i++].destroy = destroy_marc;
696 bt[i-1].next = &bt[i];
697 bt[i].construct = construct_solrmarc;
698 bt[i].convert = convert_solrmarc;
699 bt[i++].destroy = destroy_solrmarc;
701 bt[i-1].next = &bt[i];
702 bt[i].construct = construct_select;
703 bt[i].convert = convert_select;
704 bt[i++].destroy = destroy_select;
708 bt[i-1].next = &bt[i];
709 bt[i].construct = construct_xslt;
710 bt[i].convert = convert_xslt;
711 bt[i++].destroy = destroy_xslt;
714 bt[i-1].next = types;
715 yaz_record_conv_reset(p);
717 /* parsing element children */
718 for (ptr = ptr->children; ptr; ptr = ptr->next)
720 struct yaz_record_conv_type *t;
721 struct yaz_record_conv_rule *r;
723 if (ptr->type != XML_ELEMENT_NODE)
725 for (t = &bt[0]; t; t = t->next)
727 wrbuf_rewind(p->wr_error);
728 info = t->construct(ptr, p->path, p->wr_error);
730 if (info || wrbuf_len(p->wr_error))
732 /* info== 0 and no error reported , ie not handled by it */
736 if (wrbuf_len(p->wr_error) == 0)
737 wrbuf_printf(p->wr_error, "Element <backend>: expected "
738 "<marc> or <xslt> element, got <%s>"
742 r = (struct yaz_record_conv_rule *) nmem_malloc(p->nmem, sizeof(*r));
745 r->type = nmem_malloc(p->nmem, sizeof(*t));
746 memcpy(r->type, t, sizeof(*t));
748 p->rules_p = &r->next;
753 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
755 return yaz_record_conv_configure_t(p, ptr, 0);
758 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
759 struct yaz_record_conv_rule *r,
760 const char *input_record_buf,
761 size_t input_record_len,
765 WRBUF record = output_record; /* pointer transfer */
766 wrbuf_rewind(p->wr_error);
768 wrbuf_write(record, input_record_buf, input_record_len);
769 for (; ret == 0 && r; r = r->next)
770 ret = r->type->convert(r->info, record, p->wr_error);
774 int yaz_record_conv_opac_record(yaz_record_conv_t p,
775 Z_OPACRecord *input_record,
779 struct yaz_record_conv_rule *r = p->rules;
780 if (!r || r->type->construct != construct_marc)
782 wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
783 ret = -1; /* no marc rule so we can't do OPAC */
787 struct marc_info *mi = r->info;
788 const char *input_charset = mi->input_charset;
791 WRBUF res = wrbuf_alloc();
792 yaz_marc_t mt = yaz_marc_create();
794 if (yaz_opac_check_marc21_coding(input_charset, input_record))
795 input_charset = "utf-8";
796 cd = yaz_iconv_open(mi->output_charset, input_charset);
798 wrbuf_rewind(p->wr_error);
799 yaz_marc_xml(mt, mi->output_format_mode);
801 yaz_marc_iconv(mt, cd);
803 yaz_opac_decode_wrbuf(mt, input_record, res);
806 ret = yaz_record_conv_record_rule(p,
808 wrbuf_buf(res), wrbuf_len(res),
811 yaz_marc_destroy(mt);
819 int yaz_record_conv_record(yaz_record_conv_t p,
820 const char *input_record_buf,
821 size_t input_record_len,
824 return yaz_record_conv_record_rule(p, p->rules,
826 input_record_len, output_record);
829 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
831 return wrbuf_cstr(p->wr_error);
834 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
839 p->path = xstrdup(path);
842 yaz_record_conv_t yaz_record_conv_create()
844 yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
845 p->nmem = nmem_create();
846 p->wr_error = wrbuf_alloc();
858 * c-file-style: "Stroustrup"
859 * indent-tabs-mode: nil
861 * vim: shiftwidth=4 tabstop=8 expandtab