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