changed output to be non-cascarding when using -n switch
[yaz-moved-to-github.git] / src / record_conv.c
1 /*
2  * Copyright (C) 2005-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: record_conv.c,v 1.14 2007-01-03 08:42:15 adam Exp $
6  */
7 /**
8  * \file record_conv.c
9  * \brief Record Conversions utility
10  */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <string.h>
17 #include <yaz/yaz-iconv.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/record_conv.h>
20 #include <yaz/wrbuf.h>
21 #include <yaz/xmalloc.h>
22 #include <yaz/nmem.h>
23 #include <yaz/tpath.h>
24
25 #if YAZ_HAVE_XML2
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxml/xinclude.h>
29 #if YAZ_HAVE_XSLT
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/transform.h>
32 #endif
33 #if YAZ_HAVE_EXSLT
34 #include <libexslt/exslt.h>
35 #endif
36
37 /** \brief The internal structure for yaz_record_conv_t */
38 struct yaz_record_conv_struct {
39     /** \brief memory for configuration */
40     NMEM nmem;
41
42     /** \brief conversion rules (allocated using NMEM) */
43     struct yaz_record_conv_rule *rules;
44
45     /** \brief pointer to last conversion rule pointer in chain */
46     struct yaz_record_conv_rule **rules_p;
47
48     /** \brief string buffer for error messages */
49     WRBUF wr_error;
50
51     /** \brief path for opening files  */
52     char *path;
53 };
54
55 /** \brief tranformation types (rule types) */
56 enum YAZ_RECORD_CONV_RULE 
57 {
58     YAZ_RECORD_CONV_RULE_XSLT,
59     YAZ_RECORD_CONV_RULE_MARC
60 };
61
62
63 /** \brief tranformation info (rule info) */
64 struct yaz_record_conv_rule {
65     enum YAZ_RECORD_CONV_RULE which;
66     union {
67 #if YAZ_HAVE_XSLT
68         struct {
69             xsltStylesheetPtr xsp;
70         } xslt;
71 #endif
72         struct {
73             yaz_iconv_t iconv_t;
74             int input_format;
75             int output_format;
76         } marc;
77     } u;
78     struct yaz_record_conv_rule *next;
79 };
80
81 /** \brief reset rules+configuration */
82 static void yaz_record_conv_reset(yaz_record_conv_t p)
83 {
84
85     struct yaz_record_conv_rule *r;
86     for (r = p->rules; r; r = r->next)
87     {
88         if (r->which == YAZ_RECORD_CONV_RULE_MARC)
89         {
90             if (r->u.marc.iconv_t)
91                 yaz_iconv_close(r->u.marc.iconv_t);
92         }
93 #if YAZ_HAVE_XSLT
94         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
95         {
96             xsltFreeStylesheet(r->u.xslt.xsp);
97         }
98 #endif
99     }
100     wrbuf_rewind(p->wr_error);
101     nmem_reset(p->nmem);
102
103     p->rules = 0;
104
105     p->rules_p = &p->rules;
106 }
107
108 yaz_record_conv_t yaz_record_conv_create()
109 {
110     yaz_record_conv_t p = xmalloc(sizeof(*p));
111     p->nmem = nmem_create();
112     p->wr_error = wrbuf_alloc();
113     p->rules = 0;
114     p->path = 0;
115
116 #if YAZ_HAVE_EXSLT
117     exsltRegisterAll(); 
118 #endif
119     yaz_record_conv_reset(p);
120     return p;
121 }
122
123 void yaz_record_conv_destroy(yaz_record_conv_t p)
124 {
125     if (p)
126     {
127         yaz_record_conv_reset(p);
128         nmem_destroy(p->nmem);
129         wrbuf_free(p->wr_error, 1);
130         xfree(p->path);
131         xfree(p);
132     }
133 }
134
135 /** \brief adds a rule */
136 static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
137                                              enum YAZ_RECORD_CONV_RULE type)
138 {
139     struct yaz_record_conv_rule *r = nmem_malloc(p->nmem, sizeof(*r));
140     r->which = type;
141     r->next = 0;
142     *p->rules_p = r;
143     p->rules_p = &r->next;
144     return r;
145 }
146
147 /** \brief parse 'xslt' conversion node */
148 static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
149 {
150 #if YAZ_HAVE_XSLT
151     struct _xmlAttr *attr;
152     const char *stylesheet = 0;
153
154     for (attr = ptr->properties; attr; attr = attr->next)
155     {
156         if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
157             attr->children && attr->children->type == XML_TEXT_NODE)
158             stylesheet = (const char *) attr->children->content;
159         else
160         {
161             wrbuf_printf(p->wr_error, "Bad attribute '%s'"
162                          "Expected stylesheet.", attr->name);
163             return -1;
164         }
165     }
166     if (!stylesheet)
167     {
168         wrbuf_printf(p->wr_error, "Element <xslt>: "
169                      "attribute 'stylesheet' expected");
170         return -1;
171     }
172     else
173     {
174         char fullpath[1024];
175         xsltStylesheetPtr xsp;
176         if (!yaz_filepath_resolve(stylesheet, p->path, 0, fullpath))
177         {
178             wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
179                          " could not locate stylesheet '%s' with path '%s'",
180                          stylesheet, fullpath, p->path);
181             return -1;
182         }
183         xsp = xsltParseStylesheetFile((xmlChar*) fullpath);
184         if (!xsp)
185         {
186             wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
187                          " parsing stylesheet '%s' with path '%s' failed,"
188 #if YAZ_HAVE_EXSLT
189                          " EXSLT enabled",
190 #else
191                          " EXSLT not supported",
192 #endif
193                          stylesheet, fullpath, p->path);
194             return -1;
195         }
196         else
197         {
198             struct yaz_record_conv_rule *r = 
199                 add_rule(p, YAZ_RECORD_CONV_RULE_XSLT);
200             r->u.xslt.xsp = xsp;
201         }
202     }
203     return 0;
204 #else
205     wrbuf_printf(p->wr_error, "xslt unsupported."
206                  " YAZ compiled without XSLT support");
207     return -1;
208 #endif
209 }
210
211 /** \brief parse 'marc' conversion node */
212 static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
213 {
214     struct _xmlAttr *attr;
215     const char *input_charset = 0;
216     const char *output_charset = 0;
217     const char *input_format = 0;
218     const char *output_format = 0;
219     int input_format_mode = 0;
220     int output_format_mode = 0;
221     struct yaz_record_conv_rule *r;
222     yaz_iconv_t cd = 0;
223
224     for (attr = ptr->properties; attr; attr = attr->next)
225     {
226         if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
227             attr->children && attr->children->type == XML_TEXT_NODE)
228             input_charset = (const char *) attr->children->content;
229         else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
230             attr->children && attr->children->type == XML_TEXT_NODE)
231             output_charset = (const char *) attr->children->content;
232         else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
233             attr->children && attr->children->type == XML_TEXT_NODE)
234             input_format = (const char *) attr->children->content;
235         else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
236             attr->children && attr->children->type == XML_TEXT_NODE)
237             output_format = (const char *) attr->children->content;
238         else
239         {
240             wrbuf_printf(p->wr_error, "Element <marc>: expected attributes"
241                          "'inputformat', 'inputcharset', 'outputformat' or"
242                          " 'outputcharset', got attribute '%s'", 
243                          attr->name);
244             return -1;
245         }
246     }
247     if (!input_format)
248     {
249         wrbuf_printf(p->wr_error, "Element <marc>: "
250                      "attribute 'inputformat' required");
251         return -1;
252     }
253     else if (!strcmp(input_format, "marc"))
254     {
255         input_format_mode = YAZ_MARC_ISO2709;
256     }
257     else if (!strcmp(input_format, "xml"))
258     {
259         input_format_mode = YAZ_MARC_MARCXML;
260         /** Libxml2 generates UTF-8 encoding by default .
261             So we convert from UTF-8 to outputcharset (if defined) 
262         */
263         if (!input_charset && output_charset)
264             input_charset = "utf-8";
265     }
266     else
267     {
268         wrbuf_printf(p->wr_error, "Element <marc inputformat='%s'>: "
269                      " Unsupported input format"
270                      " defined by attribute value", 
271                      input_format);
272         return -1;
273     }
274     
275     if (!output_format)
276     {
277         wrbuf_printf(p->wr_error, 
278                      "Element <marc>: attribute 'outputformat' required");
279         return -1;
280     }
281     else if (!strcmp(output_format, "line"))
282     {
283         output_format_mode = YAZ_MARC_LINE;
284     }
285     else if (!strcmp(output_format, "marcxml"))
286     {
287         output_format_mode = YAZ_MARC_MARCXML;
288         if (input_charset && !output_charset)
289             output_charset = "utf-8";
290     }
291     else if (!strcmp(output_format, "marc"))
292     {
293         output_format_mode = YAZ_MARC_ISO2709;
294     }
295     else if (!strcmp(output_format, "marcxchange"))
296     {
297         output_format_mode = YAZ_MARC_XCHANGE;
298         if (input_charset && !output_charset)
299             output_charset = "utf-8";
300     }
301     else
302     {
303         wrbuf_printf(p->wr_error, "Element <marc outputformat='%s'>: "
304                      " Unsupported output format"
305                      " defined by attribute value", 
306                      output_format);
307         return -1;
308     }
309     if (input_charset && output_charset)
310     {
311         cd = yaz_iconv_open(output_charset, input_charset);
312         if (!cd)
313         {
314             wrbuf_printf(p->wr_error, 
315                          "Element <marc inputcharset='%s' outputcharset='%s'>:"
316                          " Unsupported character set mapping"
317                          " defined by attribute values",
318                          input_charset, output_charset);
319             return -1;
320         }
321     }
322     else if (input_charset)
323     {
324         wrbuf_printf(p->wr_error, "Element <marc>: "
325                      "attribute 'outputcharset' missing");
326         return -1;
327     }
328     else if (output_charset)
329     {
330         wrbuf_printf(p->wr_error, "Element <marc>: "
331                      "attribute 'inputcharset' missing");
332         return -1;
333     }
334     r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC);
335     r->u.marc.iconv_t = cd;
336
337     r->u.marc.input_format = input_format_mode;
338     r->u.marc.output_format = output_format_mode;
339     return 0;
340 }
341
342 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
343 {
344     yaz_record_conv_reset(p);
345
346     /* parsing element children */
347     for (ptr = ptr->children; ptr; ptr = ptr->next)
348         {
349             if (ptr->type != XML_ELEMENT_NODE)
350                 continue;
351             if (!strcmp((const char *) ptr->name, "xslt"))
352                 {
353                     if (conv_xslt(p, ptr))
354                         return -1;
355                 }
356             else if (!strcmp((const char *) ptr->name, "marc"))
357                 {
358                     if (conv_marc(p, ptr))
359                         return -1;
360                 }
361             else
362                 {
363                     wrbuf_printf(p->wr_error, "Element <backend>: expected "
364                                  "<marc> or <xslt> element, got <%s>"
365                                  , ptr->name);
366                     return -1;
367                 }
368         }
369     return 0;
370 }
371
372 int yaz_record_conv_record(yaz_record_conv_t p,
373                            const char *input_record_buf,
374                            size_t input_record_len,
375                            WRBUF output_record)
376 {
377     int ret = 0;
378     WRBUF record = output_record; /* pointer transfer */
379     struct yaz_record_conv_rule *r = p->rules;
380     wrbuf_rewind(p->wr_error);
381     
382     wrbuf_write(record, input_record_buf, input_record_len);
383     for (; ret == 0 && r; r = r->next)
384     {
385         if (r->which == YAZ_RECORD_CONV_RULE_MARC)
386         {
387             yaz_marc_t mt = yaz_marc_create();
388
389             yaz_marc_xml(mt, r->u.marc.output_format);
390
391             if (r->u.marc.iconv_t)
392                 yaz_marc_iconv(mt, r->u.marc.iconv_t);
393             if (r->u.marc.input_format == YAZ_MARC_ISO2709)
394             {
395                 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
396                                                wrbuf_len(record));
397                 if (sz > 0)
398                     ret = 0;
399                 else
400                     ret = -1;
401             }
402             else if (r->u.marc.input_format == YAZ_MARC_MARCXML)
403             {
404                 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
405                                                wrbuf_len(record));
406                 if (!doc)
407                 {
408                     wrbuf_printf(p->wr_error, "xmlParseMemory failed");
409                     ret = -1;
410                 }
411                 else
412                 {
413                     ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
414                     if (ret)
415                         wrbuf_printf(p->wr_error, "yaz_marc_read_xml failed");
416                 }
417                 xmlFreeDoc(doc);
418             }
419             else
420             {
421                 wrbuf_printf(p->wr_error, "unsupported input format");
422                 ret = -1;
423             }
424             if (ret == 0)
425             {
426                 wrbuf_rewind(record);
427                 ret = yaz_marc_write_mode(mt, record);
428                 if (ret)
429                     wrbuf_printf(p->wr_error, "yaz_marc_write_mode failed");
430             }
431             yaz_marc_destroy(mt);
432         }
433 #if YAZ_HAVE_XSLT
434         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
435         {
436             xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
437                                            wrbuf_len(record));
438             if (!doc)
439             {
440                 wrbuf_printf(p->wr_error, "xmlParseMemory failed");
441                 ret = -1;
442             }
443             else
444             {
445                 xmlDocPtr res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0);
446                 if (res)
447                 {
448                     xmlChar *out_buf = 0;
449                     int out_len;
450
451 #if YAZ_HAVE_XSLTSAVERESULTTOSTRING
452                     xsltSaveResultToString(&out_buf, &out_len, res,
453                                            r->u.xslt.xsp); 
454 #else
455                     xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
456 #endif
457                     if (!out_buf)
458                     {
459                         wrbuf_printf(p->wr_error,
460                                      "xsltSaveResultToString failed");
461                         ret = -1;
462                     }
463                     else
464                     {
465                         wrbuf_rewind(record);
466                         wrbuf_write(record, (const char *) out_buf, out_len);
467                         
468                         xmlFree(out_buf);
469                     }
470                     xmlFreeDoc(res);
471                 }
472                 else
473                 {
474                     wrbuf_printf(p->wr_error, "xsltApplyStylesheet failed");
475                     ret = -1;
476                 }
477                 xmlFreeDoc(doc);
478             }
479         }
480 #endif
481     }
482     return ret;
483 }
484
485 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
486 {
487     return wrbuf_buf(p->wr_error);
488 }
489
490 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
491 {
492     xfree(p->path);
493     p->path = 0;
494     if (path)
495         p->path = xstrdup(path);
496 }
497 #endif
498
499 /*
500  * Local variables:
501  * c-basic-offset: 4
502  * indent-tabs-mode: nil
503  * End:
504  * vim: shiftwidth=4 tabstop=8 expandtab
505  */
506