Added check for no transform steps
[pazpar2-moved-to-github.git] / src / normalize_record.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 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
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;
40     struct marcmap *marcmap;
41 };
42
43 struct normalize_record_s {
44     struct normalize_step *steps;
45     NMEM nmem;
46 };
47
48 normalize_record_t normalize_record_create(struct conf_service *service,
49                                            const char *spec)
50 {
51     NMEM nmem = nmem_create();
52     normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
53     struct normalize_step **m = &nt->steps;
54     int i, num;
55     int no_errors = 0;
56     char **stylesheets;
57
58     nt->nmem = nmem;
59
60     nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);
61     for (i = 0; i < num; i++)
62     {
63         WRBUF fname = conf_get_fname(service, stylesheets[i]);
64         
65         *m = nmem_malloc(nt->nmem, sizeof(**m));
66         (*m)->marcmap = NULL;
67         (*m)->stylesheet = NULL;
68         
69         // XSLT
70         if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl")) 
71         {    
72             if (!((*m)->stylesheet =
73                   xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
74             {
75                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
76                         stylesheets[i]);
77                 no_errors++;
78             }
79         }
80         // marcmap
81         else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
82         {
83             if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
84             {
85                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
86                         stylesheets[i]);
87                 no_errors++;
88             }
89         }
90         else
91         {
92             yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
93             no_errors++;
94         }
95
96         wrbuf_destroy(fname);
97         m = &(*m)->next;
98     }
99     *m = 0;  /* terminate list of steps */
100
101     if (no_errors)
102     {
103         normalize_record_destroy(nt);
104         nt = 0;
105     }
106     return nt;
107 }
108
109 void normalize_record_destroy(normalize_record_t nt)
110 {
111     if (nt)
112     {
113         struct normalize_step *m;
114         for (m = nt->steps; m; m = m->next)
115         {
116             if (m->stylesheet)
117                 xsltFreeStylesheet(m->stylesheet);
118         }
119         nmem_destroy(nt->nmem);
120     }
121 }
122
123 int normalize_record_transform(normalize_record_t nt, xmlDoc **doc,
124     const char **parms)
125 {
126     struct normalize_step *m;
127     if (nt) {
128         for (m = nt->steps; m; m = m->next)
129         {
130             xmlNodePtr root = 0;
131             xmlDoc *new;
132             if (m->stylesheet)
133             {
134                 new = xsltApplyStylesheet(m->stylesheet, *doc, parms);
135             }
136             else if (m->marcmap)
137             {
138                 new = marcmap_apply(m->marcmap, *doc);
139             }
140             
141             root = xmlDocGetRootElement(new);
142             
143             if (!new || !root || !root->children)
144             {
145                 if (new)
146                     xmlFreeDoc(new);
147                 xmlFreeDoc(*doc);
148                 return -1;
149             }
150             xmlFreeDoc(*doc);
151             *doc = new;
152         }
153     }
154     return 0;
155 }
156
157 /*
158  * Local variables:
159  * c-basic-offset: 4
160  * c-file-style: "Stroustrup"
161  * indent-tabs-mode: nil
162  * End:
163  * vim: shiftwidth=4 tabstop=8 expandtab
164  */
165