1 /* $Id: mod_dom.c,v 1.26 2007-03-03 21:39:10 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/diagbib1.h>
29 #include <yaz/tpath.h>
30 #include <yaz/snprintf.h>
32 #include <libxml/xmlversion.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xmlIO.h>
36 #include <libxml/xmlreader.h>
37 #include <libxslt/transform.h>
38 #include <libxslt/xsltutils.h>
41 #include <libexslt/exslt.h>
44 #include <idzebra/util.h>
45 #include <idzebra/recctrl.h>
47 /* DOM filter style indexing */
48 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
49 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
51 /* DOM filter style indexing */
52 #define ZEBRA_PI_NAME "zebra-2.0"
53 static const char *zebra_pi_name = ZEBRA_PI_NAME;
58 const char *stylesheet;
59 xsltStylesheetPtr stylesheet_xsp;
60 struct convert_s *next;
63 struct filter_extract {
65 struct convert_s *convert;
69 struct convert_s *convert;
72 struct filter_retrieve {
74 const char *identifier;
75 struct convert_s *convert;
76 struct filter_retrieve *next;
79 #define DOM_INPUT_XMLREADER 1
80 #define DOM_INPUT_MARC 2
84 struct convert_s *convert;
88 const char *input_charset;
93 xmlTextReaderPtr reader;
97 struct filter_input *next;
103 const char *profile_path;
106 xmlDocPtr doc_config;
107 struct filter_extract *extract;
108 struct filter_retrieve *retrieve_list;
109 struct filter_input *input_list;
110 struct filter_store *store;
111 int record_info_invoked;
116 #define XML_STRCMP(a,b) strcmp((char*)a, b)
117 #define XML_STRLEN(a) strlen((char*)a)
120 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
122 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
123 const char *fmt, ...)
125 __attribute__ ((format (printf, 4, 5)))
129 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
130 const char *fmt, ...)
136 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
139 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
140 xmlGetLineNo(ptr), buf);
144 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
150 static void set_param_str(const char **params, const char *name,
151 const char *value, ODR odr)
153 char *quoted = odr_malloc(odr, 3 + strlen(value));
154 sprintf(quoted, "'%s'", value);
162 static void set_param_int(const char **params, const char *name,
165 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
168 sprintf(quoted, "'" ZINT_FORMAT "'", value);
174 static void *filter_init(Res res, RecType recType)
176 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
178 tinfo->full_name = 0;
179 tinfo->profile_path = 0;
180 tinfo->odr_record = odr_createmem(ODR_ENCODE);
181 tinfo->odr_config = odr_createmem(ODR_ENCODE);
183 tinfo->retrieve_list = 0;
184 tinfo->input_list = 0;
186 tinfo->doc_config = 0;
187 tinfo->record_info_invoked = 0;
196 static int attr_content(struct _xmlAttr *attr, const char *name,
197 const char **dst_content)
199 if (!XML_STRCMP(attr->name, name) && attr->children
200 && attr->children->type == XML_TEXT_NODE)
202 *dst_content = (const char *)(attr->children->content);
208 static void destroy_xsp(struct convert_s *c)
212 if (c->stylesheet_xsp)
213 xsltFreeStylesheet(c->stylesheet_xsp);
218 static void destroy_dom(struct filter_info *tinfo)
222 destroy_xsp(tinfo->extract->convert);
227 destroy_xsp(tinfo->store->convert);
230 if (tinfo->input_list)
232 struct filter_input *i_ptr;
233 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
237 case DOM_INPUT_XMLREADER:
238 if (i_ptr->u.xmlreader.reader)
239 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
242 yaz_iconv_close(i_ptr->u.marc.iconv);
243 yaz_marc_destroy(i_ptr->u.marc.handle);
246 destroy_xsp(i_ptr->convert);
248 tinfo->input_list = 0;
250 if (tinfo->retrieve_list)
252 struct filter_retrieve *r_ptr;
253 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
254 destroy_xsp(r_ptr->convert);
255 tinfo->retrieve_list = 0;
258 if (tinfo->doc_config)
260 xmlFreeDoc(tinfo->doc_config);
261 tinfo->doc_config = 0;
263 odr_reset(tinfo->odr_config);
266 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
267 struct convert_s **l)
270 FOR_EACH_ELEMENT(ptr) {
271 if (!XML_STRCMP(ptr->name, "xslt"))
273 struct _xmlAttr *attr;
275 = odr_malloc(tinfo->odr_config, sizeof(*p));
279 p->stylesheet_xsp = 0;
281 for (attr = ptr->properties; attr; attr = attr->next)
282 if (attr_content(attr, "stylesheet", &p->stylesheet))
286 dom_log(YLOG_WARN, tinfo, ptr,
287 "bad attribute @%s", attr->name);
291 char tmp_xslt_full_name[1024];
292 if (!yaz_filepath_resolve(p->stylesheet,
297 dom_log(YLOG_WARN, tinfo, 0,
298 "stylesheet %s not found in "
301 tinfo->profile_path);
306 = xsltParseStylesheetFile((const xmlChar*)
308 if (!p->stylesheet_xsp)
310 dom_log(YLOG_WARN, tinfo, 0,
311 "could not parse xslt stylesheet %s",
318 dom_log(YLOG_WARN, tinfo, ptr,
319 "missing attribute 'stylesheet' ");
327 dom_log(YLOG_WARN, tinfo, ptr,
328 "bad element '%s', expected <xslt>", ptr->name);
335 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
336 struct recExtractCtrl *extctr,
337 struct convert_s *convert,
340 xsltStylesheetPtr *last_xsp)
342 for (; convert; convert = convert->next)
344 xmlChar *buf_out = 0;
346 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
349 *last_xsp = convert->stylesheet_xsp;
354 /* now saving into buffer and re-reading into DOM to avoid annoing
355 XSLT problem with thrown-out indentation text nodes */
356 xsltSaveResultToString(&buf_out, &len_out, res_doc,
357 convert->stylesheet_xsp);
362 *doc = xmlParseMemory((const char *) buf_out, len_out);
364 /* writing debug info out */
365 if (extctr->flagShowRecords)
366 yaz_log(YLOG_LOG, "%s: XSLT %s\n %.*s",
367 tinfo->fname ? tinfo->fname : "(none)",
376 static struct filter_input *new_input(struct filter_info *tinfo, int type)
378 struct filter_input *p;
379 struct filter_input **np = &tinfo->input_list;
380 for (;*np; np = &(*np)->next)
382 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
391 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
392 const char *syntax, const char *name)
394 FOR_EACH_ELEMENT(ptr) {
395 if (!XML_STRCMP(ptr->name, "marc"))
397 yaz_iconv_t iconv = 0;
398 const char *input_charset = "marc-8";
399 struct _xmlAttr *attr;
401 for (attr = ptr->properties; attr; attr = attr->next)
403 if (attr_content(attr, "inputcharset", &input_charset))
407 dom_log(YLOG_WARN, tinfo, ptr,
408 "bad attribute @%s, expected @inputcharset",
412 iconv = yaz_iconv_open("utf-8", input_charset);
415 dom_log(YLOG_WARN, tinfo, ptr,
416 "unsupported @charset '%s'", input_charset);
421 struct filter_input *p
422 = new_input(tinfo, DOM_INPUT_MARC);
423 p->u.marc.handle = yaz_marc_create();
424 p->u.marc.iconv = iconv;
426 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
430 parse_convert(tinfo, ptr, &p->convert);
435 else if (!XML_STRCMP(ptr->name, "xmlreader"))
437 struct filter_input *p
438 = new_input(tinfo, DOM_INPUT_XMLREADER);
439 struct _xmlAttr *attr;
440 const char *level_str = 0;
442 p->u.xmlreader.split_level = 0;
443 p->u.xmlreader.reader = 0;
445 for (attr = ptr->properties; attr; attr = attr->next)
447 if (attr_content(attr, "level", &level_str))
451 dom_log(YLOG_WARN, tinfo, ptr,
452 "bad attribute @%s, expected @level",
457 p->u.xmlreader.split_level = atoi(level_str);
461 parse_convert(tinfo, ptr, &p->convert);
466 dom_log(YLOG_WARN, tinfo, ptr,
467 "bad element <%s>, expected <marc>|<xmlreader>",
475 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
477 char tmp_full_name[1024];
481 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
483 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
484 NULL, tmp_full_name))
485 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
487 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
489 yaz_log(YLOG_LOG, "%s dom filter: "
490 "loading config file %s", tinfo->fname, tinfo->full_name);
492 doc = xmlParseFile(tinfo->full_name);
495 yaz_log(YLOG_WARN, "%s: dom filter: "
496 "failed to parse config file %s",
497 tinfo->fname, tinfo->full_name);
500 /* save because we store ptrs to the content */
501 tinfo->doc_config = doc;
503 ptr = xmlDocGetRootElement(doc);
504 if (!ptr || ptr->type != XML_ELEMENT_NODE
505 || XML_STRCMP(ptr->name, "dom"))
507 dom_log(YLOG_WARN, tinfo, ptr,
508 "bad root element <%s>, expected root element <dom>",
514 FOR_EACH_ELEMENT(ptr) {
515 if (!XML_STRCMP(ptr->name, "extract"))
518 <extract name="index">
519 <xslt stylesheet="first.xsl"/>
520 <xslt stylesheet="second.xsl"/>
523 struct _xmlAttr *attr;
524 struct filter_extract *f =
525 odr_malloc(tinfo->odr_config, sizeof(*f));
530 for (attr = ptr->properties; attr; attr = attr->next)
532 if (attr_content(attr, "name", &f->name))
536 dom_log(YLOG_WARN, tinfo, ptr,
537 "bad attribute @%s, expected @name",
541 parse_convert(tinfo, ptr->children, &f->convert);
543 else if (!XML_STRCMP(ptr->name, "retrieve"))
547 <xslt stylesheet="some.xsl"/>
548 <xslt stylesheet="some.xsl"/>
551 struct _xmlAttr *attr;
552 struct filter_retrieve **fp = &tinfo->retrieve_list;
553 struct filter_retrieve *f =
554 odr_malloc(tinfo->odr_config, sizeof(*f));
565 for (attr = ptr->properties; attr; attr = attr->next)
567 if (attr_content(attr, "identifier",
570 else if (attr_content(attr, "name", &f->name))
574 dom_log(YLOG_WARN, tinfo, ptr,
575 "bad attribute @%s, expected @identifier|@name",
579 parse_convert(tinfo, ptr->children, &f->convert);
581 else if (!XML_STRCMP(ptr->name, "store"))
585 <xslt stylesheet="some.xsl"/>
586 <xslt stylesheet="some.xsl"/>
589 struct filter_store *f =
590 odr_malloc(tinfo->odr_config, sizeof(*f));
594 parse_convert(tinfo, ptr->children, &f->convert);
596 else if (!XML_STRCMP(ptr->name, "input"))
600 <xmlreader level="1"/>
602 <input syntax="usmarc">
603 <marc inputcharset="marc-8"/>
606 struct _xmlAttr *attr;
607 const char *syntax = 0;
608 const char *name = 0;
609 for (attr = ptr->properties; attr; attr = attr->next)
611 if (attr_content(attr, "syntax", &syntax))
613 else if (attr_content(attr, "name", &name))
617 dom_log(YLOG_WARN, tinfo, ptr,
618 "bad attribute @%s, expected @syntax|@name",
622 parse_input(tinfo, ptr->children, syntax, name);
626 dom_log(YLOG_WARN, tinfo, ptr,
628 "expected <extract>|<input>|<retrieve>|<store>",
636 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
639 struct filter_retrieve *f = tinfo->retrieve_list;
641 /* return first schema if no est is provided */
644 for (; f; f = f->next)
646 /* find requested schema */
649 if (f->identifier && !strcmp(f->identifier, est))
651 if (f->name && !strcmp(f->name, est))
658 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
660 struct filter_info *tinfo = clientData;
663 yaz_log(YLOG_WARN, "dom filter: need config file");
667 if (tinfo->fname && !strcmp(args, tinfo->fname))
670 tinfo->profile_path = res_get(res, "profilePath");
673 return parse_dom(tinfo, args);
676 static void filter_destroy(void *clientData)
678 struct filter_info *tinfo = clientData;
680 odr_destroy(tinfo->odr_config);
681 odr_destroy(tinfo->odr_record);
685 static int ioread_ex(void *context, char *buffer, int len)
687 struct recExtractCtrl *p = context;
688 return p->stream->readf(p->stream, buffer, len);
691 static int ioclose_ex(void *context)
697 /* DOM filter style indexing */
698 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
699 xmlChar **dst_content)
701 if (0 == XML_STRCMP(attr->name, name) && attr->children
702 && attr->children->type == XML_TEXT_NODE)
704 *dst_content = (attr->children->content);
711 /* DOM filter style indexing */
712 static void index_value_of(struct filter_info *tinfo,
713 struct recExtractCtrl *extctr,
718 if (tinfo->record_info_invoked == 1)
720 xmlChar *text = xmlNodeGetContent(node);
721 size_t text_len = strlen((const char *)text);
723 /* if there is no text, we do not need to proceed */
726 xmlChar *look = index_p;
733 /* assingning text to be indexed */
734 recword->term_buf = (const char *)text;
735 recword->term_len = text_len;
737 /* parsing all index name/type pairs */
738 /* may not start with ' ' or ':' */
739 while (*look && ' ' != *look && ':' != *look)
741 /* setting name and type to zero */
745 /* parsing one index name */
747 while (*look && ':' != *look && ' ' != *look)
752 strncpy((char *)index, (const char *)bval, eval - bval);
753 index[eval - bval] = '\0';
756 /* parsing one index type, if existing */
762 while (*look && ' ' != *look)
767 strncpy((char *)type, (const char *)bval, eval - bval);
768 type[eval - bval] = '\0';
771 /* actually indexing the text given */
772 dom_log(YLOG_DEBUG, tinfo, 0,
773 "INDEX '%s:%s' '%s'",
774 index ? (const char *) index : "null",
775 type ? (const char *) type : "null",
776 text ? (const char *) text : "null");
778 recword->index_name = (const char *)index;
780 recword->index_type = *type;
782 /* writing debug out */
783 if (extctr->flagShowRecords)
784 dom_log(YLOG_LOG, tinfo, 0,
785 "INDEX '%s:%s' '%s'",
786 index ? (const char *) index : "null",
787 type ? (const char *) type : "null",
788 text ? (const char *) text : "null");
790 /* actually indexing the text given */
791 recword->index_name = (const char *)index;
793 recword->index_type = *type;
794 (extctr->tokenAdd)(recword);
796 /* eat whitespaces */
797 if (*look && ' ' == *look && *(look+1))
808 /* DOM filter style indexing */
809 static void set_record_info(struct filter_info *tinfo,
810 struct recExtractCtrl *extctr,
817 /* writing debug info out */
818 if (extctr->flagShowRecords)
819 dom_log(YLOG_LOG, tinfo, 0,
820 "RECORD id=%s rank=%s type=%s",
821 id_p ? (const char *) id_p : "(null)",
822 rank_p ? (const char *) rank_p : "(null)",
823 type_p ? (const char *) type_p : "(null)");
827 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
830 extctr->staticrank = atozint((const char *)rank_p);
832 /* if (!strcmp("update", type_str)) */
833 /* index_node(tinfo, ctrl, ptr, recword); */
834 /* else if (!strcmp("delete", type_str)) */
835 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
837 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
839 if (tinfo->record_info_invoked == 1)
841 /* warn about multiple only once */
842 dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
844 tinfo->record_info_invoked++;
849 /* DOM filter style indexing */
850 static void process_xml_element_zebra_node(struct filter_info *tinfo,
851 struct recExtractCtrl *extctr,
855 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
856 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
858 if (0 == XML_STRCMP(node->name, "index"))
860 xmlChar *index_p = 0;
862 struct _xmlAttr *attr;
863 for (attr = node->properties; attr; attr = attr->next)
865 if (attr_content_xml(attr, "name", &index_p))
867 index_value_of(tinfo, extctr, recword,node, index_p);
871 dom_log(YLOG_WARN, tinfo, node,
872 "bad attribute @%s, expected @name",
877 else if (0 == XML_STRCMP(node->name, "record"))
883 struct _xmlAttr *attr;
884 for (attr = node->properties; attr; attr = attr->next)
886 if (attr_content_xml(attr, "id", &id_p))
888 else if (attr_content_xml(attr, "rank", &rank_p))
890 else if (attr_content_xml(attr, "type", &type_p))
894 dom_log(YLOG_WARN, tinfo, node,
895 "bad attribute @%s, expected @id|@rank|@type",
899 if (type_p && 0 != strcmp("update", (const char *)type_p))
901 dom_log(YLOG_WARN, tinfo, node,
902 "attribute @%s, only implemented '@type='update'",
906 set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
910 dom_log(YLOG_WARN, tinfo, node,
912 " expected <record>|<index> in namespace '%s'",
913 node->name, zebra_dom_ns);
919 /* DOM filter style indexing */
920 static void process_xml_pi_node(struct filter_info *tinfo,
921 struct recExtractCtrl *extctr,
925 /* if right PI name, continue parsing PI */
926 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
928 xmlChar *pi_p = node->content;
929 xmlChar *look = pi_p;
934 /* parsing PI record instructions */
935 if (0 == strncmp((const char *)look, "record", 6))
948 while (*look && ' ' == *look && *(look+1))
951 /* parse possible id */
952 if (*look && 0 == strncmp((const char *)look, "id=", 3))
956 while (*look && ' ' != *look)
959 strncpy((char *)id, (const char *)bval, eval - bval);
960 id[eval - bval] = '\0';
964 while (*look && ' ' == *look && *(look+1))
967 /* parse possible rank */
968 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
972 while (*look && ' ' != *look)
975 strncpy((char *)rank, (const char *)bval, eval - bval);
976 rank[eval - bval] = '\0';
980 while (*look && ' ' == *look && *(look+1))
983 if (look && '\0' != *look)
985 dom_log(YLOG_WARN, tinfo, node,
986 "content '%s', can not parse '%s'",
990 set_record_info(tinfo, extctr, node, id, rank, 0);
993 /* parsing index instruction */
994 else if (0 == strncmp((const char *)look, "index", 5))
999 while (*look && ' ' == *look && *(look+1))
1002 /* export index instructions to outside */
1007 dom_log(YLOG_WARN, tinfo, node,
1008 "content '%s', can not parse '%s'",
1014 /* DOM filter style indexing */
1015 static void process_xml_element_node(struct filter_info *tinfo,
1016 struct recExtractCtrl *extctr,
1020 /* remember indexing instruction from PI to next element node */
1021 xmlChar *index_p = 0;
1023 /* check if we are an element node in the special zebra namespace
1024 and either set record data or index value-of node content*/
1025 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1027 /* loop through kid nodes */
1028 for (node = node->children; node; node = node->next)
1030 /* check and set PI record and index index instructions */
1031 if (node->type == XML_PI_NODE)
1033 process_xml_pi_node(tinfo, extctr, node, &index_p);
1035 else if (node->type == XML_ELEMENT_NODE)
1037 /* if there was a PI index instruction before this element */
1040 index_value_of(tinfo, extctr, recword, node, index_p);
1043 process_xml_element_node(tinfo, extctr, recword,node);
1051 /* DOM filter style indexing */
1052 static void extract_dom_doc_node(struct filter_info *tinfo,
1053 struct recExtractCtrl *extctr,
1056 /* only need to do the initialization once, reuse recword for all terms */
1058 (*extctr->init)(extctr, &recword);
1060 tinfo->record_info_invoked = 0;
1061 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1067 static int convert_extract_doc(struct filter_info *tinfo,
1068 struct filter_input *input,
1069 struct recExtractCtrl *p,
1075 const char *params[10];
1076 xsltStylesheetPtr last_xsp = 0;
1077 xmlDocPtr store_doc = 0;
1080 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1082 /* input conversion */
1083 perform_convert(tinfo, p, input->convert, params, &doc, 0);
1087 /* store conversion */
1088 store_doc = xmlCopyDoc(doc, 1);
1089 perform_convert(tinfo, p, tinfo->store->convert,
1090 params, &store_doc, &last_xsp);
1094 xsltSaveResultToString(&buf_out, &len_out,
1095 store_doc ? store_doc : doc, last_xsp);
1097 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1099 /* if (p->flagShowRecords)
1100 fwrite(buf_out, len_out, 1, stdout); */
1102 (*p->setStoreData)(p, buf_out, len_out);
1106 xmlFreeDoc(store_doc);
1108 /* extract conversion */
1109 perform_convert(tinfo, p, tinfo->extract->convert, params, &doc, 0);
1111 /* finally, do the indexing */
1113 extract_dom_doc_node(tinfo, p, doc);
1118 if (tinfo->record_info_invoked == 0)
1119 return RECCTRL_EXTRACT_SKIP;
1120 return RECCTRL_EXTRACT_OK;
1123 static int extract_xml_split(struct filter_info *tinfo,
1124 struct filter_input *input,
1125 struct recExtractCtrl *p)
1129 if (p->first_record)
1131 if (input->u.xmlreader.reader)
1132 xmlFreeTextReader(input->u.xmlreader.reader);
1133 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1134 p /* I/O handler */,
1140 if (!input->u.xmlreader.reader)
1141 return RECCTRL_EXTRACT_ERROR_GENERIC;
1143 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1146 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1147 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1148 if (type == XML_READER_TYPE_ELEMENT &&
1149 input->u.xmlreader.split_level == depth)
1152 = xmlTextReaderExpand(input->u.xmlreader.reader);
1155 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1156 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1158 xmlDocSetRootElement(doc, ptr2);
1160 /* writing debug info out */
1161 if (p->flagShowRecords)
1163 xmlChar *buf_out = 0;
1165 xmlDocDumpMemory(doc, &buf_out, &len_out);
1166 yaz_log(YLOG_LOG, "%s: XMLREADER depth: %i\n%.*s",
1167 tinfo->fname ? tinfo->fname : "(none)",
1168 depth, len_out, buf_out);
1172 return convert_extract_doc(tinfo, input, p, doc);
1176 xmlFreeTextReader(input->u.xmlreader.reader);
1177 input->u.xmlreader.reader = 0;
1178 return RECCTRL_EXTRACT_ERROR_GENERIC;
1181 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1183 xmlFreeTextReader(input->u.xmlreader.reader);
1184 input->u.xmlreader.reader = 0;
1185 return RECCTRL_EXTRACT_EOF;
1188 static int extract_xml_full(struct filter_info *tinfo,
1189 struct filter_input *input,
1190 struct recExtractCtrl *p)
1192 if (p->first_record) /* only one record per stream */
1194 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1195 p /* I/O handler */,
1198 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1201 return RECCTRL_EXTRACT_ERROR_GENERIC;
1203 return convert_extract_doc(tinfo, input, p, doc);
1206 return RECCTRL_EXTRACT_EOF;
1209 static int extract_iso2709(struct filter_info *tinfo,
1210 struct filter_input *input,
1211 struct recExtractCtrl *p)
1217 if (p->stream->readf(p->stream, buf, 5) != 5)
1218 return RECCTRL_EXTRACT_EOF;
1219 while (*buf < '0' || *buf > '9')
1223 dom_log(YLOG_WARN, tinfo, 0,
1224 "MARC: Skipping bad byte %d (0x%02X)",
1225 *buf & 0xff, *buf & 0xff);
1226 for (i = 0; i<4; i++)
1229 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1230 return RECCTRL_EXTRACT_EOF;
1232 record_length = atoi_n (buf, 5);
1233 if (record_length < 25)
1235 dom_log(YLOG_WARN, tinfo, 0,
1236 "MARC record length < 25, is %d", record_length);
1237 return RECCTRL_EXTRACT_ERROR_GENERIC;
1239 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1240 if (read_bytes < record_length-5)
1242 dom_log(YLOG_WARN, tinfo, 0,
1243 "couldn't read whole MARC record");
1244 return RECCTRL_EXTRACT_ERROR_GENERIC;
1246 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1247 if (r < record_length)
1249 dom_log (YLOG_WARN, tinfo, 0,
1250 "parsing of MARC record failed r=%d length=%d",
1252 return RECCTRL_EXTRACT_ERROR_GENERIC;
1258 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1259 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1260 xmlDocSetRootElement(rdoc, root_ptr);
1261 return convert_extract_doc(tinfo, input, p, rdoc);
1263 return RECCTRL_EXTRACT_OK;
1266 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1268 struct filter_info *tinfo = clientData;
1269 struct filter_input *input = tinfo->input_list;
1272 return RECCTRL_EXTRACT_ERROR_GENERIC;
1274 odr_reset(tinfo->odr_record);
1277 case DOM_INPUT_XMLREADER:
1278 if (input->u.xmlreader.split_level == 0)
1279 return extract_xml_full(tinfo, input, p);
1281 return extract_xml_split(tinfo, input, p);
1283 case DOM_INPUT_MARC:
1284 return extract_iso2709(tinfo, input, p);
1286 return RECCTRL_EXTRACT_ERROR_GENERIC;
1289 static int ioread_ret(void *context, char *buffer, int len)
1291 struct recRetrieveCtrl *p = context;
1292 return p->stream->readf(p->stream, buffer, len);
1295 static int ioclose_ret(void *context)
1300 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1302 /* const char *esn = zebra_dom_ns; */
1303 const char *esn = 0;
1304 const char *params[32];
1305 struct filter_info *tinfo = clientData;
1307 struct filter_retrieve *retrieve;
1308 xsltStylesheetPtr last_xsp = 0;
1312 if (p->comp->which == Z_RecordComp_simple
1313 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1315 esn = p->comp->u.simple->u.generic;
1317 else if (p->comp->which == Z_RecordComp_complex
1318 && p->comp->u.complex->generic->elementSpec
1319 && p->comp->u.complex->generic->elementSpec->which ==
1320 Z_ElementSpec_elementSetName)
1322 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1325 retrieve = lookup_retrieve(tinfo, esn);
1329 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1334 set_param_int(params, "id", p->localno, p->odr);
1336 set_param_str(params, "filename", p->fname, p->odr);
1337 if (p->staticrank >= 0)
1338 set_param_int(params, "rank", p->staticrank, p->odr);
1341 set_param_str(params, "schema", esn, p->odr);
1344 set_param_str(params, "schema", retrieve->name, p->odr);
1345 else if (retrieve->identifier)
1346 set_param_str(params, "schema", retrieve->identifier, p->odr);
1348 set_param_str(params, "schema", "", p->odr);
1351 set_param_int(params, "score", p->score, p->odr);
1352 set_param_int(params, "size", p->recordSize, p->odr);
1354 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1357 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1360 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1364 /* retrieve conversion */
1365 perform_convert(tinfo, 0, retrieve->convert, params, &doc, &last_xsp);
1368 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1370 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1376 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1378 xmlDocDumpMemory(doc, &buf_out, &len_out);
1380 p->output_format = VAL_TEXT_XML;
1381 p->rec_len = len_out;
1382 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1383 memcpy(p->rec_buf, buf_out, p->rec_len);
1386 else if (p->output_format == VAL_SUTRS)
1392 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1394 xmlDocDumpMemory(doc, &buf_out, &len_out);
1396 p->output_format = VAL_SUTRS;
1397 p->rec_len = len_out;
1398 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1399 memcpy(p->rec_buf, buf_out, p->rec_len);
1405 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1411 static struct recType filter_type = {
1422 #ifdef IDZEBRA_STATIC_DOM
1435 * indent-tabs-mode: nil
1437 * vim: shiftwidth=4 tabstop=8 expandtab