API changes to WRBUF. wrbuf_free removed; replaced by wrbuf_destroy. And
[yaz-moved-to-github.git] / test / tst_record_conv.c
1 /*
2  * Copyright (C) 2005-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_record_conv.c,v 1.14 2007-03-19 14:40:07 adam Exp $
6  *
7  */
8 #include <yaz/record_conv.h>
9 #include <yaz/test.h>
10 #include <yaz/wrbuf.h>
11 #include <string.h>
12 #include <yaz/log.h>
13 #include <yaz/libxml2_error.h>
14
15 #if HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #if YAZ_HAVE_XML2
20
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23
24 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
25 {
26     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
27     if (!doc)
28     {
29         wrbuf_printf(w, "xmlParseMemory");
30         return 0;
31     }
32     else
33     {
34         xmlNodePtr ptr = xmlDocGetRootElement(doc);
35         yaz_record_conv_t p = yaz_record_conv_create();
36
37         if (p)
38         {
39             const char *srcdir = getenv("srcdir");
40             if (srcdir)
41                 yaz_record_conv_set_path(p, srcdir);
42         }
43         if (!ptr)
44         {
45             wrbuf_printf(w, "xmlDocGetRootElement");
46             yaz_record_conv_destroy(p);
47             p = 0;
48         }
49         else if (!p)
50         {
51             wrbuf_printf(w, "yaz_record_conv_create");
52         }
53         else
54         {
55
56
57             int r = yaz_record_conv_configure(p, ptr);
58             
59             if (r)
60             {
61                 wrbuf_puts(w, yaz_record_conv_get_error(p));
62                 yaz_record_conv_destroy(p);
63                 p = 0;
64             }
65         }
66         xmlFreeDoc(doc);
67         return p;
68     }    
69 }
70
71 int conv_configure_test(const char *xmlstring, const char *expect_error,
72                         yaz_record_conv_t *pt)
73 {
74     WRBUF w = wrbuf_alloc();
75     int ret;
76
77     yaz_record_conv_t p = conv_configure(xmlstring, w);
78
79     if (!p)
80     {
81         if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
82             ret = 1;
83         else
84         {
85             ret = 0;
86             printf("%s\n", wrbuf_cstr(w));
87         }
88     }
89     else
90     {
91         if (expect_error)
92             ret = 0;
93         else
94             ret = 1;
95     }
96
97     if (pt)
98         *pt = p;
99     else
100         if (p)
101             yaz_record_conv_destroy(p);
102
103     wrbuf_destroy(w);
104     return ret;
105 }
106
107 static void tst_configure(void)
108 {
109
110
111
112     YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
113
114
115     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
116                                   "<bad/></backend>",
117                                   "Element <backend>: expected <marc> or "
118                                   "<xslt> element, got <bad>", 0));
119
120 #if YAZ_HAVE_XSLT
121     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
122                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
123                                   "<marc"
124                                   " inputcharset=\"marc-8\""
125                                   " outputcharset=\"marc-8\""
126                                   "/>"
127                                   "</backend>",
128                                   "Element <marc>: attribute 'inputformat' "
129                                   "required", 0));
130     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
131                                   "<xslt/>"
132                                   "</backend>",
133                                   "Element <xslt>: attribute 'stylesheet' "
134                                   "expected", 0));
135     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
136                                   "<marc"
137                                   " inputcharset=\"utf-8\""
138                                   " outputcharset=\"marc-8\""
139                                   " inputformat=\"xml\""
140                                   " outputformat=\"marc\""
141                                   "/>"
142                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
143                                   "</backend>",
144                                   0, 0));
145 #else
146     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
147                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
148                                   "</backend>",
149                                   "xslt unsupported."
150                                   " YAZ compiled without XSLT support", 0));
151 #endif 
152 }
153
154 static int conv_convert_test(yaz_record_conv_t p,
155                              const char *input_record,
156                              const char *output_expect_record)
157 {
158     int ret = 0;
159     if (!p)
160     {
161         YAZ_CHECK(ret);
162     }
163     else
164     {
165         WRBUF output_record = wrbuf_alloc();
166         int r = yaz_record_conv_record(p, input_record, strlen(input_record),
167                                        output_record);
168         if (r)
169         {
170             if (output_expect_record)
171             {
172                 printf("yaz_record_conv error=%s\n",
173                        yaz_record_conv_get_error(p));
174                 ret = 0;
175             }
176             else
177                 ret = 1;
178         }
179         else
180         {
181             if (!output_expect_record)
182             {
183                 ret = 0;
184             }
185             else if (strlen(output_expect_record) != wrbuf_len(output_record))
186             {
187                 int expect_len = strlen(output_expect_record);
188                 ret = 0;
189                 printf("output_record expect-len=%d got-len=%d\n", expect_len,
190                        wrbuf_len(output_record));
191                 printf("got-output_record = %s\n", wrbuf_cstr(output_record));
192                 printf("output_expect_record = %s\n", output_expect_record);
193             }
194             else if (memcmp(output_expect_record, wrbuf_buf(output_record),
195                             strlen(output_expect_record)))
196             {
197                 ret = 0;
198                 printf("got-output_record = %s\n", wrbuf_cstr(output_record));
199                 printf("output_expect_record = %s\n", output_expect_record);
200             }
201             else
202             {
203                 ret = 1;
204             }
205         }
206         wrbuf_destroy(output_record);
207     }
208     return ret;
209 }
210
211 static void tst_convert1(void)
212 {
213     yaz_record_conv_t p = 0;
214     const char *marcxml_rec =
215         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
216         "  <leader>00080nam a22000498a 4500</leader>\n"
217         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
218         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
219         "    <subfield code=\"a\">   11224466 </subfield>\n"
220         "  </datafield>\n"
221         "</record>\n";
222     const char *iso2709_rec =
223         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
224         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
225         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
226         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
227         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
228
229     YAZ_CHECK(conv_configure_test("<backend>"
230                                   "<marc"
231                                   " inputcharset=\"utf-8\""
232                                   " outputcharset=\"marc-8\""
233                                   " inputformat=\"xml\""
234                                   " outputformat=\"marc\""
235                                   "/>"
236                                   "</backend>",
237                                   0, &p));
238     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
239     yaz_record_conv_destroy(p);
240
241     YAZ_CHECK(conv_configure_test("<backend>"
242                                   "<marc"
243                                   " outputcharset=\"utf-8\""
244                                   " inputcharset=\"marc-8\""
245                                   " outputformat=\"marcxml\""
246                                   " inputformat=\"marc\""
247                                   "/>"
248                                   "</backend>",
249                                   0, &p));
250     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
251     yaz_record_conv_destroy(p);
252
253
254     YAZ_CHECK(conv_configure_test("<backend>"
255                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
256                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
257                                   "<marc"
258                                   " inputcharset=\"utf-8\""
259                                   " outputcharset=\"marc-8\""
260                                   " inputformat=\"xml\""
261                                   " outputformat=\"marc\""
262                                   "/>"
263                                   "<marc"
264                                   " outputcharset=\"utf-8\""
265                                   " inputcharset=\"marc-8\""
266                                   " outputformat=\"marcxml\""
267                                   " inputformat=\"marc\""
268                                   "/>"
269                                   "</backend>",
270                                   0, &p));
271     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
272     yaz_record_conv_destroy(p);
273
274
275     YAZ_CHECK(conv_configure_test("<backend>"
276                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
277                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
278                                   "<marc"
279                                   " outputcharset=\"marc-8\""
280                                   " inputformat=\"xml\""
281                                   " outputformat=\"marc\""
282                                   "/>"
283                                   "<marc"
284                                   " inputcharset=\"marc-8\""
285                                   " outputformat=\"marcxml\""
286                                   " inputformat=\"marc\""
287                                   "/>"
288                                   "</backend>",
289                                   0, &p));
290     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
291     yaz_record_conv_destroy(p);
292 }
293
294 static void tst_convert2(void)
295 {
296     yaz_record_conv_t p = 0;
297     const char *marcxml_rec =
298         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
299         "  <leader>00080nam a22000498a 4500</leader>\n"
300         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
301         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
302         "    <subfield code=\"a\">k&#xf8;benhavn</subfield>\n"
303         "  </datafield>\n"
304         "</record>\n";
305     const char *iso2709_rec =
306         "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
307         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
308         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
309         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
310         "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
311
312     YAZ_CHECK(conv_configure_test("<backend>"
313                                   "<marc"
314                                   " inputcharset=\"utf-8\""
315                                   " outputcharset=\"marc-8\""
316                                   " inputformat=\"xml\""
317                                   " outputformat=\"marc\""
318                                   "/>"
319                                   "</backend>",
320                                   0, &p));
321     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
322     yaz_record_conv_destroy(p);
323 }
324
325 #endif
326
327 int main(int argc, char **argv)
328 {
329     YAZ_CHECK_INIT(argc, argv);
330     libxml2_error_to_yazlog(0 /* disable log */, 0);
331 #if YAZ_HAVE_XML2
332     tst_configure();
333 #endif
334 #if  YAZ_HAVE_XSLT 
335     tst_convert1();
336     tst_convert2();
337 #endif
338     YAZ_CHECK_TERM;
339 }
340
341 /*
342  * Local variables:
343  * c-basic-offset: 4
344  * indent-tabs-mode: nil
345  * End:
346  * vim: shiftwidth=4 tabstop=8 expandtab
347  */
348