c4f09c40f391d9847ef7aae0d738e3be5995d96a
[yaz-moved-to-github.git] / test / tst_record_conv.c
1 /*
2  * Copyright (C) 2005-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_record_conv.c,v 1.7 2006-05-07 17:45: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 HAVE_XSLT
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             int r = yaz_record_conv_configure(p, ptr);
56             
57             if (r)
58             {
59                 wrbuf_puts(w, yaz_record_conv_get_error(p));
60                 yaz_record_conv_destroy(p);
61                 p = 0;
62             }
63         }
64         xmlFreeDoc(doc);
65         return p;
66     }    
67 }
68
69 int conv_configure_test(const char *xmlstring, const char *expect_error,
70                         yaz_record_conv_t *pt)
71 {
72     WRBUF w = wrbuf_alloc();
73     int ret;
74
75     yaz_record_conv_t p = conv_configure(xmlstring, w);
76
77     if (!p)
78     {
79         if (expect_error && !strcmp(wrbuf_buf(w), expect_error))
80             ret = 1;
81         else
82         {
83             ret = 0;
84             printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
85         }
86     }
87     else
88     {
89         if (expect_error)
90         {
91             ret = 0;
92             yaz_record_conv_destroy(p);
93         }
94         else
95         {
96             ret = 1;
97         }
98     }
99     if (pt)
100         *pt = p;
101     else
102         yaz_record_conv_destroy(p);
103
104     wrbuf_free(w, 1);
105     return ret;
106 }
107
108 static void tst_configure()
109 {
110     YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
111     YAZ_CHECK(conv_configure_test("<bad/>", "Missing 'convert' element", 0));
112     YAZ_CHECK(conv_configure_test("<convert/>", 0, 0));
113     YAZ_CHECK(conv_configure_test("<convert><bad/></convert>",
114                                   "Bad element 'bad'."
115                                   "Expected marc, xslt, ..", 0));
116     YAZ_CHECK(conv_configure_test("<convert>"
117                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
118                                   "<marc"
119                                   " inputcharset=\"marc-8\""
120                                   " outputcharset=\"marc-8\""
121                                   "/>"
122                                   "</convert>",
123                                   "Attribute 'inputformat' required", 0));
124     YAZ_CHECK(conv_configure_test("<convert>"
125                                   "<xslt/>"
126                                   "</convert>",
127                                   "Missing attribute 'stylesheet'", 0));
128     YAZ_CHECK(conv_configure_test("<convert>"
129                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
130                                   "<marc"
131                                   " inputcharset=\"utf-8\""
132                                   " outputcharset=\"marc-8\""
133                                   " inputformat=\"xml\""
134                                   " outputformat=\"marc\""
135                                   "/>"
136                                   "</convert>",
137                                   0, 0));
138 }
139
140 static int conv_convert_test(yaz_record_conv_t p,
141                              const char *input_record,
142                              const char *output_expect_record)
143 {
144     int ret = 0;
145     if (!p)
146     {
147         YAZ_CHECK(ret);
148     }
149     else
150     {
151         WRBUF output_record = wrbuf_alloc();
152         int r = yaz_record_conv_record(p, input_record, strlen(input_record),
153                                        output_record);
154         if (r)
155         {
156             if (output_expect_record)
157             {
158                 printf("yaz_record_conv error=%s\n",
159                        yaz_record_conv_get_error(p));
160                 ret = 0;
161             }
162             else
163                 ret = 1;
164         }
165         else
166         {
167             if (!output_expect_record)
168             {
169                 ret = 0;
170             }
171             else if (strlen(output_expect_record) != wrbuf_len(output_record))
172             {
173                 int expect_len = strlen(output_expect_record);
174                 ret = 0;
175                 printf("output_record expect-len=%d got-len=%d\n", expect_len,
176                        wrbuf_len(output_record));
177                 printf("got-output_record = %.*s\n",
178                        wrbuf_len(output_record), wrbuf_buf(output_record));
179                 printf("output_expect_record = %s\n",
180                        output_expect_record);
181             }
182             else if (memcmp(output_expect_record, wrbuf_buf(output_record),
183                             strlen(output_expect_record)))
184             {
185                 ret = 0;
186                 printf("got-output_record = %.*s\n",
187                        wrbuf_len(output_record), wrbuf_buf(output_record));
188                 printf("output_expect_record = %s\n",
189                        output_expect_record);
190             }
191             else
192             {
193                 ret = 1;
194             }
195         }
196         wrbuf_free(output_record, 1);
197     }
198     return ret;
199 }
200
201 static void tst_convert()
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("<convert>"
220                                   "<marc"
221                                   " inputcharset=\"utf-8\""
222                                   " outputcharset=\"marc-8\""
223                                   " inputformat=\"xml\""
224                                   " outputformat=\"marc\""
225                                   "/>"
226                                   "</convert>",
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("<convert>"
232                                   "<marc"
233                                   " outputcharset=\"utf-8\""
234                                   " inputcharset=\"marc-8\""
235                                   " outputformat=\"marcxml\""
236                                   " inputformat=\"marc\""
237                                   "/>"
238                                   "</convert>",
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("<convert>"
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                                   "</convert>",
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("<convert>"
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                                   "</convert>",
279                                   0, &p));
280     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
281     yaz_record_conv_destroy(p);
282 }
283
284 #endif
285
286 int main(int argc, char **argv)
287 {
288     YAZ_CHECK_INIT(argc, argv);
289     libxml2_error_to_yazlog(0 /* disable log */, 0);
290 #if HAVE_XSLT
291     tst_configure();
292     tst_convert();
293 #endif
294     YAZ_CHECK_TERM;
295 }
296
297 /*
298  * Local variables:
299  * c-basic-offset: 4
300  * indent-tabs-mode: nil
301  * End:
302  * vim: shiftwidth=4 tabstop=8 expandtab
303  */
304