2 * Copyright (C) 2005-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tst_record_conv.c,v 1.3 2006-05-04 18:22:59 adam Exp $
8 #include <yaz/record_conv.h>
10 #include <yaz/wrbuf.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
22 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
24 xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
27 wrbuf_printf(w, "xmlParseMemory");
32 xmlNodePtr ptr = xmlDocGetRootElement(doc);
33 yaz_record_conv_t p = yaz_record_conv_create();
37 const char *srcdir = getenv("srcdir");
39 yaz_record_conv_set_path(p, srcdir);
43 wrbuf_printf(w, "xmlDocGetRootElement");
44 yaz_record_conv_destroy(p);
49 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_buf(w), expect_error))
82 printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
90 yaz_record_conv_destroy(p);
97 yaz_record_conv_destroy(p);
105 static void tst_configure()
107 YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
108 YAZ_CHECK(conv_configure_test("<bad/>", "Missing 'convert' element", 0));
109 YAZ_CHECK(conv_configure_test("<convert/>", 0, 0));
110 YAZ_CHECK(conv_configure_test("<convert><bad/></convert>",
112 "Expected marc, xslt, ..", 0));
113 YAZ_CHECK(conv_configure_test("<convert>"
114 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
116 " inputcharset=\"marc-8\""
117 " outputcharset=\"marc-8\""
120 "Attribute 'inputformat' required", 0));
121 YAZ_CHECK(conv_configure_test("<convert>"
122 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
124 " inputcharset=\"utf-8\""
125 " outputcharset=\"marc-8\""
126 " inputformat=\"xml\""
127 " outputformat=\"marc\""
133 static int conv_convert_test(yaz_record_conv_t p,
134 const char *input_record,
135 const char *output_expect_record)
144 WRBUF output_record = wrbuf_alloc();
145 int r = yaz_record_conv_record(p, input_record, output_record);
148 if (output_expect_record)
150 printf("yaz_record_conv error=%s\n",
151 yaz_record_conv_get_error(p));
159 if (!output_expect_record)
163 else if (strlen(output_expect_record) != wrbuf_len(output_record))
165 int expect_len = strlen(output_expect_record);
167 printf("output_record expect-len=%d got-len=%d\n", expect_len,
168 wrbuf_len(output_record));
169 printf("got-output_record = %.*s\n",
170 wrbuf_len(output_record), wrbuf_buf(output_record));
171 printf("output_expect_record = %s\n",
172 output_expect_record);
174 else if (memcmp(output_expect_record, wrbuf_buf(output_record),
175 strlen(output_expect_record)))
178 printf("got-output_record = %.*s\n",
179 wrbuf_len(output_record), wrbuf_buf(output_record));
180 printf("output_expect_record = %s\n",
181 output_expect_record);
188 wrbuf_free(output_record, 1);
193 static void tst_convert()
195 yaz_record_conv_t p = 0;
196 const char *marcxml_rec =
197 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
198 " <leader>00080nam a22000498a 4500</leader>\n"
199 " <controlfield tag=\"001\"> 11224466 </controlfield>\n"
200 " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
201 " <subfield code=\"a\"> 11224466 </subfield>\n"
204 const char *iso2709_rec =
205 "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
206 "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
207 "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
208 "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
209 "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
211 YAZ_CHECK(conv_configure_test("<convert>"
213 " inputcharset=\"utf-8\""
214 " outputcharset=\"marc-8\""
215 " inputformat=\"xml\""
216 " outputformat=\"marc\""
220 YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
222 YAZ_CHECK(conv_configure_test("<convert>"
224 " outputcharset=\"utf-8\""
225 " inputcharset=\"marc-8\""
226 " outputformat=\"marcxml\""
227 " inputformat=\"marc\""
231 YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
234 YAZ_CHECK(conv_configure_test("<convert>"
235 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
236 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
238 " inputcharset=\"utf-8\""
239 " outputcharset=\"marc-8\""
240 " inputformat=\"xml\""
241 " outputformat=\"marc\""
244 " outputcharset=\"utf-8\""
245 " inputcharset=\"marc-8\""
246 " outputformat=\"marcxml\""
247 " inputformat=\"marc\""
251 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
255 YAZ_CHECK(conv_configure_test("<convert>"
256 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
257 "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
259 " outputcharset=\"marc-8\""
260 " inputformat=\"xml\""
261 " outputformat=\"marc\""
264 " inputcharset=\"marc-8\""
265 " outputformat=\"marcxml\""
266 " inputformat=\"marc\""
270 YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
275 int main(int argc, char **argv)
277 YAZ_CHECK_INIT(argc, argv);
288 * indent-tabs-mode: nil
290 * vim: shiftwidth=4 tabstop=8 expandtab