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