Pass the following args to XSLT during retrieval: schema, filename,
[idzebra-moved-to-github.git] / recctrl / xslt.c
1 /* $Id: xslt.c,v 1.2 2005-04-28 12:34:21 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <ctype.h>
26
27 #include <yaz/diagbib1.h>
28 #include <libxml/xmlreader.h>
29 #include <libxslt/transform.h>
30
31 #include <idzebra/util.h>
32 #include <idzebra/recctrl.h>
33
34 struct filter_info {
35     xsltStylesheetPtr stylesheet_xsp;
36     xmlTextReaderPtr reader;
37     char *fname;
38     int split_depth;
39     ODR odr;
40 };
41
42 #define ZEBRA_INDEX_NS "http://indexdata.dk/zebra/indexing/1"
43 #define ZEBRA_SCHEMA_IDENTITY_NS "http://indexdata.dk/zebra/identity/1"
44 static const char *zebra_index_ns = ZEBRA_INDEX_NS;
45
46 static void set_param_str(const char **params, const char *name,
47                           const char *value, ODR odr)
48 {
49     char *quoted = odr_malloc(odr, 3 + strlen(value));
50     sprintf(quoted, "'%s'", value);
51     while (*params)
52         params++;
53     params[0] = name;
54     params[1] = quoted;
55     params[2] = 0;
56 }
57
58 static void set_param_int(const char **params, const char *name,
59                           zint value, ODR odr)
60 {
61     char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
62     sprintf(quoted, "'" ZINT_FORMAT "'", value);
63     params[0] = name;
64     params[1] = quoted;
65     params[2] = 0;
66 }
67
68
69 static void *filter_init (Res res, RecType recType)
70 {
71     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
72     tinfo->stylesheet_xsp = 0;
73     tinfo->reader = 0;
74     tinfo->fname = 0;
75     tinfo->split_depth = 1;
76     tinfo->odr = odr_createmem(ODR_ENCODE);
77     return tinfo;
78 }
79
80 static void filter_config(void *clientData, Res res, const char *args)
81 {
82     struct filter_info *tinfo = clientData;
83     if (!args || !*args)
84         args = "default.xsl";
85     if (!tinfo->fname || strcmp(args, tinfo->fname))
86     {
87         /* different filename so must reread stylesheet */
88         xfree(tinfo->fname);
89         tinfo->fname = xstrdup(args);
90         if (tinfo->stylesheet_xsp)
91             xsltFreeStylesheet(tinfo->stylesheet_xsp);
92         tinfo->stylesheet_xsp =
93             xsltParseStylesheetFile((const xmlChar*) tinfo->fname);
94     }
95 }
96
97 static void filter_destroy(void *clientData)
98 {
99     struct filter_info *tinfo = clientData;
100     if (tinfo->stylesheet_xsp)
101         xsltFreeStylesheet(tinfo->stylesheet_xsp);
102     xfree(tinfo->fname);
103     odr_destroy(tinfo->odr);
104     xfree(tinfo);
105 }
106
107 static int ioread_ex(void *context, char *buffer, int len)
108 {
109     struct recExtractCtrl *p = context;
110     return (*p->readf)(p->fh, buffer, len);
111 }
112
113 static int ioclose_ex(void *context)
114 {
115     return 0;
116 }
117
118 static void index_field(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
119                         xmlNodePtr ptr, RecWord *recWord)
120 {
121     for(; ptr; ptr = ptr->next)
122     {
123         index_field(tinfo, ctrl, ptr->children, recWord);
124         if (ptr->type != XML_TEXT_NODE)
125             continue;
126         recWord->term_buf = ptr->content;
127         recWord->term_len = strlen(ptr->content);
128         (*ctrl->tokenAdd)(recWord);
129     }
130 }
131
132 static void index_node(struct filter_info *tinfo,  struct recExtractCtrl *ctrl,
133                        xmlNodePtr ptr, RecWord *recWord)
134 {
135     for(; ptr; ptr = ptr->next)
136     {
137         index_node(tinfo, ctrl, ptr->children, recWord);
138         if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
139             strcmp(ptr->ns->href, zebra_index_ns))
140             continue;
141         if (!strcmp(ptr->name, "index"))
142         {
143             char *field_str = 0;
144             const char *xpath_str = 0;
145             struct _xmlAttr *attr;
146             for (attr = ptr->properties; attr; attr = attr->next)
147             {
148                 if (!strcmp(attr->name, "field") 
149                     && attr->children && attr->children->type == XML_TEXT_NODE)
150                     field_str = attr->children->content;
151                 if (!strcmp(attr->name, "xpath") 
152                     && attr->children && attr->children->type == XML_TEXT_NODE)
153                     xpath_str = attr->children->content;
154             }
155             if (field_str)
156             {
157                 recWord->attrStr = field_str;
158                 index_field(tinfo, ctrl, ptr->children, recWord);
159             }
160         }
161     }
162 }
163
164 static int filter_extract(void *clientData, struct recExtractCtrl *p)
165 {
166     const char *params[10];
167     struct filter_info *tinfo = clientData;
168     RecWord recWord;
169     int ret;
170
171     params[0] = 0;
172
173     odr_reset(tinfo->odr);
174     set_param_str(params, "schema", ZEBRA_INDEX_NS, tinfo->odr);
175
176     if (p->first_record)
177     {
178         if (tinfo->reader)
179             xmlFreeTextReader(tinfo->reader);
180         tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
181                                        p /* I/O handler */,
182                                        0 /* URL */, 
183                                        0 /* encoding */,
184                                        XML_PARSE_XINCLUDE);
185     }
186     if (!tinfo->reader)
187         return RECCTRL_EXTRACT_ERROR_GENERIC;
188
189     if (!tinfo->stylesheet_xsp)
190         return RECCTRL_EXTRACT_ERROR_GENERIC;
191
192     (*p->init)(p, &recWord);
193     recWord.reg_type = 'w';
194
195     ret = xmlTextReaderRead(tinfo->reader);
196     while (ret == 1) {
197         int type = xmlTextReaderNodeType(tinfo->reader);
198         int depth = xmlTextReaderDepth(tinfo->reader);
199         if (tinfo->split_depth == 0 ||
200             (type == XML_READER_TYPE_ELEMENT && tinfo->split_depth == depth))
201         {
202             xmlChar *buf_out;
203             int len_out;
204
205             xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
206             xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
207             xmlDocPtr doc = xmlNewDoc("1.0");
208
209             xmlDocSetRootElement(doc, ptr2);
210             
211             if (tinfo->stylesheet_xsp)
212             {
213                 xmlDocPtr resDoc = 
214                     xsltApplyStylesheet(tinfo->stylesheet_xsp,
215                                         doc, params);
216                 if (p->flagShowRecords)
217                 {
218                     xmlDocDumpMemory(resDoc, &buf_out, &len_out);
219                     fwrite(buf_out, len_out, 1, stdout);
220                     xmlFree(buf_out);
221                 }
222                 index_node(tinfo, p, xmlDocGetRootElement(resDoc), &recWord);
223                 xmlFreeDoc(resDoc);
224             }
225             xmlDocDumpMemory(doc, &buf_out, &len_out);
226             if (p->flagShowRecords)
227                 fwrite(buf_out, len_out, 1, stdout);
228             (*p->setStoreData)(p, buf_out, len_out);
229             xmlFree(buf_out);
230
231             xmlFreeDoc(doc);
232             return RECCTRL_EXTRACT_OK;
233         }
234         ret = xmlTextReaderRead(tinfo->reader);
235     }
236     xmlFreeTextReader(tinfo->reader);
237     tinfo->reader = 0;
238     return RECCTRL_EXTRACT_EOF;
239 }
240
241 static int ioread_ret(void *context, char *buffer, int len)
242 {
243     struct recRetrieveCtrl *p = context;
244     return (*p->readf)(p->fh, buffer, len);
245 }
246
247 static int ioclose_ret(void *context)
248 {
249     return 0;
250 }
251
252 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
253 {
254     const char *esn = ZEBRA_SCHEMA_IDENTITY_NS;
255     const char *params[10];
256     struct filter_info *tinfo = clientData;
257     xmlDocPtr resDoc;
258     xmlDocPtr doc;
259
260     if (p->comp)
261     {
262         if (p->comp->which != Z_RecordComp_simple
263             || p->comp->u.simple->which != Z_ElementSetNames_generic)
264         {
265             p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
266             return 0;
267         }
268         esn = p->comp->u.simple->u.generic;
269     }
270     
271     params[0] = 0;
272     set_param_str(params, "schema", esn, p->odr);
273     if (p->fname)
274         set_param_str(params, "filename", p->fname, p->odr);
275     if (p->score >= 0)
276         set_param_int(params, "score", p->score, p->odr);
277     set_param_int(params, "size", p->recordSize, p->odr);
278     
279     if (!tinfo->stylesheet_xsp)
280     {
281         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
282         return 0;
283     }
284     doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
285                     0 /* URL */,
286                     0 /* encoding */,
287                     XML_PARSE_XINCLUDE);
288     if (!doc)
289     {
290         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
291         return 0;
292     }
293
294     if (!strcmp(esn, ZEBRA_SCHEMA_IDENTITY_NS))
295         resDoc = doc;
296     else
297     {
298         resDoc = xsltApplyStylesheet(tinfo->stylesheet_xsp,
299                                      doc, params);
300         xmlFreeDoc(doc);
301     }
302     if (!resDoc)
303     {
304         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
305     }
306     else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
307     {
308         xmlChar *buf_out;
309         int len_out;
310         xmlDocDumpMemory(resDoc, &buf_out, &len_out);
311
312         p->output_format = VAL_TEXT_XML;
313         p->rec_len = len_out;
314         p->rec_buf = odr_malloc(p->odr, p->rec_len);
315         memcpy(p->rec_buf, buf_out, p->rec_len);
316         
317         xmlFree(buf_out);
318     }
319     else if (p->output_format == VAL_SUTRS)
320     {
321         xmlChar *buf_out;
322         int len_out;
323         xmlDocDumpMemory(resDoc, &buf_out, &len_out);
324
325         p->output_format = VAL_SUTRS;
326         p->rec_len = len_out;
327         p->rec_buf = odr_malloc(p->odr, p->rec_len);
328         memcpy(p->rec_buf, buf_out, p->rec_len);
329         
330         xmlFree(buf_out);
331     }
332     else
333     {
334         p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
335     }
336     xmlFreeDoc(resDoc);
337     return 0;
338 }
339
340 static struct recType filter_type = {
341     0,
342     "xslt",
343     filter_init,
344     filter_config,
345     filter_destroy,
346     filter_extract,
347     filter_retrieve
348 };
349
350 RecType
351 #ifdef IDZEBRA_STATIC_XSLT
352 idzebra_filter_xslt
353 #else
354 idzebra_filter
355 #endif
356
357 [] = {
358     &filter_type,
359     0,
360 };