1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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;
49 const char *normalize_record_get_spec(normalize_record_t nt)
56 normalize_record_t normalize_record_create(struct conf_service *service,
59 normalize_record_t nt = xmalloc(sizeof(*nt));
60 struct normalize_step **m;
65 nt->nmem = nmem_create();
67 nt->spec = nmem_strdup(nt->nmem, spec);
71 nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
72 for (i = 0; i < num; i++)
74 WRBUF fname = conf_get_fname(service, stylesheets[i]);
76 *m = nmem_malloc(nt->nmem, sizeof(**m));
78 (*m)->stylesheet = NULL;
81 if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
83 if (!((*m)->stylesheet =
84 xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
86 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
92 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
94 if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
96 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
103 yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
107 wrbuf_destroy(fname);
110 *m = 0; /* terminate list of steps */
114 normalize_record_destroy(nt);
120 void normalize_record_destroy(normalize_record_t nt)
124 struct normalize_step *m;
125 for (m = nt->steps; m; m = m->next)
128 xsltFreeStylesheet(m->stylesheet);
130 nmem_destroy(nt->nmem);
136 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
139 struct normalize_step *m;
140 for (m = nt->steps; m; m = m->next)
146 new = xsltApplyStylesheet(m->stylesheet, *doc, parms);
150 new = marcmap_apply(m->marcmap, *doc);
153 root = xmlDocGetRootElement(new);
155 if (!new || !root || !root->children)
171 * c-file-style: "Stroustrup"
172 * indent-tabs-mode: nil
174 * vim: shiftwidth=4 tabstop=8 expandtab