-static int yaz_record_conv_record_rule(yaz_record_conv_t p,
- struct yaz_record_conv_rule *r,
- const char *input_record_buf,
- size_t input_record_len,
- WRBUF output_record)
-{
- int ret = 0;
- WRBUF record = output_record; /* pointer transfer */
- wrbuf_rewind(p->wr_error);
-
- wrbuf_write(record, input_record_buf, input_record_len);
- for (; ret == 0 && r; r = r->next)
- {
- if (r->which == YAZ_RECORD_CONV_RULE_MARC)
- {
- yaz_iconv_t cd =
- yaz_iconv_open(r->u.marc.output_charset,
- r->u.marc.input_charset);
- yaz_marc_t mt = yaz_marc_create();
-
- yaz_marc_xml(mt, r->u.marc.output_format);
-
- if (cd)
- yaz_marc_iconv(mt, cd);
- if (r->u.marc.input_format == YAZ_MARC_ISO2709)
- {
- int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
- wrbuf_len(record));
- if (sz > 0)
- ret = 0;
- else
- ret = -1;
- }
- else if (r->u.marc.input_format == YAZ_MARC_MARCXML)
- {
- xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
- wrbuf_len(record));
- if (!doc)
- {
- wrbuf_printf(p->wr_error, "xmlParseMemory failed");
- ret = -1;
- }
- else
- {
- ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
- if (ret)
- wrbuf_printf(p->wr_error, "yaz_marc_read_xml failed");
- }
- xmlFreeDoc(doc);
- }
- else
- {
- wrbuf_printf(p->wr_error, "unsupported input format");
- ret = -1;
- }
- if (ret == 0)
- {
- wrbuf_rewind(record);
- ret = yaz_marc_write_mode(mt, record);
- if (ret)
- wrbuf_printf(p->wr_error, "yaz_marc_write_mode failed");
- }
- if (cd)
- yaz_iconv_close(cd);
- yaz_marc_destroy(mt);
- }
-#if YAZ_HAVE_XSLT
- else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
- {
- xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
- wrbuf_len(record));
- if (!doc)
- {
- wrbuf_printf(p->wr_error, "xmlParseMemory failed");
- ret = -1;
- }
- else
- {
- xmlDocPtr xsp_doc = xmlCopyDoc(r->u.xslt.xsp_doc, 1);
- xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
- xmlDocPtr res = xsltApplyStylesheet(xsp, doc, 0);
- if (res)
- {
- xmlChar *out_buf = 0;
- int out_len;
-
-#if YAZ_HAVE_XSLTSAVERESULTTOSTRING
- xsltSaveResultToString(&out_buf, &out_len, res, xsp);
-#else
- xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
-#endif
- if (!out_buf)
- {
- wrbuf_printf(p->wr_error,
- "xsltSaveResultToString failed");
- ret = -1;
- }
- else
- {
- wrbuf_rewind(record);
- wrbuf_write(record, (const char *) out_buf, out_len);
-
- xmlFree(out_buf);
- }
- xmlFreeDoc(res);
- }
- else
- {
- wrbuf_printf(p->wr_error, "xsltApplyStylesheet failed");
- ret = -1;
- }
- xmlFreeDoc(doc);
- xsltFreeStylesheet(xsp); /* frees xsp_doc too */
- }
- }
-#endif
- }
- return ret;
-}
-