Handle OPAC for record conversion module.
[yaz-moved-to-github.git] / src / record_conv.c
index 9cb2e4c..ab5f173 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 2005-2006, Index Data ApS
+ * Copyright (C) 2005-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: record_conv.c,v 1.7 2006-05-08 10:16:47 adam Exp $
+ * $Id: record_conv.c,v 1.17 2007-12-16 11:08:51 adam Exp $
  */
 /**
  * \file record_conv.c
 #include <yaz/xmalloc.h>
 #include <yaz/nmem.h>
 #include <yaz/tpath.h>
+#include <yaz/z-opac.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include <libxml/xinclude.h>
-#if HAVE_XSLT
+#if YAZ_HAVE_XSLT
 #include <libxslt/xsltutils.h>
 #include <libxslt/transform.h>
 #endif
-#if HAVE_EXSLT
+#if YAZ_HAVE_EXSLT
 #include <libexslt/exslt.h>
 #endif
 
@@ -64,7 +65,7 @@ enum YAZ_RECORD_CONV_RULE
 struct yaz_record_conv_rule {
     enum YAZ_RECORD_CONV_RULE which;
     union {
-#if HAVE_XSLT
+#if YAZ_HAVE_XSLT
         struct {
             xsltStylesheetPtr xsp;
         } xslt;
@@ -81,6 +82,7 @@ struct yaz_record_conv_rule {
 /** \brief reset rules+configuration */
 static void yaz_record_conv_reset(yaz_record_conv_t p)
 {
+
     struct yaz_record_conv_rule *r;
     for (r = p->rules; r; r = r->next)
     {
@@ -89,7 +91,7 @@ static void yaz_record_conv_reset(yaz_record_conv_t p)
             if (r->u.marc.iconv_t)
                 yaz_iconv_close(r->u.marc.iconv_t);
         }
-#if HAVE_XSLT
+#if YAZ_HAVE_XSLT
         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
         {
             xsltFreeStylesheet(r->u.xslt.xsp);
@@ -106,14 +108,14 @@ static void yaz_record_conv_reset(yaz_record_conv_t p)
 
 yaz_record_conv_t yaz_record_conv_create()
 {
-    yaz_record_conv_t p = xmalloc(sizeof(*p));
+    yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
     p->nmem = nmem_create();
     p->wr_error = wrbuf_alloc();
     p->rules = 0;
     p->path = 0;
 
-#if HAVE_EXSLT
-    exsltDynRegister();
+#if YAZ_HAVE_EXSLT
+    exsltRegisterAll(); 
 #endif
     yaz_record_conv_reset(p);
     return p;
@@ -125,7 +127,7 @@ void yaz_record_conv_destroy(yaz_record_conv_t p)
     {
         yaz_record_conv_reset(p);
         nmem_destroy(p->nmem);
-        wrbuf_free(p->wr_error, 1);
+        wrbuf_destroy(p->wr_error);
         xfree(p->path);
         xfree(p);
     }
@@ -135,7 +137,8 @@ void yaz_record_conv_destroy(yaz_record_conv_t p)
 static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
                                              enum YAZ_RECORD_CONV_RULE type)
 {
-    struct yaz_record_conv_rule *r = nmem_malloc(p->nmem, sizeof(*r));
+    struct yaz_record_conv_rule *r = (struct yaz_record_conv_rule *)
+        nmem_malloc(p->nmem, sizeof(*r));
     r->which = type;
     r->next = 0;
     *p->rules_p = r;
@@ -146,7 +149,7 @@ static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
 /** \brief parse 'xslt' conversion node */
 static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
 {
-#if HAVE_XSLT
+#if YAZ_HAVE_XSLT
     struct _xmlAttr *attr;
     const char *stylesheet = 0;
 
@@ -164,7 +167,8 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
     }
     if (!stylesheet)
     {
-        wrbuf_printf(p->wr_error, "Missing attribute 'stylesheet'");
+        wrbuf_printf(p->wr_error, "Element <xslt>: "
+                     "attribute 'stylesheet' expected");
         return -1;
     }
     else
@@ -173,14 +177,22 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
         xsltStylesheetPtr xsp;
         if (!yaz_filepath_resolve(stylesheet, p->path, 0, fullpath))
         {
-            wrbuf_printf(p->wr_error, "could not locate '%s'. Path=%s",
-                         stylesheet, p->path);
+            wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
+                         " could not locate stylesheet '%s' with path '%s'",
+                         stylesheet, fullpath, p->path);
             return -1;
         }
         xsp = xsltParseStylesheetFile((xmlChar*) fullpath);
         if (!xsp)
         {
-            wrbuf_printf(p->wr_error, "xsltParseStylesheetFile failed'");
+            wrbuf_printf(p->wr_error, "Element <xslt stylesheet=\"%s\"/>:"
+                         " parsing stylesheet '%s' with path '%s' failed,"
+#if YAZ_HAVE_EXSLT
+                         " EXSLT enabled",
+#else
+                         " EXSLT not supported",
+#endif
+                         stylesheet, fullpath, p->path);
             return -1;
         }
         else
@@ -227,13 +239,17 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
             output_format = (const char *) attr->children->content;
         else
         {
-            wrbuf_printf(p->wr_error, "Bad attribute '%s'", attr->name);
+            wrbuf_printf(p->wr_error, "Element <marc>: expected attributes"
+                         "'inputformat', 'inputcharset', 'outputformat' or"
+                         " 'outputcharset', got attribute '%s'", 
+                         attr->name);
             return -1;
         }
     }
     if (!input_format)
     {
-        wrbuf_printf(p->wr_error, "Attribute 'inputformat' required");
+        wrbuf_printf(p->wr_error, "Element <marc>: "
+                     "attribute 'inputformat' required");
         return -1;
     }
     else if (!strcmp(input_format, "marc"))
@@ -251,13 +267,17 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
     }
     else
     {
-        wrbuf_printf(p->wr_error, "Bad inputformat: '%s'", input_format);
+        wrbuf_printf(p->wr_error, "Element <marc inputformat='%s'>: "
+                     " Unsupported input format"
+                     " defined by attribute value", 
+                     input_format);
         return -1;
     }
     
     if (!output_format)
     {
-        wrbuf_printf(p->wr_error, "Attribute 'outputformat' required");
+        wrbuf_printf(p->wr_error, 
+                     "Element <marc>: attribute 'outputformat' required");
         return -1;
     }
     else if (!strcmp(output_format, "line"))
