X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Frecord_conv.c;h=18097244387df11b3355d033714bab7c7762e874;hp=5ef8913e0c4769cec1272c7f20712d998f441c23;hb=922a7e99f2317972f9d6f3329d37a7583f57cbaa;hpb=a3e65bcd3250b107397675bc65b35b5939355025 diff --git a/src/record_conv.c b/src/record_conv.c index 5ef8913..1809724 100644 --- a/src/record_conv.c +++ b/src/record_conv.c @@ -57,6 +57,7 @@ struct marc_info { const char *output_charset; int input_format_mode; int output_format_mode; + const char *leader_spec; }; /** \brief tranformation info (rule info) */ @@ -97,11 +98,21 @@ void yaz_record_conv_destroy(yaz_record_conv_t p) } #if YAZ_HAVE_XSLT -static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, +struct xslt_info { + NMEM nmem; + xmlDocPtr xsp_doc; + const char **xsl_parms; +}; + +static void *construct_xslt(const xmlNode *ptr, const char *path, WRBUF wr_error) { struct _xmlAttr *attr; const char *stylesheet = 0; + struct xslt_info *info = 0; + NMEM nmem = 0; + int max_parms = 10; + int no_parms = 0; if (strcmp((const char *) ptr->name, "xslt")) return 0; @@ -118,6 +129,67 @@ static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, return 0; } } + nmem = nmem_create(); + info = nmem_malloc(nmem, sizeof(*info)); + info->nmem = nmem; + info->xsl_parms = nmem_malloc( + nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms)); + + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + const char *name = 0; + const char *value = 0; + char *qvalue = 0; + if (ptr->type != XML_ELEMENT_NODE) + continue; + if (strcmp((const char *) ptr->name, "param")) + { + wrbuf_printf(wr_error, "Bad element '%s'" + "Expected param.", ptr->name); + nmem_destroy(nmem); + return 0; + } + for (attr = ptr->properties; attr; attr = attr->next) + { + if (!xmlStrcmp(attr->name, BAD_CAST "name") && + attr->children && attr->children->type == XML_TEXT_NODE) + name = (const char *) attr->children->content; + else if (!xmlStrcmp(attr->name, BAD_CAST "value") && + attr->children && attr->children->type == XML_TEXT_NODE) + value = (const char *) attr->children->content; + else + { + wrbuf_printf(wr_error, "Bad attribute '%s'" + "Expected name or value.", attr->name); + nmem_destroy(nmem); + return 0; + } + } + if (!name || !value) + { + wrbuf_printf(wr_error, "Missing attributes name or value"); + nmem_destroy(nmem); + return 0; + } + if (no_parms >= max_parms) + { + wrbuf_printf(wr_error, "Too many parameters given"); + nmem_destroy(nmem); + return 0; + } + + qvalue = nmem_malloc(nmem, strlen(value) + 3); + strcpy(qvalue, "\'"); + strcat(qvalue, value); + strcat(qvalue, "\'"); + + info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name); + info->xsl_parms[2 * no_parms + 1] = qvalue; + no_parms++; + } + + info->xsl_parms[2 * no_parms] = '\0'; + if (!stylesheet) { wrbuf_printf(wr_error, "Element : " @@ -128,7 +200,6 @@ static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, { char fullpath[1024]; xsltStylesheetPtr xsp; - xmlDocPtr xsp_doc; if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath)) { wrbuf_printf(wr_error, "Element :" @@ -139,8 +210,8 @@ static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, return 0; } - xsp_doc = xmlParseFile(fullpath); - if (!xsp_doc) + info->xsp_doc = xmlParseFile(fullpath); + if (!info->xsp_doc) { wrbuf_printf(wr_error, "Element: :" " xml parse failed: %s", stylesheet, fullpath); @@ -150,7 +221,7 @@ static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, } /* 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)); + xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1)); if (!xsp) { wrbuf_printf(wr_error, "Element: :" @@ -165,21 +236,24 @@ static void *construct_xslt(yaz_record_conv_t p, const xmlNode *ptr, "EXSLT not supported" #endif ")"); - xmlFreeDoc(xsp_doc); + xmlFreeDoc(info->xsp_doc); + nmem_destroy(info->nmem); return 0; } else { xsltFreeStylesheet(xsp); - return xsp_doc; + return info; } } return 0; } -static int convert_xslt(void *info, WRBUF record, WRBUF wr_error) +static int convert_xslt(void *vinfo, WRBUF record, WRBUF wr_error) { int ret = 0; + struct xslt_info *info = vinfo; + xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record), wrbuf_len(record)); if (!doc) @@ -189,9 +263,9 @@ static int convert_xslt(void *info, WRBUF record, WRBUF wr_error) } else { - xmlDocPtr xsp_doc = xmlCopyDoc((xmlDocPtr) info, 1); + xmlDocPtr xsp_doc = xmlCopyDoc(info->xsp_doc, 1); xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc); - xmlDocPtr res = xsltApplyStylesheet(xsp, doc, 0); + xmlDocPtr res = xsltApplyStylesheet(xsp, doc, info->xsl_parms); if (res) { xmlChar *out_buf = 0; @@ -228,12 +302,14 @@ static int convert_xslt(void *info, WRBUF record, WRBUF wr_error) return ret; } -static void destroy_xslt(void *info) +static void destroy_xslt(void *vinfo) { + struct xslt_info *info = vinfo; + if (info) { - xmlDocPtr xsp_doc = info; - xmlFreeDoc(xsp_doc); + xmlFreeDoc(info->xsp_doc); + nmem_destroy(info->nmem); } } @@ -241,7 +317,7 @@ static void destroy_xslt(void *info) #endif -static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, +static void *construct_marc(const xmlNode *ptr, const char *path, WRBUF wr_error) { NMEM nmem = nmem_create(); @@ -261,6 +337,7 @@ static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, info->output_charset = 0; info->input_format_mode = 0; info->output_format_mode = 0; + info->leader_spec = 0; for (attr = ptr->properties; attr; attr = attr->next) { @@ -276,6 +353,10 @@ static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") && attr->children && attr->children->type == XML_TEXT_NODE) output_format = (const char *) attr->children->content; + else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") && + attr->children && attr->children->type == XML_TEXT_NODE) + info->leader_spec = + nmem_strdup(info->nmem,(const char *) attr->children->content); else { wrbuf_printf(wr_error, "Element : expected attributes" @@ -288,7 +369,7 @@ static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, } if (!input_format) { - wrbuf_printf(p->wr_error, "Element : " + wrbuf_printf(wr_error, "Element : " "attribute 'inputformat' required"); nmem_destroy(info->nmem); return 0; @@ -364,7 +445,7 @@ static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, info->input_charset); if (!cd) { - wrbuf_printf(p->wr_error, + wrbuf_printf(wr_error, "Element :" " Unsupported character set mapping" " defined by attribute values", @@ -374,22 +455,22 @@ static void *construct_marc(yaz_record_conv_t p, const xmlNode *ptr, } yaz_iconv_close(cd); } - else if (info->input_charset) + else if (!info->output_charset) { wrbuf_printf(wr_error, "Element : " "attribute 'outputcharset' missing"); nmem_destroy(info->nmem); return 0; } - else if (info->output_charset) + else if (!info->input_charset) { wrbuf_printf(wr_error, "Element : " "attribute 'inputcharset' missing"); nmem_destroy(info->nmem); return 0; } - info->input_charset = nmem_strdup(p->nmem, info->input_charset); - info->output_charset = nmem_strdup(p->nmem, info->output_charset); + info->input_charset = nmem_strdup(info->nmem, info->input_charset); + info->output_charset = nmem_strdup(info->nmem, info->output_charset); return info; } @@ -402,7 +483,9 @@ static int convert_marc(void *info, WRBUF record, WRBUF wr_error) yaz_marc_t mt = yaz_marc_create(); yaz_marc_xml(mt, mi->output_format_mode); - + if (mi->leader_spec) + yaz_marc_leader_spec(mt, mi->leader_spec); + if (cd) yaz_marc_iconv(mt, cd); if (mi->input_format_mode == YAZ_MARC_ISO2709) @@ -491,7 +574,7 @@ int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr, for (t = &bt[0]; t; t = t->next) { wrbuf_rewind(p->wr_error); - info = t->construct(p, ptr, p->path, p->wr_error); + info = t->construct(ptr, p->path, p->wr_error); if (info || wrbuf_len(p->wr_error)) break;