1 /* This file is part of Pazpar2.
2 Copyright (C) Index Data
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
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
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
22 #include <yaz/yaz-util.h>
29 #include "normalize_record.h"
31 #include "pazpar2_config.h"
32 #include "service_xslt.h"
34 #include <libxslt/xslt.h>
35 #include <libxslt/transform.h>
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;
44 struct normalize_record_s {
45 struct normalize_step *steps;
49 normalize_record_t normalize_record_create(struct conf_service *service,
52 NMEM nmem = nmem_create();
53 normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
54 struct normalize_step **m = &nt->steps;
65 xmlDoc *xsp_doc = xmlParseMemory(spec, strlen(spec));
70 *m = nmem_malloc(nt->nmem, sizeof(**m));
72 (*m)->stylesheet = NULL;
73 (*m)->stylesheet2 = NULL;
76 (*m)->stylesheet = xsltParseStylesheetDoc(xsp_doc);
77 if (!(*m)->stylesheet)
84 struct conf_config *conf = service->server->config;
87 nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
89 for (i = 0; i < num; i++)
91 WRBUF fname = conf_get_fname(conf, stylesheets[i]);
93 *m = nmem_malloc(nt->nmem, sizeof(**m));
95 (*m)->stylesheet = NULL;
97 (*m)->stylesheet2 = service_xslt_get(service, stylesheets[i]);
98 if ((*m)->stylesheet2)
100 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
102 if (!((*m)->stylesheet =
103 xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
105 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
110 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
112 if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
114 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
121 yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
125 wrbuf_destroy(fname);
129 *m = 0; /* terminate list of steps */
133 normalize_record_destroy(nt);
139 void normalize_record_destroy(normalize_record_t nt)
143 struct normalize_step *m;
144 for (m = nt->steps; m; m = m->next)
147 xsltFreeStylesheet(m->stylesheet);
149 nmem_destroy(nt->nmem);
153 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
158 struct normalize_step *m;
159 for (m = nt->steps; m; m = m->next)
164 ndoc = xsltApplyStylesheet(m->stylesheet, *doc, parms);
165 else if (m->stylesheet2)
166 ndoc = xsltApplyStylesheet(m->stylesheet2, *doc, parms);
168 ndoc = marcmap_apply(m->marcmap, *doc);
175 root = xmlDocGetRootElement(ndoc);
177 if (ndoc && root && root->children)
182 yaz_log(YLOG_WARN, "XSLT produced no document");
184 yaz_log(YLOG_WARN, "XSLT produced XML with no root node");
185 else if (!root->children)
186 yaz_log(YLOG_WARN, "XSLT produced XML with no root children nodes");
199 * c-file-style: "Stroustrup"
200 * indent-tabs-mode: nil
202 * vim: shiftwidth=4 tabstop=8 expandtab