2 * Copyright (C) 2005-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tst_record_conv.c,v 1.16 2007-03-20 21:37:32 adam Exp $
8 #include <yaz/record_conv.h>
10 #include <yaz/wrbuf.h>
13 #include <yaz/libxml2_error.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
24 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
26 xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
29 wrbuf_printf(w, "xmlParseMemory");
34 xmlNodePtr ptr = xmlDocGetRootElement(doc);
35 yaz_record_conv_t p = yaz_record_conv_create();
39 const char *srcdir = getenv("srcdir");
41 yaz_record_conv_set_path(p, srcdir);
45 wrbuf_printf(w, "xmlDocGetRootElement");
46 yaz_record_conv_destroy(p);
51 wrbuf_printf(w, "yaz_record_conv_create");
57 int r = yaz_record_conv_configure(p, ptr);
61 wrbuf_puts(w, yaz_record_conv_get_error(p));
62 yaz_record_conv_destroy(p);
71 int conv_configure_test(const char *xmlstring, const char *expect_error,
72 yaz_record_conv_t *pt)
74 WRBUF w = wrbuf_alloc();
77 yaz_record_conv_t p = conv_configure(xmlstring, w);
81 if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
86 printf("%s\n", wrbuf_cstr(w));
101 yaz_record_conv_destroy(p);
107 static void tst_configure(void)
112 YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
115 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
117 "Element <backend>: expected <marc> or "
118 "<xslt> element, got <bad>", 0));
121 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
122 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
124 " inputcharset=\"marc-8\""
125 " outputcharset=\"marc-8\""
128 "Element <marc>: attribute 'inputformat' "
130 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
133 "Element <xslt>: attribute 'stylesheet' "
135 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
137 " inputcharset=\"utf-8\""
138 " outputcharset=\"marc-8\""
139 " inputformat=\"xml\""
140 " outputformat=\"marc\""
142 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
146 YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
147 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
150 " YAZ compiled without XSLT support", 0));
154 static int conv_convert_test(yaz_record_conv_t p,
155 const char *input_record,
156 const char *output_expect_record)
165 WRBUF output_record = wrbuf_alloc();
166 int r = yaz_record_conv_record(p, input_record, strlen(input_record),
170 if (output_expect_record)
172 printf("yaz_record_conv error=%s\n",
173 yaz_record_conv_get_error(p));
181 if (!output_expect_record)
185 else if (strcmp(output_expect_record, wrbuf_cstr(output_record)))
188 printf("got-output_record len=%d: %s\n",
189 wrbuf_len(output_record),wrbuf_cstr(output_record));
190 printf("output_expect_record len=%d %s\n",
191 strlen(output_expect_record), output_expect_record);
198 wrbuf_destroy(output_record);
203 static void tst_convert1(void)
205 yaz_record_conv_t p = 0;
206 const char *marcxml_rec =
207 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
208 " <leader>00080nam a22000498a 4500</leader>\n"
209 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
210 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
211 " <subfield code=\"a\"> 11224466 </subfield>\n"
214 const char *iso2709_rec =
215 "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
216 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
217 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
218 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
219 "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
221 YAZ_CHECK(conv_configure_test("<backend>"
223 " inputcharset=\"utf-8\""
224 " outputcharset=\"marc-8\""
225 " inputformat=\"xml\""
226 " outputformat=\"marc\""
230 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
231 yaz_record_conv_destroy(p);
233 YAZ_CHECK(conv_configure_test("<backend>"
235 " outputcharset=\"utf-8\""
236 " inputcharset=\"marc-8\""
237 " outputformat=\"marcxml\""
238 " inputformat=\"marc\""
242 YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
243 yaz_record_conv_destroy(p);
246 YAZ_CHECK(conv_configure_test("<backend>"
247 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
248 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
250 " inputcharset=\"utf-8\""
251 " outputcharset=\"marc-8\""
252 " inputformat=\"xml\""
253 " outputformat=\"marc\""
256 " outputcharset=\"utf-8\""
257 " inputcharset=\"marc-8\""
258 " outputformat=\"marcxml\""
259 " inputformat=\"marc\""
263 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
264 yaz_record_conv_destroy(p);
267 YAZ_CHECK(conv_configure_test("<backend>"
268 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
269 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
271 " outputcharset=\"marc-8\""
272 " inputformat=\"xml\""
273 " outputformat=\"marc\""
276 " inputcharset=\"marc-8\""
277 " outputformat=\"marcxml\""
278 " inputformat=\"marc\""
282 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
283 yaz_record_conv_destroy(p);
286 static void tst_convert2(void)
288 yaz_record_conv_t p = 0;
289 const char *marcxml_rec =
290 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
291 " <leader>00080nam a22000498a 4500</leader>\n"
292 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
293 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
294 " <subfield code=\"a\">københavn</subfield>\n"
297 const char *iso2709_rec =
298 "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
299 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
300 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
301 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
302 "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
304 YAZ_CHECK(conv_configure_test("<backend>"
306 " inputcharset=\"utf-8\""
307 " outputcharset=\"marc-8\""
308 " inputformat=\"xml\""
309 " outputformat=\"marc\""
313 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
314 yaz_record_conv_destroy(p);
319 int main(int argc, char **argv)
321 YAZ_CHECK_INIT(argc, argv);
322 libxml2_error_to_yazlog(0 /* disable log */, 0);
336 * indent-tabs-mode: nil
338 * vim: shiftwidth=4 tabstop=8 expandtab