1 /* $Id: mod_dom.c,v 1.24 2007-02-28 16:46:19 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 recExtractCtrl *extctr,
335 struct convert_s *convert,
338 xsltStylesheetPtr *last_xsp)
340 for (; convert; convert = convert->next)
342 xmlChar *buf_out = 0;
344 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
347 *last_xsp = convert->stylesheet_xsp;
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 /* writing debug info out */
363 if (extctr->flagShowRecords)
364 yaz_log(YLOG_LOG, "%s: XSLT %s \n %s",
365 tinfo->fname ? tinfo->fname : "(none)",
374 static struct filter_input *new_input(struct filter_info *tinfo, int type)
376 struct filter_input *p;
377 struct filter_input **np = &tinfo->input_list;
378 for (;*np; np = &(*np)->next)
380 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
389 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
390 const char *syntax, const char *name)
392 FOR_EACH_ELEMENT(ptr) {
393 if (!XML_STRCMP(ptr->name, "marc"))
395 yaz_iconv_t iconv = 0;
396 const char *input_charset = "marc-8";
397 struct _xmlAttr *attr;
399 for (attr = ptr->properties; attr; attr = attr->next)
401 if (attr_content(attr, "inputcharset", &input_charset))
405 dom_log(YLOG_WARN, tinfo, ptr,
406 "bad attribute @%s, expected @inputcharset",
410 iconv = yaz_iconv_open("utf-8", input_charset);
413 dom_log(YLOG_WARN, tinfo, ptr,
414 "unsupported @charset '%s'", input_charset);
419 struct filter_input *p
420 = new_input(tinfo, DOM_INPUT_MARC);
421 p->u.marc.handle = yaz_marc_create();
422 p->u.marc.iconv = iconv;
424 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
428 parse_convert(tinfo, ptr, &p->convert);
433 else if (!XML_STRCMP(ptr->name, "xmlreader"))
435 struct filter_input *p
436 = new_input(tinfo, DOM_INPUT_XMLREADER);
437 struct _xmlAttr *attr;
438 const char *level_str = 0;
440 p->u.xmlreader.split_level = 0;
441 p->u.xmlreader.reader = 0;
443 for (attr = ptr->properties; attr; attr = attr->next)
445 if (attr_content(attr, "level", &level_str))
449 dom_log(YLOG_WARN, tinfo, ptr,
450 "bad attribute @%s, expected @level",
455 p->u.xmlreader.split_level = atoi(level_str);
459 parse_convert(tinfo, ptr, &p->convert);
464 dom_log(YLOG_WARN, tinfo, ptr,
465 "bad element <%s>, expected <marc>|<xmlreader>",
473 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
475 char tmp_full_name[1024];
479 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
481 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
482 NULL, tmp_full_name))
483 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
485 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
487 yaz_log(YLOG_LOG, "%s dom filter: "
488 "loading config file %s", tinfo->fname, tinfo->full_name);
490 doc = xmlParseFile(tinfo->full_name);
493 yaz_log(YLOG_WARN, "%s: dom filter: "
494 "failed to parse config file %s",
495 tinfo->fname, tinfo->full_name);
498 /* save because we store ptrs to the content */
499 tinfo->doc_config = doc;
501 ptr = xmlDocGetRootElement(doc);
502 if (!ptr || ptr->type != XML_ELEMENT_NODE
503 || XML_STRCMP(ptr->name, "dom"))
505 dom_log(YLOG_WARN, tinfo, ptr,
506 "bad root element <%s>, expected root element <dom>",
512 FOR_EACH_ELEMENT(ptr) {
513 if (!XML_STRCMP(ptr->name, "extract"))
516 <extract name="index">
517 <xslt stylesheet="first.xsl"/>
518 <xslt stylesheet="second.xsl"/>
521 struct _xmlAttr *attr;
522 struct filter_extract *f =
523 odr_malloc(tinfo->odr_config, sizeof(*f));
528 for (attr = ptr->properties; attr; attr = attr->next)
530 if (attr_content(attr, "name", &f->name))
534 dom_log(YLOG_WARN, tinfo, ptr,
535 "bad attribute @%s, expected @name",
539 parse_convert(tinfo, ptr->children, &f->convert);
541 else if (!XML_STRCMP(ptr->name, "retrieve"))
545 <xslt stylesheet="some.xsl"/>
546 <xslt stylesheet="some.xsl"/>
549 struct _xmlAttr *attr;
550 struct filter_retrieve **fp = &tinfo->retrieve_list;
551 struct filter_retrieve *f =
552 odr_malloc(tinfo->odr_config, sizeof(*f));
563 for (attr = ptr->properties; attr; attr = attr->next)
565 if (attr_content(attr, "identifier",
568 else if (attr_content(attr, "name", &f->name))
572 dom_log(YLOG_WARN, tinfo, ptr,
573 "bad attribute @%s, expected @identifier|@name",
577 parse_convert(tinfo, ptr->children, &f->convert);
579 else if (!XML_STRCMP(ptr->name, "store"))
583 <xslt stylesheet="some.xsl"/>
584 <xslt stylesheet="some.xsl"/>
587 struct filter_store *f =
588 odr_malloc(tinfo->odr_config, sizeof(*f));
592 parse_convert(tinfo, ptr->children, &f->convert);
594 else if (!XML_STRCMP(ptr->name, "input"))
598 <xmlreader level="1"/>
600 <input syntax="usmarc">
601 <marc inputcharset="marc-8"/>
604 struct _xmlAttr *attr;
605 const char *syntax = 0;
606 const char *name = 0;
607 for (attr = ptr->properties; attr; attr = attr->next)
609 if (attr_content(attr, "syntax", &syntax))
611 else if (attr_content(attr, "name", &name))
615 dom_log(YLOG_WARN, tinfo, ptr,
616 "bad attribute @%s, expected @syntax|@name",
620 parse_input(tinfo, ptr->children, syntax, name);
624 dom_log(YLOG_WARN, tinfo, ptr,
626 "expected <extract>|<input>|<retrieve>|<store>",
634 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
637 struct filter_retrieve *f = tinfo->retrieve_list;
639 /* return first schema if no est is provided */
642 for (; f; f = f->next)
644 /* find requested schema */
647 if (f->identifier && !strcmp(f->identifier, est))
649 if (f->name && !strcmp(f->name, est))
656 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
658 struct filter_info *tinfo = clientData;
661 yaz_log(YLOG_WARN, "dom filter: need config file");
665 if (tinfo->fname && !strcmp(args, tinfo->fname))
668 tinfo->profile_path = res_get(res, "profilePath");
671 return parse_dom(tinfo, args);
674 static void filter_destroy(void *clientData)
676 struct filter_info *tinfo = clientData;
678 odr_destroy(tinfo->odr_config);
679 odr_destroy(tinfo->odr_record);
683 static int ioread_ex(void *context, char *buffer, int len)
685 struct recExtractCtrl *p = context;
686 return p->stream->readf(p->stream, buffer, len);
689 static int ioclose_ex(void *context)
695 /* DOM filter style indexing */
696 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
697 xmlChar **dst_content)
699 if (0 == XML_STRCMP(attr->name, name) && attr->children
700 && attr->children->type == XML_TEXT_NODE)
702 *dst_content = (attr->children->content);
709 /* DOM filter style indexing */
710 static void index_value_of(struct filter_info *tinfo,
711 struct recExtractCtrl *extctr,
716 xmlChar *text = xmlNodeGetContent(node);
717 size_t text_len = strlen((const char *)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 /* writing debug out */
768 if (extctr->flagShowRecords)
769 dom_log(YLOG_LOG, tinfo, 0,
770 "INDEX '%s:%s' '%s'",
771 index ? (const char *) index : "null",
772 type ? (const char *) type : "null",
773 text ? (const char *) text : "null");
775 /* actually indexing the text given */
776 recword->index_name = (const char *)index;
778 recword->index_type = *type;
779 (extctr->tokenAdd)(recword);
781 /* eat whitespaces */
782 if (*look && ' ' == *look && *(look+1))
793 /* DOM filter style indexing */
794 static void set_record_info(struct filter_info *tinfo,
795 struct recExtractCtrl *extctr,
801 /* writing debug info out */
802 if (extctr->flagShowRecords)
803 dom_log(YLOG_LOG, tinfo, 0,
804 "RECORD id=%s rank=%s type=%s",
805 id_p ? (const char *) id_p : "(null)",
806 rank_p ? (const char *) rank_p : "(null)",
807 type_p ? (const char *) type_p : "(null)");
811 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
814 extctr->staticrank = atozint((const char *)rank_p);
816 /* if (!strcmp("update", type_str)) */
817 /* index_node(tinfo, ctrl, ptr, recword); */
818 /* else if (!strcmp("delete", type_str)) */
819 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
821 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
827 /* DOM filter style indexing */
828 static void process_xml_element_zebra_node(struct filter_info *tinfo,
829 struct recExtractCtrl *extctr,
833 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
834 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
836 if (0 == XML_STRCMP(node->name, "index"))
838 xmlChar *index_p = 0;
840 struct _xmlAttr *attr;
841 for (attr = node->properties; attr; attr = attr->next)
843 if (attr_content_xml(attr, "name", &index_p))
845 index_value_of(tinfo, extctr, recword,node, index_p);
849 dom_log(YLOG_WARN, tinfo, node,
850 "bad attribute @%s, expected @name",
855 else if (0 == XML_STRCMP(node->name, "record"))
861 struct _xmlAttr *attr;
862 for (attr = node->properties; attr; attr = attr->next)
864 if (attr_content_xml(attr, "id", &id_p))
866 else if (attr_content_xml(attr, "rank", &rank_p))
868 else if (attr_content_xml(attr, "type", &type_p))
872 dom_log(YLOG_WARN, tinfo, node,
873 "bad attribute @%s, expected @id|@rank|@type",
877 if (type_p && 0 != strcmp("update", (const char *)type_p))
879 dom_log(YLOG_WARN, tinfo, node,
880 "attribute @%s, only implemented '@type='update'",
884 set_record_info(tinfo, extctr, id_p, rank_p, type_p);
888 dom_log(YLOG_WARN, tinfo, node,
890 " expected <record>|<index> in namespace '%s'",
891 node->name, zebra_dom_ns);
897 /* DOM filter style indexing */
898 static void process_xml_pi_node(struct filter_info *tinfo,
899 struct recExtractCtrl *extctr,
903 /* if right PI name, continue parsing PI */
904 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
906 xmlChar *pi_p = node->content;
907 xmlChar *look = pi_p;
912 /* parsing PI record instructions */
913 if (0 == strncmp((const char *)look, "record", 6))
926 while (*look && ' ' == *look && *(look+1))
929 /* parse possible id */
930 if (*look && 0 == strncmp((const char *)look, "id=", 3))
934 while (*look && ' ' != *look)
937 strncpy((char *)id, (const char *)bval, eval - bval);
938 id[eval - bval] = '\0';
942 while (*look && ' ' == *look && *(look+1))
945 /* parse possible rank */
946 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
950 while (*look && ' ' != *look)
953 strncpy((char *)rank, (const char *)bval, eval - bval);
954 rank[eval - bval] = '\0';
958 while (*look && ' ' == *look && *(look+1))
961 if (look && '\0' != *look)
963 dom_log(YLOG_WARN, tinfo, node,
964 "content '%s', can not parse '%s'",
968 set_record_info(tinfo, extctr, id, rank, 0);
971 /* parsing index instruction */
972 else if (0 == strncmp((const char *)look, "index", 5))
977 while (*look && ' ' == *look && *(look+1))
980 /* export index instructions to outside */
985 dom_log(YLOG_WARN, tinfo, node,
986 "content '%s', can not parse '%s'",
992 /* DOM filter style indexing */
993 static void process_xml_element_node(struct filter_info *tinfo,
994 struct recExtractCtrl *extctr,
998 /* remember indexing instruction from PI to next element node */
999 xmlChar *index_p = 0;
1001 /* check if we are an element node in the special zebra namespace
1002 and either set record data or index value-of node content*/
1003 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1005 /* loop through kid nodes */
1006 for (node = node->children; node; node = node->next)
1008 /* check and set PI record and index index instructions */
1009 if (node->type == XML_PI_NODE)
1011 process_xml_pi_node(tinfo, extctr, node, &index_p);
1013 else if (node->type == XML_ELEMENT_NODE)
1015 /* if there was a PI index instruction before this element */
1018 index_value_of(tinfo, extctr, recword, node, index_p);
1021 process_xml_element_node(tinfo, extctr, recword,node);
1029 /* DOM filter style indexing */
1030 static void extract_dom_doc_node(struct filter_info *tinfo,
1031 struct recExtractCtrl *extctr,
1037 /* only need to do the initialization once, reuse recword for all terms */
1039 (*extctr->init)(extctr, &recword);
1042 if (extctr->flagShowRecords)
1044 xmlDocDumpMemory(doc, &buf_out, &len_out);
1045 fwrite(buf_out, len_out, 1, stdout);
1050 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1056 static int convert_extract_doc(struct filter_info *tinfo,
1057 struct filter_input *input,
1058 struct recExtractCtrl *p,
1064 const char *params[10];
1065 xsltStylesheetPtr last_xsp = 0;
1066 xmlDocPtr store_doc = 0;
1069 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1071 /* input conversion */
1072 perform_convert(tinfo, p, input->convert, params, &doc, 0);
1076 /* store conversion */
1077 store_doc = xmlCopyDoc(doc, 1);
1078 perform_convert(tinfo, p, tinfo->store->convert,
1079 params, &store_doc, &last_xsp);
1083 xsltSaveResultToString(&buf_out, &len_out,
1084 store_doc ? store_doc : doc, last_xsp);
1086 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1088 /* if (p->flagShowRecords)
1089 fwrite(buf_out, len_out, 1, stdout); */
1091 (*p->setStoreData)(p, buf_out, len_out);
1095 xmlFreeDoc(store_doc);
1097 /* extract conversion */
1098 perform_convert(tinfo, p, tinfo->extract->convert, params, &doc, 0);
1100 /* finally, do the indexing */
1102 extract_dom_doc_node(tinfo, p, doc);
1107 return RECCTRL_EXTRACT_OK;
1110 static int extract_xml_split(struct filter_info *tinfo,
1111 struct filter_input *input,
1112 struct recExtractCtrl *p)
1116 if (p->first_record)
1118 if (input->u.xmlreader.reader)
1119 xmlFreeTextReader(input->u.xmlreader.reader);
1120 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1121 p /* I/O handler */,
1127 if (!input->u.xmlreader.reader)
1128 return RECCTRL_EXTRACT_ERROR_GENERIC;
1130 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1133 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1134 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1135 if (type == XML_READER_TYPE_ELEMENT &&
1136 input->u.xmlreader.split_level == depth)
1139 = xmlTextReaderExpand(input->u.xmlreader.reader);
1142 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1143 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1145 xmlDocSetRootElement(doc, ptr2);
1147 /* writing debug info out */
1148 if (p->flagShowRecords){
1149 xmlChar *buf_out = 0;
1151 xmlDocDumpMemory(doc, &buf_out, &len_out);
1152 yaz_log(YLOG_LOG, "%s: XMLREADER depth: %i\n%s",
1153 tinfo->fname ? tinfo->fname : "(none)",
1159 return convert_extract_doc(tinfo, input, p, doc);
1163 xmlFreeTextReader(input->u.xmlreader.reader);
1164 input->u.xmlreader.reader = 0;
1165 return RECCTRL_EXTRACT_ERROR_GENERIC;
1168 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1170 xmlFreeTextReader(input->u.xmlreader.reader);
1171 input->u.xmlreader.reader = 0;
1172 return RECCTRL_EXTRACT_EOF;
1175 static int extract_xml_full(struct filter_info *tinfo,
1176 struct filter_input *input,
1177 struct recExtractCtrl *p)
1179 if (p->first_record) /* only one record per stream */
1181 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1182 p /* I/O handler */,
1185 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1188 return RECCTRL_EXTRACT_ERROR_GENERIC;
1190 return convert_extract_doc(tinfo, input, p, doc);
1193 return RECCTRL_EXTRACT_EOF;
1196 static int extract_iso2709(struct filter_info *tinfo,
1197 struct filter_input *input,
1198 struct recExtractCtrl *p)
1204 if (p->stream->readf(p->stream, buf, 5) != 5)
1205 return RECCTRL_EXTRACT_EOF;
1206 while (*buf < '0' || *buf > '9')
1210 dom_log(YLOG_WARN, tinfo, 0,
1211 "MARC: Skipping bad byte %d (0x%02X)",
1212 *buf & 0xff, *buf & 0xff);
1213 for (i = 0; i<4; i++)
1216 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1217 return RECCTRL_EXTRACT_EOF;
1219 record_length = atoi_n (buf, 5);
1220 if (record_length < 25)
1222 dom_log(YLOG_WARN, tinfo, 0,
1223 "MARC record length < 25, is %d", record_length);
1224 return RECCTRL_EXTRACT_ERROR_GENERIC;
1226 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1227 if (read_bytes < record_length-5)
1229 dom_log(YLOG_WARN, tinfo, 0,
1230 "couldn't read whole MARC record");
1231 return RECCTRL_EXTRACT_ERROR_GENERIC;
1233 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1234 if (r < record_length)
1236 dom_log (YLOG_WARN, tinfo, 0,
1237 "parsing of MARC record failed r=%d length=%d",
1239 return RECCTRL_EXTRACT_ERROR_GENERIC;
1245 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1246 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1247 xmlDocSetRootElement(rdoc, root_ptr);
1248 return convert_extract_doc(tinfo, input, p, rdoc);
1250 return RECCTRL_EXTRACT_OK;
1253 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1255 struct filter_info *tinfo = clientData;
1256 struct filter_input *input = tinfo->input_list;
1259 return RECCTRL_EXTRACT_ERROR_GENERIC;
1261 odr_reset(tinfo->odr_record);
1264 case DOM_INPUT_XMLREADER:
1265 if (input->u.xmlreader.split_level == 0)
1266 return extract_xml_full(tinfo, input, p);
1268 return extract_xml_split(tinfo, input, p);
1270 case DOM_INPUT_MARC:
1271 return extract_iso2709(tinfo, input, p);
1273 return RECCTRL_EXTRACT_ERROR_GENERIC;
1276 static int ioread_ret(void *context, char *buffer, int len)
1278 struct recRetrieveCtrl *p = context;
1279 return p->stream->readf(p->stream, buffer, len);
1282 static int ioclose_ret(void *context)
1287 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1289 /* const char *esn = zebra_dom_ns; */
1290 const char *esn = 0;
1291 const char *params[32];
1292 struct filter_info *tinfo = clientData;
1294 struct filter_retrieve *retrieve;
1295 xsltStylesheetPtr last_xsp = 0;
1299 if (p->comp->which == Z_RecordComp_simple
1300 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1302 esn = p->comp->u.simple->u.generic;
1304 else if (p->comp->which == Z_RecordComp_complex
1305 && p->comp->u.complex->generic->elementSpec
1306 && p->comp->u.complex->generic->elementSpec->which ==
1307 Z_ElementSpec_elementSetName)
1309 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1312 retrieve = lookup_retrieve(tinfo, esn);
1316 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1321 set_param_int(params, "id", p->localno, p->odr);
1323 set_param_str(params, "filename", p->fname, p->odr);
1324 if (p->staticrank >= 0)
1325 set_param_int(params, "rank", p->staticrank, p->odr);
1328 set_param_str(params, "schema", esn, p->odr);
1331 set_param_str(params, "schema", retrieve->name, p->odr);
1332 else if (retrieve->identifier)
1333 set_param_str(params, "schema", retrieve->identifier, p->odr);
1335 set_param_str(params, "schema", "", p->odr);
1338 set_param_int(params, "score", p->score, p->odr);
1339 set_param_int(params, "size", p->recordSize, p->odr);
1341 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1344 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1347 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1351 /* retrieve conversion */
1352 perform_convert(tinfo, 0, retrieve->convert, params, &doc, &last_xsp);
1355 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1357 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1363 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1365 xmlDocDumpMemory(doc, &buf_out, &len_out);
1367 p->output_format = VAL_TEXT_XML;
1368 p->rec_len = len_out;
1369 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1370 memcpy(p->rec_buf, buf_out, p->rec_len);
1373 else if (p->output_format == VAL_SUTRS)
1379 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1381 xmlDocDumpMemory(doc, &buf_out, &len_out);
1383 p->output_format = VAL_SUTRS;
1384 p->rec_len = len_out;
1385 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1386 memcpy(p->rec_buf, buf_out, p->rec_len);
1392 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1398 static struct recType filter_type = {
1409 #ifdef IDZEBRA_STATIC_DOM
1422 * indent-tabs-mode: nil
1424 * vim: shiftwidth=4 tabstop=8 expandtab