1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
5 #include <yaz/record_conv.h>
10 #include <yaz/libxml2_error.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
21 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
23 xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
26 wrbuf_printf(w, "xmlParseMemory");
31 xmlNodePtr ptr = xmlDocGetRootElement(doc);
32 yaz_record_conv_t p = yaz_record_conv_create();
36 const char *srcdir = getenv("srcdir");
38 yaz_record_conv_set_path(p, srcdir);
42 wrbuf_printf(w, "xmlDocGetRootElement");
43 yaz_record_conv_destroy(p);
48 wrbuf_printf(w, "yaz_record_conv_create");
54 int r = yaz_record_conv_configure(p, ptr);
58 wrbuf_puts(w, yaz_record_conv_get_error(p));
59 yaz_record_conv_destroy(p);
68 int conv_configure_test(const char *xmlstring, const char *expect_error,
69 yaz_record_conv_t *pt)
71 WRBUF w = wrbuf_alloc();
74 yaz_record_conv_t p = conv_configure(xmlstring, w);
78 if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
83 printf("%s\n", wrbuf_cstr(w));
98 yaz_record_conv_destroy(p);
104 static void tst_configure(void)
109 YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
112 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
114 "Element <backend>: expected <marc> or "
115 "<xslt> element, got <bad>", 0));
118 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
119 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
121 " inputcharset=\"marc-8\""
122 " outputcharset=\"marc-8\""
125 "Element <marc>: attribute 'inputformat' "
127 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
130 "Element <xslt>: attribute 'stylesheet' "
132 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
134 " inputcharset=\"utf-8\""
135 " outputcharset=\"marc-8\""
136 " inputformat=\"xml\""
137 " outputformat=\"marc\""
139 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
143 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
144 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
147 " YAZ compiled without XSLT support", 0));
151 static int conv_convert_test(yaz_record_conv_t p,
152 const char *input_record,
153 const char *output_expect_record)
162 WRBUF output_record = wrbuf_alloc();
163 int r = yaz_record_conv_record(p, input_record, strlen(input_record),
167 if (output_expect_record)
169 printf("yaz_record_conv error=%s\n",
170 yaz_record_conv_get_error(p));
178 if (!output_expect_record)
182 else if (strcmp(output_expect_record, wrbuf_cstr(output_record)))
185 printf("got-output_record len=%d: %s\n",
186 wrbuf_len(output_record),wrbuf_cstr(output_record));
187 printf("output_expect_record len=%d %s\n",
188 strlen(output_expect_record), output_expect_record);
195 wrbuf_destroy(output_record);
200 static int conv_convert_test_iter(yaz_record_conv_t p,
201 const char *input_record,
202 const char *output_expect_record,
207 for (i = 0; i < num_iter; i++)
209 ret = conv_convert_test(p, input_record, output_expect_record);
216 static void tst_convert1(void)
218 yaz_record_conv_t p = 0;
219 const char *marcxml_rec =
220 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
221 " <leader>00080nam a22000498a 4500</leader>\n"
222 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
223 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
224 " <subfield code=\"a\"> 11224466 </subfield>\n"
227 const char *iso2709_rec =
228 "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
229 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
230 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
231 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
232 "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
234 YAZ_CHECK(conv_configure_test("<backend>"
236 " inputcharset=\"utf-8\""
237 " outputcharset=\"marc-8\""
238 " inputformat=\"xml\""
239 " outputformat=\"marc\""
243 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
244 yaz_record_conv_destroy(p);
246 YAZ_CHECK(conv_configure_test("<backend>"
248 " outputcharset=\"utf-8\""
249 " inputcharset=\"marc-8\""
250 " outputformat=\"marcxml\""
251 " inputformat=\"marc\""
255 YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
256 yaz_record_conv_destroy(p);
259 YAZ_CHECK(conv_configure_test("<backend>"
260 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
261 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
263 " inputcharset=\"utf-8\""
264 " outputcharset=\"marc-8\""
265 " inputformat=\"xml\""
266 " outputformat=\"marc\""
269 " outputcharset=\"utf-8\""
270 " inputcharset=\"marc-8\""
271 " outputformat=\"marcxml\""
272 " inputformat=\"marc\""
276 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
277 yaz_record_conv_destroy(p);
280 YAZ_CHECK(conv_configure_test("<backend>"
281 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
282 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
284 " outputcharset=\"marc-8\""
285 " inputformat=\"xml\""
286 " outputformat=\"marc\""
289 " inputcharset=\"marc-8\""
290 " outputformat=\"marcxml\""
291 " inputformat=\"marc\""
295 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
296 yaz_record_conv_destroy(p);
299 static void tst_convert2(void)
301 yaz_record_conv_t p = 0;
302 const char *marcxml_rec =
303 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
304 " <leader>00080nam a22000498a 4500</leader>\n"
305 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
306 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
307 " <subfield code=\"a\">københavn</subfield>\n"
310 const char *iso2709_rec =
311 "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
312 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
313 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
314 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
315 "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
317 YAZ_CHECK(conv_configure_test("<backend>"
319 " inputcharset=\"utf-8\""
320 " outputcharset=\"marc-8\""
321 " inputformat=\"xml\""
322 " outputformat=\"marc\""
326 YAZ_CHECK(conv_convert_test_iter(p, marcxml_rec, iso2709_rec, 100));
327 yaz_record_conv_destroy(p);
332 int main(int argc, char **argv)
334 YAZ_CHECK_INIT(argc, argv);
335 libxml2_error_to_yazlog(0 /* disable log */, 0);
349 * indent-tabs-mode: nil
351 * vim: shiftwidth=4 tabstop=8 expandtab