1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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"
34 #include <libxslt/xslt.h>
35 #include <libxslt/transform.h>
37 struct normalize_step {
38 struct normalize_step *next;
39 xsltStylesheet *stylesheet;
40 struct marcmap *marcmap;
43 struct normalize_record_s {
44 struct normalize_step *steps;
48 normalize_record_t normalize_record_create(struct conf_config *conf,
51 NMEM nmem = nmem_create();
52 normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
53 struct normalize_step **m = &nt->steps;
60 nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
61 for (i = 0; i < num; i++)
63 WRBUF fname = conf_get_fname(conf, stylesheets[i]);
65 *m = nmem_malloc(nt->nmem, sizeof(**m));
67 (*m)->stylesheet = NULL;
70 if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
72 if (!((*m)->stylesheet =
73 xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
75 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
81 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
83 if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
85 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
92 yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
99 *m = 0; /* terminate list of steps */
103 normalize_record_destroy(nt);
109 void normalize_record_destroy(normalize_record_t nt)
113 struct normalize_step *m;
114 for (m = nt->steps; m; m = m->next)
117 xsltFreeStylesheet(m->stylesheet);
119 nmem_destroy(nt->nmem);
123 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
128 struct normalize_step *m;
129 for (m = nt->steps; m; m = m->next)
134 ndoc = xsltApplyStylesheet(m->stylesheet, *doc, parms);
136 ndoc = marcmap_apply(m->marcmap, *doc);
143 root = xmlDocGetRootElement(ndoc);
145 if (ndoc && root && root->children)
161 * c-file-style: "Stroustrup"
162 * indent-tabs-mode: nil
164 * vim: shiftwidth=4 tabstop=8 expandtab