For Libxml2 and friends, YAZ defines YAZ_HAVE_{XML2,XSLT,EXSLT) in
[yaz-moved-to-github.git] / src / record_conv.c
index 7327ef4..9d8a934 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 2005-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: record_conv.c,v 1.3 2006-05-04 15:31:04 adam Exp $
+ * $Id: record_conv.c,v 1.11 2006-07-06 10:17:53 adam Exp $
  */
 /**
  * \file record_conv.c
 #include <yaz/nmem.h>
 #include <yaz/tpath.h>
 
-#if HAVE_XSLT
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include <libxml/xinclude.h>
+#if YAZ_HAVE_XSLT
 #include <libxslt/xsltutils.h>
 #include <libxslt/transform.h>
+#endif
+#if YAZ_HAVE_EXSLT
+#include <libexslt/exslt.h>
+#endif
 
 /** \brief The internal structure for yaz_record_conv_t */
 struct yaz_record_conv_struct {
-    /** memory for configuration */
+    /** \brief memory for configuration */
     NMEM nmem;
 
-    /** conversion rules (allocated using NMEM) */
+    /** \brief conversion rules (allocated using NMEM) */
     struct yaz_record_conv_rule *rules;
 
-    /** pointer to last conversion rule pointer in chain */
+    /** \brief pointer to last conversion rule pointer in chain */
     struct yaz_record_conv_rule **rules_p;
 
-    /** string buffer for error messages */
+    /** \brief string buffer for error messages */
     WRBUF wr_error;
 
-    /** path for opening files  */
+    /** \brief path for opening files  */
     char *path;
 };
 
@@ -59,10 +64,11 @@ enum YAZ_RECORD_CONV_RULE
 struct yaz_record_conv_rule {
     enum YAZ_RECORD_CONV_RULE which;
     union {
+#if YAZ_HAVE_XSLT
         struct {
             xsltStylesheetPtr xsp;
-            int dummy;
         } xslt;
+#endif
         struct {
             yaz_iconv_t iconv_t;
             int input_format;
@@ -72,7 +78,7 @@ struct yaz_record_conv_rule {
     struct yaz_record_conv_rule *next;
 };
 
-/** reset rules+configuration */
+/** \brief reset rules+configuration */
 static void yaz_record_conv_reset(yaz_record_conv_t p)
 {
     struct yaz_record_conv_rule *r;
@@ -83,10 +89,12 @@ 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 YAZ_HAVE_XSLT
         else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
         {
             xsltFreeStylesheet(r->u.xslt.xsp);
         }
+#endif
     }
     wrbuf_rewind(p->wr_error);
     nmem_reset(p->nmem);
@@ -104,6 +112,9 @@ yaz_record_conv_t yaz_record_conv_create()
     p->rules = 0;
     p->path = 0;
 
+#if YAZ_HAVE_EXSLT
+    exsltRegisterAll(); 
+#endif
     yaz_record_conv_reset(p);
     return p;
 }
@@ -120,6 +131,7 @@ void yaz_record_conv_destroy(yaz_record_conv_t p)
     }
 }
 
+/** \brief adds a rule */
 static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
                                              enum YAZ_RECORD_CONV_RULE type)
 {
@@ -131,8 +143,10 @@ static struct yaz_record_conv_rule *add_rule(yaz_record_conv_t p,
     return r;
 }
 
+/** \brief parse 'xslt' conversion node */
 static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
 {
+#if YAZ_HAVE_XSLT
     struct _xmlAttr *attr;
     const char *stylesheet = 0;
 
@@ -143,7 +157,7 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
             stylesheet = (const char *) attr->children->content;
         else
         {
-            wrbuf_printf(p->wr_error, "Bad attribute '%s'."
+            wrbuf_printf(p->wr_error, "Bad attribute '%s'"
                          "Expected stylesheet.", attr->name);
             return -1;
         }
@@ -177,8 +191,14 @@ static int conv_xslt(yaz_record_conv_t p, const xmlNode *ptr)
         }
     }
     return 0;
+#else
+    wrbuf_printf(p->wr_error, "xslt unsupported."
+                 " YAZ compiled without XSLT support");
+    return -1;
+#endif
 }
 
