1 /* $Id: alvis.c,v 1.15 2006-05-24 18:31:33 adam 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 <libxml/xmlversion.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xmlIO.h>
32 #include <libxml/xmlreader.h>
33 #include <libxslt/transform.h>
35 #include <idzebra/util.h>
36 #include <idzebra/recctrl.h>
38 struct filter_schema {
40 const char *identifier;
41 const char *stylesheet;
42 struct filter_schema *next;
43 const char *default_schema;
44 /* char default_schema; */
45 const char *include_snippet;
46 xsltStylesheetPtr stylesheet_xsp;
52 const char *split_level;
53 const char *split_path;
55 struct filter_schema *schemas;
56 xmlTextReaderPtr reader;
59 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
61 #define XML_STRCMP(a,b) strcmp((char*)a, b)
62 #define XML_STRLEN(a) strlen((char*)a)
64 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
66 static void set_param_xml(const char **params, const char *name,
67 const char *value, ODR odr)
76 static void set_param_str(const char **params, const char *name,
77 const char *value, ODR odr)
79 char *quoted = odr_malloc(odr, 3 + strlen(value));
80 sprintf(quoted, "'%s'", value);
88 static void set_param_int(const char **params, const char *name,
91 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
94 sprintf(quoted, "'" ZINT_FORMAT "'", value);
100 #define ENABLE_INPUT_CALLBACK 0
102 #if ENABLE_INPUT_CALLBACK
103 static int zebra_xmlInputMatchCallback (char const *filename)
105 yaz_log(YLOG_LOG, "match %s", filename);
109 static void * zebra_xmlInputOpenCallback (char const *filename)
114 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
119 static int zebra_xmlInputCloseCallback (void * context)
125 static void *filter_init(Res res, RecType recType)
127 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
130 tinfo->split_level = 0;
131 tinfo->split_path = 0;
132 tinfo->odr = odr_createmem(ODR_ENCODE);
136 #if ENABLE_INPUT_CALLBACK
137 xmlRegisterDefaultInputCallbacks();
138 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
139 zebra_xmlInputOpenCallback,
140 zebra_xmlInputReadCallback,
141 zebra_xmlInputCloseCallback);
146 static int attr_content(struct _xmlAttr *attr, const char *name,
147 const char **dst_content)
149 if (!XML_STRCMP(attr->name, name) && attr->children &&
150 attr->children->type == XML_TEXT_NODE)
152 *dst_content = (const char *)(attr->children->content);
158 static void destroy_schemas(struct filter_info *tinfo)
160 struct filter_schema *schema = tinfo->schemas;
163 struct filter_schema *schema_next = schema->next;
164 if (schema->stylesheet_xsp)
165 xsltFreeStylesheet(schema->stylesheet_xsp);
167 schema = schema_next;
172 xmlFreeDoc(tinfo->doc);
176 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
179 tinfo->fname = xstrdup(fname);
180 tinfo->doc = xmlParseFile(tinfo->fname);
183 ptr = xmlDocGetRootElement(tinfo->doc);
184 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
185 XML_STRCMP(ptr->name, "schemaInfo"))
187 for (ptr = ptr->children; ptr; ptr = ptr->next)
189 if (ptr->type != XML_ELEMENT_NODE)
191 if (!XML_STRCMP(ptr->name, "schema"))
193 struct _xmlAttr *attr;
194 struct filter_schema *schema = xmalloc(sizeof(*schema));
196 schema->identifier = 0;
197 schema->stylesheet = 0;
198 schema->default_schema = 0;
199 schema->next = tinfo->schemas;
200 schema->stylesheet_xsp = 0;
201 schema->include_snippet = 0;
202 tinfo->schemas = schema;
203 for (attr = ptr->properties; attr; attr = attr->next)
205 attr_content(attr, "identifier", &schema->identifier);
206 attr_content(attr, "name", &schema->name);
207 attr_content(attr, "stylesheet", &schema->stylesheet);
208 attr_content(attr, "default", &schema->default_schema);
209 attr_content(attr, "snippet", &schema->include_snippet);
211 /*yaz_log(YLOG_LOG, "XSLT add %s %s %s",
212 schema->name, schema->identifier, schema->stylesheet); */
214 /* find requested schema */
216 if (schema->stylesheet)
217 schema->stylesheet_xsp =
218 xsltParseStylesheetFile(
219 (const xmlChar*) schema->stylesheet);
223 else if (!XML_STRCMP(ptr->name, "split"))
225 struct _xmlAttr *attr;
226 for (attr = ptr->properties; attr; attr = attr->next)
228 attr_content(attr, "level", &tinfo->split_level);
229 attr_content(attr, "path", &tinfo->split_path);
234 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
241 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
244 struct filter_schema *schema;
246 for (schema = tinfo->schemas; schema; schema = schema->next)
248 /* find requested schema */
251 if (schema->identifier && !strcmp(schema->identifier, est))
254 if (schema->name && !strcmp(schema->name, est))
257 /* or return default schema if defined */
258 else if (schema->default_schema)
262 /* return first schema if no default schema defined */
264 return tinfo->schemas;
269 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
271 struct filter_info *tinfo = clientData;
274 if (tinfo->fname && !strcmp(args, tinfo->fname))
276 destroy_schemas(tinfo);
277 create_schemas(tinfo, args);
281 static void filter_destroy(void *clientData)
283 struct filter_info *tinfo = clientData;
284 destroy_schemas(tinfo);
286 xmlFreeTextReader(tinfo->reader);
287 odr_destroy(tinfo->odr);
291 static int ioread_ex(void *context, char *buffer, int len)
293 struct recExtractCtrl *p = context;
294 return (*p->readf)(p->fh, buffer, len);
297 static int ioclose_ex(void *context)
302 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
303 xmlNodePtr ptr, RecWord *recWord)
305 for(; ptr; ptr = ptr->next)
307 index_cdata(tinfo, ctrl, ptr->children, recWord);
308 if (ptr->type != XML_TEXT_NODE)
310 recWord->term_buf = (const char *)ptr->content;
311 recWord->term_len = XML_STRLEN(ptr->content);
312 (*ctrl->tokenAdd)(recWord);
316 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
317 xmlNodePtr ptr, RecWord *recWord)
319 for(; ptr; ptr = ptr->next)
321 index_node(tinfo, ctrl, ptr->children, recWord);
322 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
323 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
325 if (!XML_STRCMP(ptr->name, "index"))
327 const char *name_str = 0;
328 const char *type_str = 0;
329 const char *xpath_str = 0;
330 struct _xmlAttr *attr;
331 for (attr = ptr->properties; attr; attr = attr->next)
333 attr_content(attr, "name", &name_str);
334 attr_content(attr, "xpath", &xpath_str);
335 attr_content(attr, "type", &type_str);
339 int prev_type = recWord->index_type; /* save default type */
341 if (type_str && *type_str)
342 recWord->index_type = *type_str; /* type was given */
343 recWord->index_name = name_str;
344 index_cdata(tinfo, ctrl, ptr->children, recWord);
346 recWord->index_type = prev_type; /* restore it again */
352 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
353 xmlNodePtr ptr, RecWord *recWord)
355 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
356 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
357 && !XML_STRCMP(ptr->name, "record"))
359 const char *type_str = "update";
360 const char *id_str = 0;
361 const char *rank_str = 0;
362 struct _xmlAttr *attr;
363 for (attr = ptr->properties; attr; attr = attr->next)
365 attr_content(attr, "type", &type_str);
366 attr_content(attr, "id", &id_str);
367 attr_content(attr, "rank", &rank_str);
370 sscanf(id_str, "%255s", ctrl->match_criteria);
373 ctrl->staticrank = atoi(rank_str);
374 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
377 yaz_log(YLOG_LOG, "no rank");
381 index_node(tinfo, ctrl, ptr, recWord);
384 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
388 const char *params[10];
392 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
395 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
397 (*p->init)(p, &recWord);
399 if (schema && schema->stylesheet_xsp)
403 xsltApplyStylesheet(schema->stylesheet_xsp,
405 if (p->flagShowRecords)
407 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
408 fwrite(buf_out, len_out, 1, stdout);
411 root_ptr = xmlDocGetRootElement(resDoc);
413 index_record(tinfo, p, root_ptr, &recWord);
416 yaz_log(YLOG_WARN, "No root for index XML record."
417 " split_level=%s stylesheet=%s",
418 tinfo->split_level, schema->stylesheet);
422 xmlDocDumpMemory(doc, &buf_out, &len_out);
423 if (p->flagShowRecords)
424 fwrite(buf_out, len_out, 1, stdout);
425 (*p->setStoreData)(p, buf_out, len_out);
429 return RECCTRL_EXTRACT_OK;
432 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
439 xmlFreeTextReader(tinfo->reader);
440 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
447 return RECCTRL_EXTRACT_ERROR_GENERIC;
449 if (tinfo->split_level)
450 split_depth = atoi(tinfo->split_level);
451 ret = xmlTextReaderRead(tinfo->reader);
453 int type = xmlTextReaderNodeType(tinfo->reader);
454 int depth = xmlTextReaderDepth(tinfo->reader);
455 if (split_depth == 0 ||
457 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
459 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
460 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
461 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
463 xmlDocSetRootElement(doc, ptr2);
465 return extract_doc(tinfo, p, doc);
467 ret = xmlTextReaderRead(tinfo->reader);
469 xmlFreeTextReader(tinfo->reader);
471 return RECCTRL_EXTRACT_EOF;
474 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
476 if (p->first_record) /* only one record per stream */
478 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
484 return RECCTRL_EXTRACT_ERROR_GENERIC;
486 return extract_doc(tinfo, p, doc);
489 return RECCTRL_EXTRACT_EOF;
492 static int filter_extract(void *clientData, struct recExtractCtrl *p)
494 struct filter_info *tinfo = clientData;
496 odr_reset(tinfo->odr);
498 if (tinfo->split_level == 0 && tinfo->split_path == 0)
499 return extract_full(tinfo, p);
502 return extract_split(tinfo, p);
506 static int ioread_ret(void *context, char *buffer, int len)
508 struct recRetrieveCtrl *p = context;
509 return (*p->readf)(p->fh, buffer, len);
512 static int ioclose_ret(void *context)
518 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
521 const char *xml_doc_str;
523 WRBUF wrbuf = wrbuf_alloc();
524 zebra_snippets *res =
525 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
526 zebra_snippet_word *w = zebra_snippets_list(res);
529 wrbuf_printf(wrbuf, "\'");
531 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
532 for (; w; w = w->next)
536 else if (ord != w->ord)
540 wrbuf_printf(wrbuf, "%s%s%s ",
543 w->match ? "*" : "");
546 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
548 (w->match ? "match='1'" : ""));
549 wrbuf_xmlputs(wrbuf, w->term);
550 wrbuf_printf(wrbuf, "</term>\n");
554 wrbuf_printf(wrbuf, "\'");
556 wrbuf_printf(wrbuf, "</snippet>\n");
558 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
560 zebra_snippets_destroy(res);
561 wrbuf_free(wrbuf, 1);
565 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
567 /* const char *esn = zebra_xslt_ns; */
569 const char *params[32];
570 struct filter_info *tinfo = clientData;
573 struct filter_schema *schema;
574 int window_size = -1;
578 if (p->comp->which == Z_RecordComp_simple
579 && p->comp->u.simple->which == Z_ElementSetNames_generic)
581 esn = p->comp->u.simple->u.generic;
583 else if (p->comp->which == Z_RecordComp_complex
584 && p->comp->u.complex->generic->elementSpec
585 && p->comp->u.complex->generic->elementSpec->which ==
586 Z_ElementSpec_elementSetName)
588 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
591 schema = lookup_schema(tinfo, esn);
595 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
599 if (schema->include_snippet)
600 window_size = atoi(schema->include_snippet);
603 set_param_int(params, "id", p->localno, p->odr);
605 set_param_str(params, "filename", p->fname, p->odr);
606 if (p->staticrank >= 0)
607 set_param_int(params, "rank", p->staticrank, p->odr);
610 set_param_str(params, "schema", esn, p->odr);
613 set_param_str(params, "schema", schema->name, p->odr);
614 else if (schema->identifier)
615 set_param_str(params, "schema", schema->identifier, p->odr);
617 set_param_str(params, "schema", "", p->odr);
620 set_param_int(params, "score", p->score, p->odr);
621 set_param_int(params, "size", p->recordSize, p->odr);
623 if (window_size >= 0)
624 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
626 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
632 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
636 if (window_size >= 0)
638 xmlNodePtr node = xmlDocGetRootElement(doc);
639 const char *snippet_str = snippet_doc(p, 0, window_size);
640 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
641 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
643 if (!schema->stylesheet_xsp)
647 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
653 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
655 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
660 xsltSaveResultToString(&buf_out, &len_out, resDoc,
661 schema->stylesheet_xsp);
663 p->output_format = VAL_TEXT_XML;
664 p->rec_len = len_out;
665 p->rec_buf = odr_malloc(p->odr, p->rec_len);
666 memcpy(p->rec_buf, buf_out, p->rec_len);
669 else if (p->output_format == VAL_SUTRS)
674 xsltSaveResultToString(&buf_out, &len_out, resDoc,
675 schema->stylesheet_xsp);
677 p->output_format = VAL_SUTRS;
678 p->rec_len = len_out;
679 p->rec_buf = odr_malloc(p->odr, p->rec_len);
680 memcpy(p->rec_buf, buf_out, p->rec_len);
686 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
692 static struct recType filter_type = {
703 #ifdef IDZEBRA_STATIC_ALVIS
716 * indent-tabs-mode: nil
718 * vim: shiftwidth=4 tabstop=8 expandtab