X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Frecord_render.c;h=291462b3847bcc8ea2b6a7fa43d85cc1c9f080d1;hb=c78f61142413410e88ed2a83627516bb4fa24b33;hp=49a26258042ccbbc520227fa3c5f3d37026b0f55;hpb=f6def128a97444384d6c0939eaa04fb0e931d6df;p=yaz-moved-to-github.git diff --git a/src/record_render.c b/src/record_render.c index 49a2625..291462b 100644 --- a/src/record_render.c +++ b/src/record_render.c @@ -1,11 +1,14 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) 1995-2012 Index Data * See the file LICENSE for details. */ /** - * \file record-render.c + * \file record_render.c * \brief Render Z39.50 records (NamePlusRecord) */ +#if HAVE_CONFIG_H +#include +#endif #include #include @@ -16,35 +19,55 @@ #include #include #include +#include +#include + +#include +#include -static yaz_iconv_t iconv_create_charset(const char *record_charset) +static yaz_iconv_t iconv_create_charset(const char *record_charset, + yaz_iconv_t *cd2) { - char to[40]; - char from[40]; + char charset_buf[40]; yaz_iconv_t cd = 0; - - *from = '\0'; - strcpy(to, "UTF-8"); + char *from_set1 = 0; + char *from_set2 = 0; + char *to_set = 0; if (record_charset && *record_charset) { - /* Use "from,to" or just "from" */ - const char *cp = strchr(record_charset, ','); - size_t clen = strlen(record_charset); - if (cp && cp[1]) + char *cp = charset_buf; + + strncpy(charset_buf, record_charset, sizeof(charset_buf)-1); + charset_buf[sizeof(charset_buf)-1] = '\0'; + + from_set1 = cp; + while (*cp && *cp != ',' && *cp != '/') + cp++; + if (*cp == '/') { - strncpy( to, cp+1, sizeof(to)-1); - to[sizeof(to)-1] = '\0'; - clen = cp - record_charset; + *cp++ = '\0'; /* terminate from_set1 */ + from_set2 = cp; + while (*cp && *cp != ',') + cp++; } - if (clen > sizeof(from)-1) - clen = sizeof(from)-1; - - if (clen) - strncpy(from, record_charset, clen); - from[clen] = '\0'; + if (*cp == ',') + { + *cp++ = '\0'; /* terminate from_set1 or from_set2 */ + to_set = cp; + while (*cp) + cp++; + } + } + + if (from_set1) + cd = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set1); + if (cd2) + { + if (from_set2) + *cd2 = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set2); + else + *cd2 = 0; } - if (*from && *to) - cd = yaz_iconv_open(to, from); return cd; } @@ -54,7 +77,7 @@ static const char *return_marc_record(WRBUF wrbuf, const char *buf, int sz, const char *record_charset) { - yaz_iconv_t cd = iconv_create_charset(record_charset); + yaz_iconv_t cd = iconv_create_charset(record_charset, 0); yaz_marc_t mt = yaz_marc_create(); const char *ret_string = 0; @@ -79,18 +102,25 @@ static const char *return_opac_record(WRBUF wrbuf, Z_OPACRecord *opac_rec, const char *record_charset) { - yaz_iconv_t cd = iconv_create_charset(record_charset); + yaz_iconv_t cd2; + yaz_iconv_t cd = iconv_create_charset(record_charset, &cd2); yaz_marc_t mt = yaz_marc_create(); if (cd) yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); - yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf); + if (cd2) + yaz_opac_decode_wrbuf2(mt, opac_rec, wrbuf, cd2); + else + yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf); + yaz_marc_destroy(mt); if (cd) yaz_iconv_close(cd); + if (cd2) + yaz_iconv_close(cd2); if (len) *len = wrbuf_len(wrbuf); return wrbuf_cstr(wrbuf); @@ -101,7 +131,7 @@ static const char *return_string_record(WRBUF wrbuf, const char *buf, int sz, const char *record_charset) { - yaz_iconv_t cd = iconv_create_charset(record_charset); + yaz_iconv_t cd = iconv_create_charset(record_charset, 0); if (cd) { @@ -197,10 +227,116 @@ static const char *get_record_format(WRBUF wrbuf, int *len, return res; } +static int replace_node(NMEM nmem, xmlNode *ptr, + const char *type_spec, char *record_buf) +{ + int ret = -1; + const char *res; + int len; + int m_len; + WRBUF wrbuf = wrbuf_alloc(); + ODR odr = odr_createmem(ODR_ENCODE); + Z_NamePlusRecord *npr = odr_malloc(odr, sizeof(*npr)); + npr->which = Z_NamePlusRecord_databaseRecord; + + if (atoi_n_check(record_buf, 5, &m_len)) + npr->u.databaseRecord = + z_ext_record_usmarc(odr, record_buf, strlen(record_buf)); + else + npr->u.databaseRecord = + z_ext_record_xml(odr, record_buf, strlen(record_buf)); + res = yaz_record_render(npr, 0, wrbuf, type_spec, &len); + if (res) + { + xmlDoc *doc = xmlParseMemory(res, strlen(res)); + xmlNode *nptr; + if (doc) + { + nptr = xmlCopyNode(xmlDocGetRootElement(doc), 1); + xmlReplaceNode(ptr, nptr); + xmlFreeDoc(doc); + } + else + { + nptr = xmlNewText(BAD_CAST res); + xmlReplaceNode(ptr, nptr); + } + ret = 0; + } + wrbuf_destroy(wrbuf); + odr_destroy(odr); + return ret; +} + +static const char *base64_render(NMEM nmem, WRBUF wrbuf, + const char *buf, int *len, + const char *expr, const char *type_spec) +{ + xmlDocPtr doc = xmlParseMemory(buf, *len); + if (doc) + { + xmlChar *buf_out; + int len_out; + xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); + if (xpathCtx) + { + xmlXPathObjectPtr xpathObj = + xmlXPathEvalExpression((const xmlChar *) expr, xpathCtx); + if (xpathObj) + { + xmlNodeSetPtr nodes = xpathObj->nodesetval; + if (nodes) + { + int i; + for (i = 0; i < nodes->nodeNr; i++) + { + xmlNode *ptr = nodes->nodeTab[i]; + if (ptr->type == XML_TEXT_NODE) + { + const char *input = + nmem_text_node_cdata(ptr, nmem); + char *output = nmem_malloc( + nmem, strlen(input) + 1); + if (yaz_base64decode(input, output) == 0) + { + if (!replace_node(nmem, ptr, type_spec, output)) + { + /* replacement OK */ + xmlFreeNode(ptr); + /* unset below to avoid a bad reference in + xmlXPathFreeObject below */ + nodes->nodeTab[i] = 0; + } + } + } + } + } + xmlXPathFreeObject(xpathObj); + } + xmlXPathFreeContext(xpathCtx); + } + xmlDocDumpMemory(doc, &buf_out, &len_out); + if (buf_out) + { + wrbuf_rewind(wrbuf); + wrbuf_write(wrbuf, (const char *) buf_out, len_out); + buf = wrbuf_cstr(wrbuf); + *len = len_out; + } + xmlFreeDoc(doc); + xmlFree(buf_out); + } + return buf; +} + const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, WRBUF wrbuf, const char *type_spec, int *len) { + const char *ret = 0; + NMEM nmem = 0; + char *base64_xpath = 0; + char *base64_type_spec = 0; size_t i; char type[40]; char charset[40]; @@ -244,18 +380,47 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, } format[j] = '\0'; } + else if (!strncmp(cp + i, "base64", 6)) + { + i = i + 6; + + while (cp[i] == ' ') + i++; + if (cp[i] == '(') + { + size_t i0; + nmem = nmem_create(); + i++; + while (cp[i] == ' ') + i++; + i0 = i; + while (cp[i] != ',' && cp[i]) + i++; + base64_xpath = nmem_strdupn(nmem, cp + i0, i - i0); + if (cp[i]) + i++; + while (cp[i] == ' ') + i++; + i0 = i; + while (cp[i] != ')' && cp[i]) + i++; + base64_type_spec = nmem_strdupn(nmem, cp + i0, i - i0); + if (cp[i]) + i++; + } + } } if (!strcmp(type, "database")) { if (len) *len = (npr->databaseName ? strlen(npr->databaseName) : 0); - return npr->databaseName; + ret = npr->databaseName; } else if (!strcmp(type, "schema")) { if (len) *len = schema ? strlen(schema) : 0; - return schema; + ret = schema; } else if (!strcmp(type, "syntax")) { @@ -269,43 +434,46 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, desc = "none"; if (len) *len = strlen(desc); - return desc; + ret = desc; } if (npr->which != Z_NamePlusRecord_databaseRecord) - return 0; - - /* from now on - we have a database record .. */ - if (!strcmp(type, "render")) + ; + else if (!strcmp(type, "render")) { - return get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format); + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format); } else if (!strcmp(type, "xml")) { - return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, - format); + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, + format); } else if (!strcmp(type, "txml")) { - return get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset, - format); + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset, + format); } else if (!strcmp(type, "raw")) { - return get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset, - format); + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset, + format); } else if (!strcmp(type, "ext")) { if (len) *len = -1; - return (const char *) npr->u.databaseRecord; + ret = (const char *) npr->u.databaseRecord; } else if (!strcmp(type, "opac")) { if (npr->u.databaseRecord->which == Z_External_OPAC) - return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, - format); + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, + format); } - return 0; + + if (base64_xpath) + ret = base64_render(nmem, wrbuf, + ret, len, base64_xpath, base64_type_spec); + nmem_destroy(nmem); + return ret; } /*