X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Frecord_conv.c;h=27381fa2db9e704b17d01635ee7be906bd2df19d;hb=db93359f0cbf9a1e9dc81ea05f7b8662f843ae3d;hp=e392e61fd492d3a062a96710c53706286f6ec663;hpb=e70cbdfde382dd605d58fc112cc2458cfce37382;p=yaz-moved-to-github.git diff --git a/src/record_conv.c b/src/record_conv.c index e392e61..27381fa 100644 --- a/src/record_conv.c +++ b/src/record_conv.c @@ -1,8 +1,8 @@ /* - * Copyright (C) 2005-2006, Index Data ApS + * Copyright (C) 2005-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: record_conv.c,v 1.2 2006-05-03 13:04:46 adam Exp $ + * $Id: record_conv.c,v 1.15 2007-03-19 14:40:07 adam Exp $ */ /** * \file record_conv.c @@ -22,28 +22,33 @@ #include #include -#if HAVE_XSLT +#if YAZ_HAVE_XML2 #include #include #include +#if YAZ_HAVE_XSLT #include #include +#endif +#if YAZ_HAVE_EXSLT +#include +#endif /** \brief The internal structure for yaz_record_conv_t */ struct yaz_record_conv_struct { - /** memory for configuration */ + /** \brief memory for configuration */ NMEM nmem; - /** conversion rules (allocated using NMEM) */ + /** \brief conversion rules (allocated using NMEM) */ struct yaz_record_conv_rule *rules; - /** pointer to last conversion rule pointer in chain */ + /** \brief pointer to last conversion rule pointer in chain */ struct yaz_record_conv_rule **rules_p; - /** string buffer for error messages */ + /** \brief string buffer for error messages */ WRBUF wr_error; - /** path for opening files */ + /** \brief path for opening files */ char *path; }; @@ -54,14 +59,16 @@ enum YAZ_RECORD_CONV_RULE 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; - int dummy; } xslt; +#endif struct { yaz_iconv_t iconv_t; int input_format; @@ -71,9 +78,10 @@ struct yaz_record_conv_rule { struct yaz_record_conv_rule *next; }; -/** reset rules+configuration */ +/** \brief reset rules+configuration */ static void yaz_record_conv_reset(yaz_record_conv_t p) { + struct yaz_record_conv_rule *r; for (r = p->rules; r; r = r->next) { @@ -82,10 +90,12 @@ static void yaz_record_conv_reset(yaz_record_conv_t p) 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); } +#endif } wrbuf_rewind(p->wr_error); nmem_reset(p->nmem); @@ -103,6 +113,9 @@ yaz_record_conv_t yaz_record_conv_create() p->rules = 0; p->path = 0; +#if YAZ_HAVE_EXSLT + exsltRegisterAll(); +#endif yaz_record_conv_reset(p); return p; } @@ -113,12 +126,13 @@ void yaz_record_conv_destroy(yaz_record_conv_t p) { 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); } } +/** \brief adds a rule */ static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p, enum YAZ_RECORD_CONV_RULE type) { @@ -130,8 +144,10 @@ static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p, return r; } +/** \brief parse 'xslt' conversion node */ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr) { +#if YAZ_HAVE_XSLT struct _xmlAttr *attr; const char *stylesheet = 0; @@ -142,14 +158,15 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr) stylesheet = (const char *) attr->children->content; else { - wrbuf_printf(p->wr_error, "Bad attribute '%s'." + wrbuf_printf(p->wr_error, "Bad attribute '%s'" "Expected stylesheet.", attr->name); return -1; } } if (!stylesheet) { - wrbuf_printf(p->wr_error, "Missing attribute 'stylesheet'"); + wrbuf_printf(p->wr_error, "Element : " + "attribute 'stylesheet' expected"); return -1; } else @@ -158,14 +175,22 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr) xsltStylesheetPtr xsp; if (!yaz_filepath_resolve(stylesheet, p->path, 0, fullpath)) { - wrbuf_printf(p->wr_error, "could not locate '%s'. Path=%s", - stylesheet, p->path); + wrbuf_printf(p->wr_error, "Element :" + " could not locate stylesheet '%s' with path '%s'", + stylesheet, fullpath, p->path); return -1; } xsp = xsltParseStylesheetFile((xmlChar*) fullpath); if (!xsp) { - wrbuf_printf(p->wr_error, "xsltParseStylesheetFile failed'"); + wrbuf_printf(p->wr_error, "Element :" + " parsing stylesheet '%s' with path '%s' failed," +#if YAZ_HAVE_EXSLT + " EXSLT enabled", +#else + " EXSLT not supported", +#endif + stylesheet, fullpath, p->path); return -1; } else @@ -176,8 +201,14 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr) } } return 0; +#else + wrbuf_printf(p->wr_error, "xslt unsupported." + " YAZ compiled without XSLT support"); + return -1; +#endif } +/** \brief parse 'marc' conversion node */ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) { struct _xmlAttr *attr; @@ -206,13 +237,17 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) output_format = (const char *) attr->children->content; else { - wrbuf_printf(p->wr_error, "Bad attribute '%s'.", attr->name); + wrbuf_printf(p->wr_error, "Element : expected attributes" + "'inputformat', 'inputcharset', 'outputformat' or" + " 'outputcharset', got attribute '%s'", + attr->name); return -1; } } if (!input_format) { - wrbuf_printf(p->wr_error, "Attribute 'inputformat' required"); + wrbuf_printf(p->wr_error, "Element : " + "attribute 'inputformat' required"); return -1; } else if (!strcmp(input_format, "marc")) @@ -230,13 +265,17 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) } else { - wrbuf_printf(p->wr_error, "Bad inputformat: '%s'", input_format); + wrbuf_printf(p->wr_error, "Element : " + " Unsupported input format" + " defined by attribute value", + input_format); return -1; } if (!output_format) { - wrbuf_printf(p->wr_error, "Attribute 'outputformat' required"); + wrbuf_printf(p->wr_error, + "Element : attribute 'outputformat' required"); return -1; } else if (!strcmp(output_format, "line")) @@ -261,7 +300,10 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) } else { - wrbuf_printf(p->wr_error, "Bad outputformat: '%s'", input_format); + wrbuf_printf(p->wr_error, "Element : " + " Unsupported output format" + " defined by attribute value", + output_format); return -1; } if (input_charset && output_charset) @@ -269,20 +311,24 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) cd = yaz_iconv_open(output_charset, input_charset); if (!cd) { - wrbuf_printf(p->wr_error, "Unsupported character set mamping" - " inputcharset=%s outputcharset=%s", + wrbuf_printf(p->wr_error, + "Element :" + " Unsupported character set mapping" + " defined by attribute values", input_charset, output_charset); return -1; } } else if (input_charset) { - wrbuf_printf(p->wr_error, "Attribute 'outputcharset' missing"); + wrbuf_printf(p->wr_error, "Element : " + "attribute 'outputcharset' missing"); return -1; } else if (output_charset) { - wrbuf_printf(p->wr_error, "Attribute 'inputcharset' missing"); + wrbuf_printf(p->wr_error, "Element : " + "attribute 'inputcharset' missing"); return -1; } r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC); @@ -293,46 +339,39 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr) 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); - if (ptr && ptr->type == XML_ELEMENT_NODE && - !strcmp((const char *) ptr->name, "convert")) - { - for (ptr = ptr->children; ptr; ptr = ptr->next) + /* parsing element children */ + for (ptr = ptr->children; ptr; ptr = ptr->next) { if (ptr->type != XML_ELEMENT_NODE) continue; if (!strcmp((const char *) ptr->name, "xslt")) - { - if (conv_xslt(p, ptr)) - return -1; - } + { + if (conv_xslt(p, ptr)) + return -1; + } else if (!strcmp((const char *) ptr->name, "marc")) - { - if (conv_marc(p, ptr)) - return -1; - } + { + if (conv_marc(p, ptr)) + return -1; + } else - { - wrbuf_printf(p->wr_error, "Bad element '%s'." - "Expected marc, xslt, ..", ptr->name); - return -1; - } + { + wrbuf_printf(p->wr_error, "Element : expected " + " or element, got <%s>" + , ptr->name); + return -1; + } } - } - else - { - wrbuf_printf(p->wr_error, "Missing 'convert' element"); - return -1; - } return 0; } -int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record, +int yaz_record_conv_record(yaz_record_conv_t p, + const char *input_record_buf, + size_t input_record_len, WRBUF output_record) { int ret = 0; @@ -340,42 +379,10 @@ int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record, struct yaz_record_conv_rule *r = p->rules; wrbuf_rewind(p->wr_error); - wrbuf_puts(record, input_record); + wrbuf_write(record, input_record_buf, input_record_len); for (; ret == 0 && r; r = r->next) { - 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 res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0); - if (res) - { - xmlChar *out_buf; - int out_len; - xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1); - - wrbuf_rewind(record); - wrbuf_write(record, (const char *) out_buf, out_len); - - xmlFree(out_buf); - xmlFreeDoc(res); - } - else - { - wrbuf_printf(p->wr_error, "xsltApplyStylesheet faailed"); - ret = -1; - } - xmlFreeDoc(doc); - } - } - else if (r->which == YAZ_RECORD_CONV_RULE_MARC) + if (r->which == YAZ_RECORD_CONV_RULE_MARC) { yaz_marc_t mt = yaz_marc_create(); @@ -423,44 +430,71 @@ int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record, } yaz_marc_destroy(mt); } - } - return ret; -} +#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 res = xsltApplyStylesheet(r->u.xslt.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); #else -/* !HAVE_XSLT */ -int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v) -{ - wrbuf_rewind(p->wr_error); - wrbuf_printf(p->wr_error, "No XML support: yaz_record_conv_configure"); - return -1; -} - -int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record, - WRBUF output_record); -{ - wrbuf_rewind(p->wr_error); - wrbuf_printf(p->wr_error, "No XML support: yaz_record_conv_record"); - return -1; -} - + 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); + } + } +#endif + } + return ret; +} 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) { - if (p) - { - xfree(p->path); - p->path = 0; - if (path) - p->path = xstrdup(path); - } + xfree(p->path); + p->path = 0; + if (path) + p->path = xstrdup(path); } +#endif /* * Local variables: