1 /* $Id: xslt.c,v 1.27 2006-05-31 16:11:58 marc Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
30 #include <libxml/xmlversion.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xmlIO.h>
34 #include <libxml/xmlreader.h>
35 #include <libxslt/transform.h>
36 /* #include <libxslt/xsltutils.h> */
38 #include <idzebra/util.h>
39 #include <idzebra/recctrl.h>
41 struct filter_xslt_schema {
43 const char *identifier;
44 const char *stylesheet;
45 struct filter_xslt_schema *next;
46 const char *default_schema;
47 const char *include_snippet;
48 xsltStylesheetPtr stylesheet_xsp;
51 struct filter_xslt_info {
55 const char *profile_path;
56 const char *split_level;
57 const char *split_path;
59 struct filter_xslt_schema *schemas;
60 xmlTextReaderPtr reader;
64 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
66 #define XML_STRCMP(a,b) strcmp((char*)a, b)
67 #define XML_STRLEN(a) strlen((char*)a)
69 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
71 static void set_param_xml(const char **params, const char *name,
72 const char *value, ODR odr)
81 static void set_param_str(const char **params, const char *name,
82 const char *value, ODR odr)
84 char *quoted = odr_malloc(odr, 3 + strlen(value));
85 sprintf(quoted, "'%s'", value);
93 static void set_param_int(const char **params, const char *name,
96 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
99 sprintf(quoted, "'" ZINT_FORMAT "'", value);
105 #define ENABLE_INPUT_CALLBACK 0
107 #if ENABLE_INPUT_CALLBACK
108 static int zebra_xmlInputMatchCallback (char const *filename)
110 yaz_log(YLOG_LOG, "match %s", filename);
114 static void * zebra_xmlInputOpenCallback (char const *filename)
119 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
124 static int zebra_xmlInputCloseCallback (void * context)
130 static void *filter_init(Res res, RecType recType)
132 struct filter_xslt_info *tinfo
133 = (struct filter_xslt_info *) xmalloc(sizeof(*tinfo));
136 tinfo->full_name = 0;
137 tinfo->profile_path = 0;
138 tinfo->split_level = 0;
139 tinfo->split_path = 0;
140 tinfo->odr = odr_createmem(ODR_ENCODE);
144 #if ENABLE_INPUT_CALLBACK
145 xmlRegisterDefaultInputCallbacks();
146 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
147 zebra_xmlInputOpenCallback,
148 zebra_xmlInputReadCallback,
149 zebra_xmlInputCloseCallback);
154 static int attr_content(struct _xmlAttr *attr, const char *name,
155 const char **dst_content)
157 if (!XML_STRCMP(attr->name, name) && attr->children &&
158 attr->children->type == XML_TEXT_NODE)
160 *dst_content = (const char *)(attr->children->content);
166 static void destroy_schemas(struct filter_xslt_info *tinfo)
168 struct filter_xslt_schema *schema = tinfo->schemas;
171 struct filter_xslt_schema *schema_next = schema->next;
172 if (schema->stylesheet_xsp)
173 xsltFreeStylesheet(schema->stylesheet_xsp);
175 schema = schema_next;
180 xmlFreeDoc(tinfo->doc);
184 static ZEBRA_RES create_schemas(struct filter_xslt_info *tinfo,
187 char tmp_full_name[1024];
189 tinfo->fname = xstrdup(fname);
191 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
192 NULL, tmp_full_name))
193 tinfo->full_name = xstrdup(tmp_full_name);
195 tinfo->full_name = xstrdup(tinfo->fname);
197 yaz_log(YLOG_LOG, "xslt filter: loading config file %s", tinfo->full_name);
199 tinfo->doc = xmlParseFile(tinfo->full_name);
201 yaz_log(YLOG_WARN, "xslt filter: could not parse config file %s",
206 ptr = xmlDocGetRootElement(tinfo->doc);
207 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
208 XML_STRCMP(ptr->name, "schemaInfo")){
210 "xslt filter: config file %s :"
211 " expected root element <schemaInfo>",
216 for (ptr = ptr->children; ptr; ptr = ptr->next)
218 if (ptr->type != XML_ELEMENT_NODE)
220 if (!XML_STRCMP(ptr->name, "schema"))
222 char tmp_xslt_full_name[1024];
223 struct _xmlAttr *attr;
224 struct filter_xslt_schema *schema = xmalloc(sizeof(*schema));
226 schema->identifier = 0;
227 schema->stylesheet = 0;
228 schema->default_schema = 0;
229 schema->next = tinfo->schemas;
230 schema->stylesheet_xsp = 0;
231 schema->include_snippet = 0;
232 tinfo->schemas = schema;
233 for (attr = ptr->properties; attr; attr = attr->next)
235 attr_content(attr, "identifier", &schema->identifier);
236 attr_content(attr, "name", &schema->name);
237 attr_content(attr, "stylesheet", &schema->stylesheet);
238 attr_content(attr, "default", &schema->default_schema);
239 attr_content(attr, "snippet", &schema->include_snippet);
242 if (schema->stylesheet){
243 yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
244 NULL, tmp_xslt_full_name);
245 schema->stylesheet_xsp
246 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
247 if (!schema->stylesheet_xsp)
249 "xslt filter: could not parse xslt stylesheet %s",
254 else if (!XML_STRCMP(ptr->name, "split"))
256 struct _xmlAttr *attr;
257 for (attr = ptr->properties; attr; attr = attr->next)
259 attr_content(attr, "level", &tinfo->split_level);
260 attr_content(attr, "path", &tinfo->split_path);
265 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
272 static struct filter_xslt_schema *lookup_schema(struct filter_xslt_info *tinfo,
275 struct filter_xslt_schema *schema;
276 for (schema = tinfo->schemas; schema; schema = schema->next)
278 /* find requested schema */
281 if (schema->identifier && !strcmp(schema->identifier, est))
284 if (schema->name && !strcmp(schema->name, est))
287 /* or return default schema if defined */
288 else if (schema->default_schema)
292 /* return first schema if no default schema defined */
294 return tinfo->schemas;
299 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
301 struct filter_xslt_info *tinfo = clientData;
302 if (!args || !*args){
303 yaz_log(YLOG_WARN, "xslt filter: need config file");
307 if (tinfo->fname && !strcmp(args, tinfo->fname))
311 /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
312 = res_get(res, "profilePath");
313 yaz_log(YLOG_LOG, "xslt filter: profilePath %s", tinfo->profile_path);
315 destroy_schemas(tinfo);
316 create_schemas(tinfo, args);
320 static void filter_destroy(void *clientData)
322 struct filter_xslt_info *tinfo = clientData;
323 destroy_schemas(tinfo);
325 xmlFreeTextReader(tinfo->reader);
326 odr_destroy(tinfo->odr);
330 static int ioread_ex(void *context, char *buffer, int len)
332 struct recExtractCtrl *p = context;
333 return (*p->readf)(p->fh, buffer, len);
336 static int ioclose_ex(void *context)
341 static void index_cdata(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
342 xmlNodePtr ptr, RecWord *recWord)
344 for(; ptr; ptr = ptr->next)
346 index_cdata(tinfo, ctrl, ptr->children, recWord);
347 if (ptr->type != XML_TEXT_NODE)
349 recWord->term_buf = (const char *)ptr->content;
350 recWord->term_len = XML_STRLEN(ptr->content);
351 (*ctrl->tokenAdd)(recWord);
355 static void index_node(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
356 xmlNodePtr ptr, RecWord *recWord)
358 for(; ptr; ptr = ptr->next)
360 index_node(tinfo, ctrl, ptr->children, recWord);
361 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
362 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
364 if (!XML_STRCMP(ptr->name, "index"))
366 const char *name_str = 0;
367 const char *type_str = 0;
368 const char *xpath_str = 0;
369 struct _xmlAttr *attr;
370 for (attr = ptr->properties; attr; attr = attr->next)
372 attr_content(attr, "name", &name_str);
373 attr_content(attr, "xpath", &xpath_str);
374 attr_content(attr, "type", &type_str);
378 int prev_type = recWord->index_type; /* save default type */
380 if (type_str && *type_str)
381 recWord->index_type = *type_str; /* type was given */
382 recWord->index_name = name_str;
383 index_cdata(tinfo, ctrl, ptr->children, recWord);
385 recWord->index_type = prev_type; /* restore it again */
391 static void index_record(struct filter_xslt_info *tinfo,struct recExtractCtrl *ctrl,
392 xmlNodePtr ptr, RecWord *recWord)
394 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
395 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
396 && !XML_STRCMP(ptr->name, "record"))
398 const char *type_str = "update";
399 const char *id_str = 0;
400 const char *rank_str = 0;
401 struct _xmlAttr *attr;
402 for (attr = ptr->properties; attr; attr = attr->next)
404 attr_content(attr, "type", &type_str);
405 attr_content(attr, "id", &id_str);
406 attr_content(attr, "rank", &rank_str);
409 sscanf(id_str, "%255s", ctrl->match_criteria);
412 ctrl->staticrank = atoi(rank_str);
413 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
416 yaz_log(YLOG_LOG, "no rank");
420 index_node(tinfo, ctrl, ptr, recWord);
423 static int extract_doc(struct filter_xslt_info *tinfo, struct recExtractCtrl *p,
427 const char *params[10];
431 struct filter_xslt_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
434 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
436 (*p->init)(p, &recWord);
438 if (schema && schema->stylesheet_xsp)
442 xsltApplyStylesheet(schema->stylesheet_xsp,
444 if (p->flagShowRecords)
446 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
447 fwrite(buf_out, len_out, 1, stdout);
450 root_ptr = xmlDocGetRootElement(resDoc);
452 index_record(tinfo, p, root_ptr, &recWord);
455 yaz_log(YLOG_WARN, "No root for index XML record."
456 " split_level=%s stylesheet=%s",
457 tinfo->split_level, schema->stylesheet);
461 xmlDocDumpMemory(doc, &buf_out, &len_out);
462 if (p->flagShowRecords)
463 fwrite(buf_out, len_out, 1, stdout);
464 (*p->setStoreData)(p, buf_out, len_out);
468 return RECCTRL_EXTRACT_OK;
471 static int extract_split(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
478 xmlFreeTextReader(tinfo->reader);
479 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
486 return RECCTRL_EXTRACT_ERROR_GENERIC;
488 if (tinfo->split_level)
489 split_depth = atoi(tinfo->split_level);
490 ret = xmlTextReaderRead(tinfo->reader);
492 int type = xmlTextReaderNodeType(tinfo->reader);
493 int depth = xmlTextReaderDepth(tinfo->reader);
494 if (split_depth == 0 ||
496 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
498 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
499 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
500 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
502 xmlDocSetRootElement(doc, ptr2);
504 return extract_doc(tinfo, p, doc);
506 ret = xmlTextReaderRead(tinfo->reader);
508 xmlFreeTextReader(tinfo->reader);
510 return RECCTRL_EXTRACT_EOF;
513 static int extract_full(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
515 if (p->first_record) /* only one record per stream */
517 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
523 return RECCTRL_EXTRACT_ERROR_GENERIC;
525 return extract_doc(tinfo, p, doc);
528 return RECCTRL_EXTRACT_EOF;
531 static int filter_extract(void *clientData, struct recExtractCtrl *p)
533 struct filter_xslt_info *tinfo = clientData;
535 odr_reset(tinfo->odr);
537 if (tinfo->split_level == 0 && tinfo->split_path == 0)
538 return extract_full(tinfo, p);
541 return extract_split(tinfo, p);
545 static int ioread_ret(void *context, char *buffer, int len)
547 struct recRetrieveCtrl *p = context;
548 return (*p->readf)(p->fh, buffer, len);
551 static int ioclose_ret(void *context)
557 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
560 const char *xml_doc_str;
562 WRBUF wrbuf = wrbuf_alloc();
563 zebra_snippets *res =
564 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
565 zebra_snippet_word *w = zebra_snippets_list(res);
568 wrbuf_printf(wrbuf, "\'");
570 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
571 for (; w; w = w->next)
575 else if (ord != w->ord)
579 wrbuf_printf(wrbuf, "%s%s%s ",
582 w->match ? "*" : "");
585 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
587 (w->match ? "match='1'" : ""));
588 wrbuf_xmlputs(wrbuf, w->term);
589 wrbuf_printf(wrbuf, "</term>\n");
593 wrbuf_printf(wrbuf, "\'");
595 wrbuf_printf(wrbuf, "</snippet>\n");
597 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
599 zebra_snippets_destroy(res);
600 wrbuf_free(wrbuf, 1);
604 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
607 const char *params[32];
608 struct filter_xslt_info *tinfo = clientData;
611 struct filter_xslt_schema *schema;
612 int window_size = -1;
616 if (p->comp->which == Z_RecordComp_simple
617 && p->comp->u.simple->which == Z_ElementSetNames_generic)
619 esn = p->comp->u.simple->u.generic;
621 else if (p->comp->which == Z_RecordComp_complex
622 && p->comp->u.complex->generic->elementSpec
623 && p->comp->u.complex->generic->elementSpec->which ==
624 Z_ElementSpec_elementSetName)
626 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
629 schema = lookup_schema(tinfo, esn);
633 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
637 if (schema->include_snippet)
638 window_size = atoi(schema->include_snippet);
641 set_param_int(params, "id", p->localno, p->odr);
643 set_param_str(params, "filename", p->fname, p->odr);
644 if (p->staticrank >= 0)
645 set_param_int(params, "rank", p->staticrank, p->odr);
648 set_param_str(params, "schema", esn, p->odr);
651 set_param_str(params, "schema", schema->name, p->odr);
652 else if (schema->identifier)
653 set_param_str(params, "schema", schema->identifier, p->odr);
655 set_param_str(params, "schema", "", p->odr);
658 set_param_int(params, "score", p->score, p->odr);
659 set_param_int(params, "size", p->recordSize, p->odr);
661 if (window_size >= 0)
662 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
664 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
670 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
674 if (window_size >= 0)
676 xmlNodePtr node = xmlDocGetRootElement(doc);
677 const char *snippet_str = snippet_doc(p, 0, window_size);
678 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
679 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
681 if (!schema->stylesheet_xsp)
685 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
691 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
693 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
698 xsltSaveResultToString(&buf_out, &len_out, resDoc,
699 schema->stylesheet_xsp);
701 p->output_format = VAL_TEXT_XML;
702 p->rec_len = len_out;
703 p->rec_buf = odr_malloc(p->odr, p->rec_len);
704 memcpy(p->rec_buf, buf_out, p->rec_len);
708 else if (p->output_format == VAL_SUTRS)
713 xsltSaveResultToString(&buf_out, &len_out, resDoc,
714 schema->stylesheet_xsp);
716 p->output_format = VAL_SUTRS;
717 p->rec_len = len_out;
718 p->rec_buf = odr_malloc(p->odr, p->rec_len);
719 memcpy(p->rec_buf, buf_out, p->rec_len);
725 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
731 static struct recType filter_type = {
742 #ifdef IDZEBRA_STATIC_XSLT
755 * indent-tabs-mode: nil
757 * vim: shiftwidth=4 tabstop=8 expandtab