1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
6 * \file record_render.c
7 * \brief Render Z39.50 records (NamePlusRecord)
17 #include <yaz/marcdisp.h>
18 #include <yaz/record_render.h>
19 #include <yaz/yaz-iconv.h>
20 #include <yaz/proto.h>
21 #include <yaz/oid_db.h>
23 static yaz_iconv_t iconv_create_charset(const char *record_charset,
31 if (record_charset && *record_charset)
33 char *cp = charset_buf;
35 strncpy(charset_buf, record_charset, sizeof(charset_buf)-1);
36 charset_buf[sizeof(charset_buf)-1] = '\0';
39 while (*cp && *cp != ',' && *cp != '/')
43 *cp++ = '\0'; /* terminate from_set1 */
45 while (*cp && *cp != ',')
50 *cp++ = '\0'; /* terminate from_set1 or from_set2 */
58 cd = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set1);
62 *cd2 = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set2);
69 static const char *return_marc_record(WRBUF wrbuf,
72 const char *buf, int sz,
73 const char *record_charset)
75 yaz_iconv_t cd = iconv_create_charset(record_charset, 0);
76 yaz_marc_t mt = yaz_marc_create();
77 const char *ret_string = 0;
80 yaz_marc_iconv(mt, cd);
81 yaz_marc_xml(mt, marc_type);
82 if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0)
85 *len = wrbuf_len(wrbuf);
86 ret_string = wrbuf_cstr(wrbuf);
94 static const char *return_opac_record(WRBUF wrbuf,
97 Z_OPACRecord *opac_rec,
98 const char *record_charset)
101 yaz_iconv_t cd = iconv_create_charset(record_charset, &cd2);
102 yaz_marc_t mt = yaz_marc_create();
105 yaz_marc_iconv(mt, cd);
106 yaz_marc_xml(mt, marc_type);
109 yaz_opac_decode_wrbuf2(mt, opac_rec, wrbuf, cd2);
111 yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf);
113 yaz_marc_destroy(mt);
118 yaz_iconv_close(cd2);
120 *len = wrbuf_len(wrbuf);
121 return wrbuf_cstr(wrbuf);
124 static const char *return_string_record(WRBUF wrbuf,
126 const char *buf, int sz,
127 const char *record_charset)
129 yaz_iconv_t cd = iconv_create_charset(record_charset, 0);
133 wrbuf_iconv_write(wrbuf, cd, buf, sz);
134 wrbuf_iconv_reset(wrbuf, cd);
136 buf = wrbuf_cstr(wrbuf);
137 sz = wrbuf_len(wrbuf);
145 static const char *return_record_wrbuf(WRBUF wrbuf, int *len,
146 Z_NamePlusRecord *npr,
147 int marctype, const char *charset)
149 Z_External *r = (Z_External *) npr->u.databaseRecord;
150 const Odr_oid *oid = r->direct_reference;
153 /* render bibliographic record .. */
154 if (r->which == Z_External_OPAC)
156 return return_opac_record(wrbuf, marctype, len,
159 if (r->which == Z_External_sutrs)
160 return return_string_record(wrbuf, len,
161 (char*) r->u.sutrs->buf,
164 else if (r->which == Z_External_octet)
166 if (yaz_oid_is_iso2709(oid))
168 const char *ret_buf = return_marc_record(
169 wrbuf, marctype, len,
170 (const char *) r->u.octet_aligned->buf,
171 r->u.octet_aligned->len,
175 /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
176 if (marctype != YAZ_MARC_ISO2709)
179 return return_string_record(wrbuf, len,
180 (const char *) r->u.octet_aligned->buf,
181 r->u.octet_aligned->len,
184 else if (r->which == Z_External_grs1)
186 yaz_display_grs1(wrbuf, r->u.grs1, 0);
187 return return_string_record(wrbuf, len,
195 static const char *get_record_format(WRBUF wrbuf, int *len,
196 Z_NamePlusRecord *npr,
197 int marctype, const char *charset,
200 const char *res = return_record_wrbuf(wrbuf, len, npr, marctype, charset);
202 if (*format == '1' && len)
204 /* try to XML format res */
206 xmlKeepBlanksDefault(0); /* get get xmlDocFormatMemory to work! */
207 doc = xmlParseMemory(res, *len);
212 xmlDocDumpFormatMemory(doc, &xml_mem, &xml_size, 1);
214 wrbuf_write(wrbuf, (const char *) xml_mem, xml_size);
217 res = wrbuf_cstr(wrbuf);
218 *len = wrbuf_len(wrbuf);
225 const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema,
227 const char *type_spec, int *len)
233 const char *cp = type_spec;
235 for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
250 if (!strncmp(cp + i, "charset=", 8))
253 i = i + 8; /* skip charset= */
254 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
256 if (j < sizeof(charset)-1)
257 charset[j++] = cp[i];
261 else if (!strncmp(cp + i, "format=", 7))
265 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
267 if (j < sizeof(format)-1)
273 if (!strcmp(type, "database"))
276 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
277 return npr->databaseName;
279 else if (!strcmp(type, "schema"))
282 *len = schema ? strlen(schema) : 0;
285 else if (!strcmp(type, "syntax"))
287 const char *desc = 0;
288 if (npr->which == Z_NamePlusRecord_databaseRecord)
290 Z_External *r = (Z_External *) npr->u.databaseRecord;
291 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
299 if (npr->which != Z_NamePlusRecord_databaseRecord)
302 /* from now on - we have a database record .. */
303 if (!strcmp(type, "render"))
305 return get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format);
307 else if (!strcmp(type, "xml"))
309 return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
312 else if (!strcmp(type, "txml"))
314 return get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset,
317 else if (!strcmp(type, "raw"))
319 return get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset,
322 else if (!strcmp(type, "ext"))
325 return (const char *) npr->u.databaseRecord;
327 else if (!strcmp(type, "opac"))
329 if (npr->u.databaseRecord->which == Z_External_OPAC)
330 return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
339 * c-file-style: "Stroustrup"
340 * indent-tabs-mode: nil
342 * vim: shiftwidth=4 tabstop=8 expandtab