1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
5 #include <yaz/record_conv.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
20 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
22 xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
25 wrbuf_printf(w, "xmlParseMemory");
30 xmlNodePtr ptr = xmlDocGetRootElement(doc);
31 yaz_record_conv_t p = yaz_record_conv_create();
35 const char *srcdir = getenv("srcdir");
37 yaz_record_conv_set_path(p, srcdir);
41 wrbuf_printf(w, "xmlDocGetRootElement");
42 yaz_record_conv_destroy(p);
47 wrbuf_printf(w, "yaz_record_conv_create");
53 int r = yaz_record_conv_configure(p, ptr);
57 wrbuf_puts(w, yaz_record_conv_get_error(p));
58 yaz_record_conv_destroy(p);
67 int conv_configure_test(const char *xmlstring, const char *expect_error,
68 yaz_record_conv_t *pt)
70 WRBUF w = wrbuf_alloc();
73 yaz_record_conv_t p = conv_configure(xmlstring, w);
77 if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
82 printf("%s\n", wrbuf_cstr(w));
97 yaz_record_conv_destroy(p);
103 static void tst_configure(void)
108 YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
111 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
113 "Element <backend>: expected <marc> or "
114 "<xslt> element, got <bad>", 0));
117 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
118 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
120 " inputcharset=\"marc-8\""
121 " outputcharset=\"marc-8\""
124 "Element <marc>: attribute 'inputformat' "
126 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
129 "Element <xslt>: attribute 'stylesheet' "
131 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
133 " inputcharset=\"utf-8\""
134 " outputcharset=\"marc-8\""
135 " inputformat=\"xml\""
136 " outputformat=\"marc\""
138 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
142 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
143 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
146 " YAZ compiled without XSLT support", 0));
150 static int conv_convert_test(yaz_record_conv_t p,
151 const char *input_record,
152 const char *output_expect_record)
161 WRBUF output_record = wrbuf_alloc();
162 int r = yaz_record_conv_record(p, input_record, strlen(input_record),
166 if (output_expect_record)
168 printf("yaz_record_conv error=%s\n",
169 yaz_record_conv_get_error(p));
177 if (!output_expect_record)
181 else if (strcmp(output_expect_record, wrbuf_cstr(output_record)))
184 printf("got-output_record len=%ld: %s\n",
185 (long) wrbuf_len(output_record),
186 wrbuf_cstr(output_record));
187 printf("output_expect_record len=%ld %s\n",
188 (long) strlen(output_expect_record),
189 output_expect_record);
196 wrbuf_destroy(output_record);
201 static int conv_convert_test_iter(yaz_record_conv_t p,
202 const char *input_record,
203 const char *output_expect_record,
208 for (i = 0; i < num_iter; i++)
210 ret = conv_convert_test(p, input_record, output_expect_record);
217 static void tst_convert1(void)
219 yaz_record_conv_t p = 0;
220 const char *marcxml_rec =
221 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
222 " <leader>00080nam a22000498a 4500</leader>\n"
223 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
224 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
225 " <subfield code=\"a\"> 11224466 </subfield>\n"
228 const char *iso2709_rec =
229 "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
230 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
231 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
232 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
233 "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
235 YAZ_CHECK(conv_configure_test("<backend>"
237 " inputcharset=\"utf-8\""
238 " outputcharset=\"marc-8\""
239 " inputformat=\"xml\""
240 " outputformat=\"marc\""
244 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
245 yaz_record_conv_destroy(p);
247 YAZ_CHECK(conv_configure_test("<backend>"
249 " outputcharset=\"utf-8\""
250 " inputcharset=\"marc-8\""
251 " outputformat=\"marcxml\""
252 " inputformat=\"marc\""
256 YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
257 yaz_record_conv_destroy(p);
260 YAZ_CHECK(conv_configure_test("<backend>"
261 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
262 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
264 " inputcharset=\"utf-8\""
265 " outputcharset=\"marc-8\""
266 " inputformat=\"xml\""
267 " outputformat=\"marc\""
270 " outputcharset=\"utf-8\""
271 " inputcharset=\"marc-8\""
272 " outputformat=\"marcxml\""
273 " inputformat=\"marc\""
277 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
278 yaz_record_conv_destroy(p);
281 YAZ_CHECK(conv_configure_test("<backend>"
282 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
283 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
285 " outputcharset=\"marc-8\""
286 " inputformat=\"xml\""
287 " outputformat=\"marc\""
290 " inputcharset=\"marc-8\""
291 " outputformat=\"marcxml\""
292 " inputformat=\"marc\""
296 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
297 yaz_record_conv_destroy(p);
300 static void tst_convert2(void)
302 yaz_record_conv_t p = 0;
303 const char *marcxml_rec =
304 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
305 " <leader>00080nam a22000498a 4500</leader>\n"
306 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
307 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
308 " <subfield code=\"a\">københavn</subfield>\n"
311 const char *iso2709_rec =
312 "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
313 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
314 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
315 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
316 "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
318 YAZ_CHECK(conv_configure_test("<backend>"
320 " inputcharset=\"utf-8\""
321 " outputcharset=\"marc-8\""
322 " inputformat=\"xml\""
323 " outputformat=\"marc\""
327 YAZ_CHECK(conv_convert_test_iter(p, marcxml_rec, iso2709_rec, 100));
328 yaz_record_conv_destroy(p);
333 int main(int argc, char **argv)
335 YAZ_CHECK_INIT(argc, argv);
336 yaz_log_xml_errors(0, 0 /* disable log */);
350 * c-file-style: "Stroustrup"
351 * indent-tabs-mode: nil
353 * vim: shiftwidth=4 tabstop=8 expandtab