X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Frecord_render.c;h=82fa6980118950a91ca414aaf06318a41c09aefe;hp=983667ae21d06582d5a49aedaf9e68f352bc8ac1;hb=8ceaeefe2e491935cba91f56007308be6e4996e6;hpb=95a76bd311b56586f2f3e9bb6e2e2a6c3f2dbce4 diff --git a/src/record_render.c b/src/record_render.c index 983667a..82fa698 100644 --- a/src/record_render.c +++ b/src/record_render.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2012 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ /** @@ -28,20 +28,22 @@ #endif static yaz_iconv_t iconv_create_charset(const char *record_charset, - yaz_iconv_t *cd2) + yaz_iconv_t *cd2, + const char *marc_buf, + int sz) { char charset_buf[40]; yaz_iconv_t cd = 0; char *from_set1 = 0; char *from_set2 = 0; - char *to_set = 0; + char *to_set = "utf-8"; if (record_charset && *record_charset) { 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++; @@ -60,13 +62,17 @@ static yaz_iconv_t iconv_create_charset(const char *record_charset, cp++; } } - + if (from_set1) - cd = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set1); + { + if (yaz_marc_check_marc21_coding(from_set1, marc_buf, sz)) + from_set1 = "utf-8"; + cd = yaz_iconv_open(to_set, from_set1); + } if (cd2) { if (from_set2) - *cd2 = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set2); + *cd2 = yaz_iconv_open(to_set, from_set2); else *cd2 = 0; } @@ -79,7 +85,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, 0); + yaz_iconv_t cd = iconv_create_charset(record_charset, 0, buf, sz); yaz_marc_t mt = yaz_marc_create(); const char *ret_string = 0; @@ -88,8 +94,7 @@ static const char *return_marc_record(WRBUF wrbuf, yaz_marc_xml(mt, marc_type); if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0) { - if (len) - *len = wrbuf_len(wrbuf); + *len = wrbuf_len(wrbuf); ret_string = wrbuf_cstr(wrbuf); } yaz_marc_destroy(mt); @@ -104,10 +109,22 @@ static const char *return_opac_record(WRBUF wrbuf, Z_OPACRecord *opac_rec, const char *record_charset) { - yaz_iconv_t cd2; - yaz_iconv_t cd = iconv_create_charset(record_charset, &cd2); + yaz_iconv_t cd, cd2; + const char *marc_buf = 0; + int marc_sz = 0; yaz_marc_t mt = yaz_marc_create(); + if (opac_rec->bibliographicRecord) + { + Z_External *ext = opac_rec->bibliographicRecord; + if (ext->which == Z_External_octet) + { + marc_buf = (const char *) ext->u.octet_aligned->buf; + marc_sz = ext->u.octet_aligned->len; + } + } + cd = iconv_create_charset(record_charset, &cd2, marc_buf, marc_sz); + if (cd) yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); @@ -116,15 +133,14 @@ static const char *return_opac_record(WRBUF wrbuf, 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); + *len = wrbuf_len(wrbuf); return wrbuf_cstr(wrbuf); } @@ -133,7 +149,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, 0); + yaz_iconv_t cd = iconv_create_charset(record_charset, 0, 0, 0); if (cd) { @@ -144,8 +160,7 @@ static const char *return_string_record(WRBUF wrbuf, sz = wrbuf_len(wrbuf); yaz_iconv_close(cd); } - if (len) - *len = sz; + *len = sz; return buf; } @@ -170,7 +185,10 @@ static const char *return_record_wrbuf(WRBUF wrbuf, int *len, charset); else if (r->which == Z_External_octet) { - if (yaz_oid_is_iso2709(oid)) + if (oid_oidcmp(oid, yaz_oid_recsyn_xml) + && oid_oidcmp(oid, yaz_oid_recsyn_application_xml) + && oid_oidcmp(oid, yaz_oid_recsyn_mab) + && oid_oidcmp(oid, yaz_oid_recsyn_html)) { const char *ret_buf = return_marc_record( wrbuf, marctype, len, @@ -179,8 +197,8 @@ static const char *return_record_wrbuf(WRBUF wrbuf, int *len, charset); if (ret_buf) return ret_buf; - /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */ - if (marctype != YAZ_MARC_ISO2709) + /* not ISO2709. Return fail unless raw (ISO2709) is wanted */ + if (yaz_oid_is_iso2709(oid) && marctype != YAZ_MARC_ISO2709) return 0; } return return_string_record(wrbuf, len, @@ -198,7 +216,7 @@ static const char *return_record_wrbuf(WRBUF wrbuf, int *len, } return 0; } - + static const char *get_record_format(WRBUF wrbuf, int *len, Z_NamePlusRecord *npr, int marctype, const char *charset, @@ -206,7 +224,7 @@ static const char *get_record_format(WRBUF wrbuf, int *len, { const char *res = return_record_wrbuf(wrbuf, len, npr, marctype, charset); #if YAZ_HAVE_XML2 - if (*format == '1' && len) + if (*format == '1') { /* try to XML format res */ xmlDocPtr doc; @@ -223,7 +241,7 @@ static const char *get_record_format(WRBUF wrbuf, int *len, xmlFreeDoc(doc); res = wrbuf_cstr(wrbuf); *len = wrbuf_len(wrbuf); - } + } } #endif return res; @@ -346,6 +364,10 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, char charset[40]; char format[3]; const char *cp = type_spec; + int len0; + + if (!len) + len = &len0; for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1; i++) @@ -366,6 +388,8 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, { size_t j = 0; i = i + 8; /* skip charset= */ + while (cp[i] == ' ') + i++; for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++) { if (j < sizeof(charset)-1) @@ -375,43 +399,44 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, } else if (!strncmp(cp + i, "format=", 7)) { - size_t j = 0; + size_t j = 0; i = i + 7; + while (cp[i] == ' ') + i++; for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++) { if (j < sizeof(format)-1) format[j++] = cp[i]; } format[j] = '\0'; - } + } else if (!strncmp(cp + i, "base64=", 7)) { size_t i0; i = i + 7; - + while (cp[i] == ' ') + i++; i0 = i; while (cp[i] && cp[i] != ';') i++; nmem = nmem_create(); base64_xpath = nmem_strdupn(nmem, cp + i0, i - i0); - } + } } if (!strcmp(type, "database")) { - if (len) - *len = (npr->databaseName ? strlen(npr->databaseName) : 0); + *len = (npr->databaseName ? strlen(npr->databaseName) : 0); ret = npr->databaseName; } else if (!strcmp(type, "schema")) { - if (len) - *len = schema ? strlen(schema) : 0; + *len = schema ? strlen(schema) : 0; ret = schema; } else if (!strcmp(type, "syntax")) { - const char *desc = 0; + const char *desc = 0; if (npr->which == Z_NamePlusRecord_databaseRecord) { Z_External *r = (Z_External *) npr->u.databaseRecord; @@ -419,8 +444,7 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, } if (!desc) desc = "none"; - if (len) - *len = strlen(desc); + *len = strlen(desc); ret = desc; } if (npr->which != Z_NamePlusRecord_databaseRecord) @@ -439,6 +463,11 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, ret = get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset, format); } + else if (!strcmp(type, "json")) + { + ret = get_record_format(wrbuf, len, npr, YAZ_MARC_JSON, charset, + format); + } else if (!strcmp(type, "raw")) { ret = get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset, @@ -446,7 +475,7 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, } else if (!strcmp(type, "ext")) { - if (len) *len = -1; + *len = -1; ret = (const char *) npr->u.databaseRecord; } else if (!strcmp(type, "opac")) @@ -456,10 +485,10 @@ const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema, format); } - if (base64_xpath) + if (base64_xpath && *len != -1) { char *type_spec = nmem_malloc(nmem, - strlen(format) + strlen(charset) + 11); + strlen(type) + strlen(charset) + 11); strcpy(type_spec, type); if (*charset) {