Happy new year
[pazpar2-moved-to-github.git] / src / normalize_record.c
index c7706e5..70082fd 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -29,83 +29,102 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "normalize_record.h"
 
 #include "pazpar2_config.h"
-
+#include "service_xslt.h"
 #include "marcmap.h"
 #include <libxslt/xslt.h>
 #include <libxslt/transform.h>
 
 struct normalize_step {
     struct normalize_step *next;
-    xsltStylesheet *stylesheet;
+    xsltStylesheet *stylesheet1; /* created by normalize_record */
+    xsltStylesheet *stylesheet2; /* external stylesheet (service) */
     struct marcmap *marcmap;
 };
 
 struct normalize_record_s {
     struct normalize_step *steps;
-    char *spec;
     NMEM nmem;
 };
 
-const char *normalize_record_get_spec(normalize_record_t nt)
-{
-    if (nt)
-        return nt->spec;
-    return 0;
-}
-
 normalize_record_t normalize_record_create(struct conf_service *service,
                                            const char *spec)
 {
-    normalize_record_t nt = xmalloc(sizeof(*nt));
-    struct normalize_step **m;
-    int i, num;
+    NMEM nmem = nmem_create();
+    normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
+    struct normalize_step **m = &nt->steps;
     int no_errors = 0;
-    char **stylesheets;
-
-    nt->nmem = nmem_create();
+    int embed = 0;
 
-    nt->spec = nmem_strdup(nt->nmem, spec);
+    if (*spec == '<')
+        embed = 1;
 
-    m = &nt->steps;
+    nt->nmem = nmem;
 
-    nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
-    for (i = 0; i < num; i++)
+    if (embed)
     {
-        WRBUF fname = conf_get_fname(service, stylesheets[i]);
-        
-        *m = nmem_malloc(nt->nmem, sizeof(**m));
-        (*m)->marcmap = NULL;
-        (*m)->stylesheet = NULL;
-        
-        // XSLT
-        if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl")) 
-        {    
-            if (!((*m)->stylesheet =
-                  xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
-            {
-                yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
-                        stylesheets[i]);
+        xmlDoc *xsp_doc = xmlParseMemory(spec, strlen(spec));
+
+        if (!xsp_doc)
+            no_errors++;
+        else
+        {
+            *m = nmem_malloc(nt->nmem, sizeof(**m));
+            (*m)->marcmap = NULL;
+            (*m)->stylesheet1 = NULL;
+            (*m)->stylesheet2 = NULL;
+
+
+            (*m)->stylesheet1 = xsltParseStylesheetDoc(xsp_doc);
+            if (!(*m)->stylesheet1)
                 no_errors++;
-            }
+            m = &(*m)->next;
         }
-        // marcmap
-        else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
+    }
+    else
+    {
+        struct conf_config *conf = service->server->config;
+        int i, num;
+        char **stylesheets;
+        nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
+
+        for (i = 0; i < num; i++)
         {
-            if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
+            *m = nmem_malloc(nt->nmem, sizeof(**m));
+            (*m)->marcmap = NULL;
+            (*m)->stylesheet1 = NULL;
+            (*m)->stylesheet2 = service_xslt_get(service, stylesheets[i]);
+            if ((*m)->stylesheet2)
+                ;
+            else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
             {
-                yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
-                        stylesheets[i]);
+                WRBUF fname = conf_get_fname(conf, stylesheets[i]);
+                if (!((*m)->stylesheet1 =
+                      xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
+                {
+                    yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
+                            stylesheets[i]);
+                    no_errors++;
+                }
+                wrbuf_destroy(fname);
+            }
+            else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
+            {
+                WRBUF fname = conf_get_fname(conf, stylesheets[i]);
+                if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
+                {
+                    yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
+                            stylesheets[i]);
+                    no_errors++;
+                }
+                wrbuf_destroy(fname);
+            }
+            else
+            {
+                yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
                 no_errors++;
             }
+            m = &(*m)->next;
         }
-        else
-        {
-            yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
-            no_errors++;
-        }
-
-        wrbuf_destroy(fname);
-        m = &(*m)->next;
     }
     *m = 0;  /* terminate list of steps */
 
@@ -124,43 +143,52 @@ void normalize_record_destroy(normalize_record_t nt)
         struct normalize_step *m;
         for (m = nt->steps; m; m = m->next)
         {
-            if (m->stylesheet)
-                xsltFreeStylesheet(m->stylesheet);
+            if (m->stylesheet1)
+                xsltFreeStylesheet(m->stylesheet1);
         }
         nmem_destroy(nt->nmem);
-
-        xfree(nt);
     }
 }
 
 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
-    const char **parms)
+                               const char **parms)
 {
-    struct normalize_step *m;
-    for (m = nt->steps; m; m = m->next)
+    if (nt)
     {
-        xmlNodePtr root = 0;
-        xmlDoc *new;
-        if (m->stylesheet)
-        {
-            new = xsltApplyStylesheet(m->stylesheet, *doc, parms);
-        }
-        else if (m->marcmap)
-        {
-            new = marcmap_apply(m->marcmap, *doc);
-        }
-        
-        root = xmlDocGetRootElement(new);
-        
-        if (!new || !root || !root->children)
-        {
-            if (new)
-                xmlFreeDoc(new);
-            xmlFreeDoc(*doc);
-            return -1;
-        }
-        xmlFreeDoc(*doc);
-        *doc = new;
+        struct normalize_step *m;
+       for (m = nt->steps; m; m = m->next)
+       {
+           xmlNodePtr root = 0;
+           xmlDoc *ndoc;
+           if (m->stylesheet1)
+               ndoc = xsltApplyStylesheet(m->stylesheet1, *doc, parms);
+           else if (m->stylesheet2)
+               ndoc = xsltApplyStylesheet(m->stylesheet2, *doc, parms);
+           else if (m->marcmap)
+               ndoc = marcmap_apply(m->marcmap, *doc);
+            else
+                ndoc = 0;
+           xmlFreeDoc(*doc);
+            *doc = 0;
+
+            if (ndoc)
+                root = xmlDocGetRootElement(ndoc);
+
+            if (ndoc && root && root->children)
+                *doc = ndoc;
+            else
+           {
+                if (!ndoc)
+                    yaz_log(YLOG_WARN, "XSLT produced no document");
+                else if (!root)
+                    yaz_log(YLOG_WARN, "XSLT produced XML with no root node");
+                else if (!root->children)
+                    yaz_log(YLOG_WARN, "XSLT produced XML with no root children nodes");
+               if (ndoc)
+                   xmlFreeDoc(ndoc);
+               return -1;
+           }
+       }
     }
     return 0;
 }