@@ -282,7 +302,10 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
     }
     else
     {
-        wrbuf_printf(p->wr_error, "Bad outputformat: '%s'", input_format);
+        wrbuf_printf(p->wr_error, "Element <marc outputformat='%s'>: "
+                     " Unsupported output format"
+                     " defined by attribute value", 
+                     output_format);
         return -1;
     }
     if (input_charset && output_charset)
@@ -290,20 +313,24 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
         cd = yaz_iconv_open(output_charset, input_charset);
         if (!cd)
         {
-            wrbuf_printf(p->wr_error, "Unsupported character set mamping"
-                         " inputcharset=%s outputcharset=%s",
+            wrbuf_printf(p->wr_error, 
+                         "Element <marc inputcharset='%s' outputcharset='%s'>:"
+                         " Unsupported character set mapping"
+                         " defined by attribute values",
                          input_charset, output_charset);
             return -1;
         }
     }
     else if (input_charset)
     {
-        wrbuf_printf(p->wr_error, "Attribute 'outputcharset' missing");
+        wrbuf_printf(p->wr_error, "Element <marc>: "
+                     "attribute 'outputcharset' missing");
         return -1;
     }
     else if (output_charset)
     {
-        wrbuf_printf(p->wr_error, "Attribute 'inputcharset' missing");
+        wrbuf_printf(p->wr_error, "Element <marc>: "
+                     "attribute 'inputcharset' missing");
         return -1;
     }
     r = add_rule(p, YAZ_RECORD_CONV_RULE_MARC);
@@ -314,54 +341,66 @@ static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
     return 0;
 }
 