+/** \brief parse 'marc' conversion node */
 static int conv_marc(yaz_record_conv_t p, const xmlNode *ptr)
 {
     struct _xmlAttr *attr;
@@ -207,7 +227,7 @@ 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, "Bad attribute '%s'", attr->name);
             return -1;
         }
     }
@@ -312,6 +332,17 @@ int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v)
                 if (conv_xslt(p, ptr))
                     return -1;
             }
+            else if (!strcmp((const char *) ptr->name, "exslt"))
+            {
+#if YAZ_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
+            }
             else if (!strcmp((const char *) ptr->name, "marc"))
             {
                 if (conv_marc(p, ptr))
@@ -320,7 +351,7 @@ int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v)
             else
             {
                 wrbuf_printf(p->wr_error, "Bad element '%s'."
-                             "Expected marc, xslt, ..", ptr->name);
+                              "Expected marc, xslt, ..", ptr->name);
                 return -1;
             }
         }
@@ -333,7 +364,9 @@ int yaz_record_conv_configure(yaz_record_conv_t p, const void *ptr_v)
     return 0;
 }
 
-int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record,
+int yaz_record_conv_record(yaz_record_conv_t p,
+                           const char *input_record_buf,
+                           size_t input_record_len,
                            WRBUF output_record)
 {
     int ret = 0;
@@ -341,42 +374,10 @@ int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record,
     struct yaz_record_conv_rule *r = p->rules;
     wrbuf_rewind(p->wr_error);
     
-    wrbuf_puts(record, input_record);
+    wrbuf_write(record, input_record_buf, input_record_len);
     for (; ret == 0 && r; r = r->next)
     {
-        if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
-        {
-            xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
-                                           wrbuf_len(record));
-            if (!doc)
-            {
-                wrbuf_printf(p->wr_error, "xmlParseMemory failed");
-                ret = -1;
-            }
-            else
-            {
-                xmlDocPtr res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0);
-                if (res)
-                {
-                    xmlChar *out_buf;
-                    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);
-                    xmlFreeDoc(res);
-                }
-                else
-                {
-                    wrbuf_printf(p->wr_error, "xsltApplyStylesheet faailed");
-                    ret = -1;
-                }
-                xmlFreeDoc(doc);
-            }
-        }
-        else if (r->which == YAZ_RECORD_CONV_RULE_MARC)
+        if (r->which == YAZ_RECORD_CONV_RULE_MARC)
         {
             yaz_marc_t mt = yaz_marc_create();
 
@@ -424,6 +425,54 @@ int yaz_record_conv_record(yaz_record_conv_t p, const char *input_record,
             }
             yaz_marc_destroy(mt);
         }
+#if YAZ_HAVE_XSLT
+        else if (r->which == YAZ_RECORD_CONV_RULE_XSLT)
+        {
+            xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
+                                           wrbuf_len(record));
+            if (!doc)
+            {
+                wrbuf_printf(p->wr_error, "xmlParseMemory failed");
+                ret = -1;
+            }
+            else
+            {
+                xmlDocPtr res = xsltApplyStylesheet(r->u.xslt.xsp, doc, 0);
+                if (res)
+                {
+                    xmlChar *out_buf = 0;
+                    int out_len;
+
+#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 failed");
+                    ret = -1;
+                }
+                xmlFreeDoc(doc);
+            }
+        }
+#endif
     }
     return ret;
 }
@@ -435,13 +484,10 @@ const char *yaz_record_conv_get_error(yaz_record_conv_t p)
 
 void yaz_record_conv_set_path(yaz_record_conv_t p, const char *path)
 {
-    if (p)
-    {
-        xfree(p->path);
-        p->path = 0;
-        if (path)
-            p->path = xstrdup(path);
-    }
+    xfree(p->path);
+    p->path = 0;
+    if (path)
+        p->path = xstrdup(path);
 }
 #endif