Moved ignore files.
[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.16 2007-03-20 21:37:32 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 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);
192             }
193             else
194             {
195                 ret = 1;
196             }
197         }
198         wrbuf_destroy(output_record);
199     }
200     return ret;
201 }
202
203 static void tst_convert1(void)
204 {
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"
212         "  </datafield>\n"
213         "</record>\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";
220
221     YAZ_CHECK(conv_configure_test("<backend>"
222                                   "<marc"
223                                   " inputcharset=\"utf-8\""
224                                   " outputcharset=\"marc-8\""
225                                   " inputformat=\"xml\""
226                                   " outputformat=\"marc\""
227                                   "/>"
228                                   "</backend>",
229                                   0, &p));
230     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
231     yaz_record_conv_destroy(p);
232
233     YAZ_CHECK(conv_configure_test("<backend>"
234                                   "<marc"
235                                   " outputcharset=\"utf-8\""
236                                   " inputcharset=\"marc-8\""
237                                   " outputformat=\"marcxml\""
238                                   " inputformat=\"marc\""
239                                   "/>"
240                                   "</backend>",
241                                   0, &p));
242     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
243     yaz_record_conv_destroy(p);
244
245
246     YAZ_CHECK(conv_configure_test("<backend>"
247                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
248                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
249                                   "<marc"
250                                   " inputcharset=\"utf-8\""
251                                   " outputcharset=\"marc-8\""
252                                   " inputformat=\"xml\""
253                                   " outputformat=\"marc\""
254                                   "/>"
255                                   "<marc"
256                                   " outputcharset=\"utf-8\""
257                                   " inputcharset=\"marc-8\""
258                                   " outputformat=\"marcxml\""
259                                   " inputformat=\"marc\""
260                                   "/>"
261                                   "</backend>",
262                                   0, &p));
263     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
264     yaz_record_conv_destroy(p);
265
266
267     YAZ_CHECK(conv_configure_test("<backend>"
268                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
269                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
270                                   "<marc"
271                                   " outputcharset=\"marc-8\""
272                                   " inputformat=\"xml\""
273                                   " outputformat=\"marc\""
274                                   "/>"
275                                   "<marc"
276                                   " inputcharset=\"marc-8\""
277                                   " outputformat=\"marcxml\""
278                                   " inputformat=\"marc\""
279                                   "/>"
280                                   "</backend>",
281                                   0, &p));
282     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
283     yaz_record_conv_destroy(p);
284 }
285
286 static void tst_convert2(void)
287 {
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&#xf8;benhavn</subfield>\n"
295         "  </datafield>\n"
296         "</record>\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";
303
304     YAZ_CHECK(conv_configure_test("<backend>"
305                                   "<marc"
306                                   " inputcharset=\"utf-8\""
307                                   " outputcharset=\"marc-8\""
308                                   " inputformat=\"xml\""
309                                   " outputformat=\"marc\""
310                                   "/>"
311                                   "</backend>",
312                                   0, &p));
313     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
314     yaz_record_conv_destroy(p);
315 }
316
317 #endif
318
319 int main(int argc, char **argv)
320 {
321     YAZ_CHECK_INIT(argc, argv);
322     libxml2_error_to_yazlog(0 /* disable log */, 0);
323 #if YAZ_HAVE_XML2
324     tst_configure();
325 #endif
326 #if  YAZ_HAVE_XSLT 
327     tst_convert1();
328     tst_convert2();
329 #endif
330     YAZ_CHECK_TERM;
331 }
332
333 /*
334  * Local variables:
335  * c-basic-offset: 4
336  * indent-tabs-mode: nil
337  * End:
338  * vim: shiftwidth=4 tabstop=8 expandtab
339  */
340