1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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
25 #include <yaz/diagbib1.h>
26 #include <yaz/tpath.h>
27 #include <yaz/snprintf.h>
29 #include <libxml/xmlversion.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xmlIO.h>
33 #include <libxml/xmlreader.h>
34 #include <libxslt/transform.h>
35 #include <libxslt/xsltutils.h>
38 #include <libexslt/exslt.h>
41 #include <idzebra/util.h>
42 #include <idzebra/recctrl.h>
43 #include <yaz/oid_db.h>
45 /* DOM filter style indexing */
46 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
47 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
49 /* DOM filter style indexing */
50 #define ZEBRA_PI_NAME "zebra-2.0"
51 static const char *zebra_pi_name = ZEBRA_PI_NAME;
59 const char *stylesheet;
60 xsltStylesheetPtr stylesheet_xsp;
68 enum convert_type which;
70 struct convert_xslt xslt;
71 struct convert_meta meta;
73 struct convert_s *next;
76 struct filter_extract {
78 struct convert_s *convert;
82 struct convert_s *convert;
85 struct filter_retrieve {
87 const char *identifier;
88 struct convert_s *convert;
89 struct filter_retrieve *next;
92 #define DOM_INPUT_XMLREADER 1
93 #define DOM_INPUT_MARC 2
97 struct convert_s *convert;
101 xmlTextReaderPtr reader;
105 const char *input_charset;
110 struct filter_input *next;
116 const char *profile_path;
119 xmlDocPtr doc_config;
120 struct filter_extract *extract;
121 struct filter_retrieve *retrieve_list;
122 struct filter_input *input_list;
123 struct filter_store *store;
124 int record_info_invoked;
129 #define XML_STRCMP(a,b) strcmp((char*)a, b)
130 #define XML_STRLEN(a) strlen((char*)a)
133 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
135 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
136 const char *fmt, ...)
138 __attribute__ ((format (printf, 4, 5)))
142 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
143 const char *fmt, ...)
149 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
152 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
153 xmlGetLineNo(ptr), buf);
157 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
163 static void set_param_str(const char **params, const char *name,
164 const char *value, NMEM nmem)
166 char *quoted = nmem_malloc(nmem, 3 + strlen(value));
167 sprintf(quoted, "'%s'", value);
175 static void set_param_int(const char **params, const char *name,
176 zint value, NMEM nmem)
178 char *quoted = nmem_malloc(nmem, 30); /* 25 digits enough for 2^64 */
181 sprintf(quoted, "'" ZINT_FORMAT "'", value);
187 static void *filter_init(Res res, RecType recType)
189 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
191 tinfo->full_name = 0;
192 tinfo->profile_path = 0;
193 tinfo->nmem_record = nmem_create();
194 tinfo->nmem_config = nmem_create();
196 tinfo->retrieve_list = 0;
197 tinfo->input_list = 0;
199 tinfo->doc_config = 0;
200 tinfo->record_info_invoked = 0;
209 static int attr_content(struct _xmlAttr *attr, const char *name,
210 const char **dst_content)
212 if (!XML_STRCMP(attr->name, name) && attr->children
213 && attr->children->type == XML_TEXT_NODE)
215 *dst_content = (const char *)(attr->children->content);
221 static void destroy_xsp(struct convert_s *c)
225 if (c->which == convert_xslt_type)
227 if (c->u.xslt.stylesheet_xsp)
228 xsltFreeStylesheet(c->u.xslt.stylesheet_xsp);
234 static void destroy_dom(struct filter_info *tinfo)
238 destroy_xsp(tinfo->extract->convert);
243 destroy_xsp(tinfo->store->convert);
246 if (tinfo->input_list)
248 struct filter_input *i_ptr;
249 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
253 case DOM_INPUT_XMLREADER:
254 if (i_ptr->u.xmlreader.reader)
255 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
258 yaz_iconv_close(i_ptr->u.marc.iconv);
259 yaz_marc_destroy(i_ptr->u.marc.handle);
262 destroy_xsp(i_ptr->convert);
264 tinfo->input_list = 0;
266 if (tinfo->retrieve_list)
268 struct filter_retrieve *r_ptr;
269 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
270 destroy_xsp(r_ptr->convert);
271 tinfo->retrieve_list = 0;
274 if (tinfo->doc_config)
276 xmlFreeDoc(tinfo->doc_config);
277 tinfo->doc_config = 0;
279 nmem_reset(tinfo->nmem_config);
282 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
283 struct convert_s **l)
286 FOR_EACH_ELEMENT(ptr) {
287 if (!XML_STRCMP(ptr->name, "xslt"))
289 struct _xmlAttr *attr;
290 struct convert_s *p = nmem_malloc(tinfo->nmem_config, sizeof(*p));
293 p->which = convert_xslt_type;
294 p->u.xslt.stylesheet = 0;
295 p->u.xslt.stylesheet_xsp = 0;
297 for (attr = ptr->properties; attr; attr = attr->next)
298 if (attr_content(attr, "stylesheet", &p->u.xslt.stylesheet))
302 dom_log(YLOG_WARN, tinfo, ptr,
303 "bad attribute @%s", attr->name);
305 if (p->u.xslt.stylesheet)
307 char tmp_xslt_full_name[1024];
308 if (!yaz_filepath_resolve(p->u.xslt.stylesheet,
313 dom_log(YLOG_WARN, tinfo, 0,
314 "stylesheet %s not found in "
316 p->u.xslt.stylesheet,
317 tinfo->profile_path);
321 p->u.xslt.stylesheet_xsp
322 = xsltParseStylesheetFile((const xmlChar*)
324 if (!p->u.xslt.stylesheet_xsp)
326 dom_log(YLOG_WARN, tinfo, 0,
327 "could not parse xslt stylesheet %s",
334 dom_log(YLOG_WARN, tinfo, ptr,
335 "missing attribute 'stylesheet'");
341 else if (!XML_STRCMP(ptr->name, "meta"))
343 struct _xmlAttr *attr;
344 struct convert_s *p = nmem_malloc(tinfo->nmem_config, sizeof(*p));
347 p->which = convert_meta_type;
349 for (attr = ptr->properties; attr; attr = attr->next)
350 dom_log(YLOG_WARN, tinfo, ptr,
351 "bad attribute @%s", attr->name);
357 dom_log(YLOG_WARN, tinfo, ptr,
358 "bad element '%s', expected <xslt>", ptr->name);
365 static int process_meta(struct filter_info *tinfo, xmlDocPtr doc, xmlNodePtr node,
366 struct recRetrieveCtrl *retctr)
369 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href &&
370 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
372 if (0 == XML_STRCMP(node->name, "meta"))
374 const char *element_set_name = 0;
376 struct _xmlAttr *attr;
377 for (attr = node->properties; attr; attr = attr->next)
379 if (attr_content(attr, "element_set_name", &element_set_name))
383 dom_log(YLOG_WARN, tinfo, node,
384 "bad attribute @%s, expected @element_set_name",
388 if (element_set_name)
390 WRBUF result = wrbuf_alloc();
391 WRBUF addinfo = wrbuf_alloc();
392 const Odr_oid *input_format = yaz_oid_recsyn_xml;
393 const Odr_oid *output_format = 0;
396 ret = retctr->special_fetch(retctr->handle,
398 input_format, &output_format,
403 xmlParseMemory( wrbuf_buf(result), wrbuf_len(result));
406 xmlNodePtr t = xmlDocGetRootElement(sub_doc);
407 xmlAddChild(node, xmlCopyNode(t, 1));
411 wrbuf_destroy(result);
412 wrbuf_destroy(addinfo);
416 for (node = node->children; node; node = node->next)
417 process_meta(tinfo, doc, node, retctr);
421 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
422 struct recExtractCtrl *extctr,
423 struct recRetrieveCtrl *retctr,
424 struct convert_s *convert,
427 xsltStylesheetPtr *last_xsp)
429 for (; convert; convert = convert->next)
431 if (convert->which == convert_xslt_type)
433 xmlChar *buf_out = 0;
435 xmlDocPtr res_doc = xsltApplyStylesheet(convert->u.xslt.stylesheet_xsp,
438 *last_xsp = convert->u.xslt.stylesheet_xsp;
443 /* now saving into buffer and re-reading into DOM to avoid annoing
444 XSLT problem with thrown-out indentation text nodes */
445 xsltSaveResultToString(&buf_out, &len_out, res_doc,
446 convert->u.xslt.stylesheet_xsp);
451 *doc = xmlParseMemory((const char *) buf_out, len_out);
453 /* writing debug info out */
454 if (extctr && extctr->flagShowRecords)
455 yaz_log(YLOG_LOG, "%s: XSLT %s\n %.*s",
456 tinfo->fname ? tinfo->fname : "(none)",
457 convert->u.xslt.stylesheet,
462 else if (convert->which == convert_meta_type)
464 if (retctr) /* only execute meta on retrieval */
466 process_meta(tinfo, *doc, xmlDocGetRootElement(*doc), retctr);
468 /* last stylesheet absent */
477 static struct filter_input *new_input(struct filter_info *tinfo, int type)
479 struct filter_input *p;
480 struct filter_input **np = &tinfo->input_list;
481 for (;*np; np = &(*np)->next)
483 p = *np = nmem_malloc(tinfo->nmem_config, sizeof(*p));
492 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
493 const char *syntax, const char *name)
495 FOR_EACH_ELEMENT(ptr) {
496 if (!XML_STRCMP(ptr->name, "marc"))
498 yaz_iconv_t iconv = 0;
499 const char *input_charset = "marc-8";
500 struct _xmlAttr *attr;
502 for (attr = ptr->properties; attr; attr = attr->next)
504 if (attr_content(attr, "inputcharset", &input_charset))
508 dom_log(YLOG_WARN, tinfo, ptr,
509 "bad attribute @%s, expected @inputcharset",
513 iconv = yaz_iconv_open("utf-8", input_charset);
516 dom_log(YLOG_WARN, tinfo, ptr,
517 "unsupported @charset '%s'", input_charset);
522 struct filter_input *p
523 = new_input(tinfo, DOM_INPUT_MARC);
524 p->u.marc.handle = yaz_marc_create();
525 p->u.marc.iconv = iconv;
527 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
531 parse_convert(tinfo, ptr, &p->convert);
536 else if (!XML_STRCMP(ptr->name, "xmlreader"))
538 struct filter_input *p
539 = new_input(tinfo, DOM_INPUT_XMLREADER);
540 struct _xmlAttr *attr;
541 const char *level_str = 0;
543 p->u.xmlreader.split_level = 0;
544 p->u.xmlreader.reader = 0;
546 for (attr = ptr->properties; attr; attr = attr->next)
548 if (attr_content(attr, "level", &level_str))
552 dom_log(YLOG_WARN, tinfo, ptr,
553 "bad attribute @%s, expected @level",
558 p->u.xmlreader.split_level = atoi(level_str);
562 parse_convert(tinfo, ptr, &p->convert);
567 dom_log(YLOG_WARN, tinfo, ptr,
568 "bad element <%s>, expected <marc>|<xmlreader>",
576 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
578 char tmp_full_name[1024];
582 tinfo->fname = nmem_strdup(tinfo->nmem_config, fname);
584 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
585 NULL, tmp_full_name))
586 tinfo->full_name = nmem_strdup(tinfo->nmem_config, tmp_full_name);
588 tinfo->full_name = nmem_strdup(tinfo->nmem_config, tinfo->fname);
590 yaz_log(YLOG_LOG, "%s dom filter: "
591 "loading config file %s", tinfo->fname, tinfo->full_name);
593 doc = xmlParseFile(tinfo->full_name);
596 yaz_log(YLOG_WARN, "%s: dom filter: "
597 "failed to parse config file %s",
598 tinfo->fname, tinfo->full_name);
601 /* save because we store ptrs to the content */
602 tinfo->doc_config = doc;
604 ptr = xmlDocGetRootElement(doc);
605 if (!ptr || ptr->type != XML_ELEMENT_NODE
606 || XML_STRCMP(ptr->name, "dom"))
608 dom_log(YLOG_WARN, tinfo, ptr,
609 "bad root element <%s>, expected root element <dom>",
615 FOR_EACH_ELEMENT(ptr) {
616 if (!XML_STRCMP(ptr->name, "extract"))
619 <extract name="index">
620 <xslt stylesheet="first.xsl"/>
621 <xslt stylesheet="second.xsl"/>
624 struct _xmlAttr *attr;
625 struct filter_extract *f =
626 nmem_malloc(tinfo->nmem_config, sizeof(*f));
631 for (attr = ptr->properties; attr; attr = attr->next)
633 if (attr_content(attr, "name", &f->name))
637 dom_log(YLOG_WARN, tinfo, ptr,
638 "bad attribute @%s, expected @name",
642 parse_convert(tinfo, ptr->children, &f->convert);
644 else if (!XML_STRCMP(ptr->name, "retrieve"))
648 <xslt stylesheet="some.xsl"/>
649 <xslt stylesheet="some.xsl"/>
652 struct _xmlAttr *attr;
653 struct filter_retrieve **fp = &tinfo->retrieve_list;
654 struct filter_retrieve *f =
655 nmem_malloc(tinfo->nmem_config, sizeof(*f));
666 for (attr = ptr->properties; attr; attr = attr->next)
668 if (attr_content(attr, "identifier",
671 else if (attr_content(attr, "name", &f->name))
675 dom_log(YLOG_WARN, tinfo, ptr,
676 "bad attribute @%s, expected @identifier|@name",
680 parse_convert(tinfo, ptr->children, &f->convert);
682 else if (!XML_STRCMP(ptr->name, "store"))
686 <xslt stylesheet="some.xsl"/>
687 <xslt stylesheet="some.xsl"/>
690 struct filter_store *f =
691 nmem_malloc(tinfo->nmem_config, sizeof(*f));
695 parse_convert(tinfo, ptr->children, &f->convert);
697 else if (!XML_STRCMP(ptr->name, "input"))
701 <xmlreader level="1"/>
703 <input syntax="usmarc">
704 <marc inputcharset="marc-8"/>
707 struct _xmlAttr *attr;
708 const char *syntax = 0;
709 const char *name = 0;
710 for (attr = ptr->properties; attr; attr = attr->next)
712 if (attr_content(attr, "syntax", &syntax))
714 else if (attr_content(attr, "name", &name))
718 dom_log(YLOG_WARN, tinfo, ptr,
719 "bad attribute @%s, expected @syntax|@name",
723 parse_input(tinfo, ptr->children, syntax, name);
727 dom_log(YLOG_WARN, tinfo, ptr,
729 "expected <extract>|<input>|<retrieve>|<store>",
734 if (!tinfo->input_list)
736 struct filter_input *p
737 = new_input(tinfo, DOM_INPUT_XMLREADER);
738 p->u.xmlreader.split_level = 0;
739 p->u.xmlreader.reader = 0;
744 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
747 struct filter_retrieve *f = tinfo->retrieve_list;
749 /* return first schema if no est is provided */
752 for (; f; f = f->next)
754 /* find requested schema */
757 if (f->identifier && !strcmp(f->identifier, est))
759 if (f->name && !strcmp(f->name, est))
766 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
768 struct filter_info *tinfo = clientData;
771 yaz_log(YLOG_WARN, "dom filter: need config file");
775 if (tinfo->fname && !strcmp(args, tinfo->fname))
778 tinfo->profile_path = res_get(res, "profilePath");
781 return parse_dom(tinfo, args);
784 static void filter_destroy(void *clientData)
786 struct filter_info *tinfo = clientData;
788 nmem_destroy(tinfo->nmem_config);
789 nmem_destroy(tinfo->nmem_record);
793 static int ioread_ex(void *context, char *buffer, int len)
795 struct recExtractCtrl *p = context;
796 return p->stream->readf(p->stream, buffer, len);
799 static int ioclose_ex(void *context)
806 /* DOM filter style indexing */
807 static void index_value_of(struct filter_info *tinfo,
808 struct recExtractCtrl *extctr,
813 if (tinfo->record_info_invoked == 1)
815 xmlChar *text = xmlNodeGetContent(node);
816 size_t text_len = strlen((const char *)text);
818 /* if there is no text, we do not need to proceed */
821 const char *look = index_p;
828 /* assingning text to be indexed */
829 recword->term_buf = (const char *)text;
830 recword->term_len = text_len;
832 /* parsing all index name/type pairs */
833 /* may not start with ' ' or ':' */
834 while (*look && ' ' != *look && ':' != *look)
836 /* setting name and type to zero */
840 /* parsing one index name */
842 while (*look && ':' != *look && ' ' != *look)
847 strncpy((char *)index, (const char *)bval, eval - bval);
848 index[eval - bval] = '\0';
851 /* parsing one index type, if existing */
857 while (*look && ' ' != *look)
862 strncpy((char *)type, (const char *)bval, eval - bval);
863 type[eval - bval] = '\0';
866 /* actually indexing the text given */
868 recword->index_name = (const char *)index;
870 recword->index_type = (const char *) type;
872 /* writing debug out */
873 if (extctr->flagShowRecords)
874 dom_log(YLOG_LOG, tinfo, 0,
875 "INDEX '%s:%s' '%s'",
876 (const char *) index,
878 (const char *) text);
880 (extctr->tokenAdd)(recword);
882 /* eat whitespaces */
883 if (*look && ' ' == *look)
894 /* DOM filter style indexing */
895 static void set_record_info(struct filter_info *tinfo,
896 struct recExtractCtrl *extctr,
902 /* writing debug info out */
903 if (extctr && extctr->flagShowRecords)
904 dom_log(YLOG_LOG, tinfo, node,
905 "RECORD id=%s rank=%s type=%s",
906 id_p ? (const char *) id_p : "(null)",
907 rank_p ? (const char *) rank_p : "(null)",
908 type_p ? (const char *) type_p : "(null)");
912 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
914 if (rank_p && *rank_p)
915 extctr->staticrank = atozint((const char *)rank_p);
917 if (type_p && *type_p)
919 enum zebra_recctrl_action_t action = action_update;
920 if (!strcmp(type_p, "insert"))
921 action = action_insert;
922 else if (!strcmp(type_p, "delete"))
923 action = action_delete;
924 else if (!strcmp(type_p, "replace"))
925 action = action_replace;
926 else if (!strcmp(type_p, "update"))
927 action = action_update;
929 dom_log(YLOG_WARN, tinfo, node, "bad @type value: %s", type_p);
930 extctr->action = action;
933 if (tinfo->record_info_invoked == 1)
935 /* warn about multiple only once */
936 dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
938 tinfo->record_info_invoked++;
943 /* DOM filter style indexing */
944 static void process_xml_element_zebra_node(struct filter_info *tinfo,
945 struct recExtractCtrl *extctr,
949 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
950 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
952 if (0 == XML_STRCMP(node->name, "index"))
954 const char *index_p = 0;
956 struct _xmlAttr *attr;
957 for (attr = node->properties; attr; attr = attr->next)
959 if (attr_content(attr, "name", &index_p))
961 index_value_of(tinfo, extctr, recword, node, index_p);
965 dom_log(YLOG_WARN, tinfo, node,
966 "bad attribute @%s, expected @name",
971 else if (0 == XML_STRCMP(node->name, "record"))
973 const char *id_p = 0;
974 const char *rank_p = 0;
975 const char *type_p = 0;
977 struct _xmlAttr *attr;
978 for (attr = node->properties; attr; attr = attr->next)
980 if (attr_content(attr, "id", &id_p))
982 else if (attr_content(attr, "rank", &rank_p))
984 else if (attr_content(attr, "type", &type_p))
988 dom_log(YLOG_WARN, tinfo, node,
989 "bad attribute @%s, expected @id|@rank|@type",
993 set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
997 dom_log(YLOG_WARN, tinfo, node,
999 " expected <record>|<index> in namespace '%s'",
1000 node->name, zebra_dom_ns);
1005 static int attr_content_pi(const char **c_ptr, const char *name,
1006 char *value, size_t value_max)
1008 size_t name_len = strlen(name);
1009 const char *look = *c_ptr;
1013 while (*look && ' ' == *look)
1015 if (strlen(look) > name_len)
1017 if (look[name_len] == '=' && !memcmp(look, name, name_len))
1021 while (*look && ' ' != *look)
1023 if (i < value_max-1)
1031 while (*look && ' ' == *look)
1037 /* DOM filter style indexing */
1038 static void process_xml_pi_node(struct filter_info *tinfo,
1039 struct recExtractCtrl *extctr,
1041 const char **index_pp)
1043 /* if right PI name, continue parsing PI */
1044 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
1046 xmlChar *pi_p = node->content;
1047 const char *look = (const char *) node->content;
1049 /* parsing PI record instructions */
1050 if (0 == strncmp((const char *)look, "record", 6))
1061 if (attr_content_pi(&look, "id", id, sizeof(id)))
1063 else if (attr_content_pi(&look, "rank", rank, sizeof(rank)))
1065 else if (attr_content_pi(&look, "type", type, sizeof(type)))
1067 dom_log(YLOG_WARN, tinfo, node,
1068 "content '%s', can not parse '%s'",
1072 set_record_info(tinfo, extctr, node, id, rank, type);
1074 /* parsing index instruction */
1075 else if (0 == strncmp((const char *)look, "index", 5))
1079 /* eat whitespace */
1080 while (*look && ' ' == *look)
1083 /* export index instructions to outside */
1088 dom_log(YLOG_WARN, tinfo, node,
1089 "content '%s', can not parse '%s'",
1095 /* DOM filter style indexing */
1096 static void process_xml_element_node(struct filter_info *tinfo,
1097 struct recExtractCtrl *extctr,
1101 /* remember indexing instruction from PI to next element node */
1102 const char *index_p = 0;
1104 /* check if we are an element node in the special zebra namespace
1105 and either set record data or index value-of node content*/
1106 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1108 /* loop through kid nodes */
1109 for (node = node->children; node; node = node->next)
1111 /* check and set PI record and index index instructions */
1112 if (node->type == XML_PI_NODE)
1114 process_xml_pi_node(tinfo, extctr, node, &index_p);
1116 else if (node->type == XML_ELEMENT_NODE)
1118 /* if there was a PI index instruction before this element */
1121 index_value_of(tinfo, extctr, recword, node, index_p);
1124 process_xml_element_node(tinfo, extctr, recword,node);
1132 /* DOM filter style indexing */
1133 static void extract_dom_doc_node(struct filter_info *tinfo,
1134 struct recExtractCtrl *extctr,
1137 /* only need to do the initialization once, reuse recword for all terms */
1139 (*extctr->init)(extctr, &recword);
1141 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1145 static int convert_extract_doc(struct filter_info *tinfo,
1146 struct filter_input *input,
1147 struct recExtractCtrl *p,
1152 const char *params[10];
1153 xsltStylesheetPtr last_xsp = 0;
1154 xmlDocPtr store_doc = 0;
1156 /* per default do not ingest record */
1157 tinfo->record_info_invoked = 0;
1159 /* exit if empty document given */
1161 return RECCTRL_EXTRACT_SKIP;
1163 /* we actuallu have a document which needs to be processed further */
1165 set_param_str(params, "schema", zebra_dom_ns, tinfo->nmem_record);
1167 if (p && p->flagShowRecords)
1172 FILE *outf = fopen("extract.xml", "w");
1173 xmlDocDumpMemory(doc, &buf_out, &len_out);
1174 fwrite(buf_out, 1, len_out, outf);
1176 yaz_log(YLOG_LOG, "Extract Doc: %.*s", len_out, buf_out);
1182 /* input conversion */
1183 perform_convert(tinfo, p, 0, input->convert, params, &doc, 0);
1187 /* store conversion */
1188 store_doc = xmlCopyDoc(doc, 1);
1189 perform_convert(tinfo, p, 0, tinfo->store->convert,
1190 params, &store_doc, &last_xsp);
1193 /* saving either store doc or original doc in case no store doc exists */
1195 xsltSaveResultToString(&buf_out, &len_out,
1196 store_doc ? store_doc : doc, last_xsp);
1198 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1200 if (p->setStoreData)
1201 (*p->setStoreData)(p, buf_out, len_out);
1205 xmlFreeDoc(store_doc);
1207 /* extract conversion */
1208 perform_convert(tinfo, p, 0, tinfo->extract->convert, params, &doc, 0);
1211 /* finally, do the indexing */
1213 extract_dom_doc_node(tinfo, p, doc);
1217 /* there was nothing to index, so there is no inserted/updated record */
1218 if (tinfo->record_info_invoked == 0)
1219 return RECCTRL_EXTRACT_SKIP;
1221 return RECCTRL_EXTRACT_OK;
1224 static int extract_xml_split(struct filter_info *tinfo,
1225 struct filter_input *input,
1226 struct recExtractCtrl *p)
1230 if (p->first_record)
1232 if (input->u.xmlreader.reader)
1233 xmlFreeTextReader(input->u.xmlreader.reader);
1234 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1235 p /* I/O handler */,
1242 if (!input->u.xmlreader.reader)
1243 return RECCTRL_EXTRACT_ERROR_GENERIC;
1245 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1248 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1249 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1251 if (type == XML_READER_TYPE_ELEMENT &&
1252 input->u.xmlreader.split_level == depth)
1256 /* per default do not ingest record */
1257 tinfo->record_info_invoked = 0;
1259 ptr = xmlTextReaderExpand(input->u.xmlreader.reader);
1262 /* we have a new document */
1264 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1265 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1267 xmlDocSetRootElement(doc, ptr2);
1269 /* writing debug info out */
1270 if (p->flagShowRecords)
1272 xmlChar *buf_out = 0;
1274 xmlDocDumpMemory(doc, &buf_out, &len_out);
1275 yaz_log(YLOG_LOG, "%s: XMLREADER level: %i\n%.*s",
1276 tinfo->fname ? tinfo->fname : "(none)",
1277 depth, len_out, buf_out);
1281 return convert_extract_doc(tinfo, input, p, doc);
1285 xmlFreeTextReader(input->u.xmlreader.reader);
1286 input->u.xmlreader.reader = 0;
1287 return RECCTRL_EXTRACT_ERROR_GENERIC;
1290 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1292 xmlFreeTextReader(input->u.xmlreader.reader);
1293 input->u.xmlreader.reader = 0;
1294 return RECCTRL_EXTRACT_EOF;
1297 static int extract_xml_full(struct filter_info *tinfo,
1298 struct filter_input *input,
1299 struct recExtractCtrl *p)
1301 if (p->first_record) /* only one record per stream */
1303 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1304 p /* I/O handler */,
1312 return RECCTRL_EXTRACT_ERROR_GENERIC;
1314 return convert_extract_doc(tinfo, input, p, doc);
1317 return RECCTRL_EXTRACT_EOF;
1320 static int extract_iso2709(struct filter_info *tinfo,
1321 struct filter_input *input,
1322 struct recExtractCtrl *p)
1328 if (p->stream->readf(p->stream, buf, 5) != 5)
1329 return RECCTRL_EXTRACT_EOF;
1330 while (*buf < '0' || *buf > '9')
1334 dom_log(YLOG_WARN, tinfo, 0,
1335 "MARC: Skipping bad byte %d (0x%02X)",
1336 *buf & 0xff, *buf & 0xff);
1337 for (i = 0; i<4; i++)
1340 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1341 return RECCTRL_EXTRACT_EOF;
1343 record_length = atoi_n (buf, 5);
1344 if (record_length < 25)
1346 dom_log(YLOG_WARN, tinfo, 0,
1347 "MARC record length < 25, is %d", record_length);
1348 return RECCTRL_EXTRACT_ERROR_GENERIC;
1350 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1351 if (read_bytes < record_length-5)
1353 dom_log(YLOG_WARN, tinfo, 0,
1354 "couldn't read whole MARC record");
1355 return RECCTRL_EXTRACT_ERROR_GENERIC;
1357 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1358 if (r < record_length)
1360 dom_log (YLOG_WARN, tinfo, 0,
1361 "parsing of MARC record failed r=%d length=%d",
1363 return RECCTRL_EXTRACT_ERROR_GENERIC;
1369 yaz_marc_write_xml(input->u.marc.handle, &root_ptr,
1370 "http://www.loc.gov/MARC21/slim", 0, 0);
1371 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1372 xmlDocSetRootElement(rdoc, root_ptr);
1373 return convert_extract_doc(tinfo, input, p, rdoc);
1375 return RECCTRL_EXTRACT_OK;
1378 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1380 struct filter_info *tinfo = clientData;
1381 struct filter_input *input = tinfo->input_list;
1384 return RECCTRL_EXTRACT_ERROR_GENERIC;
1386 nmem_reset(tinfo->nmem_record);
1388 if (p->setStoreData == 0)
1389 return extract_xml_full(tinfo, input, p);
1392 case DOM_INPUT_XMLREADER:
1393 if (input->u.xmlreader.split_level == 0)
1394 return extract_xml_full(tinfo, input, p);
1396 return extract_xml_split(tinfo, input, p);
1398 case DOM_INPUT_MARC:
1399 return extract_iso2709(tinfo, input, p);
1401 return RECCTRL_EXTRACT_ERROR_GENERIC;
1404 static int ioread_ret(void *context, char *buffer, int len)
1406 struct recRetrieveCtrl *p = context;
1407 return p->stream->readf(p->stream, buffer, len);
1410 static int ioclose_ret(void *context)
1415 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
1417 /* const char *esn = zebra_dom_ns; */
1418 const char *esn = 0;
1419 const char *params[32];
1420 struct filter_info *tinfo = clientData;
1422 struct filter_retrieve *retrieve;
1423 xsltStylesheetPtr last_xsp = 0;
1427 if (p->comp->which == Z_RecordComp_simple
1428 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1430 esn = p->comp->u.simple->u.generic;
1432 else if (p->comp->which == Z_RecordComp_complex
1433 && p->comp->u.complex->generic->elementSpec
1434 && p->comp->u.complex->generic->elementSpec->which ==
1435 Z_ElementSpec_elementSetName)
1437 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1440 retrieve = lookup_retrieve(tinfo, esn);
1444 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1445 p->addinfo = odr_strdup(p->odr, esn);
1450 set_param_int(params, "id", p->localno, p->odr->mem);
1452 set_param_str(params, "filename", p->fname, p->odr->mem);
1453 if (p->staticrank >= 0)
1454 set_param_int(params, "rank", p->staticrank, p->odr->mem);
1457 set_param_str(params, "schema", esn, p->odr->mem);
1460 set_param_str(params, "schema", retrieve->name, p->odr->mem);
1461 else if (retrieve->identifier)
1462 set_param_str(params, "schema", retrieve->identifier, p->odr->mem);
1464 set_param_str(params, "schema", "", p->odr->mem);
1467 set_param_int(params, "score", p->score, p->odr->mem);
1468 set_param_int(params, "size", p->recordSize, p->odr->mem);
1470 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1473 XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
1476 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1480 /* retrieve conversion */
1481 perform_convert(tinfo, 0, p, retrieve->convert, params, &doc, &last_xsp);
1484 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1486 else if (!p->input_format
1487 || !oid_oidcmp(p->input_format, yaz_oid_recsyn_xml))
1493 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1495 xmlDocDumpMemory(doc, &buf_out, &len_out);
1497 p->output_format = yaz_oid_recsyn_xml;
1498 p->rec_len = len_out;
1499 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1500 memcpy(p->rec_buf, buf_out, p->rec_len);
1503 else if (!oid_oidcmp(p->output_format, yaz_oid_recsyn_sutrs))
1509 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1511 xmlDocDumpMemory(doc, &buf_out, &len_out);
1513 p->output_format = yaz_oid_recsyn_sutrs;
1514 p->rec_len = len_out;
1515 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1516 memcpy(p->rec_buf, buf_out, p->rec_len);
1522 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1528 static struct recType filter_type = {
1539 #ifdef IDZEBRA_STATIC_DOM
1552 * indent-tabs-mode: nil
1554 * vim: shiftwidth=4 tabstop=8 expandtab