Removed duplicate variable for turbo marcxml (using_turbo_format). Now works in zooms...
[yaz-moved-to-github.git] / src / record_conv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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 /** \brief tranformation info (rule info) */
62 struct yaz_record_conv_rule {
63     enum YAZ_RECORD_CONV_RULE which;
64     union {
65 #if YAZ_HAVE_XSLT
66         struct {
67             xmlDocPtr xsp_doc;
68         } xslt;
69 #endif
70         struct {
71             const char *input_charset;
72             const char *output_charset;
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             ;
90         }
91 #if YAZ_HAVE_XSLT
92         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
93         {
94             xmlFreeDoc(r->u.xslt.xsp_doc);
95         }
96 #endif
97     }
98     wrbuf_rewind(p->wr_error);
99     nmem_reset(p->nmem);
100
101     p->rules = 0;
102
103     p->rules_p = &p->rules;
104 }
105
106 yaz_record_conv_t yaz_record_conv_create()
107 {
108     yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
109     p->nmem = nmem_create();
110     p->wr_error = wrbuf_alloc();
111     p->rules = 0;
112     p->path = 0;
113
114 #if YAZ_HAVE_EXSLT
115     exsltRegisterAll(); 
116 #endif
117     yaz_record_conv_reset(p);
118     return p;
119 }
120
121 void yaz_record_conv_destroy(yaz_record_conv_t p)
122 {
123     if (p)
124     {
125         yaz_record_conv_reset(p);
126         nmem_destroy(p->nmem);
127         wrbuf_destroy(p->wr_error);
128         xfree(p->path);
129         xfree(p);
130     }
131 }
132
133 /** \brief adds a rule */
134 static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
135                                              enum YAZ_RECORD_CONV_RULE type)
136 {
137     struct yaz_record_conv_rule *r = (struct yaz_record_conv_rule *)
138         nmem_malloc(p->nmem, sizeof(*r));
139     r->which = type;
140     r->next = 0;
141     *p->rules_p = r;
142     p->rules_p = &r->next;
143     return r;
144 }
145
146 /** \brief parse 'xslt' conversion node */
147 static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
148 {
149 #if YAZ_HAVE_XSLT
150     struct _xmlAttr *attr;
151     const char *stylesheet = 0;
152
153     for (attr = ptr->properties; attr; attr = attr->next)
154     {
155         if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
156             attr->children && attr->children->type == XML_TEXT_NODE)
157             stylesheet = (const char *) attr->children->content;
158         else
159         {
160             wrbuf_printf(p->wr_error, "Bad attribute '%s'"
161                          "Expected stylesheet.", attr->name);
162             return -1;
163         }
164     }
165     if (!stylesheet)
166     {
167         wrbuf_printf(p->wr_error, "Element <xslt>: "
168                      "attribute 'stylesheet' expected");
169         return -1;
170     }
171     else
172     {
173         char fullpath[1024];
174         xsltStylesheetPtr xsp;
175         xmlDocPtr xsp_doc;
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_doc = xmlParseFile(fullpath);
187         if (!xsp_doc)
188         {
189             wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
190                          " xml parse failed: %s", stylesheet, fullpath);
191             if (p->path)
192                 wrbuf_printf(p->wr_error, " with path '%s'", p->path);
193             return -1;
194         }
195         xsp = xsltParseStylesheetDoc(xsp_doc);
196         if (!xsp)
197         {
198             wrbuf_printf(p->wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
199                          " xslt parse failed: %s", stylesheet, fullpath);
200             if (p->path)
201                 wrbuf_printf(p->wr_error, " with path '%s'", p->path);
202             wrbuf_printf(p->wr_error, " ("
203 #if YAZ_HAVE_EXSLT
204                          
205                          "EXSLT enabled"
206 #else
207                          "EXSLT not supported"
208 #endif
209                          ")");
210             return -1;
211         }
212         else
213         {
214             struct yaz_record_conv_rule *r = 
215                 add_rule(p, YAZ_RECORD_CONV_RULE_XSLT);
216             r->u.xslt.xsp_doc = xmlCopyDoc(xsp_doc, 1);
217             xsltFreeStylesheet(xsp); /* will free xsp_doc */
218         }
219     }
220     return 0;
221 #else
222     wrbuf_printf(p->wr_error, "xslt unsupported."
223                  " YAZ compiled without XSLT support");
224     return -1;
225 #endif
226 }
227
228 /** \brief parse 'marc' conversion node */
229 static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
230 {
231     struct _xmlAttr *attr;
232     const char *input_charset = 0;
233     const char *output_charset = 0;
234     const char *input_format = 0;
235     const char *output_format = 0;
236     int input_format_mode = 0;
237     int output_format_mode = 0;
238     struct yaz_record_conv_rule *r;
239
240     for (attr = ptr->properties; attr; attr = attr->next)
241     {
242         if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
243             attr->children && attr->children->type == XML_TEXT_NODE)
244             input_charset = (const char *) attr->children->content;
245         else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
246             attr->children && attr->children->type == XML_TEXT_NODE)
247             output_charset = (const char *) attr->children->content;
248         else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
249             attr->children && attr->children->type == XML_TEXT_NODE)
250             input_format = (const char *) attr->children->content;
251         else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
252             attr->children && attr->children->type == XML_TEXT_NODE)
253             output_format = (const char *) attr->children->content;
254         else
255         {
256             wrbuf_printf(p->wr_error, "Element <marc>: expected attributes"
257                          "'inputformat', 'inputcharset', 'outputformat' or"
258                          " 'outputcharset', got attribute '%s'", 
259                          attr->name);
260             return -1;
261         }
262     }
263     if (!input_format)
264     {
265         wrbuf_printf(p->wr_error, "Element <marc>: "
266                      "attribute 'inputformat' required");
267         return -1;
268     }
269     else if (!strcmp(input_format, "marc"))
270     {
271         input_format_mode = YAZ_MARC_ISO2709;
272     }
273     else if (!strcmp(input_format, "xml"))
274     {
275         input_format_mode = YAZ_MARC_MARCXML;
276         /** Libxml2 generates UTF-8 encoding by default .
277             So we convert from UTF-8 to outputcharset (if defined) 
278         */
279         if (!input_charset && output_charset)
280             input_charset = "utf-8";
281     }
282     else
283     {
284         wrbuf_printf(p->wr_error, "Element <marc inputformat='%s'>: "
285                      " Unsupported input format"
286                      " defined by attribute value", 
287                      input_format);
288         return -1;
289     }
290     
291     if (!output_format)
292     {
293         wrbuf_printf(p->wr_error, 
294                      "Element <marc>: attribute 'outputformat' required");
295         return -1;
296     }
297     else if (!strcmp(output_format, "line"))
298     {
299         output_format_mode = YAZ_MARC_LINE;
300     }
301     else if (!strcmp(output_format, "marcxml"))
302     {
303         output_format_mode = YAZ_MARC_MARCXML;
304         if (input_charset && !output_charset)
305             output_charset = "utf-8";
306     }
307     else if (!strcmp(output_format, "marc"))
308     {
309         output_format_mode = YAZ_MARC_ISO2709;
310     }
311     else if (!strcmp(output_format, "marcxchange"))
312     {
313         output_format_mode = YAZ_MARC_XCHANGE;
314         if (input_charset && !output_charset)
315             output_charset = "utf-8";
316     }
317     else
318     {
319         wrbuf_printf(p->wr_error, "Element <marc outputformat='%s'>: "
320                      " Unsupported output format"
321                      " defined by attribute value", 
322                      output_format);
323         return -1;
324     }
325     if (input_charset && output_charset)
326     {
327         yaz_iconv_t cd = yaz_iconv_open(output_charset, input_charset);
328         if (!cd)
329         {
330             wrbuf_printf(p->wr_error, 
331                          "Element <marc inputcharset='%s' outputcharset='%s'>:"
332                          " Unsupported character set mapping"
333                          " defined by attribute values",
334                          input_charset, output_charset);
335             return -1;
336         }
337         yaz_iconv_close(cd);
338     }
339     else if (input_charset)
340     {
341         wrbuf_printf(p->wr_error, "Element <marc>: "
342                      "attribute 'outputcharset' missing");
343         return -1;
344     }
345     else if (output_charset)
346     {
347         wrbuf_printf(p->wr_error, "Element <marc>: "
348                      "attribute 'inputcharset' missing");
349         return -1;
350     }
351     r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC);
352
353     r->u.marc.input_charset = nmem_strdup(p->nmem, input_charset);
354     r->u.marc.output_charset = nmem_strdup(p->nmem, output_charset);
355     r->u.marc.input_format = input_format_mode;
356     r->u.marc.output_format = output_format_mode;
357     return 0;
358 }
359
360 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
361 {
362     yaz_record_conv_reset(p);
363
364     /* parsing element children */
365     for (ptr = ptr->children; ptr; ptr = ptr->next)
366         {
367             if (ptr->type != XML_ELEMENT_NODE)
368                 continue;
369             if (!strcmp((const char *) ptr->name, "xslt"))
370                 {
371                     if (conv_xslt(p, ptr))
372                         return -1;
373                 }
374             else if (!strcmp((const char *) ptr->name, "marc"))
375                 {
376                     if (conv_marc(p, ptr))
377                         return -1;
378                 }
379             else
380                 {
381                     wrbuf_printf(p->wr_error, "Element <backend>: expected "
382                                  "<marc> or <xslt> element, got <%s>"
383                                  , ptr->name);
384                     return -1;
385                 }
386         }
387     return 0;
388 }
389
390 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
391                                        struct yaz_record_conv_rule *r,
392                                        const char *input_record_buf,
393                                        size_t input_record_len,
394                                        WRBUF output_record);
395
396 int yaz_record_conv_opac_record(yaz_record_conv_t p,
397                                 Z_OPACRecord *input_record,
398                                 WRBUF output_record)
399 {
400     int ret = 0;
401     struct yaz_record_conv_rule *r = p->rules;
402     if (!r || r->which != YAZ_RECORD_CONV_RULE_MARC)
403         ret = -1; /* no marc rule so we can't do OPAC */
404     else
405     {
406         WRBUF res = wrbuf_alloc();
407         yaz_marc_t mt = yaz_marc_create();
408         yaz_iconv_t cd = yaz_iconv_open(r->u.marc.output_charset,
409                                         r->u.marc.input_charset);
410         
411         wrbuf_rewind(p->wr_error);
412         yaz_marc_xml(mt, r->u.marc.output_format);
413         
414         yaz_marc_iconv(mt, cd);
415         
416         yaz_opac_decode_wrbuf(mt, input_record, res);
417         if (ret != -1)
418         {
419             ret = yaz_record_conv_record_rule(p, 
420                                               r->next,
421                                               wrbuf_buf(res), wrbuf_len(res),
422                                               output_record);
423         }
424         yaz_marc_destroy(mt);
425         if (cd)
426             yaz_iconv_close(cd);
427         wrbuf_destroy(res);
428     }
429     return ret;
430 }
431
432 int yaz_record_conv_record(yaz_record_conv_t p,
433                            const char *input_record_buf,
434                            size_t input_record_len,
435                            WRBUF output_record)
436 {
437     return yaz_record_conv_record_rule(p, p->rules,
438                                        input_record_buf,
439                                        input_record_len, output_record);
440 }
441
442 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
443                                        struct yaz_record_conv_rule *r,
444                                        const char *input_record_buf,
445                                        size_t input_record_len,
446                                        WRBUF output_record)
447 {
448     int ret = 0;
449     WRBUF record = output_record; /* pointer transfer */
450     wrbuf_rewind(p->wr_error);
451     
452     wrbuf_write(record, input_record_buf, input_record_len);
453     for (; ret == 0 && r; r = r->next)
454     {
455         if (r->which == YAZ_RECORD_CONV_RULE_MARC)
456         {
457             yaz_iconv_t cd = 
458                 yaz_iconv_open(r->u.marc.output_charset,
459                                r->u.marc.input_charset);
460             yaz_marc_t mt = yaz_marc_create();
461
462             yaz_marc_xml(mt, r->u.marc.output_format);
463
464             if (cd)
465                 yaz_marc_iconv(mt, cd);
466             if (r->u.marc.input_format == YAZ_MARC_ISO2709)
467             {
468                 int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
469                                                wrbuf_len(record));
470                 if (sz > 0)
471                     ret = 0;
472                 else
473                     ret = -1;
474             }
475             else if (r->u.marc.input_format == YAZ_MARC_MARCXML ||
476                                          r->u.marc.input_format == YAZ_MARC_TMARCXML)
477             {
478                 xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
479                                                wrbuf_len(record));
480                 if (!doc)
481                 {
482                     wrbuf_printf(p->wr_error, "xmlParseMemory failed");
483                     ret = -1;
484                 }
485                 else
486                 {
487                                         ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
488                     if (ret)
489                         wrbuf_printf(p->wr_error, "yaz_marc_read_xml failed");
490                 }
491                 xmlFreeDoc(doc);
492             }
493             else
494             {
495                 wrbuf_printf(p->wr_error, "unsupported input format");
496                 ret = -1;
497             }
498             if (ret == 0)
499             {
500                 wrbuf_rewind(record);
501                 ret = yaz_marc_write_mode(mt, record);
502                 if (ret)
503                     wrbuf_printf(p->wr_error, "yaz_marc_write_mode failed");
504             }
505             if (cd)
506                 yaz_iconv_close(cd);
507             yaz_marc_destroy(mt);
508         }
509 #if YAZ_HAVE_XSLT
510         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
511         {
512             xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
513                                            wrbuf_len(record));
514             if (!doc)
515             {
516                 wrbuf_printf(p->wr_error, "xmlParseMemory failed");
517                 ret = -1;
518             }
519             else
520             {
521                 xmlDocPtr xsp_doc = xmlCopyDoc(r->u.xslt.xsp_doc, 1);
522                 xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
523                 xmlDocPtr res = xsltApplyStylesheet(xsp, doc, 0);
524                 if (res)
525                 {
526                     xmlChar *out_buf = 0;
527                     int out_len;
528
529 #if YAZ_HAVE_XSLTSAVERESULTTOSTRING
530                     xsltSaveResultToString(&out_buf, &out_len, res, xsp);
531 #else
532                     xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
533 #endif
534                     if (!out_buf)
535                     {
536                         wrbuf_printf(p->wr_error,
537                                      "xsltSaveResultToString failed");
538                         ret = -1;
539                     }
540                     else
541                     {
542                         wrbuf_rewind(record);
543                         wrbuf_write(record, (const char *) out_buf, out_len);
544                         
545                         xmlFree(out_buf);
546                     }
547                     xmlFreeDoc(res);
548                 }
549                 else
550                 {
551                     wrbuf_printf(p->wr_error, "xsltApplyStylesheet failed");
552                     ret = -1;
553                 }
554                 xmlFreeDoc(doc);
555                 xsltFreeStylesheet(xsp); /* frees xsp_doc too */
556             }
557         }
558 #endif
559     }
560     return ret;
561 }
562
563 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
564 {
565     return wrbuf_cstr(p->wr_error);
566 }
567
568 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
569 {
570     xfree(p->path);
571     p->path = 0;
572     if (path)
573         p->path = xstrdup(path);
574 }
575 #endif
576
577 /*
578  * Local variables:
579  * c-basic-offset: 4
580  * c-file-style: "Stroustrup"
581  * indent-tabs-mode: nil
582  * End:
583  * vim: shiftwidth=4 tabstop=8 expandtab
584  */
585