Embedded XSLT stylesheets for service
[pazpar2-moved-to-github.git] / src / normalize_record.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <string.h>
21
22 #include <yaz/yaz-util.h>
23 #include <yaz/nmem.h>
24
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "normalize_record.h"
30
31 #include "pazpar2_config.h"
32 #include "service_xslt.h"
33 #include "marcmap.h"
34 #include <libxslt/xslt.h>
35 #include <libxslt/transform.h>
36
37 struct normalize_step {
38     struct normalize_step *next;
39     xsltStylesheet *stylesheet;  /* created by normalize_record */
40     xsltStylesheet *stylesheet2; /* external stylesheet (service) */
41     struct marcmap *marcmap;
42 };
43
44 struct normalize_record_s {
45     struct normalize_step *steps;
46     NMEM nmem;
47 };
48
49 normalize_record_t normalize_record_create(struct conf_service *service,
50                                            const char *spec)
51 {
52     NMEM nmem = nmem_create();
53     normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
54     struct normalize_step **m = &nt->steps;
55     int i, num;
56     int no_errors = 0;
57     char **stylesheets;
58     struct conf_config *conf = service->server->config;
59
60     nt->nmem = nmem;
61
62     nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
63     for (i = 0; i < num; i++)
64     {
65         WRBUF fname = conf_get_fname(conf, stylesheets[i]);
66         
67         *m = nmem_malloc(nt->nmem, sizeof(**m));
68         (*m)->marcmap = NULL;
69         (*m)->stylesheet = NULL;
70
71         (*m)->stylesheet2 = service_xslt_get(service, stylesheets[i]);
72         if ((*m)->stylesheet2)
73             ;
74         else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl")) 
75         {    
76             if (!((*m)->stylesheet =
77                   xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
78             {
79                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
80                         stylesheets[i]);
81                 no_errors++;
82             }
83         }
84         else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
85         {
86             if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
87             {
88                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
89                         stylesheets[i]);
90                 no_errors++;
91             }
92         }
93         else
94         {
95             yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
96             no_errors++;
97         }
98
99         wrbuf_destroy(fname);
100         m = &(*m)->next;
101     }
102     *m = 0;  /* terminate list of steps */
103
104     if (no_errors)
105     {
106         normalize_record_destroy(nt);
107         nt = 0;
108     }
109     return nt;
110 }
111
112 void normalize_record_destroy(normalize_record_t nt)
113 {
114     if (nt)
115     {
116         struct normalize_step *m;
117         for (m = nt->steps; m; m = m->next)
118         {
119             if (m->stylesheet)
120                 xsltFreeStylesheet(m->stylesheet);
121         }
122         nmem_destroy(nt->nmem);
123     }
124 }
125
126 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
127                                const char **parms)
128 {
129     if (nt)
130     {
131         struct normalize_step *m;
132         for (m = nt->steps; m; m = m->next)
133         {
134             xmlNodePtr root = 0;
135             xmlDoc *ndoc;
136             if (m->stylesheet)
137                 ndoc = xsltApplyStylesheet(m->stylesheet, *doc, parms);
138             else if (m->stylesheet2)
139                 ndoc = xsltApplyStylesheet(m->stylesheet2, *doc, parms);
140             else if (m->marcmap)
141                 ndoc = marcmap_apply(m->marcmap, *doc);
142             else
143                 ndoc = 0;
144             xmlFreeDoc(*doc);
145             *doc = 0;
146             
147             if (ndoc)
148                 root = xmlDocGetRootElement(ndoc);
149
150             if (ndoc && root && root->children)
151                 *doc = ndoc;
152             else
153             {
154                 if (ndoc)
155                     xmlFreeDoc(ndoc);
156                 return -1;
157             }
158         }
159     }
160     return 0;
161 }
162
163 /*
164  * Local variables:
165  * c-basic-offset: 4
166  * c-file-style: "Stroustrup"
167  * indent-tabs-mode: nil
168  * End:
169  * vim: shiftwidth=4 tabstop=8 expandtab
170  */
171