e6e1cdda3924f11b78d074d745029bc161baef28
[yaz-moved-to-github.git] / src / record_conv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 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 #include <libxml/xpath.h>
29 #include <libxml/xpathInternals.h>
30 #if YAZ_HAVE_XSLT
31 #include <libxslt/xsltutils.h>
32 #include <libxslt/transform.h>
33 #endif
34 #if YAZ_HAVE_EXSLT
35 #include <libexslt/exslt.h>
36 #endif
37
38 /** \brief The internal structure for yaz_record_conv_t */
39 struct yaz_record_conv_struct {
40     /** \brief memory for configuration */
41     NMEM nmem;
42
43     /** \brief conversion rules (allocated using NMEM) */
44     struct yaz_record_conv_rule *rules;
45
46     /** \brief pointer to last conversion rule pointer in chain */
47     struct yaz_record_conv_rule **rules_p;
48
49     /** \brief string buffer for error messages */
50     WRBUF wr_error;
51
52     /** \brief path for opening files  */
53     char *path;
54 };
55
56 struct marc_info {
57     NMEM nmem;
58     const char *input_charset;
59     const char *output_charset;
60     int input_format_mode;
61     int output_format_mode;
62     const char *leader_spec;
63 };
64
65 /** \brief tranformation info (rule info) */
66 struct yaz_record_conv_rule {
67     struct yaz_record_conv_type *type;
68     void *info;
69     struct yaz_record_conv_rule *next;
70 };
71
72 /** \brief reset rules+configuration */
73 static void yaz_record_conv_reset(yaz_record_conv_t p)
74 {
75
76     struct yaz_record_conv_rule *r;
77     for (r = p->rules; r; r = r->next)
78     {
79         r->type->destroy(r->info);
80     }
81     wrbuf_rewind(p->wr_error);
82     nmem_reset(p->nmem);
83
84     p->rules = 0;
85
86     p->rules_p = &p->rules;
87 }
88
89 void yaz_record_conv_destroy(yaz_record_conv_t p)
90 {
91     if (p)
92     {
93         yaz_record_conv_reset(p);
94         nmem_destroy(p->nmem);
95         wrbuf_destroy(p->wr_error);
96
97         xfree(p->path);
98         xfree(p);
99     }
100 }
101
102 #if YAZ_HAVE_XSLT
103 struct xslt_info {
104     NMEM nmem;
105     xmlDocPtr xsp_doc;
106     const char **xsl_parms;
107 };
108
109 static void *construct_xslt(const xmlNode *ptr,
110                             const char *path, WRBUF wr_error)
111 {
112     struct _xmlAttr *attr;
113     const char *stylesheet = 0;
114     struct xslt_info *info = 0;
115     NMEM nmem = 0;
116     int max_parms = 10;
117     int no_parms = 0;
118
119     if (strcmp((const char *) ptr->name, "xslt"))
120         return 0;
121
122     for (attr = ptr->properties; attr; attr = attr->next)
123     {
124         if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
125             attr->children && attr->children->type == XML_TEXT_NODE)
126             stylesheet = (const char *) attr->children->content;
127         else
128         {
129             wrbuf_printf(wr_error, "Bad attribute '%s'"
130                          "Expected stylesheet.", attr->name);
131             return 0;
132         }
133     }
134     nmem = nmem_create();
135     info = nmem_malloc(nmem, sizeof(*info));
136     info->nmem = nmem;
137     info->xsl_parms = nmem_malloc(
138         nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms));
139
140     for (ptr = ptr->children; ptr; ptr = ptr->next)
141     {
142         const char *name = 0;
143         const char *value = 0;
144         char *qvalue = 0;
145         if (ptr->type != XML_ELEMENT_NODE)
146             continue;
147         if (strcmp((const char *) ptr->name, "param"))
148         {
149             wrbuf_printf(wr_error, "Bad element '%s'"
150                          "Expected param.", ptr->name);
151             nmem_destroy(nmem);
152             return 0;
153         }
154         for (attr = ptr->properties; attr; attr = attr->next)
155         {
156             if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
157                 attr->children && attr->children->type == XML_TEXT_NODE)
158                 name = (const char *) attr->children->content;
159             else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
160                 attr->children && attr->children->type == XML_TEXT_NODE)
161                 value = (const char *) attr->children->content;
162             else
163             {
164                 wrbuf_printf(wr_error, "Bad attribute '%s'"
165                              "Expected name or value.", attr->name);
166                 nmem_destroy(nmem);
167                 return 0;
168             }
169         }
170         if (!name || !value)
171         {
172             wrbuf_printf(wr_error, "Missing attributes name or value");
173             nmem_destroy(nmem);
174             return 0;
175         }
176         if (no_parms >= max_parms)
177         {
178             wrbuf_printf(wr_error, "Too many parameters given");
179             nmem_destroy(nmem);
180             return 0;
181         }
182
183         qvalue = nmem_malloc(nmem, strlen(value) + 3);
184         strcpy(qvalue, "\'");
185         strcat(qvalue, value);
186         strcat(qvalue, "\'");
187
188         info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name);
189         info->xsl_parms[2 * no_parms + 1] = qvalue;
190         no_parms++;
191     }
192
193     info->xsl_parms[2 * no_parms] = '\0';
194
195     if (!stylesheet)
196     {
197         wrbuf_printf(wr_error, "Element <xslt>: "
198                      "attribute 'stylesheet' expected");
199         nmem_destroy(nmem);
200     }
201     else
202     {
203         char fullpath[1024];
204         xsltStylesheetPtr xsp;
205         if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath))
206         {
207             wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:"
208                          " could not locate stylesheet '%s'",
209                          stylesheet, stylesheet);
210             if (path)
211                 wrbuf_printf(wr_error, " with path '%s'", path);
212
213             nmem_destroy(nmem);
214             return 0;
215         }
216         info->xsp_doc = xmlParseFile(fullpath);
217         if (!info->xsp_doc)
218         {
219             wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
220                          " xml parse failed: %s", stylesheet, fullpath);
221             if (path)
222                 wrbuf_printf(wr_error, " with path '%s'", path);
223             nmem_destroy(nmem);
224             return 0;
225         }
226         /* need to copy this before passing it to the processor. It will
227            be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
228         xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1));
229         if (!xsp)
230         {
231             wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
232                          " xslt parse failed: %s", stylesheet, fullpath);
233             if (path)
234                 wrbuf_printf(wr_error, " with path '%s'", path);
235             wrbuf_printf(wr_error, " ("
236 #if YAZ_HAVE_EXSLT
237
238                          "EXSLT enabled"
239 #else
240                          "EXSLT not supported"
241 #endif
242                          ")");
243             xmlFreeDoc(info->xsp_doc);
244             nmem_destroy(info->nmem);
245         }
246         else
247         {
248             xsltFreeStylesheet(xsp);
249             return info;
250         }
251     }
252     return 0;
253 }
254
255 static int convert_xslt(void *vinfo, WRBUF record, WRBUF wr_error)
256 {
257     int ret = 0;
258     struct xslt_info *info = vinfo;
259
260     xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
261                                    wrbuf_len(record));
262     if (!doc)
263     {
264         wrbuf_printf(wr_error, "xmlParseMemory failed");
265         ret = -1;
266     }
267     else
268     {
269         xmlDocPtr xsp_doc = xmlCopyDoc(info->xsp_doc, 1);
270         xsltStylesheetPtr xsp = xsltParseStylesheetDoc(xsp_doc);
271         xmlDocPtr res = xsltApplyStylesheet(xsp, doc, info->xsl_parms);
272         if (res)
273         {
274             xmlChar *out_buf = 0;
275             int out_len;
276
277 #if HAVE_XSLTSAVERESULTTOSTRING
278             xsltSaveResultToString(&out_buf, &out_len, res, xsp);
279 #else
280             xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
281 #endif
282             if (!out_buf)
283             {
284                 wrbuf_printf(wr_error,
285                              "xsltSaveResultToString failed");
286                 ret = -1;
287             }
288             else
289             {
290                 wrbuf_rewind(record);
291                 wrbuf_write(record, (const char *) out_buf, out_len);
292
293                 xmlFree(out_buf);
294             }
295             xmlFreeDoc(res);
296         }
297         else
298         {
299             wrbuf_printf(wr_error, "xsltApplyStylesheet failed");
300             ret = -1;
301         }
302         xmlFreeDoc(doc);
303         xsltFreeStylesheet(xsp); /* frees xsp_doc too */
304     }
305     return ret;
306 }
307
308 static void destroy_xslt(void *vinfo)
309 {
310     struct xslt_info *info = vinfo;
311
312     if (info)
313     {
314         xmlFreeDoc(info->xsp_doc);
315         nmem_destroy(info->nmem);
316     }
317 }
318
319 /* YAZ_HAVE_XSLT */
320 #endif
321
322 struct select_info {
323     NMEM nmem;
324     char *xpath_expr;
325 };
326
327 static void *construct_select(const xmlNode *ptr,
328                               const char *path, WRBUF wr_error)
329 {
330     if (strcmp((const char *) ptr->name, "select"))
331         return 0;
332     else
333     {
334         struct _xmlAttr *attr;
335         NMEM nmem = nmem_create();
336         struct select_info *info = nmem_malloc(nmem, sizeof(*info));
337
338         info->nmem = nmem;
339         info->xpath_expr = 0;
340         for (attr = ptr->properties; attr; attr = attr->next)
341         {
342             if (!xmlStrcmp(attr->name, BAD_CAST "path") &&
343                 attr->children && attr->children->type == XML_TEXT_NODE)
344                 info->xpath_expr =
345                     nmem_strdup(nmem, (const char *) attr->children->content);
346             else
347             {
348                 wrbuf_printf(wr_error, "Bad attribute '%s'"
349                              "Expected xpath.", attr->name);
350                 nmem_destroy(nmem);
351                 return 0;
352             }
353         }
354         return info;
355     }
356 }
357
358 static int convert_select(void *vinfo, WRBUF record, WRBUF wr_error)
359 {
360     int ret = 0;
361     struct select_info *info = vinfo;
362
363     xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
364                                    wrbuf_len(record));
365     if (!doc)
366     {
367         wrbuf_printf(wr_error, "xmlParseMemory failed");
368         ret = -1;
369     }
370     else
371     {
372         xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
373         if (xpathCtx && info->xpath_expr)
374         {
375             xmlXPathObjectPtr xpathObj =
376                 xmlXPathEvalExpression((const xmlChar *) info->xpath_expr,
377                                        xpathCtx);
378             if (xpathObj)
379             {
380                 xmlNodeSetPtr nodes = xpathObj->nodesetval;
381                 wrbuf_rewind(record);
382                 if (nodes)
383                 {
384                     int i;
385                     for (i = 0; i < nodes->nodeNr; i++)
386                     {
387                         xmlNode *ptr = nodes->nodeTab[i];
388                         if (ptr->type == XML_ELEMENT_NODE)
389                             ptr = ptr->children;
390                         if (ptr->type == XML_TEXT_NODE)
391                             for (; ptr; ptr = ptr->next)
392                                 wrbuf_puts(record, (const char *) ptr->content);
393                     }
394                 }
395                 xmlXPathFreeObject(xpathObj);
396             }
397             xmlXPathFreeContext(xpathCtx);
398         }
399         xmlFreeDoc(doc);
400     }
401     return ret;
402 }
403
404 static void destroy_select(void *vinfo)
405 {
406     struct select_info *info = vinfo;
407
408     if (info)
409         nmem_destroy(info->nmem);
410 }
411
412
413 static void *construct_solrmarc(const xmlNode *ptr,
414                                 const char *path, WRBUF wr_error)
415 {
416     if (strcmp((const char *) ptr->name, "solrmarc"))
417         return 0;
418     return wr_error; /* any non-null ptr will do; we don't use it later*/
419 }
420
421 static int convert_solrmarc(void *info, WRBUF record, WRBUF wr_error)
422 {
423     WRBUF w = wrbuf_alloc();
424     const char *buf = wrbuf_buf(record);
425     size_t i, sz = wrbuf_len(record);
426     for (i = 0; i < sz; i++)
427     {
428         int ch;
429         if (buf[i] == '#' && i < sz - 3 && buf[i+3] == ';'
430             && atoi_n_check(buf+i+1, 2, &ch))
431             i += 3;
432         else
433             ch = buf[i];
434         wrbuf_putc(w, ch);
435     }
436     wrbuf_rewind(record);
437     wrbuf_write(record, wrbuf_buf(w), wrbuf_len(w));
438     wrbuf_destroy(w);
439     return 0;
440 }
441
442 static void destroy_solrmarc(void *info)
443 {
444 }
445
446 static void *construct_marc(const xmlNode *ptr,
447                             const char *path, WRBUF wr_error)
448 {
449     NMEM nmem = nmem_create();
450     struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
451     struct _xmlAttr *attr;
452     const char *input_format = 0;
453     const char *output_format = 0;
454
455     if (strcmp((const char *) ptr->name, "marc"))
456     {
457         nmem_destroy(nmem);
458         return 0;
459     }
460     info->nmem = nmem;
461     info->input_charset = 0;
462     info->output_charset = 0;
463     info->input_format_mode = 0;
464     info->output_format_mode = 0;
465     info->leader_spec = 0;
466
467     for (attr = ptr->properties; attr; attr = attr->next)
468     {
469         if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
470             attr->children && attr->children->type == XML_TEXT_NODE)
471             info->input_charset = (const char *) attr->children->content;
472         else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
473             attr->children && attr->children->type == XML_TEXT_NODE)
474             info->output_charset = (const char *) attr->children->content;
475         else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
476             attr->children && attr->children->type == XML_TEXT_NODE)
477             input_format = (const char *) attr->children->content;
478         else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
479             attr->children && attr->children->type == XML_TEXT_NODE)
480             output_format = (const char *) attr->children->content;
481         else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
482                  attr->children && attr->children->type == XML_TEXT_NODE)
483             info->leader_spec =
484                 nmem_strdup(info->nmem,(const char *) attr->children->content);
485         else
486         {
487             wrbuf_printf(wr_error, "Element <marc>: expected attributes"
488                          "'inputformat', 'inputcharset', 'outputformat' or"
489                          " 'outputcharset', got attribute '%s'",
490                          attr->name);
491             nmem_destroy(info->nmem);
492             return 0;
493         }
494     }
495     if (!input_format)
496     {
497         wrbuf_printf(wr_error, "Element <marc>: "
498                      "attribute 'inputformat' required");
499         nmem_destroy(info->nmem);
500         return 0;
501     }
502     else if (!strcmp(input_format, "marc"))
503     {
504         info->input_format_mode = YAZ_MARC_ISO2709;
505     }
506     else if (!strcmp(input_format, "xml"))
507     {
508         info->input_format_mode = YAZ_MARC_MARCXML;
509         /** Libxml2 generates UTF-8 encoding by default .
510             So we convert from UTF-8 to outputcharset (if defined)
511         */
512         if (!info->input_charset && info->output_charset)
513             info->input_charset = "utf-8";
514     }
515     else if (!strcmp(input_format, "json"))
516     {
517         info->input_format_mode = YAZ_MARC_JSON;
518     }
519     else
520     {
521         wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
522                      " Unsupported input format"
523                      " defined by attribute value",
524                      input_format);
525         nmem_destroy(info->nmem);
526         return 0;
527     }
528
529     if (!output_format)
530     {
531         wrbuf_printf(wr_error,
532                      "Element <marc>: attribute 'outputformat' required");
533         nmem_destroy(info->nmem);
534         return 0;
535     }
536     else if (!strcmp(output_format, "line"))
537     {
538         info->output_format_mode = YAZ_MARC_LINE;
539     }
540     else if (!strcmp(output_format, "marcxml"))
541     {
542         info->output_format_mode = YAZ_MARC_MARCXML;
543         if (info->input_charset && !info->output_charset)
544             info->output_charset = "utf-8";
545     }
546     else if (!strcmp(output_format, "turbomarc"))
547     {
548         info->output_format_mode = YAZ_MARC_TURBOMARC;
549         if (info->input_charset && !info->output_charset)
550             info->output_charset = "utf-8";
551     }
552     else if (!strcmp(output_format, "marc"))
553     {
554         info->output_format_mode = YAZ_MARC_ISO2709;
555     }
556     else if (!strcmp(output_format, "marcxchange"))
557     {
558         info->output_format_mode = YAZ_MARC_XCHANGE;
559         if (info->input_charset && !info->output_charset)
560             info->output_charset = "utf-8";
561     }
562     else if (!strcmp(output_format, "json"))
563     {
564         info->output_format_mode = YAZ_MARC_JSON;
565         if (info->input_charset && !info->output_charset)
566             info->output_charset = "utf-8";
567     }
568     else
569     {
570         wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
571                      " Unsupported output format"
572                      " defined by attribute value",
573                      output_format);
574         nmem_destroy(info->nmem);
575         return 0;
576     }
577     if (info->input_charset && info->output_charset)
578     {
579         yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
580                                         info->input_charset);
581         if (!cd)
582         {
583             wrbuf_printf(wr_error,
584                          "Element <marc inputcharset='%s' outputcharset='%s'>:"
585                          " Unsupported character set mapping"
586                          " defined by attribute values",
587                          info->input_charset, info->output_charset);
588             nmem_destroy(info->nmem);
589             return 0;
590         }
591         yaz_iconv_close(cd);
592     }
593     else if (!info->output_charset)
594     {
595         wrbuf_printf(wr_error, "Element <marc>: "
596                      "attribute 'outputcharset' missing");
597         nmem_destroy(info->nmem);
598         return 0;
599     }
600     else if (!info->input_charset)
601     {
602         wrbuf_printf(wr_error, "Element <marc>: "
603                      "attribute 'inputcharset' missing");
604         nmem_destroy(info->nmem);
605         return 0;
606     }
607     info->input_charset = nmem_strdup(info->nmem, info->input_charset);
608     info->output_charset = nmem_strdup(info->nmem, info->output_charset);
609     return info;
610 }
611
612 static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
613 {
614     struct marc_info *mi = info;
615     const char *input_charset = mi->input_charset;
616     int ret = 0;
617     yaz_marc_t mt = yaz_marc_create();
618
619     yaz_marc_xml(mt, mi->output_format_mode);
620     if (mi->leader_spec)
621         yaz_marc_leader_spec(mt, mi->leader_spec);
622
623     if (mi->input_format_mode == YAZ_MARC_ISO2709)
624     {
625         int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
626                                        wrbuf_len(record));
627         if (sz > 0)
628         {
629             if (yaz_marc_check_marc21_coding(input_charset, wrbuf_buf(record),
630                                              wrbuf_len(record)))
631                 input_charset = "utf-8";
632             ret = 0;
633         }
634         else
635             ret = -1;
636     }
637     else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
638              mi->input_format_mode == YAZ_MARC_TURBOMARC)
639     {
640         xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
641                                        wrbuf_len(record));
642         if (!doc)
643         {
644             wrbuf_printf(wr_error, "xmlParseMemory failed");
645             ret = -1;
646         }
647         else
648         {
649             ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
650             if (ret)
651                 wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
652         }
653         xmlFreeDoc(doc);
654     }
655     else
656     {
657         wrbuf_printf(wr_error, "unsupported input format");
658         ret = -1;
659     }
660     if (ret == 0)
661     {
662         yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, input_charset);
663
664         if (cd)
665             yaz_marc_iconv(mt, cd);
666
667         wrbuf_rewind(record);
668         ret = yaz_marc_write_mode(mt, record);
669         if (ret)
670             wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
671         if (cd)
672             yaz_iconv_close(cd);
673     }
674     yaz_marc_destroy(mt);
675     return ret;
676 }
677
678 static void destroy_marc(void *info)
679 {
680     struct marc_info *mi = info;
681
682     nmem_destroy(mi->nmem);
683 }
684
685 int yaz_record_conv_configure_t(yaz_record_conv_t p, const xmlNode *ptr,
686                                 struct yaz_record_conv_type *types)
687 {
688     struct yaz_record_conv_type bt[4];
689     size_t i = 0;
690
691     /* register marc */
692     bt[i].construct = construct_marc;
693     bt[i].convert = convert_marc;
694     bt[i++].destroy = destroy_marc;
695
696     bt[i-1].next = &bt[i];
697     bt[i].construct = construct_solrmarc;
698     bt[i].convert = convert_solrmarc;
699     bt[i++].destroy = destroy_solrmarc;
700
701     bt[i-1].next = &bt[i];
702     bt[i].construct = construct_select;
703     bt[i].convert = convert_select;
704     bt[i++].destroy = destroy_select;
705
706 #if YAZ_HAVE_XSLT
707     /* register xslt */
708     bt[i-1].next = &bt[i];
709     bt[i].construct = construct_xslt;
710     bt[i].convert = convert_xslt;
711     bt[i++].destroy = destroy_xslt;
712 #endif
713
714     bt[i-1].next = types;
715     yaz_record_conv_reset(p);
716
717     /* parsing element children */
718     for (ptr = ptr->children; ptr; ptr = ptr->next)
719     {
720         struct yaz_record_conv_type *t;
721         struct yaz_record_conv_rule *r;
722         void *info = 0;
723         if (ptr->type != XML_ELEMENT_NODE)
724             continue;
725         for (t = &bt[0]; t; t = t->next)
726         {
727             wrbuf_rewind(p->wr_error);
728             info = t->construct(ptr, p->path, p->wr_error);
729
730             if (info || wrbuf_len(p->wr_error))
731                 break;
732             /* info== 0 and no error reported , ie not handled by it */
733         }
734         if (!info)
735         {
736             if (wrbuf_len(p->wr_error) == 0)
737                 wrbuf_printf(p->wr_error, "Element <backend>: expected "
738                              "<marc> or <xslt> element, got <%s>"
739                              , ptr->name);
740             return -1;
741         }
742         r = (struct yaz_record_conv_rule *) nmem_malloc(p->nmem, sizeof(*r));
743         r->next = 0;
744         r->info = info;
745         r->type = nmem_malloc(p->nmem, sizeof(*t));
746         memcpy(r->type, t, sizeof(*t));
747         *p->rules_p = r;
748         p->rules_p = &r->next;
749     }
750     return 0;
751 }
752
753 int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
754 {
755     return yaz_record_conv_configure_t(p, ptr, 0);
756 }
757
758 static int yaz_record_conv_record_rule(yaz_record_conv_t p,
759                                        struct yaz_record_conv_rule *r,
760                                        const char *input_record_buf,
761                                        size_t input_record_len,
762                                        WRBUF output_record)
763 {
764     int ret = 0;
765     WRBUF record = output_record; /* pointer transfer */
766     wrbuf_rewind(p->wr_error);
767
768     wrbuf_write(record, input_record_buf, input_record_len);
769     for (; ret == 0 && r; r = r->next)
770         ret = r->type->convert(r->info, record, p->wr_error);
771     return ret;
772 }
773
774 int yaz_record_conv_opac_record(yaz_record_conv_t p,
775                                 Z_OPACRecord *input_record,
776                                 WRBUF output_record)
777 {
778     int ret = 0;
779     struct yaz_record_conv_rule *r = p->rules;
780     if (!r || r->type->construct != construct_marc)
781     {
782         wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
783         ret = -1; /* no marc rule so we can't do OPAC */
784     }
785     else
786     {
787         struct marc_info *mi = r->info;
788         const char *input_charset = mi->input_charset;
789         yaz_iconv_t cd;
790
791         WRBUF res = wrbuf_alloc();
792         yaz_marc_t mt = yaz_marc_create();
793
794         if (yaz_opac_check_marc21_coding(input_charset, input_record))
795             input_charset = "utf-8";
796         cd = yaz_iconv_open(mi->output_charset, input_charset);
797
798         wrbuf_rewind(p->wr_error);
799         yaz_marc_xml(mt, mi->output_format_mode);
800
801         yaz_marc_iconv(mt, cd);
802
803         yaz_opac_decode_wrbuf(mt, input_record, res);
804         if (ret != -1)
805         {
806             ret = yaz_record_conv_record_rule(p,
807                                               r->next,
808                                               wrbuf_buf(res), wrbuf_len(res),
809                                               output_record);
810         }
811         yaz_marc_destroy(mt);
812         if (cd)
813             yaz_iconv_close(cd);
814         wrbuf_destroy(res);
815     }
816     return ret;
817 }
818
819 int yaz_record_conv_record(yaz_record_conv_t p,
820                            const char *input_record_buf,
821                            size_t input_record_len,
822                            WRBUF output_record)
823 {
824     return yaz_record_conv_record_rule(p, p->rules,
825                                        input_record_buf,
826                                        input_record_len, output_record);
827 }
828
829 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
830 {
831     return wrbuf_cstr(p->wr_error);
832 }
833
834 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
835 {
836     xfree(p->path);
837     p->path = 0;
838     if (path)
839         p->path = xstrdup(path);
840 }
841
842 yaz_record_conv_t yaz_record_conv_create()
843 {
844     yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
845     p->nmem = nmem_create();
846     p->wr_error = wrbuf_alloc();
847     p->rules = 0;
848     p->path = 0;
849     return p;
850 }
851
852 /* YAZ_HAVE_XML2 */
853 #endif
854
855 /*
856  * Local variables:
857  * c-basic-offset: 4
858  * c-file-style: "Stroustrup"
859  * indent-tabs-mode: nil
860  * End:
861  * vim: shiftwidth=4 tabstop=8 expandtab
862  */
863