1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
9 #include <yaz/record_conv.h>
11 #include <yaz/wrbuf.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=\"test_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=\"test_record_conv.xsl\"/>"
142 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
143 "<xslt stylesheet=\"test_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 *tmarcxml_rec =
229 "<r xmlns=\"http://www.indexdata.com/MARC21/turboxml\">\n"
230 " <l>00080nam a22000498a 4500</l>\n"
231 " <c001> 11224466 </c001>\n"
232 " <d010 i1=\" \" i2=\" \">\n"
233 " <sa> 11224466 </sa>\n"
236 const char *iso2709_rec =
237 "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
238 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
239 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
240 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
241 "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
243 YAZ_CHECK(conv_configure_test("<backend>"
245 " inputcharset=\"utf-8\""
246 " outputcharset=\"marc-8\""
247 " inputformat=\"xml\""
248 " outputformat=\"marc\""
252 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
253 YAZ_CHECK(conv_convert_test(p, tmarcxml_rec, iso2709_rec));
254 yaz_record_conv_destroy(p);
256 YAZ_CHECK(conv_configure_test("<backend>"
258 " outputcharset=\"utf-8\""
259 " inputcharset=\"marc-8\""
260 " outputformat=\"marcxml\""
261 " inputformat=\"marc\""
265 YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
266 yaz_record_conv_destroy(p);
269 YAZ_CHECK(conv_configure_test("<backend>"
270 "<xslt stylesheet=\"test_record_conv.xsl\"/>"
271 "<xslt stylesheet=\"test_record_conv.xsl\"/>"
273 " inputcharset=\"utf-8\""
274 " outputcharset=\"marc-8\""
275 " inputformat=\"xml\""
276 " outputformat=\"marc\""
279 " outputcharset=\"utf-8\""
280 " inputcharset=\"marc-8\""
281 " outputformat=\"marcxml\""
282 " inputformat=\"marc\""
286 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
287 yaz_record_conv_destroy(p);
290 YAZ_CHECK(conv_configure_test("<backend>"
291 "<xslt stylesheet=\"test_record_conv.xsl\"/>"
292 "<xslt stylesheet=\"test_record_conv.xsl\"/>"
294 " outputcharset=\"marc-8\""
295 " inputformat=\"xml\""
296 " outputformat=\"marc\""
299 " inputcharset=\"marc-8\""
300 " outputformat=\"marcxml\""
301 " inputformat=\"marc\""
305 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
306 yaz_record_conv_destroy(p);
309 static void tst_convert2(void)
311 yaz_record_conv_t p = 0;
312 const char *marcxml_rec =
313 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
314 " <leader>00080nam a22000498a 4500</leader>\n"
315 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
316 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
317 " <subfield code=\"a\">københavn</subfield>\n"
320 const char *iso2709_rec =
321 "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
322 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
323 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
324 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
325 "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
327 YAZ_CHECK(conv_configure_test("<backend>"
329 " inputcharset=\"utf-8\""
330 " outputcharset=\"marc-8\""
331 " inputformat=\"xml\""
332 " outputformat=\"marc\""
336 YAZ_CHECK(conv_convert_test_iter(p, marcxml_rec, iso2709_rec, 100));
337 yaz_record_conv_destroy(p);
342 int main(int argc, char **argv)
344 YAZ_CHECK_INIT(argc, argv);
345 yaz_log_xml_errors(0, 0 /* disable log */);
359 * c-file-style: "Stroustrup"
360 * indent-tabs-mode: nil
362 * vim: shiftwidth=4 tabstop=8 expandtab