c6d9ac0f4af0c5df21247a94a9464bfaf1c12f44
[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.5 2006-05-05 18:37:08 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
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #if HAVE_XSLT
18
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21
22 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
23 {
24     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
25     if (!doc)
26     {
27         wrbuf_printf(w, "xmlParseMemory");
28         return 0;
29     }
30     else
31     {
32         xmlNodePtr ptr = xmlDocGetRootElement(doc);
33         yaz_record_conv_t p = yaz_record_conv_create();
34
35         if (p)
36         {
37             const char *srcdir = getenv("srcdir");
38             if (srcdir)
39                 yaz_record_conv_set_path(p, srcdir);
40         }
41         if (!ptr)
42         {
43             wrbuf_printf(w, "xmlDocGetRootElement");
44             yaz_record_conv_destroy(p);
45             p = 0;
46         }
47         else if (!p)
48         {
49             wrbuf_printf(w, "yaz_record_conv_create");
50         }
51         else
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_buf(w), expect_error))
78             ret = 1;
79         else
80         {
81             ret = 0;
82             printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
83         }
84     }
85     else
86     {
87         if (expect_error)
88         {
89             ret = 0;
90             yaz_record_conv_destroy(p);
91         }
92         else
93         {
94             ret = 1;
95         }
96     }
97     if (pt)
98         *pt = p;
99     else
100         yaz_record_conv_destroy(p);
101
102     wrbuf_free(w, 1);
103     return ret;
104 }
105
106 static void tst_configure()
107 {
108     YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
109     YAZ_CHECK(conv_configure_test("<bad/>", "Missing 'convert' element", 0));
110     YAZ_CHECK(conv_configure_test("<convert/>", 0, 0));
111     YAZ_CHECK(conv_configure_test("<convert><bad/></convert>",
112                                   "Bad element 'bad'."
113                                   "Expected marc, xslt, ..", 0));
114     YAZ_CHECK(conv_configure_test("<convert>"
115                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
116                                   "<marc"
117                                   " inputcharset=\"marc-8\""
118                                   " outputcharset=\"marc-8\""
119                                   "/>"
120                                   "</convert>",
121                                   "Attribute 'inputformat' required", 0));
122     YAZ_CHECK(conv_configure_test("<convert>"
123                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
124                                   "<marc"
125                                   " inputcharset=\"utf-8\""
126                                   " outputcharset=\"marc-8\""
127                                   " inputformat=\"xml\""
128                                   " outputformat=\"marc\""
129                                   "/>"
130                                   "</convert>",
131                                   0, 0));
132 }
133
134 static int conv_convert_test(yaz_record_conv_t p,
135                              const char *input_record,
136                              const char *output_expect_record)
137 {
138     int ret = 0;
139     if (!p)
140     {
141         YAZ_CHECK(ret);
142     }
143     else
144     {
145         WRBUF output_record = wrbuf_alloc();
146         int r = yaz_record_conv_record(p, input_record, output_record);
147         if (r)
148         {
149             if (output_expect_record)
150             {
151                 printf("yaz_record_conv error=%s\n",
152                        yaz_record_conv_get_error(p));
153                 ret = 0;
154             }
155             else
156                 ret = 1;
157         }
158         else
159         {
160             if (!output_expect_record)
161             {
162                 ret = 0;
163             }
164             else if (strlen(output_expect_record) != wrbuf_len(output_record))
165             {
166                 int expect_len = strlen(output_expect_record);
167                 ret = 0;
168                 printf("output_record expect-len=%d got-len=%d\n", expect_len,
169                        wrbuf_len(output_record));
170                 printf("got-output_record = %.*s\n",
171                        wrbuf_len(output_record), wrbuf_buf(output_record));
172                 printf("output_expect_record = %s\n",
173                        output_expect_record);
174             }
175             else if (memcmp(output_expect_record, wrbuf_buf(output_record),
176                             strlen(output_expect_record)))
177             {
178                 ret = 0;
179                 printf("got-output_record = %.*s\n",
180                        wrbuf_len(output_record), wrbuf_buf(output_record));
181                 printf("output_expect_record = %s\n",
182                        output_expect_record);
183             }
184             else
185             {
186                 ret = 1;
187             }
188         }
189         wrbuf_free(output_record, 1);
190     }
191     return ret;
192 }
193
194 static void tst_convert()
195 {
196     yaz_record_conv_t p = 0;
197     const char *marcxml_rec =
198         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
199         "  <leader>00080nam a22000498a 4500</leader>\n"
200         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
201         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
202         "    <subfield code=\"a\">   11224466 </subfield>\n"
203         "  </datafield>\n"
204         "</record>\n";
205     const char *iso2709_rec =
206         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
207         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
208         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
209         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
210         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
211
212     YAZ_CHECK(conv_configure_test("<convert>"
213                                   "<marc"
214                                   " inputcharset=\"utf-8\""
215                                   " outputcharset=\"marc-8\""
216                                   " inputformat=\"xml\""
217                                   " outputformat=\"marc\""
218                                   "/>"
219                                   "</convert>",
220                                   0, &p));
221     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
222     yaz_record_conv_destroy(p);
223
224     YAZ_CHECK(conv_configure_test("<convert>"
225                                   "<marc"
226                                   " outputcharset=\"utf-8\""
227                                   " inputcharset=\"marc-8\""
228                                   " outputformat=\"marcxml\""
229                                   " inputformat=\"marc\""
230                                   "/>"
231                                   "</convert>",
232                                   0, &p));
233     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
234     yaz_record_conv_destroy(p);
235
236
237     YAZ_CHECK(conv_configure_test("<convert>"
238                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
239                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
240                                   "<marc"
241                                   " inputcharset=\"utf-8\""
242                                   " outputcharset=\"marc-8\""
243                                   " inputformat=\"xml\""
244                                   " outputformat=\"marc\""
245                                   "/>"
246                                   "<marc"
247                                   " outputcharset=\"utf-8\""
248                                   " inputcharset=\"marc-8\""
249                                   " outputformat=\"marcxml\""
250                                   " inputformat=\"marc\""
251                                   "/>"
252                                   "</convert>",
253                                   0, &p));
254     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
255     yaz_record_conv_destroy(p);
256
257
258     YAZ_CHECK(conv_configure_test("<convert>"
259                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
260                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
261                                   "<marc"
262                                   " outputcharset=\"marc-8\""
263                                   " inputformat=\"xml\""
264                                   " outputformat=\"marc\""
265                                   "/>"
266                                   "<marc"
267                                   " inputcharset=\"marc-8\""
268                                   " outputformat=\"marcxml\""
269                                   " inputformat=\"marc\""
270                                   "/>"
271                                   "</convert>",
272                                   0, &p));
273     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
274     yaz_record_conv_destroy(p);
275 }
276
277 #endif
278
279 int main(int argc, char **argv)
280 {
281     YAZ_CHECK_INIT(argc, argv);
282 #if HAVE_XSLT
283     tst_configure();
284     tst_convert();
285 #endif
286     YAZ_CHECK_TERM;
287 }
288
289 /*
290  * Local variables:
291  * c-basic-offset: 4
292  * indent-tabs-mode: nil
293  * End:
294  * vim: shiftwidth=4 tabstop=8 expandtab
295  */
296