1 /* $Id: mod_dom.c,v 1.11 2007-02-14 16:43:37 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
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
30 #include <libxml/xmlversion.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xmlIO.h>
34 #include <libxml/xmlreader.h>
35 #include <libxslt/transform.h>
36 #include <libxslt/xsltutils.h>
39 #include <libexslt/exslt.h>
42 #include <idzebra/util.h>
43 #include <idzebra/recctrl.h>
46 const char *stylesheet;
47 xsltStylesheetPtr stylesheet_xsp;
48 struct convert_s *next;
51 struct filter_extract {
53 struct convert_s *convert;
57 struct convert_s *convert;
60 struct filter_retrieve {
62 const char *identifier;
63 struct convert_s *convert;
64 struct filter_retrieve *next;
67 #define DOM_INPUT_XMLREADER 1
68 #define DOM_INPUT_MARC 2
72 struct convert_s *convert;
76 const char *input_charset;
81 xmlTextReaderPtr reader;
85 struct filter_input *next;
91 const char *profile_path;
95 struct filter_extract *extract;
96 struct filter_retrieve *retrieve_list;
97 struct filter_input *input_list;
98 struct filter_store *store;
101 #define XML_STRCMP(a,b) strcmp((char*)a, b)
102 #define XML_STRLEN(a) strlen((char*)a)
107 static void set_param_str(const char **params, const char *name,
108 const char *value, ODR odr)
110 char *quoted = odr_malloc(odr, 3 + strlen(value));
111 sprintf(quoted, "'%s'", value);
119 static void set_param_int(const char **params, const char *name,
122 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
125 sprintf(quoted, "'" ZINT_FORMAT "'", value);
131 static void *filter_init(Res res, RecType recType)
133 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
135 tinfo->full_name = 0;
136 tinfo->profile_path = 0;
137 tinfo->odr_record = odr_createmem(ODR_ENCODE);
138 tinfo->odr_config = odr_createmem(ODR_ENCODE);
140 tinfo->retrieve_list = 0;
141 tinfo->input_list = 0;
143 tinfo->doc_config = 0;
152 static int attr_content(struct _xmlAttr *attr, const char *name,
153 const char **dst_content)
155 if (!XML_STRCMP(attr->name, name) && attr->children
156 && attr->children->type == XML_TEXT_NODE)
158 *dst_content = (const char *)(attr->children->content);
164 static void destroy_xsp(struct convert_s *c)
168 if (c->stylesheet_xsp)
169 xsltFreeStylesheet(c->stylesheet_xsp);
174 static void destroy_dom(struct filter_info *tinfo)
178 destroy_xsp(tinfo->extract->convert);
183 destroy_xsp(tinfo->store->convert);
186 if (tinfo->input_list)
188 struct filter_input *i_ptr;
189 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
193 case DOM_INPUT_XMLREADER:
194 if (i_ptr->u.xmlreader.reader)
195 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
198 yaz_iconv_close(i_ptr->u.marc.iconv);
199 yaz_marc_destroy(i_ptr->u.marc.handle);
202 destroy_xsp(i_ptr->convert);
204 tinfo->input_list = 0;
206 if (tinfo->retrieve_list)
208 struct filter_retrieve *r_ptr;
209 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
210 destroy_xsp(r_ptr->convert);
211 tinfo->retrieve_list = 0;
214 if (tinfo->doc_config)
216 xmlFreeDoc(tinfo->doc_config);
217 tinfo->doc_config = 0;
219 odr_reset(tinfo->odr_config);
222 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
223 struct convert_s **l)
226 for(; ptr; ptr = ptr->next)
228 if (ptr->type != XML_ELEMENT_NODE)
230 if (!XML_STRCMP(ptr->name, "xslt"))
232 struct _xmlAttr *attr;
234 = odr_malloc(tinfo->odr_config, sizeof(*p));
238 p->stylesheet_xsp = 0;
240 for (attr = ptr->properties; attr; attr = attr->next)
241 if (attr_content(attr, "stylesheet", &p->stylesheet))
244 yaz_log(YLOG_WARN, "%s: dom filter: "
247 tinfo->fname, attr->name);
250 char tmp_xslt_full_name[1024];
251 if (!yaz_filepath_resolve(p->stylesheet,
258 "stylesheet %s not found in "
262 tinfo->profile_path);
267 = xsltParseStylesheetFile((const xmlChar*)
269 if (!p->stylesheet_xsp)
273 "could not parse xslt "
275 tinfo->fname, tmp_xslt_full_name);
283 "missing attribute 'stylesheet' "
284 "for element 'xslt'", tinfo->fname);
293 "%s: dom filter: bad node '%s' for <conv>",
294 tinfo->fname, ptr->name);
302 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
303 struct convert_s *convert,
306 xsltStylesheetPtr *last_xsp)
308 for (; convert; convert = convert->next)
310 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
313 *last_xsp = convert->stylesheet_xsp;
320 static struct filter_input *new_input(struct filter_info *tinfo, int type)
322 struct filter_input *p;
323 struct filter_input **np = &tinfo->input_list;
324 for (;*np; np = &(*np)->next)
326 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
335 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
339 for (; ptr; ptr = ptr->next)
341 if (ptr->type != XML_ELEMENT_NODE)
343 if (!XML_STRCMP(ptr->name, "marc"))
345 yaz_iconv_t iconv = 0;
346 const char *input_charset = "marc-8";
347 struct _xmlAttr *attr;
349 for (attr = ptr->properties; attr; attr = attr->next)
351 if (attr_content(attr, "charset", &input_charset))
355 "%s: dom filter: bad attribute %s"
357 tinfo->fname, attr->name);
359 iconv = yaz_iconv_open("utf-8", input_charset);
363 "%s: dom filter: unsupported charset "
365 tinfo->fname, input_charset);
370 struct filter_input *p
371 = new_input(tinfo, DOM_INPUT_MARC);
372 p->u.marc.handle = yaz_marc_create();
373 p->u.marc.iconv = iconv;
375 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
379 parse_convert(tinfo, ptr, &p->convert);
384 else if (!XML_STRCMP(ptr->name, "xmlreader"))
386 struct filter_input *p
387 = new_input(tinfo, DOM_INPUT_XMLREADER);
388 struct _xmlAttr *attr;
389 const char *level_str = 0;
391 p->u.xmlreader.split_level = 0;
392 p->u.xmlreader.reader = 0;
394 for (attr = ptr->properties; attr; attr = attr->next)
396 if (attr_content(attr, "level", &level_str))
400 "%s: dom filter: bad attribute %s"
402 tinfo->fname, attr->name);
405 p->u.xmlreader.split_level = atoi(level_str);
409 parse_convert(tinfo, ptr, &p->convert);
414 yaz_log(YLOG_WARN, "%s: dom filter: bad input type %s",
415 tinfo->fname, ptr->name);
422 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
424 char tmp_full_name[1024];
428 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
430 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
431 NULL, tmp_full_name))
432 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
434 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
436 yaz_log(YLOG_LOG, "dom filter: loading config file %s", tinfo->full_name);
438 doc = xmlParseFile(tinfo->full_name);
442 "%s: dom filter: failed to parse config file %s",
443 tinfo->fname, tinfo->full_name);
446 /* save because we store ptrs to the content */
447 tinfo->doc_config = doc;
449 ptr = xmlDocGetRootElement(doc);
450 if (!ptr || ptr->type != XML_ELEMENT_NODE
451 || XML_STRCMP(ptr->name, "dom"))
454 "%s: dom filter: expected root element <dom>",
459 for (ptr = ptr->children; ptr; ptr = ptr->next)
461 if (ptr->type != XML_ELEMENT_NODE)
463 if (!XML_STRCMP(ptr->name, "extract"))
466 <extract name="index">
467 <xslt stylesheet="first.xsl"/>
468 <xslt stylesheet="second.xsl"/>
471 struct _xmlAttr *attr;
472 struct filter_extract *f =
473 odr_malloc(tinfo->odr_config, sizeof(*f));
478 for (attr = ptr->properties; attr; attr = attr->next)
480 if (attr_content(attr, "name", &f->name))
484 "%s: dom filter: bad attribute %s"
486 tinfo->fname, attr->name);
489 parse_convert(tinfo, ptr->children, &f->convert);
491 else if (!XML_STRCMP(ptr->name, "retrieve"))
495 <xslt stylesheet="some.xsl"/>
496 <xslt stylesheet="some.xsl"/>
499 struct _xmlAttr *attr;
500 struct filter_retrieve **fp = &tinfo->retrieve_list;
501 struct filter_retrieve *f =
502 odr_malloc(tinfo->odr_config, sizeof(*f));
513 for (attr = ptr->properties; attr; attr = attr->next)
515 if (attr_content(attr, "identifier",
518 else if (attr_content(attr, "name", &f->name))
522 "%s: dom filter: bad attribute %s"
524 tinfo->fname, attr->name);
526 parse_convert(tinfo, ptr->children, &f->convert);
528 else if (!XML_STRCMP(ptr->name, "store"))
532 <xslt stylesheet="some.xsl"/>
533 <xslt stylesheet="some.xsl"/>
536 struct filter_store *f =
537 odr_malloc(tinfo->odr_config, sizeof(*f));
541 parse_convert(tinfo, ptr->children, &f->convert);
543 else if (!XML_STRCMP(ptr->name, "input"))
547 <xmlreader level="1"/>
549 <input syntax="usmarc">
550 <marc inputcharset="marc-8"/>
553 struct _xmlAttr *attr;
554 const char *syntax = 0;
555 const char *name = 0;
556 for (attr = ptr->properties; attr; attr = attr->next)
558 if (attr_content(attr, "syntax", &syntax))
560 else if (attr_content(attr, "name", &name))
564 "%s: dom filter: bad attribute %s"
566 tinfo->fname, attr->name);
568 parse_input(tinfo, ptr->children, syntax, name);
572 yaz_log(YLOG_WARN, "%s: dom filter: bad element %s",
573 tinfo->fname, ptr->name);
580 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
583 struct filter_retrieve *f = tinfo->retrieve_list;
585 /* return first schema if no est is provided */
588 for (; f; f = f->next)
590 /* find requested schema */
593 if (f->identifier && !strcmp(f->identifier, est))
595 if (f->name && !strcmp(f->name, est))
602 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
604 struct filter_info *tinfo = clientData;
607 yaz_log(YLOG_WARN, "dom filter: need config file");
611 if (tinfo->fname && !strcmp(args, tinfo->fname))
614 tinfo->profile_path = res_get(res, "profilePath");
617 return parse_dom(tinfo, args);
620 static void filter_destroy(void *clientData)
622 struct filter_info *tinfo = clientData;
624 odr_destroy(tinfo->odr_config);
625 odr_destroy(tinfo->odr_record);
629 static int ioread_ex(void *context, char *buffer, int len)
631 struct recExtractCtrl *p = context;
632 return p->stream->readf(p->stream, buffer, len);
635 static int ioclose_ex(void *context)
641 /* Alvis style indexing */
642 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
643 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
645 /* Alvis style indexing */
646 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
647 xmlNodePtr ptr, RecWord *recWord)
649 for(; ptr; ptr = ptr->next)
651 index_cdata(tinfo, ctrl, ptr->children, recWord);
652 if (ptr->type != XML_TEXT_NODE)
654 recWord->term_buf = (const char *)ptr->content;
655 recWord->term_len = XML_STRLEN(ptr->content);
656 (*ctrl->tokenAdd)(recWord);
660 /* Alvis style indexing */
661 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
662 xmlNodePtr ptr, RecWord *recWord)
664 for(; ptr; ptr = ptr->next)
666 index_node(tinfo, ctrl, ptr->children, recWord);
667 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
668 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
670 if (!XML_STRCMP(ptr->name, "index"))
672 const char *name_str = 0;
673 const char *type_str = 0;
674 const char *xpath_str = 0;
675 struct _xmlAttr *attr;
676 for (attr = ptr->properties; attr; attr = attr->next)
678 if (attr_content(attr, "name", &name_str))
680 else if (attr_content(attr, "xpath", &xpath_str))
682 else if (attr_content(attr, "type", &type_str))
686 "%s: dom filter: bad attribute %s"
688 tinfo->fname, attr->name);
692 /* save default type */
693 int prev_type = recWord->index_type;
696 if (type_str && *type_str)
697 recWord->index_type = *type_str;
699 recWord->index_name = name_str;
700 index_cdata(tinfo, ctrl, ptr->children, recWord);
702 /* restore it again */
703 recWord->index_type = prev_type;
709 /* Alvis style indexing */
710 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
711 xmlNodePtr ptr, RecWord *recWord)
713 const char *type_str = "update";
715 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
716 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
717 && !XML_STRCMP(ptr->name, "record"))
719 const char *id_str = 0;
720 const char *rank_str = 0;
721 struct _xmlAttr *attr;
722 for (attr = ptr->properties; attr; attr = attr->next)
724 if (attr_content(attr, "type", &type_str))
726 else if (attr_content(attr, "id", &id_str))
728 else if (attr_content(attr, "rank", &rank_str))
731 yaz_log(YLOG_WARN, "%s: dom filter: bad attribute %s"
733 tinfo->fname, attr->name);
736 sscanf(id_str, "%255s", ctrl->match_criteria);
739 ctrl->staticrank = atozint(rank_str);
743 if (!strcmp("update", type_str))
744 index_node(tinfo, ctrl, ptr, recWord);
745 else if (!strcmp("delete", type_str))
746 yaz_log(YLOG_WARN, "dom filter delete: to be implemented");
748 yaz_log(YLOG_WARN, "dom filter: unknown record type '%s'",
753 /* Alvis style indexing */
754 static void extract_doc_alvis(struct filter_info *tinfo,
755 struct recExtractCtrl *recctr,
764 (*recctr->init)(recctr, &recWord);
766 if (recctr->flagShowRecords){
767 xmlDocDumpMemory(doc, &buf_out, &len_out);
768 fwrite(buf_out, len_out, 1, stdout);
771 root_ptr = xmlDocGetRootElement(doc);
773 index_record(tinfo, recctr, root_ptr, &recWord);
775 yaz_log(YLOG_WARN, "No root for index XML record");
780 /* DOM filter style indexing */
781 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
782 xmlChar **dst_content)
784 if (0 == XML_STRCMP(attr->name, name) && attr->children
785 && attr->children->type == XML_TEXT_NODE)
787 *dst_content = (attr->children->content);
793 /* DOM filter style indexing */
794 /* #define ZEBRA_XSLT_NS "http://indexdata.com/zebra-2.0" */
795 /* static const char *zebra_xslt_ns = ZEBRA_XSLT_NS; */
797 /* DOM filter style indexing */
798 #define ZEBRA_PI_NAME "zebra-2.0"
799 static const char *zebra_pi_name = ZEBRA_PI_NAME;
802 /* DOM filter style indexing */
803 static void index_value_of(struct filter_info *tinfo,
804 struct recExtractCtrl *recctr,
808 xmlChar *text = xmlNodeGetContent(node);
810 xmlChar *look = index_p;
817 /* parsing all index name/type pairs - may not start with ' ' or ':' */
818 while (*look && ' ' != *look && ':' != *look){
820 /* setting name and type to zero */
824 /* parsing one index name */
826 while (*look && ':' != *look && ' ' != *look){
830 strncpy((char *)index, (const char *)bval, eval - bval);
831 index[eval - bval] = '\0';
834 /* parsing one index type, if existing */
839 while (*look && ' ' != *look){
843 strncpy((char *)type, (const char *)bval, eval - bval);
844 type[eval - bval] = '\0';
847 printf("INDEX '%s:%s' '%s'\n", index, type, text);
849 if (*look && ' ' == *look && *(look+1)){
856 /* //recWord->term_buf = (const char *)ptr->content; */
857 /* //recWord->term_len = XML_STRLEN(ptr->content); */
858 /* // if (type_str && *type_str) */
859 /* // recWord->index_type = *type_str; /\* type was given *\/ */
860 /* // recWord->index_name = name_str; */
861 /* // recWord->index_type = prev_type; /\* restore it again *\/ */
865 /* DOM filter style indexing */
866 static void set_record_info(struct filter_info *tinfo,
867 struct recExtractCtrl *recctr,
872 printf("RECORD id=%s rank=%s type=%s\n", id_p, rank_p, type_p);
876 /* DOM filter style indexing */
877 static void process_xml_element_zebra_node(struct filter_info *tinfo,
878 struct recExtractCtrl *recctr,
881 if (node->type == XML_ELEMENT_NODE
882 && node->ns && 0 == XML_STRCMP(node->ns->href, zebra_xslt_ns)){
884 if (0 == XML_STRCMP(node->name, "index")){
885 xmlChar *index_p = 0;
887 struct _xmlAttr *attr;
888 for (attr = node->properties; attr; attr = attr->next){
889 if (attr_content_xml(attr, "name", &index_p)){
890 index_value_of(tinfo, recctr, node, index_p);
893 // printf("%s: dom filter: s% bad attribute %s",
894 // tinfo->fname, xmlGetNodePath(node)), nodeattr->name);
895 printf("dom filter: %s bad attribute @%s, "
897 xmlGetNodePath(node), attr->name);
900 else if (0 == XML_STRCMP(node->name, "record")){
905 struct _xmlAttr *attr;
906 for (attr = node->properties; attr; attr = attr->next){
907 if (attr_content_xml(attr, "id", &id_p))
909 else if (attr_content_xml(attr, "rank", &rank_p))
911 else if (attr_content_xml(attr, "type", &type_p))
914 // printf("%s: dom filter: s% bad attribute %s",
915 // tinfo->fname, xmlGetNodePath(node)), nodeattr->name);
916 printf("dom filter: %s bad attribute @%s,"
917 " expected @id|@rank|@type\n",
918 xmlGetNodePath(node), attr->name);
920 if (type_p && 0 != strcmp("update", (const char *)type_p))
921 printf("dom filter: %s attribute @%s,"
922 " only implemented '@type=\"update\"\n",
923 xmlGetNodePath(node), attr->name);
927 set_record_info(tinfo, recctr, id_p, rank_p, type_p);
929 // printf("%s: dom filter: s% bad attribute %s",
930 // tinfo->fname, xmlGetNodePath(node)), nodeattr->name);
931 printf("dom filter: %s bad element <%s>,"
932 " expected <record>|<index> in namespace '%s'\n",
933 xmlGetNodePath(node), node->name, zebra_xslt_ns);
940 /* DOM filter style indexing */
941 static void process_xml_pi_node(struct filter_info *tinfo,
942 struct recExtractCtrl *recctr,
947 /* printf("PI %s\n", xmlGetNodePath(node)); */
949 /* if right PI name, continue parsing PI */
950 if (0 == strcmp(zebra_pi_name, (const char *)node->name)){
951 xmlChar *pi_p = node->content;
952 xmlChar *look = pi_p;
957 /* parsing PI record instructions */
958 if (0 == strncmp((const char *)look, "record", 6)){
970 while (*look && ' ' == *look && *(look+1))
973 /* parse possible id */
974 if (*look && 0 == strncmp((const char *)look, "id=", 3)){
977 while (*look && ' ' != *look)
980 strncpy((char *)id, (const char *)bval, eval - bval);
981 id[eval - bval] = '\0';
985 while (*look && ' ' == *look && *(look+1))
988 /* parse possible rank */
989 if (*look && 0 == strncmp((const char *)look, "rank=", 5)){
992 while (*look && ' ' != *look)
995 strncpy((char *)rank, (const char *)bval, eval - bval);
996 rank[eval - bval] = '\0';
1000 while (*look && ' ' == *look && *(look+1))
1003 if (look && '\0' != *look){
1004 printf ("ERROR %s: content '%s'; can not parse '%s'\n",
1005 xmlGetNodePath(node), pi_p, look);
1007 /* set_record_info(id, rank, type); */
1008 set_record_info(tinfo, recctr, id, rank, 0);
1013 /* parsing index instruction */
1014 else if (0 == strncmp((const char *)look, "index", 5)){
1017 /* eat whitespace */
1018 while (*look && ' ' == *look && *(look+1))
1021 /* export index instructions to outside */
1024 /* nor record, neither index */
1027 printf ("ERROR %s: content '%s'; can not parse '%s'\n",
1028 xmlGetNodePath(node), pi_p, look);
1033 /* DOM filter style indexing */
1034 static void process_xml_element_node(struct filter_info *tinfo,
1035 struct recExtractCtrl *recctr,
1038 /* remember indexing instruction from PI to next element node */
1039 xmlChar *index_p = 0;
1041 /* printf("ELEM %s\n", xmlGetNodePath(node)); */
1043 /* check if we are an element node in the special zebra namespace
1044 and either set record data or index value-of node content*/
1045 process_xml_element_zebra_node(tinfo, recctr, node);
1047 /* loop through kid nodes */
1048 for (node = node->children; node; node = node->next)
1050 /* check and set PI record and index index instructions */
1051 if (node->type == XML_PI_NODE){
1052 process_xml_pi_node(tinfo, recctr, node, &index_p);
1054 else if (node->type == XML_ELEMENT_NODE){
1055 /* if there was a PI index instruction before this element */
1057 index_value_of(tinfo, recctr, node, index_p);
1060 process_xml_element_node(tinfo, recctr, node);
1068 /* DOM filter style indexing */
1069 static void extract_dom_doc_node(struct filter_info *tinfo,
1070 struct recExtractCtrl *recctr,
1073 /* printf("DOC %s\n", xmlGetNodePath((xmlNodePtr)doc)); */
1075 process_xml_element_node(tinfo, recctr, (xmlNodePtr)doc);
1081 static int convert_extract_doc(struct filter_info *tinfo,
1082 struct filter_input *input,
1083 struct recExtractCtrl *p,
1087 /* RecWord recWord; */
1090 const char *params[10];
1091 xsltStylesheetPtr last_xsp = 0;
1092 xmlDocPtr store_doc = 0;
1095 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr_record);
1097 /* input conversion */
1098 perform_convert(tinfo, input->convert, params, &doc, 0);
1102 /* store conversion */
1103 store_doc = xmlCopyDoc(doc, 1);
1104 perform_convert(tinfo, tinfo->store->convert,
1105 params, &store_doc, &last_xsp);
1109 xsltSaveResultToString(&buf_out, &len_out,
1110 store_doc ? store_doc : doc, last_xsp);
1112 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1113 if (p->flagShowRecords)
1114 fwrite(buf_out, len_out, 1, stdout);
1115 (*p->setStoreData)(p, buf_out, len_out);
1119 xmlFreeDoc(store_doc);
1121 /* extract conversion */
1122 perform_convert(tinfo, tinfo->extract->convert, params, &doc, 0);
1124 /* finally, do the indexing */
1126 extract_dom_doc_node(tinfo, p, doc);
1127 extract_doc_alvis(tinfo, p, doc);
1131 return RECCTRL_EXTRACT_OK;
1134 static int extract_xml_split(struct filter_info *tinfo,
1135 struct filter_input *input,
1136 struct recExtractCtrl *p)
1140 if (p->first_record)
1142 if (input->u.xmlreader.reader)
1143 xmlFreeTextReader(input->u.xmlreader.reader);
1144 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1145 p /* I/O handler */,
1151 if (!input->u.xmlreader.reader)
1152 return RECCTRL_EXTRACT_ERROR_GENERIC;
1154 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1157 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1158 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1159 if (type == XML_READER_TYPE_ELEMENT &&
1160 input->u.xmlreader.split_level == depth)
1163 = xmlTextReaderExpand(input->u.xmlreader.reader);
1166 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1167 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1169 xmlDocSetRootElement(doc, ptr2);
1171 return convert_extract_doc(tinfo, input, p, doc);
1175 xmlFreeTextReader(input->u.xmlreader.reader);
1176 input->u.xmlreader.reader = 0;
1177 return RECCTRL_EXTRACT_ERROR_GENERIC;
1180 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1182 xmlFreeTextReader(input->u.xmlreader.reader);
1183 input->u.xmlreader.reader = 0;
1184 return RECCTRL_EXTRACT_EOF;
1187 static int extract_xml_full(struct filter_info *tinfo,
1188 struct filter_input *input,
1189 struct recExtractCtrl *p)
1191 if (p->first_record) /* only one record per stream */
1193 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1194 p /* I/O handler */,
1197 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1200 return RECCTRL_EXTRACT_ERROR_GENERIC;
1202 return convert_extract_doc(tinfo, input, p, doc);
1205 return RECCTRL_EXTRACT_EOF;
1208 static int extract_iso2709(struct filter_info *tinfo,
1209 struct filter_input *input,
1210 struct recExtractCtrl *p)
1216 if (p->stream->readf(p->stream, buf, 5) != 5)
1217 return RECCTRL_EXTRACT_EOF;
1218 while (*buf < '0' || *buf > '9')
1222 yaz_log(YLOG_WARN, "MARC: Skipping bad byte %d (0x%02X)",
1223 *buf & 0xff, *buf & 0xff);
1224 for (i = 0; i<4; i++)
1227 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1228 return RECCTRL_EXTRACT_EOF;
1230 record_length = atoi_n (buf, 5);
1231 if (record_length < 25)
1233 yaz_log (YLOG_WARN, "MARC record length < 25, is %d",
1235 return RECCTRL_EXTRACT_ERROR_GENERIC;
1237 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1238 if (read_bytes < record_length-5)
1240 yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
1241 return RECCTRL_EXTRACT_ERROR_GENERIC;
1243 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1244 if (r < record_length)
1246 yaz_log (YLOG_WARN, "Parsing of MARC record failed r=%d length=%d",
1248 return RECCTRL_EXTRACT_ERROR_GENERIC;
1254 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1255 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1256 xmlDocSetRootElement(rdoc, root_ptr);
1257 return convert_extract_doc(tinfo, input, p, rdoc);
1259 return RECCTRL_EXTRACT_OK;
1262 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1264 struct filter_info *tinfo = clientData;
1265 struct filter_input *input = tinfo->input_list;
1268 return RECCTRL_EXTRACT_ERROR_GENERIC;
1270 odr_reset(tinfo->odr_record);
1273 case DOM_INPUT_XMLREADER:
1274 if (input->u.xmlreader.split_level == 0)
1275 return extract_xml_full(tinfo, input, p);
1277 return extract_xml_split(tinfo, input, p);
1279 case DOM_INPUT_MARC:
1280 return extract_iso2709(tinfo, input, p);
1282 return RECCTRL_EXTRACT_ERROR_GENERIC;
1285 static int ioread_ret(void *context, char *buffer, int len)
1287 struct recRetrieveCtrl *p = context;
1288 return p->stream->readf(p->stream, buffer, len);
1291 static int ioclose_ret(void *context)
1296 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1298 /* const char *esn = zebra_xslt_ns; */
1299 const char *esn = 0;
1300 const char *params[32];
1301 struct filter_info *tinfo = clientData;
1303 struct filter_retrieve *retrieve;
1304 xsltStylesheetPtr last_xsp = 0;
1308 if (p->comp->which == Z_RecordComp_simple
1309 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1311 esn = p->comp->u.simple->u.generic;
1313 else if (p->comp->which == Z_RecordComp_complex
1314 && p->comp->u.complex->generic->elementSpec
1315 && p->comp->u.complex->generic->elementSpec->which ==
1316 Z_ElementSpec_elementSetName)
1318 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1321 retrieve = lookup_retrieve(tinfo, esn);
1325 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1330 set_param_int(params, "id", p->localno, p->odr);
1332 set_param_str(params, "filename", p->fname, p->odr);
1333 if (p->staticrank >= 0)
1334 set_param_int(params, "rank", p->staticrank, p->odr);
1337 set_param_str(params, "schema", esn, p->odr);
1340 set_param_str(params, "schema", retrieve->name, p->odr);
1341 else if (retrieve->identifier)
1342 set_param_str(params, "schema", retrieve->identifier, p->odr);
1344 set_param_str(params, "schema", "", p->odr);
1347 set_param_int(params, "score", p->score, p->odr);
1348 set_param_int(params, "size", p->recordSize, p->odr);
1350 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1353 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1356 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1360 /* retrieve conversion */
1361 perform_convert(tinfo, retrieve->convert, params, &doc, &last_xsp);
1364 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1366 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1372 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1374 xmlDocDumpMemory(doc, &buf_out, &len_out);
1376 p->output_format = VAL_TEXT_XML;
1377 p->rec_len = len_out;
1378 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1379 memcpy(p->rec_buf, buf_out, p->rec_len);
1382 else if (p->output_format == VAL_SUTRS)
1388 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1390 xmlDocDumpMemory(doc, &buf_out, &len_out);
1392 p->output_format = VAL_SUTRS;
1393 p->rec_len = len_out;
1394 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1395 memcpy(p->rec_buf, buf_out, p->rec_len);
1401 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1407 static struct recType filter_type = {
1418 #ifdef IDZEBRA_STATIC_DOM
1431 * indent-tabs-mode: nil
1433 * vim: shiftwidth=4 tabstop=8 expandtab