More wrbuf_buf woes.
[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.15 2007-03-19 22:17:41 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 (strcmp(output_expect_record, wrbuf_cstr(output_record)))
186             {
187                 ret = 0;
188                 printf("got-output_record = %s\n", wrbuf_cstr(output_record));
189                 printf("output_expect_record = %s\n", 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 void tst_convert1(void)
202 {
203     yaz_record_conv_t p = 0;
204     const char *marcxml_rec =
205         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
206         "  <leader>00080nam a22000498a 4500</leader>\n"
207         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
208         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
209         "    <subfield code=\"a\">   11224466 </subfield>\n"
210         "  </datafield>\n"
211         "</record>\n";
212     const char *iso2709_rec =
213         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
214         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
215         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
216         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
217         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
218
219     YAZ_CHECK(conv_configure_test("<backend>"
220                                   "<marc"
221                                   " inputcharset=\"utf-8\""
222                                   " outputcharset=\"marc-8\""
223                                   " inputformat=\"xml\""
224                                   " outputformat=\"marc\""
225                                   "/>"
226                                   "</backend>",
227                                   0, &p));
228     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
229     yaz_record_conv_destroy(p);
230
231     YAZ_CHECK(conv_configure_test("<backend>"
232                                   "<marc"
233                                   " outputcharset=\"utf-8\""
234                                   " inputcharset=\"marc-8\""
235                                   " outputformat=\"marcxml\""
236                                   " inputformat=\"marc\""
237                                   "/>"
238                                   "</backend>",
239                                   0, &p));
240     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
241     yaz_record_conv_destroy(p);
242
243
244     YAZ_CHECK(conv_configure_test("<backend>"
245                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
246                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
247                                   "<marc"
248                                   " inputcharset=\"utf-8\""
249                                   " outputcharset=\"marc-8\""
250                                   " inputformat=\"xml\""
251                                   " outputformat=\"marc\""
252                                   "/>"
253                                   "<marc"
254                                   " outputcharset=\"utf-8\""
255                                   " inputcharset=\"marc-8\""
256                                   " outputformat=\"marcxml\""
257                                   " inputformat=\"marc\""
258                                   "/>"
259                                   "</backend>",
260                                   0, &p));
261     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
262     yaz_record_conv_destroy(p);
263
264
265     YAZ_CHECK(conv_configure_test("<backend>"
266                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
267                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
268                                   "<marc"
269                                   " outputcharset=\"marc-8\""
270                                   " inputformat=\"xml\""
271                                   " outputformat=\"marc\""
272                                   "/>"
273                                   "<marc"
274                                   " inputcharset=\"marc-8\""
275                                   " outputformat=\"marcxml\""
276                                   " inputformat=\"marc\""
277                                   "/>"
278                                   "</backend>",
279                                   0, &p));
280     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
281     yaz_record_conv_destroy(p);
282 }
283
284 static void tst_convert2(void)
285 {
286     yaz_record_conv_t p = 0;
287     const char *marcxml_rec =
288         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
289         "  <leader>00080nam a22000498a 4500</leader>\n"
290         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
291         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
292         "    <subfield code=\"a\">k&#xf8;benhavn</subfield>\n"
293         "  </datafield>\n"
294         "</record>\n";
295     const char *iso2709_rec =
296         "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
297         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
298         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
299         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
300         "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
301
302     YAZ_CHECK(conv_configure_test("<backend>"
303                                   "<marc"
304                                   " inputcharset=\"utf-8\""
305                                   " outputcharset=\"marc-8\""
306                                   " inputformat=\"xml\""
307                                   " outputformat=\"marc\""
308                                   "/>"
309                                   "</backend>",
310                                   0, &p));
311     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
312     yaz_record_conv_destroy(p);
313 }
314
315 #endif
316
317 int main(int argc, char **argv)
318 {
319     YAZ_CHECK_INIT(argc, argv);
320     libxml2_error_to_yazlog(0 /* disable log */, 0);
321 #if YAZ_HAVE_XML2
322     tst_configure();
323 #endif
324 #if  YAZ_HAVE_XSLT 
325     tst_convert1();
326     tst_convert2();
327 #endif
328     YAZ_CHECK_TERM;
329 }
330
331 /*
332  * Local variables:
333  * c-basic-offset: 4
334  * indent-tabs-mode: nil
335  * End:
336  * vim: shiftwidth=4 tabstop=8 expandtab
337  */
338