Fix return value in service_xslt_config
[pazpar2-moved-to-github.git] / src / service_xslt.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <assert.h>
26
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29
30 #include <yaz/yaz-util.h>
31 #include <yaz/nmem.h>
32 #include <yaz/snprintf.h>
33 #include <yaz/tpath.h>
34 #include <yaz/xml_include.h>
35
36 #include "service_xslt.h"
37 #include "pazpar2_config.h"
38
39 struct service_xslt
40 {
41     char *id;
42     xsltStylesheetPtr xsp;
43     struct service_xslt *next;
44 };
45     
46 xsltStylesheetPtr service_xslt_get(struct conf_service *service,
47                                    const char *id)
48 {
49     struct service_xslt *sx;
50     for (sx = service->xslt_list; sx; sx = sx->next)
51         if (!strcmp(id, sx->id))
52             return sx->xsp;
53     return 0;
54 }
55
56 void service_xslt_destroy(struct conf_service *service)
57 {
58     struct service_xslt *sx = service->xslt_list;
59     for (; sx; sx = sx->next)
60         xsltFreeStylesheet(sx->xsp);
61 }
62
63 int service_xslt_config(struct conf_service *service, xmlNode *n)
64 {
65     xmlDoc *xsp_doc;
66     xmlNode *root = n->children;
67     struct service_xslt *sx;
68     const char *id = 0;
69     struct _xmlAttr *attr;
70     for (attr = n->properties; attr; attr = attr->next)
71         if (!strcmp((const char *) attr->name, "id"))
72             id = (const char *) attr->children->content;
73         else
74         {
75             yaz_log(YLOG_FATAL, "Invalid attribute %s for xslt element",
76                     (const char *) attr->name);
77             return -1;
78         }
79     if (!id)
80     {
81         yaz_log(YLOG_FATAL, "Missing attribute id for xslt element");
82         return -1;
83     }
84     while (root && root->type != XML_ELEMENT_NODE)
85         root = root->next;
86     if (!root)
87     {
88         yaz_log(YLOG_FATAL, "Missing content for xslt element");
89         return -1;
90     }
91     for (sx = service->xslt_list; sx; sx = sx->next)
92         if (!strcmp(sx->id, id))
93         {
94             yaz_log(YLOG_FATAL, "Multiple xslt with id=%s", id);
95             return -1;
96         }
97     
98     sx = nmem_malloc(service->nmem, sizeof(*sx));
99     sx->id = nmem_strdup(service->nmem, id);
100     sx->next = service->xslt_list;
101     service->xslt_list = sx;
102     
103     xsp_doc = xmlNewDoc(BAD_CAST "1.0");
104     xmlDocSetRootElement(xsp_doc, xmlCopyNode(root, 1));
105     sx->xsp = xsltParseStylesheetDoc(xsp_doc);
106     if (!sx->xsp)
107     {
108         xmlFreeDoc(xsp_doc);
109         yaz_log(YLOG_FATAL, "Failed to parse XSLT");
110         return -1;
111     }
112     return 0;
113 }
114
115 /*
116  * Local variables:
117  * c-basic-offset: 4
118  * c-file-style: "Stroustrup"
119  * indent-tabs-mode: nil
120  * End:
121  * vim: shiftwidth=4 tabstop=8 expandtab
122  */
123