1 /* $Id: alvis.c,v 1.17 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_schema {
43 const char *identifier;
44 const char *stylesheet;
45 struct filter_schema *next;
46 const char *default_schema;
47 /* char default_schema; */
48 const char *include_snippet;
49 xsltStylesheetPtr stylesheet_xsp;
56 const char *profile_path;
57 const char *split_level;
58 const char *split_path;
60 struct filter_schema *schemas;
61 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_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
135 tinfo->full_name = 0;
136 tinfo->profile_path = 0;
137 tinfo->split_level = 0;
138 tinfo->split_path = 0;
139 tinfo->odr = odr_createmem(ODR_ENCODE);
143 #if ENABLE_INPUT_CALLBACK
144 xmlRegisterDefaultInputCallbacks();
145 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
146 zebra_xmlInputOpenCallback,
147 zebra_xmlInputReadCallback,
148 zebra_xmlInputCloseCallback);
153 static int attr_content(struct _xmlAttr *attr, const char *name,
154 const char **dst_content)
156 if (!XML_STRCMP(attr->name, name) && attr->children &&
157 attr->children->type == XML_TEXT_NODE)
159 *dst_content = (const char *)(attr->children->content);
165 static void destroy_schemas(struct filter_info *tinfo)
167 struct filter_schema *schema = tinfo->schemas;
170 struct filter_schema *schema_next = schema->next;
171 if (schema->stylesheet_xsp)
172 xsltFreeStylesheet(schema->stylesheet_xsp);
174 schema = schema_next;
179 xmlFreeDoc(tinfo->doc);
183 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
185 char tmp_full_name[1024];
187 tinfo->fname = xstrdup(fname);
189 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
190 NULL, tmp_full_name))
191 tinfo->full_name = xstrdup(tmp_full_name);
193 tinfo->full_name = xstrdup(tinfo->fname);
195 yaz_log(YLOG_LOG, "alvis filter: loading config file %s", tinfo->full_name);
197 tinfo->doc = xmlParseFile(tinfo->full_name);
200 yaz_log(YLOG_WARN, "alvis 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 "alvis 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_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);
241 /*yaz_log(YLOG_LOG, "XSLT add %s %s %s",
242 schema->name, schema->identifier, schema->stylesheet); */
244 /* find requested schema */
246 if (schema->stylesheet){
247 yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
248 NULL, tmp_xslt_full_name);
249 schema->stylesheet_xsp
250 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
251 if (!schema->stylesheet_xsp)
253 "alvis filter: could not parse xslt stylesheet %s",
259 else if (!XML_STRCMP(ptr->name, "split"))
261 struct _xmlAttr *attr;
262 for (attr = ptr->properties; attr; attr = attr->next)
264 attr_content(attr, "level", &tinfo->split_level);
265 attr_content(attr, "path", &tinfo->split_path);
270 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
277 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
280 struct filter_schema *schema;
282 for (schema = tinfo->schemas; schema; schema = schema->next)
284 /* find requested schema */
287 if (schema->identifier && !strcmp(schema->identifier, est))
290 if (schema->name && !strcmp(schema->name, est))
293 /* or return default schema if defined */
294 else if (schema->default_schema)
298 /* return first schema if no default schema defined */
300 return tinfo->schemas;
305 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
307 struct filter_info *tinfo = clientData;
308 if (!args || !*args){
309 yaz_log(YLOG_WARN, "alvis filter: need config file");
313 if (tinfo->fname && !strcmp(args, tinfo->fname))
317 /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
318 = res_get(res, "profilePath");
319 yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
321 destroy_schemas(tinfo);
322 create_schemas(tinfo, args);
326 static void filter_destroy(void *clientData)
328 struct filter_info *tinfo = clientData;
329 destroy_schemas(tinfo);
331 xmlFreeTextReader(tinfo->reader);
332 odr_destroy(tinfo->odr);
336 static int ioread_ex(void *context, char *buffer, int len)
338 struct recExtractCtrl *p = context;
339 return (*p->readf)(p->fh, buffer, len);
342 static int ioclose_ex(void *context)
347 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
348 xmlNodePtr ptr, RecWord *recWord)
350 for(; ptr; ptr = ptr->next)
352 index_cdata(tinfo, ctrl, ptr->children, recWord);
353 if (ptr->type != XML_TEXT_NODE)
355 recWord->term_buf = (const char *)ptr->content;
356 recWord->term_len = XML_STRLEN(ptr->content);
357 (*ctrl->tokenAdd)(recWord);
361 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
362 xmlNodePtr ptr, RecWord *recWord)
364 for(; ptr; ptr = ptr->next)
366 index_node(tinfo, ctrl, ptr->children, recWord);
367 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
368 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
370 if (!XML_STRCMP(ptr->name, "index"))
372 const char *name_str = 0;
373 const char *type_str = 0;
374 const char *xpath_str = 0;
375 struct _xmlAttr *attr;
376 for (attr = ptr->properties; attr; attr = attr->next)
378 attr_content(attr, "name", &name_str);
379 attr_content(attr, "xpath", &xpath_str);
380 attr_content(attr, "type", &type_str);
384 int prev_type = recWord->index_type; /* save default type */
386 if (type_str && *type_str)
387 recWord->index_type = *type_str; /* type was given */
388 recWord->index_name = name_str;
389 index_cdata(tinfo, ctrl, ptr->children, recWord);
391 recWord->index_type = prev_type; /* restore it again */
397 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
398 xmlNodePtr ptr, RecWord *recWord)
400 const char *type_str = "update";
402 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
403 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
404 && !XML_STRCMP(ptr->name, "record"))
406 const char *id_str = 0;
407 const char *rank_str = 0;
408 struct _xmlAttr *attr;
409 for (attr = ptr->properties; attr; attr = attr->next)
411 attr_content(attr, "type", &type_str);
412 attr_content(attr, "id", &id_str);
413 attr_content(attr, "rank", &rank_str);
416 sscanf(id_str, "%255s", ctrl->match_criteria);
419 ctrl->staticrank = atoi(rank_str);
424 if (!strcmp("update", type_str))
425 index_node(tinfo, ctrl, ptr, recWord);
426 else if (!strcmp("delete", type_str))
427 yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
429 yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'",
433 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
437 const char *params[10];
441 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
444 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
446 (*p->init)(p, &recWord);
448 if (schema && schema->stylesheet_xsp)
452 xsltApplyStylesheet(schema->stylesheet_xsp,
454 if (p->flagShowRecords)
456 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
457 fwrite(buf_out, len_out, 1, stdout);
460 root_ptr = xmlDocGetRootElement(resDoc);
462 index_record(tinfo, p, root_ptr, &recWord);
465 yaz_log(YLOG_WARN, "No root for index XML record."
466 " split_level=%s stylesheet=%s",
467 tinfo->split_level, schema->stylesheet);
471 xmlDocDumpMemory(doc, &buf_out, &len_out);
472 if (p->flagShowRecords)
473 fwrite(buf_out, len_out, 1, stdout);
474 (*p->setStoreData)(p, buf_out, len_out);
478 return RECCTRL_EXTRACT_OK;
481 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
488 xmlFreeTextReader(tinfo->reader);
489 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
496 return RECCTRL_EXTRACT_ERROR_GENERIC;
498 if (tinfo->split_level)
499 split_depth = atoi(tinfo->split_level);
500 ret = xmlTextReaderRead(tinfo->reader);
502 int type = xmlTextReaderNodeType(tinfo->reader);
503 int depth = xmlTextReaderDepth(tinfo->reader);
504 if (split_depth == 0 ||
506 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
508 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
509 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
510 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
512 xmlDocSetRootElement(doc, ptr2);
514 return extract_doc(tinfo, p, doc);
516 ret = xmlTextReaderRead(tinfo->reader);
518 xmlFreeTextReader(tinfo->reader);
520 return RECCTRL_EXTRACT_EOF;
523 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
525 if (p->first_record) /* only one record per stream */
527 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
533 return RECCTRL_EXTRACT_ERROR_GENERIC;
535 return extract_doc(tinfo, p, doc);
538 return RECCTRL_EXTRACT_EOF;
541 static int filter_extract(void *clientData, struct recExtractCtrl *p)
543 struct filter_info *tinfo = clientData;
545 odr_reset(tinfo->odr);
547 if (tinfo->split_level == 0 && tinfo->split_path == 0)
548 return extract_full(tinfo, p);
551 return extract_split(tinfo, p);
555 static int ioread_ret(void *context, char *buffer, int len)
557 struct recRetrieveCtrl *p = context;
558 return (*p->readf)(p->fh, buffer, len);
561 static int ioclose_ret(void *context)
567 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
570 const char *xml_doc_str;
572 WRBUF wrbuf = wrbuf_alloc();
573 zebra_snippets *res =
574 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
575 zebra_snippet_word *w = zebra_snippets_list(res);
578 wrbuf_printf(wrbuf, "\'");
580 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
581 for (; w; w = w->next)
585 else if (ord != w->ord)
589 wrbuf_printf(wrbuf, "%s%s%s ",
592 w->match ? "*" : "");
595 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
597 (w->match ? "match='1'" : ""));
598 wrbuf_xmlputs(wrbuf, w->term);
599 wrbuf_printf(wrbuf, "</term>\n");
603 wrbuf_printf(wrbuf, "\'");
605 wrbuf_printf(wrbuf, "</snippet>\n");
607 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
609 zebra_snippets_destroy(res);
610 wrbuf_free(wrbuf, 1);
614 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
616 /* const char *esn = zebra_xslt_ns; */
618 const char *params[32];
619 struct filter_info *tinfo = clientData;
622 struct filter_schema *schema;
623 int window_size = -1;
627 if (p->comp->which == Z_RecordComp_simple
628 && p->comp->u.simple->which == Z_ElementSetNames_generic)
630 esn = p->comp->u.simple->u.generic;
632 else if (p->comp->which == Z_RecordComp_complex
633 && p->comp->u.complex->generic->elementSpec
634 && p->comp->u.complex->generic->elementSpec->which ==
635 Z_ElementSpec_elementSetName)
637 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
640 schema = lookup_schema(tinfo, esn);
644 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
648 if (schema->include_snippet)
649 window_size = atoi(schema->include_snippet);
652 set_param_int(params, "id", p->localno, p->odr);
654 set_param_str(params, "filename", p->fname, p->odr);
655 if (p->staticrank >= 0)
656 set_param_int(params, "rank", p->staticrank, p->odr);
659 set_param_str(params, "schema", esn, p->odr);
662 set_param_str(params, "schema", schema->name, p->odr);
663 else if (schema->identifier)
664 set_param_str(params, "schema", schema->identifier, p->odr);
666 set_param_str(params, "schema", "", p->odr);
669 set_param_int(params, "score", p->score, p->odr);
670 set_param_int(params, "size", p->recordSize, p->odr);
672 if (window_size >= 0)
673 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
675 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
681 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
685 if (window_size >= 0)
687 xmlNodePtr node = xmlDocGetRootElement(doc);
688 const char *snippet_str = snippet_doc(p, 0, window_size);
689 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
690 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
692 if (!schema->stylesheet_xsp)
696 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
702 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
704 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
709 xsltSaveResultToString(&buf_out, &len_out, resDoc,
710 schema->stylesheet_xsp);
712 p->output_format = VAL_TEXT_XML;
713 p->rec_len = len_out;
714 p->rec_buf = odr_malloc(p->odr, p->rec_len);
715 memcpy(p->rec_buf, buf_out, p->rec_len);
718 else if (p->output_format == VAL_SUTRS)
723 xsltSaveResultToString(&buf_out, &len_out, resDoc,
724 schema->stylesheet_xsp);
726 p->output_format = VAL_SUTRS;
727 p->rec_len = len_out;
728 p->rec_buf = odr_malloc(p->odr, p->rec_len);
729 memcpy(p->rec_buf, buf_out, p->rec_len);
735 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
741 static struct recType filter_type = {
752 #ifdef IDZEBRA_STATIC_ALVIS
765 * indent-tabs-mode: nil
767 * vim: shiftwidth=4 tabstop=8 expandtab