-/*
- * Copyright (C) 2005-2006, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
* See the file LICENSE for details.
- *
- * $Id: record_conv.c,v 1.12 2006-12-12 10:41:38 marc Exp $
*/
/**
* \file record_conv.c
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
#include <yaz/tpath.h>
+#include <yaz/z-opac.h>
#if YAZ_HAVE_XML2
#include <libxml/parser.h>
YAZ_RECORD_CONV_RULE_MARC
};
-
/** \brief tranformation info (rule info) */
struct yaz_record_conv_rule {
enum YAZ_RECORD_CONV_RULE which;
union {
#if YAZ_HAVE_XSLT
struct {
- xsltStylesheetPtr xsp;
+ xmlDocPtr xsp_doc;
} xslt;
#endif
struct {
- yaz_iconv_t iconv_t;
+ const char *input_charset;
+ const char *output_charset;
int input_format;
int output_format;
} marc;
{
if (r->which == YAZ_RECORD_CONV_RULE_MARC)
{
- if (r->u.marc.iconv_t)
- yaz_iconv_close(r->u.marc.iconv_t);
+ ;
}
#if YAZ_HAVE_XSLT
else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
{
- xsltFreeStylesheet(r->u.xslt.xsp);
+ xmlFreeDoc(r->u.xslt.xsp_doc);
}
#endif
}
yaz_record_conv_t yaz_record_conv_create()
{
- yaz_record_conv_t p = xmalloc(sizeof(*p));
+ yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
p->nmem = nmem_create();
p->wr_error = wrbuf_alloc();
p->rules = 0;
{
yaz_record_conv_reset(p);
nmem_destroy(p->nmem);
- wrbuf_free(p->wr_error, 1);
+ wrbuf_destroy(p->wr_error);
xfree(p->path);
xfree(p);
}
static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
enum YAZ_RECORD_CONV_RULE type)
{
- struct yaz_record_conv_rule *r = nmem_malloc(p->nmem, sizeof(*r));
+ struct yaz_record_conv_rule *r = (struct yaz_record_conv_rule *)
+ nmem_malloc(p->nmem, sizeof(*r));
r->which = type;
r->next = 0;
*p->rules_p = r;
{
char fullpath[1024];
xsltStylesheetPtr xsp;
+ xmlDocPtr xsp_doc;
if (!yaz_filepath_resolve(stylesheet, p->path, 0, fullpath))
{
wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
- " could not locate stylesheet '%s' with path '%s'",
- stylesheet, fullpath, p->path);
+ " could not locate stylesheet '%s'",
+ stylesheet, fullpath);
+ if (p->path)
+ wrbuf_printf(p->wr_error, " with path '%s'", p->path);
+
return -1;
}
- xsp = xsltParseStylesheetFile((xmlChar*) fullpath);
+ xsp_doc = xmlParseFile(fullpath);
+ if (!xsp_doc)
+ {
+ wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
+ " xml parse failed: %s", stylesheet, fullpath);
+ if (p->path)
+ wrbuf_printf(p->wr_error, " with path '%s'", p->path);
+ return -1;
+ }
+ /* need to copy this before passing it to the processor. It will
+ be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
+ xsp = xsltParseStylesheetDoc(xmlCopyDoc(xsp_doc, 1));
if (!xsp)
{
- wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
- " parsing stylesheet '%s' with path '%s' failed,"
+ wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
+ " xslt parse failed: %s", stylesheet, fullpath);
+ if (p->path)
+ wrbuf_printf(p->wr_error, " with path '%s'", p->path);
+ wrbuf_printf(p->wr_error, " ("
#if YAZ_HAVE_EXSLT
- " EXSLT enabled",
+
+ "EXSLT enabled"
#else
- " EXSLT not supported",
+ "EXSLT not supported"
#endif
- stylesheet, fullpath, p->path);
+ ")");
+ xmlFreeDoc(xsp_doc);
return -1;
}
else
{
struct yaz_record_conv_rule *r =
add_rule(p, YAZ_RECORD_CONV_RULE_XSLT);
- r->u.xslt.xsp = xsp;
+ r->u.xslt.xsp_doc = xsp_doc;
+ xsltFreeStylesheet(xsp);
}
}
return 0;
int input_format_mode = 0;
int output_format_mode = 0;
struct yaz_record_conv_rule *r;
- yaz_iconv_t cd = 0;
for (attr = ptr->properties; attr; attr = attr->next)
{
if (input_charset && !output_charset)
output_charset = "utf-8";
}
+ else if (!strcmp(output_format, "turbomarc"))
+ {
+ output_format_mode = YAZ_MARC_TURBOMARC;
+ if (input_charset && !output_charset)
+ output_charset = "utf-8";
+ }
else if (!strcmp(output_format, "marc"))
{
output_format_mode = YAZ_MARC_ISO2709;
}
if (input_charset && output_charset)
{
- cd = yaz_iconv_open(output_charset, input_charset);
+ yaz_iconv_t cd = yaz_iconv_open(output_charset, input_charset);
if (!cd)
{
wrbuf_printf(p->wr_error,
input_charset, output_charset);
return -1;
}
+ yaz_iconv_close(cd);
}
else if (input_charset)
{
return -1;
}
r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC);
- r->u.marc.iconv_t = cd;
+ r->u.marc.input_charset = nmem_strdup(p->nmem, input_charset);
+ r->u.marc.output_charset = nmem_strdup(p->nmem, output_charset);
r->u.marc.input_format = input_format_mode;
r->u.marc.output_format = output_format_mode;
return 0;
}
-int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v)
+int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
{
- const xmlNode *ptr = ptr_v;
-
yaz_record_conv_reset(p);
/* parsing element children */
return 0;
}
+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 yaz_record_conv_opac_record(yaz_record_conv_t p,
+ Z_OPACRecord *input_record,
+ WRBUF output_record)
+{
+ int ret = 0;
+ struct yaz_record_conv_rule *r = p->rules;
+ if (!r || r->which != YAZ_RECORD_CONV_RULE_MARC)
+ ret = -1; /* no marc rule so we can't do OPAC */
+ else
+ {
+ WRBUF res = wrbuf_alloc();
+ yaz_marc_t mt = yaz_marc_create();
+ yaz_iconv_t cd = yaz_iconv_open(r->u.marc.output_charset,
+ r->u.marc.input_charset);
+
+ wrbuf_rewind(p->wr_error);
+ yaz_marc_xml(mt, r->u.marc.output_format);
+
+ yaz_marc_iconv(mt, cd);
+
+ yaz_opac_decode_wrbuf(mt, input_record, res);
+ if (ret != -1)
+ {
+ ret = yaz_record_conv_record_rule(p,
+ r->next,
+ wrbuf_buf(res), wrbuf_len(res),
+ output_record);
+ }
+ yaz_marc_destroy(mt);
+ if (cd)
+ yaz_iconv_close(cd);
+ wrbuf_destroy(res);
+ }
+ return ret;
+}
+
int yaz_record_conv_record(yaz_record_conv_t p,
const char *input_record_buf,
size_t input_record_len,
WRBUF output_record)
{
+ return yaz_record_conv_record_rule(p, p->rules,
+ input_record_buf,
+ input_record_len, output_record);
+}
+
+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 */
- struct yaz_record_conv_rule *r = p->rules;
wrbuf_rewind(p->wr_error);
wrbuf_write(record, input_record_buf, input_record_len);
{
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 (r->u.marc.iconv_t)
- yaz_marc_iconv(mt, r->u.marc.iconv_t);
+ 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),
else
ret = -1;
}
- else if (r->u.marc.input_format == YAZ_MARC_MARCXML)
+ else if (r->u.marc.input_format == YAZ_MARC_MARCXML ||
+ r->u.marc.input_format == YAZ_MARC_TURBOMARC)
{
xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
wrbuf_len(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
{
- xmlDocPtr res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0);
+ 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,
- r->u.xslt.xsp);
+#if HAVE_XSLTSAVERESULTTOSTRING
+ xsltSaveResultToString(&out_buf, &out_len, res, xsp);
#else
xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
#endif
ret = -1;
}
xmlFreeDoc(doc);
+ xsltFreeStylesheet(xsp); /* frees xsp_doc too */
}
}
#endif
const char *yaz_record_conv_get_error(yaz_record_conv_t p)
{
- return wrbuf_buf(p->wr_error);
+ return wrbuf_cstr(p->wr_error);
}
void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
/*
* Local variables:
* c-basic-offset: 4
+ * c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab