Ignore zoomst10
[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.8 2006-05-08 10:16:47 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_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             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 #if HAVE_XSLT
117     YAZ_CHECK(conv_configure_test("<convert>"
118                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
119                                   "<marc"
120                                   " inputcharset=\"marc-8\""
121                                   " outputcharset=\"marc-8\""
122                                   "/>"
123                                   "</convert>",
124                                   "Attribute 'inputformat' required", 0));
125     YAZ_CHECK(conv_configure_test("<convert>"
126                                   "<xslt/>"
127                                   "</convert>",
128                                   "Missing attribute 'stylesheet'", 0));
129     YAZ_CHECK(conv_configure_test("<convert>"
130                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
131                                   "<marc"
132                                   " inputcharset=\"utf-8\""
133                                   " outputcharset=\"marc-8\""
134                                   " inputformat=\"xml\""
135                                   " outputformat=\"marc\""
136                                   "/>"
137                                   "</convert>",
138                                   0, 0));
139 #else
140     YAZ_CHECK(conv_configure_test("<convert>"
141                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
142                                   "</convert>",
143                                   "xslt unsupported."
144                                   " YAZ compiled without XSLT support", 0));
145 #endif
146 }
147
148 static int conv_convert_test(yaz_record_conv_t p,
149                              const char *input_record,
150                              const char *output_expect_record)
151 {
152     int ret = 0;
153     if (!p)
154     {
155         YAZ_CHECK(ret);
156     }
157     else
158     {
159         WRBUF output_record = wrbuf_alloc();
160         int r = yaz_record_conv_record(p, input_record, strlen(input_record),
161                                        output_record);
162         if (r)
163         {
164             if (output_expect_record)
165             {
166                 printf("yaz_record_conv error=%s\n",
167                        yaz_record_conv_get_error(p));
168                 ret = 0;
169             }
170             else
171                 ret = 1;
172         }
173         else
174         {
175             if (!output_expect_record)
176             {
177                 ret = 0;
178             }
179             else if (strlen(output_expect_record) != wrbuf_len(output_record))
180             {
181                 int expect_len = strlen(output_expect_record);
182                 ret = 0;
183                 printf("output_record expect-len=%d got-len=%d\n", expect_len,
184                        wrbuf_len(output_record));
185                 printf("got-output_record = %.*s\n",
186                        wrbuf_len(output_record), wrbuf_buf(output_record));
187                 printf("output_expect_record = %s\n",
188                        output_expect_record);
189             }
190             else if (memcmp(output_expect_record, wrbuf_buf(output_record),
191                             strlen(output_expect_record)))
192             {
193                 ret = 0;
194                 printf("got-output_record = %.*s\n",
195                        wrbuf_len(output_record), wrbuf_buf(output_record));
196                 printf("output_expect_record = %s\n",
197                        output_expect_record);
198             }
199             else
200             {
201                 ret = 1;
202             }
203         }
204         wrbuf_free(output_record, 1);
205     }
206     return ret;
207 }
208
209 static void tst_convert()
210 {
211     yaz_record_conv_t p = 0;
212     const char *marcxml_rec =
213         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
214         "  <leader>00080nam a22000498a 4500</leader>\n"
215         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
216         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
217         "    <subfield code=\"a\">   11224466 </subfield>\n"
218         "  </datafield>\n"
219         "</record>\n";
220     const char *iso2709_rec =
221         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
222         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
223         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
224         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
225         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
226
227     YAZ_CHECK(conv_configure_test("<convert>"
228                                   "<marc"
229                                   " inputcharset=\"utf-8\""
230                                   " outputcharset=\"marc-8\""
231                                   " inputformat=\"xml\""
232                                   " outputformat=\"marc\""
233                                   "/>"
234                                   "</convert>",
235                                   0, &p));
236     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
237     yaz_record_conv_destroy(p);
238
239     YAZ_CHECK(conv_configure_test("<convert>"
240                                   "<marc"
241                                   " outputcharset=\"utf-8\""
242                                   " inputcharset=\"marc-8\""
243                                   " outputformat=\"marcxml\""
244                                   " inputformat=\"marc\""
245                                   "/>"
246                                   "</convert>",
247                                   0, &p));
248     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
249     yaz_record_conv_destroy(p);
250
251
252     YAZ_CHECK(conv_configure_test("<convert>"
253                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
254                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
255                                   "<marc"
256                                   " inputcharset=\"utf-8\""
257                                   " outputcharset=\"marc-8\""
258                                   " inputformat=\"xml\""
259                                   " outputformat=\"marc\""
260                                   "/>"
261                                   "<marc"
262                                   " outputcharset=\"utf-8\""
263                                   " inputcharset=\"marc-8\""
264                                   " outputformat=\"marcxml\""
265                                   " inputformat=\"marc\""
266                                   "/>"
267                                   "</convert>",
268                                   0, &p));
269     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
270     yaz_record_conv_destroy(p);
271
272
273     YAZ_CHECK(conv_configure_test("<convert>"
274                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
275                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
276                                   "<marc"
277                                   " outputcharset=\"marc-8\""
278                                   " inputformat=\"xml\""
279                                   " outputformat=\"marc\""
280                                   "/>"
281                                   "<marc"
282                                   " inputcharset=\"marc-8\""
283                                   " outputformat=\"marcxml\""
284                                   " inputformat=\"marc\""
285                                   "/>"
286                                   "</convert>",
287                                   0, &p));
288     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
289     yaz_record_conv_destroy(p);
290 }
291
292 #endif
293
294 int main(int argc, char **argv)
295 {
296     YAZ_CHECK_INIT(argc, argv);
297     libxml2_error_to_yazlog(0 /* disable log */, 0);
298 #if HAVE_XML2
299     tst_configure();
300 #endif
301 #if HAVE_XSLT
302     tst_convert();
303 #endif
304     YAZ_CHECK_TERM;
305 }
306
307 /*
308  * Local variables:
309  * c-basic-offset: 4
310  * indent-tabs-mode: nil
311  * End:
312  * vim: shiftwidth=4 tabstop=8 expandtab
313  */
314