1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
29 #include <yaz/oid_db.h>
31 #include <libxml/xmlversion.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxml/xmlIO.h>
35 #include <libxml/xmlreader.h>
36 #include <libxslt/transform.h>
37 #include <libxslt/xsltutils.h>
40 #include <libexslt/exslt.h>
43 #include <idzebra/util.h>
44 #include <idzebra/recctrl.h>
46 struct filter_schema {
48 const char *identifier;
49 const char *stylesheet;
50 struct filter_schema *next;
51 const char *default_schema;
52 /* char default_schema; */
53 xsltStylesheetPtr stylesheet_xsp;
60 const char *profile_path;
62 const char *split_path;
64 struct filter_schema *schemas;
65 xmlTextReaderPtr reader;
68 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
70 #define XML_STRCMP(a,b) strcmp((char*)a, b)
71 #define XML_STRLEN(a) strlen((char*)a)
73 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
75 static void set_param_str(const char **params, const char *name,
76 const char *value, ODR odr)
78 char *quoted = odr_malloc(odr, 3 + strlen(value));
79 sprintf(quoted, "'%s'", value);
87 static void set_param_int(const char **params, const char *name,
90 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
93 sprintf(quoted, "'" ZINT_FORMAT "'", value);
99 #define ENABLE_INPUT_CALLBACK 0
101 #if ENABLE_INPUT_CALLBACK
102 static int zebra_xmlInputMatchCallback (char const *filename)
104 yaz_log(YLOG_LOG, "match %s", filename);
108 static void * zebra_xmlInputOpenCallback (char const *filename)
113 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
118 static int zebra_xmlInputCloseCallback (void * context)
124 static void *filter_init(Res res, RecType recType)
126 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
129 tinfo->full_name = 0;
130 tinfo->profile_path = 0;
131 tinfo->split_level = 0;
132 tinfo->split_path = 0;
133 tinfo->odr = odr_createmem(ODR_ENCODE);
141 #if ENABLE_INPUT_CALLBACK
142 xmlRegisterDefaultInputCallbacks();
143 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
144 zebra_xmlInputOpenCallback,
145 zebra_xmlInputReadCallback,
146 zebra_xmlInputCloseCallback);
151 static int attr_content(struct _xmlAttr *attr, const char *name,
152 const char **dst_content)
154 if (!XML_STRCMP(attr->name, name) && attr->children
155 && attr->children->type == XML_TEXT_NODE)
157 *dst_content = (const char *)(attr->children->content);
163 static void destroy_schemas(struct filter_info *tinfo)
165 struct filter_schema *schema = tinfo->schemas;
168 struct filter_schema *schema_next = schema->next;
169 if (schema->stylesheet_xsp)
170 xsltFreeStylesheet(schema->stylesheet_xsp);
172 schema = schema_next;
177 xmlFreeDoc(tinfo->doc);
181 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
183 char tmp_full_name[1024];
185 tinfo->fname = xstrdup(fname);
187 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
188 NULL, tmp_full_name))
189 tinfo->full_name = xstrdup(tmp_full_name);
191 tinfo->full_name = xstrdup(tinfo->fname);
193 yaz_log(YLOG_LOG, "alvis filter: loading config file %s", tinfo->full_name);
195 tinfo->doc = xmlParseFile(tinfo->full_name);
199 yaz_log(YLOG_WARN, "alvis filter: could not parse config file %s",
205 ptr = xmlDocGetRootElement(tinfo->doc);
206 if (!ptr || ptr->type != XML_ELEMENT_NODE
207 || 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 struct _xmlAttr *attr;
223 struct filter_schema *schema = xmalloc(sizeof(*schema));
225 schema->identifier = 0;
226 schema->stylesheet = 0;
227 schema->default_schema = 0;
228 schema->next = tinfo->schemas;
229 schema->stylesheet_xsp = 0;
230 tinfo->schemas = schema;
231 for (attr = ptr->properties; attr; attr = attr->next)
233 attr_content(attr, "identifier", &schema->identifier);
234 attr_content(attr, "name", &schema->name);
235 attr_content(attr, "stylesheet", &schema->stylesheet);
236 attr_content(attr, "default", &schema->default_schema);
238 /*yaz_log(YLOG_LOG, "XSLT add %s %s %s",
239 schema->name, schema->identifier, schema->stylesheet); */
241 /* find requested schema */
243 if (schema->stylesheet)
245 char tmp_xslt_full_name[1024];
246 if (!yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
247 NULL, tmp_xslt_full_name))
250 "alvis filter: stylesheet %s not found in path %s",
251 schema->stylesheet, tinfo->profile_path);
254 schema->stylesheet_xsp
255 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
256 if (!schema->stylesheet_xsp)
259 "alvis filter: could not parse xslt stylesheet %s",
265 else if (!XML_STRCMP(ptr->name, "split"))
267 struct _xmlAttr *attr;
268 for (attr = ptr->properties; attr; attr = attr->next)
270 const char *split_level_str = 0;
271 attr_content(attr, "level", &split_level_str);
273 split_level_str ? atoi(split_level_str) : 0;
278 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
285 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
288 struct filter_schema *schema;
290 for (schema = tinfo->schemas; schema; schema = schema->next)
292 /* find requested schema */
295 if (schema->identifier && !strcmp(schema->identifier, est))
298 if (schema->name && !strcmp(schema->name, est))
301 /* or return default schema if defined */
302 else if (schema->default_schema)
306 /* return first schema if no default schema defined */
308 return tinfo->schemas;
313 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
315 struct filter_info *tinfo = clientData;
318 yaz_log(YLOG_WARN, "alvis filter: need config file");
322 if (tinfo->fname && !strcmp(args, tinfo->fname))
325 tinfo->profile_path = res_get(res, "profilePath");
326 yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
328 destroy_schemas(tinfo);
329 return create_schemas(tinfo, args);
332 static void filter_destroy(void *clientData)
334 struct filter_info *tinfo = clientData;
335 destroy_schemas(tinfo);
336 xfree(tinfo->full_name);
338 xmlFreeTextReader(tinfo->reader);
339 odr_destroy(tinfo->odr);
343 static int ioread_ex(void *context, char *buffer, int len)
345 struct recExtractCtrl *p = context;
346 return p->stream->readf(p->stream, buffer, len);
349 static int ioclose_ex(void *context)
354 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
355 xmlNodePtr ptr, RecWord *recWord)
357 for(; ptr; ptr = ptr->next)
359 index_cdata(tinfo, ctrl, ptr->children, recWord);
360 if (ptr->type != XML_TEXT_NODE)
362 recWord->term_buf = (const char *)ptr->content;
363 recWord->term_len = XML_STRLEN(ptr->content);
364 (*ctrl->tokenAdd)(recWord);
368 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
369 xmlNodePtr ptr, RecWord *recWord)
371 for(; ptr; ptr = ptr->next)
373 index_node(tinfo, ctrl, ptr->children, recWord);
374 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
375 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
377 if (!XML_STRCMP(ptr->name, "index"))
379 const char *name_str = 0;
380 const char *type_str = 0;
381 const char *xpath_str = 0;
382 struct _xmlAttr *attr;
383 for (attr = ptr->properties; attr; attr = attr->next)
385 attr_content(attr, "name", &name_str);
386 attr_content(attr, "xpath", &xpath_str);
387 attr_content(attr, "type", &type_str);
391 const char *prev_type = recWord->index_type; /* save default type */
393 if (type_str && *type_str)
394 recWord->index_type = (const char *) type_str; /* type was given */
395 recWord->index_name = name_str;
396 index_cdata(tinfo, ctrl, ptr->children, recWord);
398 recWord->index_type = prev_type; /* restore it again */
404 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
405 xmlNodePtr ptr, RecWord *recWord)
407 const char *type_str = "update";
409 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
410 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
411 && !XML_STRCMP(ptr->name, "record"))
413 const char *id_str = 0;
414 const char *rank_str = 0;
415 struct _xmlAttr *attr;
416 for (attr = ptr->properties; attr; attr = attr->next)
418 attr_content(attr, "type", &type_str);
419 attr_content(attr, "id", &id_str);
420 attr_content(attr, "rank", &rank_str);
423 sscanf(id_str, "%255s", ctrl->match_criteria);
426 ctrl->staticrank = atozint(rank_str);
430 if (!strcmp("update", type_str))
431 index_node(tinfo, ctrl, ptr, recWord);
432 else if (!strcmp("delete", type_str))
433 yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
435 yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'",
439 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
443 const char *params[10];
447 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
450 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
452 (*p->init)(p, &recWord);
454 if (schema && schema->stylesheet_xsp)
458 xsltApplyStylesheet(schema->stylesheet_xsp,
460 if (p->flagShowRecords)
462 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
463 fwrite(buf_out, len_out, 1, stdout);
466 root_ptr = xmlDocGetRootElement(resDoc);
468 index_record(tinfo, p, root_ptr, &recWord);
471 yaz_log(YLOG_WARN, "No root for index XML record."
472 " split_level=%d stylesheet=%s",
473 tinfo->split_level, schema->stylesheet);
477 xmlDocDumpMemory(doc, &buf_out, &len_out);
478 if (p->flagShowRecords)
479 fwrite(buf_out, len_out, 1, stdout);
481 (*p->setStoreData)(p, buf_out, len_out);
485 return RECCTRL_EXTRACT_OK;
488 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
495 xmlFreeTextReader(tinfo->reader);
496 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
505 return RECCTRL_EXTRACT_ERROR_GENERIC;
507 ret = xmlTextReaderRead(tinfo->reader);
510 int type = xmlTextReaderNodeType(tinfo->reader);
511 int depth = xmlTextReaderDepth(tinfo->reader);
512 if (type == XML_READER_TYPE_ELEMENT && tinfo->split_level == depth)
514 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
517 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
518 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
520 xmlDocSetRootElement(doc, ptr2);
522 return extract_doc(tinfo, p, doc);
526 xmlFreeTextReader(tinfo->reader);
528 return RECCTRL_EXTRACT_ERROR_GENERIC;
531 ret = xmlTextReaderRead(tinfo->reader);
533 xmlFreeTextReader(tinfo->reader);
535 return RECCTRL_EXTRACT_EOF;
538 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
540 if (p->first_record) /* only one record per stream */
542 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
549 return RECCTRL_EXTRACT_ERROR_GENERIC;
551 xmlNodePtr root = xmlDocGetRootElement(doc);
553 return RECCTRL_EXTRACT_ERROR_GENERIC;
556 return extract_doc(tinfo, p, doc);
559 return RECCTRL_EXTRACT_EOF;
562 static int filter_extract(void *clientData, struct recExtractCtrl *p)
564 struct filter_info *tinfo = clientData;
566 odr_reset(tinfo->odr);
567 if (tinfo->split_level == 0 || p->setStoreData == 0)
568 return extract_full(tinfo, p);
570 return extract_split(tinfo, p);
573 static int ioread_ret(void *context, char *buffer, int len)
575 struct recRetrieveCtrl *p = context;
576 return p->stream->readf(p->stream, buffer, len);
579 static int ioclose_ret(void *context)
584 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
586 /* const char *esn = zebra_xslt_ns; */
588 const char *params[32];
589 struct filter_info *tinfo = clientData;
592 struct filter_schema *schema;
596 if (p->comp->which == Z_RecordComp_simple
597 && p->comp->u.simple->which == Z_ElementSetNames_generic)
599 esn = p->comp->u.simple->u.generic;
601 else if (p->comp->which == Z_RecordComp_complex
602 && p->comp->u.complex->generic->elementSpec
603 && p->comp->u.complex->generic->elementSpec->which ==
604 Z_ElementSpec_elementSetName)
606 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
609 schema = lookup_schema(tinfo, esn);
613 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
618 set_param_int(params, "id", p->localno, p->odr);
620 set_param_str(params, "filename", p->fname, p->odr);
621 if (p->staticrank >= 0)
622 set_param_int(params, "rank", p->staticrank, p->odr);
625 set_param_str(params, "schema", esn, p->odr);
628 set_param_str(params, "schema", schema->name, p->odr);
629 else if (schema->identifier)
630 set_param_str(params, "schema", schema->identifier, p->odr);
632 set_param_str(params, "schema", "", p->odr);
635 set_param_int(params, "score", p->score, p->odr);
636 set_param_int(params, "size", p->recordSize, p->odr);
638 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
641 XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
644 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
648 if (!schema->stylesheet_xsp)
652 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
658 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
660 else if (!p->input_format
661 || !oid_oidcmp(p->input_format, yaz_oid_recsyn_xml))
666 if (schema->stylesheet_xsp)
667 xsltSaveResultToString(&buf_out, &len_out, resDoc,
668 schema->stylesheet_xsp);
670 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
672 p->output_format = yaz_oid_recsyn_xml;
673 p->rec_len = len_out;
674 p->rec_buf = odr_malloc(p->odr, p->rec_len);
675 memcpy(p->rec_buf, buf_out, p->rec_len);
678 else if (!oid_oidcmp(p->output_format, yaz_oid_recsyn_sutrs))
683 if (schema->stylesheet_xsp)
684 xsltSaveResultToString(&buf_out, &len_out, resDoc,
685 schema->stylesheet_xsp);
687 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
689 p->output_format = yaz_oid_recsyn_sutrs;
690 p->rec_len = len_out;
691 p->rec_buf = odr_malloc(p->odr, p->rec_len);
692 memcpy(p->rec_buf, buf_out, p->rec_len);
698 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
704 static struct recType filter_type = {
715 #if IDZEBRA_STATIC_ALVIS
728 * c-file-style: "Stroustrup"
729 * indent-tabs-mode: nil
731 * vim: shiftwidth=4 tabstop=8 expandtab