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>
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");
202 xsltStylesheetPtr xsp;
203 if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath))
205 wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:"
206 " could not locate stylesheet '%s'",
207 stylesheet, stylesheet);
209 wrbuf_printf(wr_error, " with path '%s'", path);
214 info->xsp_doc = xmlParseFile(fullpath);
217 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
218 " xml parse failed: %s", stylesheet, fullpath);
220 wrbuf_printf(wr_error, " with path '%s'", path);
224 /* need to copy this before passing it to the processor. It will
225 be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
226 xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1));
229 wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
230 " xslt parse failed: %s", stylesheet, fullpath);
232 wrbuf_printf(wr_error, " with path '%s'", path);
233 wrbuf_printf(wr_error, " ("
238 "EXSLT not supported"
241 xmlFreeDoc(info->xsp_doc);
242 nmem_destroy(info->nmem);
246 xsltFreeStylesheet(xsp);
253 static int convert_xslt(void *vinfo, WRBUF record, WRBUF wr_error)
256 struct xslt_info *info = vinfo;
258 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
262 wrbuf_printf(wr_error, "xmlParseMemory failed");
267 xmlDocPtr xsp_doc = xmlCopyDoc(info->xsp_doc, 1);
268 xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
269 xmlDocPtr res = xsltApplyStylesheet(xsp, doc, info->xsl_parms);
272 xmlChar *out_buf = 0;
275 #if HAVE_XSLTSAVERESULTTOSTRING
276 xsltSaveResultToString(&out_buf, &out_len, res, xsp);
278 xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
282 wrbuf_printf(wr_error,
283 "xsltSaveResultToString failed");
288 wrbuf_rewind(record);
289 wrbuf_write(record, (const char *) out_buf, out_len);
297 wrbuf_printf(wr_error, "xsltApplyStylesheet failed");
301 xsltFreeStylesheet(xsp); /* frees xsp_doc too */
306 static void destroy_xslt(void *vinfo)
308 struct xslt_info *info = vinfo;
312 xmlFreeDoc(info->xsp_doc);
313 nmem_destroy(info->nmem);
321 static void *construct_marc(const xmlNode *ptr,
322 const char *path, WRBUF wr_error)
324 NMEM nmem = nmem_create();
325 struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
326 struct _xmlAttr *attr;
327 const char *input_format = 0;
328 const char *output_format = 0;
330 if (strcmp((const char *) ptr->name, "marc"))
337 info->input_charset = 0;
338 info->output_charset = 0;
339 info->input_format_mode = 0;
340 info->output_format_mode = 0;
341 info->leader_spec = 0;
343 for (attr = ptr->properties; attr; attr = attr->next)
345 if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
346 attr->children && attr->children->type == XML_TEXT_NODE)
347 info->input_charset = (const char *) attr->children->content;
348 else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
349 attr->children && attr->children->type == XML_TEXT_NODE)
350 info->output_charset = (const char *) attr->children->content;
351 else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
352 attr->children && attr->children->type == XML_TEXT_NODE)
353 input_format = (const char *) attr->children->content;
354 else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
355 attr->children && attr->children->type == XML_TEXT_NODE)
356 output_format = (const char *) attr->children->content;
357 else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
358 attr->children && attr->children->type == XML_TEXT_NODE)
360 nmem_strdup(info->nmem,(const char *) attr->children->content);
363 wrbuf_printf(wr_error, "Element <marc>: expected attributes"
364 "'inputformat', 'inputcharset', 'outputformat' or"
365 " 'outputcharset', got attribute '%s'",
367 nmem_destroy(info->nmem);
373 wrbuf_printf(wr_error, "Element <marc>: "
374 "attribute 'inputformat' required");
375 nmem_destroy(info->nmem);
378 else if (!strcmp(input_format, "marc"))
380 info->input_format_mode = YAZ_MARC_ISO2709;
382 else if (!strcmp(input_format, "xml"))
384 info->input_format_mode = YAZ_MARC_MARCXML;
385 /** Libxml2 generates UTF-8 encoding by default .
386 So we convert from UTF-8 to outputcharset (if defined)
388 if (!info->input_charset && info->output_charset)
389 info->input_charset = "utf-8";
391 else if (!strcmp(input_format, "json"))
393 info->input_format_mode = YAZ_MARC_JSON;
397 wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
398 " Unsupported input format"
399 " defined by attribute value",
401 nmem_destroy(info->nmem);
407 wrbuf_printf(wr_error,
408 "Element <marc>: attribute 'outputformat' required");
409 nmem_destroy(info->nmem);
412 else if (!strcmp(output_format, "line"))
414 info->output_format_mode = YAZ_MARC_LINE;
416 else if (!strcmp(output_format, "marcxml"))
418 info->output_format_mode = YAZ_MARC_MARCXML;
419 if (info->input_charset && !info->output_charset)
420 info->output_charset = "utf-8";
422 else if (!strcmp(output_format, "turbomarc"))
424 info->output_format_mode = YAZ_MARC_TURBOMARC;
425 if (info->input_charset && !info->output_charset)
426 info->output_charset = "utf-8";
428 else if (!strcmp(output_format, "marc"))
430 info->output_format_mode = YAZ_MARC_ISO2709;
432 else if (!strcmp(output_format, "marcxchange"))
434 info->output_format_mode = YAZ_MARC_XCHANGE;
435 if (info->input_charset && !info->output_charset)
436 info->output_charset = "utf-8";
438 else if (!strcmp(output_format, "json"))
440 info->output_format_mode = YAZ_MARC_JSON;
441 if (info->input_charset && !info->output_charset)
442 info->output_charset = "utf-8";
446 wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
447 " Unsupported output format"
448 " defined by attribute value",
450 nmem_destroy(info->nmem);
453 if (info->input_charset && info->output_charset)
455 yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
456 info->input_charset);
459 wrbuf_printf(wr_error,
460 "Element <marc inputcharset='%s' outputcharset='%s'>:"
461 " Unsupported character set mapping"
462 " defined by attribute values",
463 info->input_charset, info->output_charset);
464 nmem_destroy(info->nmem);
469 else if (!info->output_charset)
471 wrbuf_printf(wr_error, "Element <marc>: "
472 "attribute 'outputcharset' missing");
473 nmem_destroy(info->nmem);
476 else if (!info->input_charset)
478 wrbuf_printf(wr_error, "Element <marc>: "
479 "attribute 'inputcharset' missing");
480 nmem_destroy(info->nmem);
483 info->input_charset = nmem_strdup(info->nmem, info->input_charset);
484 info->output_charset = nmem_strdup(info->nmem, info->output_charset);
488 static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
490 struct marc_info *mi = info;
493 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, mi->input_charset);
494 yaz_marc_t mt = yaz_marc_create();
496 yaz_marc_xml(mt, mi->output_format_mode);
498 yaz_marc_leader_spec(mt, mi->leader_spec);
501 yaz_marc_iconv(mt, cd);
502 if (mi->input_format_mode == YAZ_MARC_ISO2709)
504 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
511 else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
512 mi->input_format_mode == YAZ_MARC_TURBOMARC)
514 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
518 wrbuf_printf(wr_error, "xmlParseMemory failed");
523 ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
525 wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
531 wrbuf_printf(wr_error, "unsupported input format");
536 wrbuf_rewind(record);
537 ret = yaz_marc_write_mode(mt, record);
539 wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
543 yaz_marc_destroy(mt);
547 static void destroy_marc(void *info)
549 struct marc_info *mi = info;
551 nmem_destroy(mi->nmem);
554 int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr,
555 struct yaz_record_conv_type *types)
557 struct yaz_record_conv_type bt[2];
560 bt[0].construct = construct_marc;
561 bt[0].convert = convert_marc;
562 bt[0].destroy = destroy_marc;
568 bt[1].construct = construct_xslt;
569 bt[1].convert = convert_xslt;
570 bt[1].destroy = destroy_xslt;
575 yaz_record_conv_reset(p);
577 /* parsing element children */
578 for (ptr = ptr->children; ptr; ptr = ptr->next)
580 struct yaz_record_conv_type *t;
581 struct yaz_record_conv_rule *r;
583 if (ptr->type != XML_ELEMENT_NODE)
585 for (t = &bt[0]; t; t = t->next)
587 wrbuf_rewind(p->wr_error);
588 info = t->construct(ptr, p->path, p->wr_error);
590 if (info || wrbuf_len(p->wr_error))
592 /* info== 0 and no error reported , ie not handled by it */
596 if (wrbuf_len(p->wr_error) == 0)
597 wrbuf_printf(p->wr_error, "Element <backend>: expected "
598 "<marc> or <xslt> element, got <%s>"
602 r = (struct yaz_record_conv_rule *) nmem_malloc(p->nmem, sizeof(*r));
605 r->type = nmem_malloc(p->nmem, sizeof(*t));
606 memcpy(r->type, t, sizeof(*t));
608 p->rules_p = &r->next;
613 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
615 return yaz_record_conv_configure_t(p, ptr, 0);
618 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
619 struct yaz_record_conv_rule *r,
620 const char *input_record_buf,
621 size_t input_record_len,
625 WRBUF record = output_record; /* pointer transfer */
626 wrbuf_rewind(p->wr_error);
628 wrbuf_write(record, input_record_buf, input_record_len);
629 for (; ret == 0 && r; r = r->next)
630 ret = r->type->convert(r->info, record, p->wr_error);
634 int yaz_record_conv_opac_record(yaz_record_conv_t p,
635 Z_OPACRecord *input_record,
639 struct yaz_record_conv_rule *r = p->rules;
640 if (!r || r->type->construct != construct_marc)
642 wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
643 ret = -1; /* no marc rule so we can't do OPAC */
647 struct marc_info *mi = r->info;
649 WRBUF res = wrbuf_alloc();
650 yaz_marc_t mt = yaz_marc_create();
651 yaz_iconv_t cd = yaz_iconv_open(mi->output_charset,
654 wrbuf_rewind(p->wr_error);
655 yaz_marc_xml(mt, mi->output_format_mode);
657 yaz_marc_iconv(mt, cd);
659 yaz_opac_decode_wrbuf(mt, input_record, res);
662 ret = yaz_record_conv_record_rule(p,
664 wrbuf_buf(res), wrbuf_len(res),
667 yaz_marc_destroy(mt);
675 int yaz_record_conv_record(yaz_record_conv_t p,
676 const char *input_record_buf,
677 size_t input_record_len,
680 return yaz_record_conv_record_rule(p, p->rules,
682 input_record_len, output_record);
685 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
687 return wrbuf_cstr(p->wr_error);
690 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
695 p->path = xstrdup(path);
698 yaz_record_conv_t yaz_record_conv_create()
700 yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
701 p->nmem = nmem_create();
702 p->wr_error = wrbuf_alloc();
714 * c-file-style: "Stroustrup"
715 * indent-tabs-mode: nil
717 * vim: shiftwidth=4 tabstop=8 expandtab