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