New setting embed_xslt
[pazpar2-moved-to-github.git] / src / normalize_record.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2012 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, int embed)
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 no_errors = 0;
56
57     nt->nmem = nmem;
58
59     if (embed)
60     {
61         xmlDoc *xsp_doc = xmlParseMemory(spec, strlen(spec));
62
63         if (!xsp_doc)
64             no_errors++;
65         {
66             *m = nmem_malloc(nt->nmem, sizeof(**m));
67             (*m)->marcmap = NULL;
68             (*m)->stylesheet = NULL;
69             (*m)->stylesheet2 = NULL;
70             
71             
72             (*m)->stylesheet = xsltParseStylesheetDoc(xsp_doc);
73             if (!(*m)->stylesheet)
74                 no_errors++;
75             m = &(*m)->next;
76         }
77     }
78     else
79     {
80         struct conf_config *conf = service->server->config;
81         int i, num;
82         char **stylesheets;
83         nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
84
85         for (i = 0; i < num; i++)
86         {
87             WRBUF fname = conf_get_fname(conf, stylesheets[i]);
88             
89             *m = nmem_malloc(nt->nmem, sizeof(**m));
90             (*m)->marcmap = NULL;
91             (*m)->stylesheet = NULL;
92             
93             (*m)->stylesheet2 = service_xslt_get(service, stylesheets[i]);
94             if ((*m)->stylesheet2)
95                 ;
96             else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl")) 
97             {    
98                 if (!((*m)->stylesheet =
99                       xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
100                 {
101                     yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
102                             stylesheets[i]);
103                     no_errors++;
104                 }
105             }
106             else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
107             {
108                 if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
109                 {
110                     yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
111                             stylesheets[i]);
112                     no_errors++;
113                 }
114             }
115             else
116             {
117                 yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
118                 no_errors++;
119             }
120             
121             wrbuf_destroy(fname);
122             m = &(*m)->next;
123         }
124     }
125     *m = 0;  /* terminate list of steps */
126
127     if (no_errors)
128     {
129         normalize_record_destroy(nt);
130         nt = 0;
131     }
132     return nt;
133 }
134
135 void normalize_record_destroy(normalize_record_t nt)
136 {
137     if (nt)
138     {
139         struct normalize_step *m;
140         for (m = nt->steps; m; m = m->next)
141         {
142             if (m->stylesheet)
143                 xsltFreeStylesheet(m->stylesheet);
144         }
145         nmem_destroy(nt->nmem);
146     }
147 }
148
149 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
150                                const char **parms)
151 {
152     if (nt)
153     {
154         struct normalize_step *m;
155         for (m = nt->steps; m; m = m->next)
156         {
157             xmlNodePtr root = 0;
158             xmlDoc *ndoc;
159             if (m->stylesheet)
160                 ndoc = xsltApplyStylesheet(m->stylesheet, *doc, parms);
161             else if (m->stylesheet2)
162                 ndoc = xsltApplyStylesheet(m->stylesheet2, *doc, parms);
163             else if (m->marcmap)
164                 ndoc = marcmap_apply(m->marcmap, *doc);
165             else
166                 ndoc = 0;
167             xmlFreeDoc(*doc);
168             *doc = 0;
169             
170             if (ndoc)
171                 root = xmlDocGetRootElement(ndoc);
172
173             if (ndoc && root && root->children)
174                 *doc = ndoc;
175             else
176             {
177                 if (ndoc)
178                     xmlFreeDoc(ndoc);
179                 return -1;
180             }
181         }
182     }
183     return 0;
184 }
185
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * c-file-style: "Stroustrup"
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194