1 /* $Id: alvis.c,v 1.13 2006-05-24 08:23:01 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 <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 if (schema->stylesheet)
212 schema->stylesheet_xsp =
213 xsltParseStylesheetFile(
214 (const xmlChar*) schema->stylesheet);
218 else if (!XML_STRCMP(ptr->name, "split"))
220 struct _xmlAttr *attr;
221 for (attr = ptr->properties; attr; attr = attr->next)
223 attr_content(attr, "level", &tinfo->split_level);
224 attr_content(attr, "path", &tinfo->split_path);
229 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
236 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
239 struct filter_schema *schema;
240 for (schema = tinfo->schemas; schema; schema = schema->next)
242 /* find requested schema */
245 if (schema->identifier && !strcmp(schema->identifier, est))
247 if (schema->name && !strcmp(schema->name, est))
251 /* or return default schema if defined */
252 if (schema->default_schema)
256 /* return first schema if no default schema defined */
258 return tinfo->schemas;
263 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
265 struct filter_info *tinfo = clientData;
268 if (tinfo->fname && !strcmp(args, tinfo->fname))
270 destroy_schemas(tinfo);
271 create_schemas(tinfo, args);
275 static void filter_destroy(void *clientData)
277 struct filter_info *tinfo = clientData;
278 destroy_schemas(tinfo);
280 xmlFreeTextReader(tinfo->reader);
281 odr_destroy(tinfo->odr);
285 static int ioread_ex(void *context, char *buffer, int len)
287 struct recExtractCtrl *p = context;
288 return (*p->readf)(p->fh, buffer, len);
291 static int ioclose_ex(void *context)
296 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
297 xmlNodePtr ptr, RecWord *recWord)
299 for(; ptr; ptr = ptr->next)
301 index_cdata(tinfo, ctrl, ptr->children, recWord);
302 if (ptr->type != XML_TEXT_NODE)
304 recWord->term_buf = (const char *)ptr->content;
305 recWord->term_len = XML_STRLEN(ptr->content);
306 (*ctrl->tokenAdd)(recWord);
310 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
311 xmlNodePtr ptr, RecWord *recWord)
313 for(; ptr; ptr = ptr->next)
315 index_node(tinfo, ctrl, ptr->children, recWord);
316 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
317 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
319 if (!XML_STRCMP(ptr->name, "index"))
321 const char *name_str = 0;
322 const char *type_str = 0;
323 const char *xpath_str = 0;
324 struct _xmlAttr *attr;
325 for (attr = ptr->properties; attr; attr = attr->next)
327 attr_content(attr, "name", &name_str);
328 attr_content(attr, "xpath", &xpath_str);
329 attr_content(attr, "type", &type_str);
333 int prev_type = recWord->index_type; /* save default type */
335 if (type_str && *type_str)
336 recWord->index_type = *type_str; /* type was given */
337 recWord->index_name = name_str;
338 index_cdata(tinfo, ctrl, ptr->children, recWord);
340 recWord->index_type = prev_type; /* restore it again */
346 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
347 xmlNodePtr ptr, RecWord *recWord)
349 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
350 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
351 && !XML_STRCMP(ptr->name, "record"))
353 const char *type_str = "update";
354 const char *id_str = 0;
355 const char *rank_str = 0;
356 struct _xmlAttr *attr;
357 for (attr = ptr->properties; attr; attr = attr->next)
359 attr_content(attr, "type", &type_str);
360 attr_content(attr, "id", &id_str);
361 attr_content(attr, "rank", &rank_str);
364 sscanf(id_str, "%255s", ctrl->match_criteria);
367 ctrl->staticrank = atoi(rank_str);
368 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
371 yaz_log(YLOG_LOG, "no rank");
375 index_node(tinfo, ctrl, ptr, recWord);
378 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
382 const char *params[10];
386 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
389 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
391 (*p->init)(p, &recWord);
393 if (schema && schema->stylesheet_xsp)
397 xsltApplyStylesheet(schema->stylesheet_xsp,
399 if (p->flagShowRecords)
401 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
402 fwrite(buf_out, len_out, 1, stdout);
405 root_ptr = xmlDocGetRootElement(resDoc);
407 index_record(tinfo, p, root_ptr, &recWord);
410 yaz_log(YLOG_WARN, "No root for index XML record."
411 " split_level=%s stylesheet=%s",
412 tinfo->split_level, schema->stylesheet);
416 xmlDocDumpMemory(doc, &buf_out, &len_out);
417 if (p->flagShowRecords)
418 fwrite(buf_out, len_out, 1, stdout);
419 (*p->setStoreData)(p, buf_out, len_out);
423 return RECCTRL_EXTRACT_OK;
426 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
433 xmlFreeTextReader(tinfo->reader);
434 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
441 return RECCTRL_EXTRACT_ERROR_GENERIC;
443 if (tinfo->split_level)
444 split_depth = atoi(tinfo->split_level);
445 ret = xmlTextReaderRead(tinfo->reader);
447 int type = xmlTextReaderNodeType(tinfo->reader);
448 int depth = xmlTextReaderDepth(tinfo->reader);
449 if (split_depth == 0 ||
451 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
453 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
454 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
455 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
457 xmlDocSetRootElement(doc, ptr2);
459 return extract_doc(tinfo, p, doc);
461 ret = xmlTextReaderRead(tinfo->reader);
463 xmlFreeTextReader(tinfo->reader);
465 return RECCTRL_EXTRACT_EOF;
468 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
470 if (p->first_record) /* only one record per stream */
472 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
478 return RECCTRL_EXTRACT_ERROR_GENERIC;
480 return extract_doc(tinfo, p, doc);
483 return RECCTRL_EXTRACT_EOF;
486 static int filter_extract(void *clientData, struct recExtractCtrl *p)
488 struct filter_info *tinfo = clientData;
490 odr_reset(tinfo->odr);
492 if (tinfo->split_level == 0 && tinfo->split_path == 0)
493 return extract_full(tinfo, p);
496 return extract_split(tinfo, p);
500 static int ioread_ret(void *context, char *buffer, int len)
502 struct recRetrieveCtrl *p = context;
503 return (*p->readf)(p->fh, buffer, len);
506 static int ioclose_ret(void *context)
512 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
515 const char *xml_doc_str;
517 WRBUF wrbuf = wrbuf_alloc();
518 zebra_snippets *res =
519 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
520 zebra_snippet_word *w = zebra_snippets_list(res);
523 wrbuf_printf(wrbuf, "\'");
525 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
526 for (; w; w = w->next)
530 else if (ord != w->ord)
534 wrbuf_printf(wrbuf, "%s%s%s ",
537 w->match ? "*" : "");
540 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
542 (w->match ? "match='1'" : ""));
543 wrbuf_xmlputs(wrbuf, w->term);
544 wrbuf_printf(wrbuf, "</term>\n");
548 wrbuf_printf(wrbuf, "\'");
550 wrbuf_printf(wrbuf, "</snippet>\n");
552 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
554 zebra_snippets_destroy(res);
555 wrbuf_free(wrbuf, 1);
559 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
561 /* const char *esn = zebra_xslt_ns; */
563 const char *params[32];
564 struct filter_info *tinfo = clientData;
567 struct filter_schema *schema;
568 int window_size = -1;
572 if (p->comp->which == Z_RecordComp_simple
573 && p->comp->u.simple->which == Z_ElementSetNames_generic)
575 esn = p->comp->u.simple->u.generic;
577 else if (p->comp->which == Z_RecordComp_complex
578 && p->comp->u.complex->generic->elementSpec
579 && p->comp->u.complex->generic->elementSpec->which ==
580 Z_ElementSpec_elementSetName)
582 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
585 schema = lookup_schema(tinfo, esn);
589 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
593 if (schema->include_snippet)
594 window_size = atoi(schema->include_snippet);
597 set_param_int(params, "id", p->localno, p->odr);
599 set_param_str(params, "filename", p->fname, p->odr);
600 if (p->staticrank >= 0)
601 set_param_int(params, "rank", p->staticrank, p->odr);
604 set_param_str(params, "schema", esn, p->odr);
607 set_param_str(params, "schema", schema->name, p->odr);
608 else if (schema->identifier)
609 set_param_str(params, "schema", schema->identifier, p->odr);
611 set_param_str(params, "schema", "", p->odr);
614 set_param_int(params, "score", p->score, p->odr);
615 set_param_int(params, "size", p->recordSize, p->odr);
617 if (window_size >= 0)
618 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
620 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
626 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
630 if (window_size >= 0)
632 xmlNodePtr node = xmlDocGetRootElement(doc);
633 const char *snippet_str = snippet_doc(p, 0, window_size);
634 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
635 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
637 if (!schema->stylesheet_xsp)
641 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
647 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
649 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
653 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
655 p->output_format = VAL_TEXT_XML;
656 p->rec_len = len_out;
657 p->rec_buf = odr_malloc(p->odr, p->rec_len);
658 memcpy(p->rec_buf, buf_out, p->rec_len);
662 else if (p->output_format == VAL_SUTRS)
666 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
668 p->output_format = VAL_SUTRS;
669 p->rec_len = len_out;
670 p->rec_buf = odr_malloc(p->odr, p->rec_len);
671 memcpy(p->rec_buf, buf_out, p->rec_len);
677 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
683 static struct recType filter_type = {
694 #ifdef IDZEBRA_STATIC_ALVIS
707 * indent-tabs-mode: nil
709 * vim: shiftwidth=4 tabstop=8 expandtab