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