-int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v)
+int yaz_record_conv_configure(yaz_record_conv_t p, const xmlNode *ptr)
 {
-    const xmlNode *ptr = ptr_v; 
-
     yaz_record_conv_reset(p);
 
-    if (ptr && ptr->type == XML_ELEMENT_NODE &&
-        !strcmp((const char *) ptr->name, "convert"))
-    {
-        for (ptr = ptr->children; ptr; ptr = ptr->next)
+    /* parsing element children */
+    for (ptr = ptr->children; ptr; ptr = ptr->next)
         {
             if (ptr->type != XML_ELEMENT_NODE)
                 continue;
             if (!strcmp((const char *) ptr->name, "xslt"))
-            {
-                if (conv_xslt(p, ptr))
-                    return -1;
-            }
-            else if (!strcmp((const char *) ptr->name, "exslt"))
-            {
-#if HAVE_EXSLT
-                if (conv_xslt(p, ptr))
-                    return -1;
-#else
-                wrbuf_printf(p->wr_error, "exslt unsupported."
-                             " YAZ compiled without EXSLT support");
-                return -1;
-#endif
-            }
+                {
+                    if (conv_xslt(p, ptr))
+                        return -1;
+                }
             else if (!strcmp((const char *) ptr->name, "marc"))
-            {
-                if (conv_marc(p, ptr))
-                    return -1;
-            }
+                {
+                    if (conv_marc(p, ptr))
+                        return -1;
+                }
             else
-            {
-                wrbuf_printf(p->wr_error, "Bad element '%s'."
-                              "Expected marc, xslt, ..", ptr->name);
-                return -1;
-            }
+                {
+                    wrbuf_printf(p->wr_error, "Element <backend>: expected "
+                                 "<marc> or <xslt> element, got <%s>"
+                                 , ptr->name);
+                    return -1;
+                }
         }
-    }
-    else
+    return 0;
+}
+
+static int yaz_record_conv_record_rule(yaz_record_conv_t p,
+                                       struct yaz_record_conv_rule *r,
+                                       const char *input_record_buf,
+                                       size_t input_record_len,
+                                       WRBUF output_record);
+
+int yaz_record_conv_opac_record(yaz_record_conv_t p,
+                                Z_OPACRecord *input_record,
+                                WRBUF output_record)
+{
+    int ret = 0;
+    struct yaz_record_conv_rule *r = p->rules;
+    WRBUF res = wrbuf_alloc();
+    yaz_marc_t mt = yaz_marc_create();
+    
+    wrbuf_rewind(p->wr_error);
+    yaz_marc_xml(mt, r->u.marc.output_format);
+    if (r->u.marc.iconv_t)
+        yaz_marc_iconv(mt, r->u.marc.iconv_t);
+    yaz_opac_decode_wrbuf(mt, input_record, res);
+    if (ret != -1)
     {
-        wrbuf_printf(p->wr_error, "Missing 'convert' element");
-        return -1;
+        ret = yaz_record_conv_record_rule(p, 
+                                          r->next,
+                                          wrbuf_buf(res), wrbuf_len(res),
+                                          output_record);
     }
-    return 0;
+    yaz_marc_destroy(mt);
+    wrbuf_destroy(res);
+    return ret;
 }
 
 int yaz_record_conv_record(yaz_record_conv_t p,
@@ -369,9 +408,19 @@ int yaz_record_conv_record(yaz_record_conv_t p,
                            size_t input_record_len,
                            WRBUF output_record)
 {
+    return yaz_record_conv_record_rule(p, p->rules,
+                                       input_record_buf,
+                                       input_record_len, output_record);
+}
+
+static int yaz_record_conv_record_rule(yaz_record_conv_t p,
+                                       struct yaz_record_conv_rule *r,
+                                       const char *input_record_buf,
+                                       size_t input_record_len,
+                                       WRBUF output_record)
+{
     int ret = 0;
     WRBUF record = output_record; /* pointer transfer */
-    struct yaz_record_conv_rule *r = p->rules;
     wrbuf_rewind(p->wr_error);
     
     wrbuf_write(record, input_record_buf, input_record_len);
@@ -425,7 +474,7 @@ int yaz_record_conv_record(yaz_record_conv_t p,
             }
             yaz_marc_destroy(mt);
         }
-#if HAVE_XSLT
+#if YAZ_HAVE_XSLT
         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
         {
             xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
@@ -440,19 +489,33 @@ int yaz_record_conv_record(yaz_record_conv_t p,
                 xmlDocPtr res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0);
                 if (res)
                 {
-                    xmlChar *out_buf;
+                    xmlChar *out_buf = 0;
                     int out_len;
-                    xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
-
-                    wrbuf_rewind(record);
-                    wrbuf_write(record, (const char *) out_buf, out_len);
 
-                    xmlFree(out_buf);
+#if YAZ_HAVE_XSLTSAVERESULTTOSTRING
+                    xsltSaveResultToString(&out_buf, &out_len, res,
+                                           r->u.xslt.xsp); 
+#else
+                    xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1);
+#endif
+                    if (!out_buf)
+                    {
+                        wrbuf_printf(p->wr_error,
+                                     "xsltSaveResultToString failed");
+                        ret = -1;
+                    }
+                    else
+                    {
+                        wrbuf_rewind(record);
+                        wrbuf_write(record, (const char *) out_buf, out_len);
+                        
+                        xmlFree(out_buf);
+                    }
                     xmlFreeDoc(res);
                 }
                 else
                 {
-                    wrbuf_printf(p->wr_error, "xsltApplyStylesheet faailed");
+                    wrbuf_printf(p->wr_error, "xsltApplyStylesheet failed");
                     ret = -1;
                 }
                 xmlFreeDoc(doc);
@@ -465,7 +528,7 @@ int yaz_record_conv_record(yaz_record_conv_t p,
 
 const char *yaz_record_conv_get_error(yaz_record_conv_t p)
 {
-    return wrbuf_buf(p->wr_error);
+    return wrbuf_cstr(p->wr_error);
 }
 
 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)