1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
6 * \file record_render.c
7 * \brief Render Z39.50 records (NamePlusRecord)
14 #include <yaz/marcdisp.h>
15 #include <yaz/record_render.h>
16 #include <yaz/yaz-iconv.h>
17 #include <yaz/proto.h>
18 #include <yaz/oid_db.h>
20 static yaz_iconv_t iconv_create_charset(const char *record_charset)
28 if (record_charset && *record_charset)
30 /* Use "from,to" or just "from" */
31 const char *cp = strchr(record_charset, ',');
32 size_t clen = strlen(record_charset);
35 strncpy( to, cp+1, sizeof(to)-1);
36 to[sizeof(to)-1] = '\0';
37 clen = cp - record_charset;
39 if (clen > sizeof(from)-1)
40 clen = sizeof(from)-1;
43 strncpy(from, record_charset, clen);
47 cd = yaz_iconv_open(to, from);
51 static const char *return_marc_record(WRBUF wrbuf,
54 const char *buf, int sz,
55 const char *record_charset)
57 yaz_iconv_t cd = iconv_create_charset(record_charset);
58 yaz_marc_t mt = yaz_marc_create();
59 const char *ret_string = 0;
62 yaz_marc_iconv(mt, cd);
63 yaz_marc_xml(mt, marc_type);
64 if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0)
67 *len = wrbuf_len(wrbuf);
68 ret_string = wrbuf_cstr(wrbuf);
76 static const char *return_opac_record(WRBUF wrbuf,
79 Z_OPACRecord *opac_rec,
80 const char *record_charset)
82 yaz_iconv_t cd = iconv_create_charset(record_charset);
83 yaz_marc_t mt = yaz_marc_create();
86 yaz_marc_iconv(mt, cd);
87 yaz_marc_xml(mt, marc_type);
89 yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf);
95 *len = wrbuf_len(wrbuf);
96 return wrbuf_cstr(wrbuf);
99 static const char *return_string_record(WRBUF wrbuf,
101 const char *buf, int sz,
102 const char *record_charset)
104 yaz_iconv_t cd = iconv_create_charset(record_charset);
108 wrbuf_iconv_write(wrbuf, cd, buf, sz);
109 wrbuf_iconv_reset(wrbuf, cd);
111 buf = wrbuf_cstr(wrbuf);
112 sz = wrbuf_len(wrbuf);
120 static const char *return_record_wrbuf(WRBUF wrbuf, int *len,
121 Z_NamePlusRecord *npr,
122 int marctype, const char *charset)
124 Z_External *r = (Z_External *) npr->u.databaseRecord;
125 const Odr_oid *oid = r->direct_reference;
128 /* render bibliographic record .. */
129 if (r->which == Z_External_OPAC)
131 return return_opac_record(wrbuf, marctype, len,
134 if (r->which == Z_External_sutrs)
135 return return_string_record(wrbuf, len,
136 (char*) r->u.sutrs->buf,
139 else if (r->which == Z_External_octet)
141 if (yaz_oid_is_iso2709(oid))
143 const char *ret_buf = return_marc_record(
144 wrbuf, marctype, len,
145 (const char *) r->u.octet_aligned->buf,
146 r->u.octet_aligned->len,
150 /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
151 if (marctype != YAZ_MARC_ISO2709)
154 return return_string_record(wrbuf, len,
155 (const char *) r->u.octet_aligned->buf,
156 r->u.octet_aligned->len,
159 else if (r->which == Z_External_grs1)
161 yaz_display_grs1(wrbuf, r->u.grs1, 0);
162 return return_string_record(wrbuf, len,
170 static const char *get_record_format(WRBUF wrbuf, int *len,
171 Z_NamePlusRecord *npr,
172 int marctype, const char *charset,
175 const char *res = return_record_wrbuf(wrbuf, len, npr, marctype, charset);
177 if (*format == '1' && len)
179 /* try to XML format res */
181 xmlKeepBlanksDefault(0); /* get get xmlDocFormatMemory to work! */
182 doc = xmlParseMemory(res, *len);
187 xmlDocDumpFormatMemory(doc, &xml_mem, &xml_size, 1);
189 wrbuf_write(wrbuf, (const char *) xml_mem, xml_size);
192 res = wrbuf_cstr(wrbuf);
193 *len = wrbuf_len(wrbuf);
200 const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema,
202 const char *type_spec, int *len)
208 const char *cp = type_spec;
210 for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
225 if (!strncmp(cp + i, "charset=", 8))
228 i = i + 8; /* skip charset= */
229 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
231 if (j < sizeof(charset)-1)
232 charset[j++] = cp[i];
236 else if (!strncmp(cp + i, "format=", 7))
240 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
242 if (j < sizeof(format)-1)
248 if (!strcmp(type, "database"))
251 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
252 return npr->databaseName;
254 else if (!strcmp(type, "schema"))
257 *len = schema ? strlen(schema) : 0;
260 else if (!strcmp(type, "syntax"))
262 const char *desc = 0;
263 if (npr->which == Z_NamePlusRecord_databaseRecord)
265 Z_External *r = (Z_External *) npr->u.databaseRecord;
266 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
274 if (npr->which != Z_NamePlusRecord_databaseRecord)
277 /* from now on - we have a database record .. */
278 if (!strcmp(type, "render"))
280 return get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format);
282 else if (!strcmp(type, "xml"))
284 return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
287 else if (!strcmp(type, "txml"))
289 return get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset,
292 else if (!strcmp(type, "raw"))
294 return get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset,
297 else if (!strcmp(type, "ext"))
300 return (const char *) npr->u.databaseRecord;
302 else if (!strcmp(type, "opac"))
304 if (npr->u.databaseRecord->which == Z_External_OPAC)
305 return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
314 * c-file-style: "Stroustrup"
315 * indent-tabs-mode: nil
317 * vim: shiftwidth=4 tabstop=8 expandtab