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