1 /* $Id: mod_dom.c,v 1.23 2007-02-28 14:46:41 marc 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;
115 #define XML_STRCMP(a,b) strcmp((char*)a, b)
116 #define XML_STRLEN(a) strlen((char*)a)
119 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
121 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
122 const char *fmt, ...)
124 __attribute__ ((format (printf, 4, 5)))
128 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
129 const char *fmt, ...)
135 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
138 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
139 xmlGetLineNo(ptr), buf);
143 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
149 static void set_param_str(const char **params, const char *name,
150 const char *value, ODR odr)
152 char *quoted = odr_malloc(odr, 3 + strlen(value));
153 sprintf(quoted, "'%s'", value);
161 static void set_param_int(const char **params, const char *name,
164 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
167 sprintf(quoted, "'" ZINT_FORMAT "'", value);
173 static void *filter_init(Res res, RecType recType)
175 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
177 tinfo->full_name = 0;
178 tinfo->profile_path = 0;
179 tinfo->odr_record = odr_createmem(ODR_ENCODE);
180 tinfo->odr_config = odr_createmem(ODR_ENCODE);
182 tinfo->retrieve_list = 0;
183 tinfo->input_list = 0;
185 tinfo->doc_config = 0;
194 static int attr_content(struct _xmlAttr *attr, const char *name,
195 const char **dst_content)
197 if (!XML_STRCMP(attr->name, name) && attr->children
198 && attr->children->type == XML_TEXT_NODE)
200 *dst_content = (const char *)(attr->children->content);
206 static void destroy_xsp(struct convert_s *c)
210 if (c->stylesheet_xsp)
211 xsltFreeStylesheet(c->stylesheet_xsp);
216 static void destroy_dom(struct filter_info *tinfo)
220 destroy_xsp(tinfo->extract->convert);
225 destroy_xsp(tinfo->store->convert);
228 if (tinfo->input_list)
230 struct filter_input *i_ptr;
231 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
235 case DOM_INPUT_XMLREADER:
236 if (i_ptr->u.xmlreader.reader)
237 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
240 yaz_iconv_close(i_ptr->u.marc.iconv);
241 yaz_marc_destroy(i_ptr->u.marc.handle);
244 destroy_xsp(i_ptr->convert);
246 tinfo->input_list = 0;
248 if (tinfo->retrieve_list)
250 struct filter_retrieve *r_ptr;
251 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
252 destroy_xsp(r_ptr->convert);
253 tinfo->retrieve_list = 0;
256 if (tinfo->doc_config)
258 xmlFreeDoc(tinfo->doc_config);
259 tinfo->doc_config = 0;
261 odr_reset(tinfo->odr_config);
264 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
265 struct convert_s **l)
268 FOR_EACH_ELEMENT(ptr) {
269 if (!XML_STRCMP(ptr->name, "xslt"))
271 struct _xmlAttr *attr;
273 = odr_malloc(tinfo->odr_config, sizeof(*p));
277 p->stylesheet_xsp = 0;
279 for (attr = ptr->properties; attr; attr = attr->next)
280 if (attr_content(attr, "stylesheet", &p->stylesheet))
284 dom_log(YLOG_WARN, tinfo, ptr,
285 "bad attribute @%s", attr->name);
289 char tmp_xslt_full_name[1024];
290 if (!yaz_filepath_resolve(p->stylesheet,
295 dom_log(YLOG_WARN, tinfo, 0,
296 "stylesheet %s not found in "
299 tinfo->profile_path);
304 = xsltParseStylesheetFile((const xmlChar*)
306 if (!p->stylesheet_xsp)
308 dom_log(YLOG_WARN, tinfo, 0,
309 "could not parse xslt stylesheet %s",
316 dom_log(YLOG_WARN, tinfo, ptr,
317 "missing attribute 'stylesheet' ");
325 dom_log(YLOG_WARN, tinfo, ptr,
326 "bad element '%s', expected <xslt>", ptr->name);
333 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
334 struct convert_s *convert,
337 xsltStylesheetPtr *last_xsp)
339 for (; convert; convert = convert->next)
341 xmlChar *buf_out = 0;
343 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
346 *last_xsp = convert->stylesheet_xsp;
349 /* *doc = res_doc; */
351 /* now saving into buffer and re-reading into DOM to avoid annoing
352 XSLT problem with thrown-out indentation text nodes */
354 xsltSaveResultToString(&buf_out, &len_out, res_doc,
355 convert->stylesheet_xsp);
360 *doc = xmlParseDoc(buf_out);
362 yaz_log(YLOG_DEBUG, "%s: %s \n %s",
363 tinfo->fname ? tinfo->fname : "none ",
372 static struct filter_input *new_input(struct filter_info *tinfo, int type)
374 struct filter_input *p;
375 struct filter_input **np = &tinfo->input_list;
376 for (;*np; np = &(*np)->next)
378 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
387 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
388 const char *syntax, const char *name)
390 FOR_EACH_ELEMENT(ptr) {
391 if (!XML_STRCMP(ptr->name, "marc"))
393 yaz_iconv_t iconv = 0;
394 const char *input_charset = "marc-8";
395 struct _xmlAttr *attr;
397 for (attr = ptr->properties; attr; attr = attr->next)
399 if (attr_content(attr, "inputcharset", &input_charset))
403 dom_log(YLOG_WARN, tinfo, ptr,
404 "bad attribute @%s, expected @inputcharset",
408 iconv = yaz_iconv_open("utf-8", input_charset);
411 dom_log(YLOG_WARN, tinfo, ptr,
412 "unsupported @charset '%s'", input_charset);
417 struct filter_input *p
418 = new_input(tinfo, DOM_INPUT_MARC);
419 p->u.marc.handle = yaz_marc_create();
420 p->u.marc.iconv = iconv;
422 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
426 parse_convert(tinfo, ptr, &p->convert);
431 else if (!XML_STRCMP(ptr->name, "xmlreader"))
433 struct filter_input *p
434 = new_input(tinfo, DOM_INPUT_XMLREADER);
435 struct _xmlAttr *attr;
436 const char *level_str = 0;
438 p->u.xmlreader.split_level = 0;
439 p->u.xmlreader.reader = 0;
441 for (attr = ptr->properties; attr; attr = attr->next)
443 if (attr_content(attr, "level", &level_str))
447 dom_log(YLOG_WARN, tinfo, ptr,
448 "bad attribute @%s, expected @level",
453 p->u.xmlreader.split_level = atoi(level_str);
457 parse_convert(tinfo, ptr, &p->convert);
462 dom_log(YLOG_WARN, tinfo, ptr,
463 "bad element <%s>, expected <marc>|<xmlreader>",
471 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
473 char tmp_full_name[1024];
477 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
479 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
480 NULL, tmp_full_name))
481 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
483 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
485 yaz_log(YLOG_LOG, "%s dom filter: "
486 "loading config file %s", tinfo->fname, tinfo->full_name);
488 doc = xmlParseFile(tinfo->full_name);
491 yaz_log(YLOG_WARN, "%s: dom filter: "
492 "failed to parse config file %s",
493 tinfo->fname, tinfo->full_name);
496 /* save because we store ptrs to the content */
497 tinfo->doc_config = doc;
499 ptr = xmlDocGetRootElement(doc);
500 if (!ptr || ptr->type != XML_ELEMENT_NODE
501 || XML_STRCMP(ptr->name, "dom"))
503 dom_log(YLOG_WARN, tinfo, ptr,
504 "bad root element <%s>, expected root element <dom>",
510 FOR_EACH_ELEMENT(ptr) {
511 if (!XML_STRCMP(ptr->name, "extract"))
514 <extract name="index">
515 <xslt stylesheet="first.xsl"/>
516 <xslt stylesheet="second.xsl"/>
519 struct _xmlAttr *attr;
520 struct filter_extract *f =
521 odr_malloc(tinfo->odr_config, sizeof(*f));
526 for (attr = ptr->properties; attr; attr = attr->next)
528 if (attr_content(attr, "name", &f->name))
532 dom_log(YLOG_WARN, tinfo, ptr,
533 "bad attribute @%s, expected @name",
537 parse_convert(tinfo, ptr->children, &f->convert);
539 else if (!XML_STRCMP(ptr->name, "retrieve"))
543 <xslt stylesheet="some.xsl"/>
544 <xslt stylesheet="some.xsl"/>
547 struct _xmlAttr *attr;
548 struct filter_retrieve **fp = &tinfo->retrieve_list;
549 struct filter_retrieve *f =
550 odr_malloc(tinfo->odr_config, sizeof(*f));
561 for (attr = ptr->properties; attr; attr = attr->next)
563 if (attr_content(attr, "identifier",
566 else if (attr_content(attr, "name", &f->name))
570 dom_log(YLOG_WARN, tinfo, ptr,
571 "bad attribute @%s, expected @identifier|@name",
575 parse_convert(tinfo, ptr->children, &f->convert);
577 else if (!XML_STRCMP(ptr->name, "store"))
581 <xslt stylesheet="some.xsl"/>
582 <xslt stylesheet="some.xsl"/>
585 struct filter_store *f =
586 odr_malloc(tinfo->odr_config, sizeof(*f));
590 parse_convert(tinfo, ptr->children, &f->convert);
592 else if (!XML_STRCMP(ptr->name, "input"))
596 <xmlreader level="1"/>
598 <input syntax="usmarc">
599 <marc inputcharset="marc-8"/>
602 struct _xmlAttr *attr;
603 const char *syntax = 0;
604 const char *name = 0;
605 for (attr = ptr->properties; attr; attr = attr->next)
607 if (attr_content(attr, "syntax", &syntax))
609 else if (attr_content(attr, "name", &name))
613 dom_log(YLOG_WARN, tinfo, ptr,
614 "bad attribute @%s, expected @syntax|@name",
618 parse_input(tinfo, ptr->children, syntax, name);
622 dom_log(YLOG_WARN, tinfo, ptr,
624 "expected <extract>|<input>|<retrieve>|<store>",
632 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
635 struct filter_retrieve *f = tinfo->retrieve_list;
637 /* return first schema if no est is provided */
640 for (; f; f = f->next)
642 /* find requested schema */
645 if (f->identifier && !strcmp(f->identifier, est))
647 if (f->name && !strcmp(f->name, est))
654 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
656 struct filter_info *tinfo = clientData;
659 yaz_log(YLOG_WARN, "dom filter: need config file");
663 if (tinfo->fname && !strcmp(args, tinfo->fname))
666 tinfo->profile_path = res_get(res, "profilePath");
669 return parse_dom(tinfo, args);
672 static void filter_destroy(void *clientData)
674 struct filter_info *tinfo = clientData;
676 odr_destroy(tinfo->odr_config);
677 odr_destroy(tinfo->odr_record);
681 static int ioread_ex(void *context, char *buffer, int len)
683 struct recExtractCtrl *p = context;
684 return p->stream->readf(p->stream, buffer, len);
687 static int ioclose_ex(void *context)
693 /* DOM filter style indexing */
694 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
695 xmlChar **dst_content)
697 if (0 == XML_STRCMP(attr->name, name) && attr->children
698 && attr->children->type == XML_TEXT_NODE)
700 *dst_content = (attr->children->content);
707 /* DOM filter style indexing */
708 static void index_value_of(struct filter_info *tinfo,
709 struct recExtractCtrl *extctr,
714 xmlChar *text = xmlNodeGetContent(node);
715 size_t text_len = strlen((const char *)text);
717 /*dom_log(YLOG_DEBUG, tinfo, node, "Indexing: '%s' '%s'", index_p, text);*/
719 /* if there is no text, we do not need to proceed */
722 xmlChar *look = index_p;
729 /* assingning text to be indexed */
730 recword->term_buf = (const char *)text;
731 recword->term_len = text_len;
733 /* parsing all index name/type pairs */
734 /* may not start with ' ' or ':' */
735 while (*look && ' ' != *look && ':' != *look)
737 /* setting name and type to zero */
741 /* parsing one index name */
743 while (*look && ':' != *look && ' ' != *look)
748 strncpy((char *)index, (const char *)bval, eval - bval);
749 index[eval - bval] = '\0';
752 /* parsing one index type, if existing */
758 while (*look && ' ' != *look)
763 strncpy((char *)type, (const char *)bval, eval - bval);
764 type[eval - bval] = '\0';
767 /* actually indexing the text given */
768 dom_log(YLOG_DEBUG, tinfo, 0,
769 "INDEX '%s:%s' '%s'",
770 index ? (const char *) index : "null",
771 type ? (const char *) type : "null",
772 text ? (const char *) text : "null");
774 recword->index_name = (const char *)index;
776 recword->index_type = *type;
777 (extctr->tokenAdd)(recword);
779 /* eat whitespaces */
780 if (*look && ' ' == *look && *(look+1))
791 /* DOM filter style indexing */
792 static void set_record_info(struct filter_info *tinfo,
793 struct recExtractCtrl *extctr,
798 dom_log(YLOG_DEBUG, tinfo, 0,
799 "RECORD id=%s rank=%s type=%s",
800 id_p ? (const char *) id_p : "null",
801 rank_p ? (const char *) rank_p : "null",
802 type_p ? (const char *) type_p : "null");
805 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
808 extctr->staticrank = atozint((const char *)rank_p);
810 /* if (!strcmp("update", type_str)) */
811 /* index_node(tinfo, ctrl, ptr, recword); */
812 /* else if (!strcmp("delete", type_str)) */
813 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
815 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
821 /* DOM filter style indexing */
822 static void process_xml_element_zebra_node(struct filter_info *tinfo,
823 struct recExtractCtrl *extctr,
827 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
828 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
830 if (0 == XML_STRCMP(node->name, "index"))
832 xmlChar *index_p = 0;
834 struct _xmlAttr *attr;
835 for (attr = node->properties; attr; attr = attr->next)
837 if (attr_content_xml(attr, "name", &index_p))
839 index_value_of(tinfo, extctr, recword,node, index_p);
843 dom_log(YLOG_WARN, tinfo, node,
844 "bad attribute @%s, expected @name",
849 else if (0 == XML_STRCMP(node->name, "record"))
855 struct _xmlAttr *attr;
856 for (attr = node->properties; attr; attr = attr->next)
858 if (attr_content_xml(attr, "id", &id_p))
860 else if (attr_content_xml(attr, "rank", &rank_p))
862 else if (attr_content_xml(attr, "type", &type_p))
866 dom_log(YLOG_WARN, tinfo, node,
867 "bad attribute @%s, expected @id|@rank|@type",
871 if (type_p && 0 != strcmp("update", (const char *)type_p))
873 dom_log(YLOG_WARN, tinfo, node,
874 "attribute @%s, only implemented '@type='update'",
878 set_record_info(tinfo, extctr, id_p, rank_p, type_p);
882 dom_log(YLOG_WARN, tinfo, node,
884 " expected <record>|<index> in namespace '%s'",
885 node->name, zebra_dom_ns);
891 /* DOM filter style indexing */
892 static void process_xml_pi_node(struct filter_info *tinfo,
893 struct recExtractCtrl *extctr,
897 /* if right PI name, continue parsing PI */
898 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
900 xmlChar *pi_p = node->content;
901 xmlChar *look = pi_p;
906 /* parsing PI record instructions */
907 if (0 == strncmp((const char *)look, "record", 6))
920 while (*look && ' ' == *look && *(look+1))
923 /* parse possible id */
924 if (*look && 0 == strncmp((const char *)look, "id=", 3))
928 while (*look && ' ' != *look)
931 strncpy((char *)id, (const char *)bval, eval - bval);
932 id[eval - bval] = '\0';
936 while (*look && ' ' == *look && *(look+1))
939 /* parse possible rank */
940 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
944 while (*look && ' ' != *look)
947 strncpy((char *)rank, (const char *)bval, eval - bval);
948 rank[eval - bval] = '\0';
952 while (*look && ' ' == *look && *(look+1))
955 if (look && '\0' != *look)
957 dom_log(YLOG_WARN, tinfo, node,
958 "content '%s', can not parse '%s'",
962 set_record_info(tinfo, extctr, id, rank, 0);
965 /* parsing index instruction */
966 else if (0 == strncmp((const char *)look, "index", 5))
971 while (*look && ' ' == *look && *(look+1))
974 /* export index instructions to outside */
979 dom_log(YLOG_WARN, tinfo, node,
980 "content '%s', can not parse '%s'",
986 /* DOM filter style indexing */
987 static void process_xml_element_node(struct filter_info *tinfo,
988 struct recExtractCtrl *extctr,
992 /* remember indexing instruction from PI to next element node */
993 xmlChar *index_p = 0;
995 /* check if we are an element node in the special zebra namespace
996 and either set record data or index value-of node content*/
997 process_xml_element_zebra_node(tinfo, extctr, recword, node);
999 /* loop through kid nodes */
1000 for (node = node->children; node; node = node->next)
1002 /* check and set PI record and index index instructions */
1003 if (node->type == XML_PI_NODE)
1005 process_xml_pi_node(tinfo, extctr, node, &index_p);
1007 else if (node->type == XML_ELEMENT_NODE)
1009 /* if there was a PI index instruction before this element */
1012 index_value_of(tinfo, extctr, recword, node, index_p);
1015 process_xml_element_node(tinfo, extctr, recword,node);
1023 /* DOM filter style indexing */
1024 static void extract_dom_doc_node(struct filter_info *tinfo,
1025 struct recExtractCtrl *extctr,
1031 /* only need to do the initialization once, reuse recword for all terms */
1033 (*extctr->init)(extctr, &recword);
1035 if (extctr->flagShowRecords)
1037 xmlDocDumpMemory(doc, &buf_out, &len_out);
1038 fwrite(buf_out, len_out, 1, stdout);
1042 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1048 static int convert_extract_doc(struct filter_info *tinfo,
1049 struct filter_input *input,
1050 struct recExtractCtrl *p,
1056 const char *params[10];
1057 xsltStylesheetPtr last_xsp = 0;
1058 xmlDocPtr store_doc = 0;
1061 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1063 /* input conversion */
1064 perform_convert(tinfo, input->convert, params, &doc, 0);
1068 /* store conversion */
1069 store_doc = xmlCopyDoc(doc, 1);
1070 perform_convert(tinfo, tinfo->store->convert,
1071 params, &store_doc, &last_xsp);
1075 xsltSaveResultToString(&buf_out, &len_out,
1076 store_doc ? store_doc : doc, last_xsp);
1078 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1079 if (p->flagShowRecords)
1080 fwrite(buf_out, len_out, 1, stdout);
1081 (*p->setStoreData)(p, buf_out, len_out);
1085 xmlFreeDoc(store_doc);
1087 /* extract conversion */
1088 perform_convert(tinfo, tinfo->extract->convert, params, &doc, 0);
1090 /* finally, do the indexing */
1092 extract_dom_doc_node(tinfo, p, doc);
1097 return RECCTRL_EXTRACT_OK;
1100 static int extract_xml_split(struct filter_info *tinfo,
1101 struct filter_input *input,
1102 struct recExtractCtrl *p)
1106 if (p->first_record)
1108 if (input->u.xmlreader.reader)
1109 xmlFreeTextReader(input->u.xmlreader.reader);
1110 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1111 p /* I/O handler */,
1117 if (!input->u.xmlreader.reader)
1118 return RECCTRL_EXTRACT_ERROR_GENERIC;
1120 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1123 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1124 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1125 if (type == XML_READER_TYPE_ELEMENT &&
1126 input->u.xmlreader.split_level == depth)
1129 = xmlTextReaderExpand(input->u.xmlreader.reader);
1132 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1133 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1135 xmlDocSetRootElement(doc, ptr2);
1137 return convert_extract_doc(tinfo, input, p, doc);
1141 xmlFreeTextReader(input->u.xmlreader.reader);
1142 input->u.xmlreader.reader = 0;
1143 return RECCTRL_EXTRACT_ERROR_GENERIC;
1146 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1148 xmlFreeTextReader(input->u.xmlreader.reader);
1149 input->u.xmlreader.reader = 0;
1150 return RECCTRL_EXTRACT_EOF;
1153 static int extract_xml_full(struct filter_info *tinfo,
1154 struct filter_input *input,
1155 struct recExtractCtrl *p)
1157 if (p->first_record) /* only one record per stream */
1159 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1160 p /* I/O handler */,
1163 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1166 return RECCTRL_EXTRACT_ERROR_GENERIC;
1168 return convert_extract_doc(tinfo, input, p, doc);
1171 return RECCTRL_EXTRACT_EOF;
1174 static int extract_iso2709(struct filter_info *tinfo,
1175 struct filter_input *input,
1176 struct recExtractCtrl *p)
1182 if (p->stream->readf(p->stream, buf, 5) != 5)
1183 return RECCTRL_EXTRACT_EOF;
1184 while (*buf < '0' || *buf > '9')
1188 dom_log(YLOG_WARN, tinfo, 0,
1189 "MARC: Skipping bad byte %d (0x%02X)",
1190 *buf & 0xff, *buf & 0xff);
1191 for (i = 0; i<4; i++)
1194 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1195 return RECCTRL_EXTRACT_EOF;
1197 record_length = atoi_n (buf, 5);
1198 if (record_length < 25)
1200 dom_log(YLOG_WARN, tinfo, 0,
1201 "MARC record length < 25, is %d", record_length);
1202 return RECCTRL_EXTRACT_ERROR_GENERIC;
1204 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1205 if (read_bytes < record_length-5)
1207 dom_log(YLOG_WARN, tinfo, 0,
1208 "couldn't read whole MARC record");
1209 return RECCTRL_EXTRACT_ERROR_GENERIC;
1211 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1212 if (r < record_length)
1214 dom_log (YLOG_WARN, tinfo, 0,
1215 "parsing of MARC record failed r=%d length=%d",
1217 return RECCTRL_EXTRACT_ERROR_GENERIC;
1223 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1224 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1225 xmlDocSetRootElement(rdoc, root_ptr);
1226 return convert_extract_doc(tinfo, input, p, rdoc);
1228 return RECCTRL_EXTRACT_OK;
1231 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1233 struct filter_info *tinfo = clientData;
1234 struct filter_input *input = tinfo->input_list;
1237 return RECCTRL_EXTRACT_ERROR_GENERIC;
1239 odr_reset(tinfo->odr_record);
1242 case DOM_INPUT_XMLREADER:
1243 if (input->u.xmlreader.split_level == 0)
1244 return extract_xml_full(tinfo, input, p);
1246 return extract_xml_split(tinfo, input, p);
1248 case DOM_INPUT_MARC:
1249 return extract_iso2709(tinfo, input, p);
1251 return RECCTRL_EXTRACT_ERROR_GENERIC;
1254 static int ioread_ret(void *context, char *buffer, int len)
1256 struct recRetrieveCtrl *p = context;
1257 return p->stream->readf(p->stream, buffer, len);
1260 static int ioclose_ret(void *context)
1265 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1267 /* const char *esn = zebra_dom_ns; */
1268 const char *esn = 0;
1269 const char *params[32];
1270 struct filter_info *tinfo = clientData;
1272 struct filter_retrieve *retrieve;
1273 xsltStylesheetPtr last_xsp = 0;
1277 if (p->comp->which == Z_RecordComp_simple
1278 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1280 esn = p->comp->u.simple->u.generic;
1282 else if (p->comp->which == Z_RecordComp_complex
1283 && p->comp->u.complex->generic->elementSpec
1284 && p->comp->u.complex->generic->elementSpec->which ==
1285 Z_ElementSpec_elementSetName)
1287 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1290 retrieve = lookup_retrieve(tinfo, esn);
1294 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1299 set_param_int(params, "id", p->localno, p->odr);
1301 set_param_str(params, "filename", p->fname, p->odr);
1302 if (p->staticrank >= 0)
1303 set_param_int(params, "rank", p->staticrank, p->odr);
1306 set_param_str(params, "schema", esn, p->odr);
1309 set_param_str(params, "schema", retrieve->name, p->odr);
1310 else if (retrieve->identifier)
1311 set_param_str(params, "schema", retrieve->identifier, p->odr);
1313 set_param_str(params, "schema", "", p->odr);
1316 set_param_int(params, "score", p->score, p->odr);
1317 set_param_int(params, "size", p->recordSize, p->odr);
1319 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1322 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1325 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1329 /* retrieve conversion */
1330 perform_convert(tinfo, retrieve->convert, params, &doc, &last_xsp);
1333 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1335 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1341 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1343 xmlDocDumpMemory(doc, &buf_out, &len_out);
1345 p->output_format = VAL_TEXT_XML;
1346 p->rec_len = len_out;
1347 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1348 memcpy(p->rec_buf, buf_out, p->rec_len);
1351 else if (p->output_format == VAL_SUTRS)
1357 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1359 xmlDocDumpMemory(doc, &buf_out, &len_out);
1361 p->output_format = VAL_SUTRS;
1362 p->rec_len = len_out;
1363 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1364 memcpy(p->rec_buf, buf_out, p->rec_len);
1370 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1376 static struct recType filter_type = {
1387 #ifdef IDZEBRA_STATIC_DOM
1400 * indent-tabs-mode: nil
1402 * vim: shiftwidth=4 tabstop=8 expandtab