0670ab4c616543c75cdae2173b06610fc7ee0237
[idzebra-moved-to-github.git] / index / mod_dom.c
1 /* $Id: mod_dom.c,v 1.36 2007-04-16 21:54:37 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5    This file is part of the Zebra server.
6
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
10    version.
11
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
15    for more details.
16
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
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27
28 #include <yaz/diagbib1.h>
29 #include <yaz/tpath.h>
30 #include <yaz/snprintf.h>
31
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>
39
40 #if YAZ_HAVE_EXSLT
41 #include <libexslt/exslt.h>
42 #endif
43
44 #include <idzebra/util.h>
45 #include <idzebra/recctrl.h>
46 #include <yaz/oid_db.h>
47
48 /* DOM filter style indexing */
49 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
50 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
51
52 /* DOM filter style indexing */
53 #define ZEBRA_PI_NAME "zebra-2.0"
54 static const char *zebra_pi_name = ZEBRA_PI_NAME;
55
56
57
58 struct convert_s {
59     const char *stylesheet;
60     xsltStylesheetPtr stylesheet_xsp;
61     struct convert_s *next;
62 };
63
64 struct filter_extract {
65     const char *name;
66     struct convert_s *convert;
67 };
68
69 struct filter_store {
70     struct convert_s *convert;
71 };
72
73 struct filter_retrieve {
74     const char *name;
75     const char *identifier;
76     struct convert_s *convert;
77     struct filter_retrieve *next;
78 };
79
80 #define DOM_INPUT_XMLREADER 1
81 #define DOM_INPUT_MARC 2
82 struct filter_input {
83     const char *syntax;
84     const char *name;
85     struct convert_s *convert;
86     int type;
87     union {
88         struct {
89             xmlTextReaderPtr reader;
90             int split_level;
91         } xmlreader;
92         struct {
93             const char *input_charset;
94             yaz_marc_t handle;
95             yaz_iconv_t iconv;
96         } marc;
97     } u;
98     struct filter_input *next;
99 };
100   
101 struct filter_info {
102     char *fname;
103     char *full_name;
104     const char *profile_path;
105     ODR odr_record;
106     ODR odr_config;
107     xmlDocPtr doc_config;
108     struct filter_extract *extract;
109     struct filter_retrieve *retrieve_list;
110     struct filter_input *input_list;
111     struct filter_store *store;
112     int record_info_invoked;
113 };
114
115
116
117 #define XML_STRCMP(a,b)   strcmp((char*)a, b)
118 #define XML_STRLEN(a) strlen((char*)a)
119
120
121 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
122
123 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
124                     const char *fmt, ...)
125 #ifdef __GNUC__
126     __attribute__ ((format (printf, 4, 5)))
127 #endif
128     ;
129
130 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
131                     const char *fmt, ...)
132 {
133     va_list ap;
134     char buf[4096];
135
136     va_start(ap, fmt);
137     yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
138     if (ptr)
139     {
140         yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none", 
141                 xmlGetLineNo(ptr), buf);
142     }
143     else
144     {
145         yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
146     }
147     va_end(ap);
148 }
149
150
151 static void set_param_str(const char **params, const char *name,
152                           const char *value, ODR odr)
153 {
154     char *quoted = odr_malloc(odr, 3 + strlen(value));
155     sprintf(quoted, "'%s'", value);
156     while (*params)
157         params++;
158     params[0] = name;
159     params[1] = quoted;
160     params[2] = 0;
161 }
162
163 static void set_param_int(const char **params, const char *name,
164                           zint value, ODR odr)
165 {
166     char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
167     while (*params)
168         params++;
169     sprintf(quoted, "'" ZINT_FORMAT "'", value);
170     params[0] = name;
171     params[1] = quoted;
172     params[2] = 0;
173 }
174
175 static void *filter_init(Res res, RecType recType)
176 {
177     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
178     tinfo->fname = 0;
179     tinfo->full_name = 0;
180     tinfo->profile_path = 0;
181     tinfo->odr_record = odr_createmem(ODR_ENCODE);
182     tinfo->odr_config = odr_createmem(ODR_ENCODE);
183     tinfo->extract = 0;
184     tinfo->retrieve_list = 0;
185     tinfo->input_list = 0;
186     tinfo->store = 0;
187     tinfo->doc_config = 0;
188     tinfo->record_info_invoked = 0;
189
190 #if YAZ_HAVE_EXSLT
191     exsltRegisterAll(); 
192 #endif
193
194     return tinfo;
195 }
196
197 static int attr_content(struct _xmlAttr *attr, const char *name,
198                         const char **dst_content)
199 {
200     if (!XML_STRCMP(attr->name, name) && attr->children 
201         && attr->children->type == XML_TEXT_NODE)
202     {
203         *dst_content = (const char *)(attr->children->content);
204         return 1;
205     }
206     return 0;
207 }
208
209 static void destroy_xsp(struct convert_s *c)
210 {
211     while(c)
212     {
213         if (c->stylesheet_xsp)
214             xsltFreeStylesheet(c->stylesheet_xsp);
215         c = c->next;
216     }
217 }
218
219 static void destroy_dom(struct filter_info *tinfo)
220 {
221     if (tinfo->extract)
222     {
223         destroy_xsp(tinfo->extract->convert);
224         tinfo->extract = 0;
225     }
226     if (tinfo->store)
227     {
228         destroy_xsp(tinfo->store->convert);
229         tinfo->store = 0;
230     }
231     if (tinfo->input_list)
232     {
233         struct filter_input *i_ptr;
234         for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
235         {
236             switch(i_ptr->type)
237             {
238             case DOM_INPUT_XMLREADER:
239                 if (i_ptr->u.xmlreader.reader)
240                     xmlFreeTextReader(i_ptr->u.xmlreader.reader);
241                 break;
242             case DOM_INPUT_MARC:
243                 yaz_iconv_close(i_ptr->u.marc.iconv);
244                 yaz_marc_destroy(i_ptr->u.marc.handle);
245                 break;
246             }
247             destroy_xsp(i_ptr->convert);
248         }
249         tinfo->input_list = 0;
250     }
251     if (tinfo->retrieve_list)
252     {
253         struct filter_retrieve *r_ptr;
254         for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
255             destroy_xsp(r_ptr->convert);
256         tinfo->retrieve_list = 0;
257     }
258
259     if (tinfo->doc_config)
260     {
261         xmlFreeDoc(tinfo->doc_config);
262         tinfo->doc_config = 0;
263     }
264     odr_reset(tinfo->odr_config);
265 }
266
267 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
268                                struct convert_s **l)
269 {
270     *l = 0;
271     FOR_EACH_ELEMENT(ptr) {
272         if (!XML_STRCMP(ptr->name, "xslt"))
273         {
274             struct _xmlAttr *attr;
275             struct convert_s *p 
276                 = odr_malloc(tinfo->odr_config, sizeof(*p));
277             
278             p->next = 0;
279             p->stylesheet = 0;
280             p->stylesheet_xsp = 0;
281             
282             for (attr = ptr->properties; attr; attr = attr->next)
283                 if (attr_content(attr, "stylesheet", &p->stylesheet))
284                     ;
285                 else
286                 {
287                     dom_log(YLOG_WARN, tinfo, ptr,
288                             "bad attribute @%s", attr->name);
289                 }
290             if (p->stylesheet)
291             {
292                 char tmp_xslt_full_name[1024];
293                 if (!yaz_filepath_resolve(p->stylesheet, 
294                                           tinfo->profile_path,
295                                           NULL, 
296                                           tmp_xslt_full_name))
297                 {
298                     dom_log(YLOG_WARN, tinfo, 0,
299                             "stylesheet %s not found in "
300                             "path %s",
301                             p->stylesheet, 
302                             tinfo->profile_path);
303                     return ZEBRA_FAIL;
304                 }
305                 
306                 p->stylesheet_xsp
307                     = xsltParseStylesheetFile((const xmlChar*) 
308                                               tmp_xslt_full_name);
309                 if (!p->stylesheet_xsp)
310                 {
311                     dom_log(YLOG_WARN, tinfo, 0,
312                             "could not parse xslt stylesheet %s",
313                             tmp_xslt_full_name);
314                     return ZEBRA_FAIL;
315                 }
316                 }
317                 else
318                 {
319                     dom_log(YLOG_WARN, tinfo, ptr,
320                             "missing attribute 'stylesheet' ");
321                     return ZEBRA_FAIL;
322                 }
323                 *l = p;
324                 l = &p->next;
325         }
326         else
327         {
328             dom_log(YLOG_WARN, tinfo, ptr,
329                     "bad element '%s', expected <xslt>", ptr->name);
330             return ZEBRA_FAIL;
331         }
332     }
333     return ZEBRA_OK;
334 }
335
336 static ZEBRA_RES perform_convert(struct filter_info *tinfo, 
337                                  struct recExtractCtrl *extctr,
338                                  struct convert_s *convert,
339                                  const char **params,
340                                  xmlDocPtr *doc,
341                                  xsltStylesheetPtr *last_xsp)
342 {
343     for (; convert; convert = convert->next)
344     {
345         xmlChar *buf_out = 0;
346         int len_out = 0;
347         xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
348                                                 *doc, params);
349         if (last_xsp)
350             *last_xsp = convert->stylesheet_xsp;
351         
352         if (!res_doc)
353             break;
354
355         /* now saving into buffer and re-reading into DOM to avoid annoing
356            XSLT problem with thrown-out indentation text nodes */
357         xsltSaveResultToString(&buf_out, &len_out, res_doc,
358                                convert->stylesheet_xsp); 
359         xmlFreeDoc(res_doc);
360
361         xmlFreeDoc(*doc);
362
363         *doc = xmlParseMemory((const char *) buf_out, len_out);
364
365         /* writing debug info out */
366         if (extctr && extctr->flagShowRecords)
367             yaz_log(YLOG_LOG, "%s: XSLT %s\n %.*s", 
368                     tinfo->fname ? tinfo->fname : "(none)", 
369                     convert->stylesheet,
370                     len_out, buf_out);
371         
372         xmlFree(buf_out);
373     }
374     return ZEBRA_OK;
375 }
376
377 static struct filter_input *new_input(struct filter_info *tinfo, int type)
378 {
379     struct filter_input *p;
380     struct filter_input **np = &tinfo->input_list;
381     for (;*np; np = &(*np)->next)
382         ;
383     p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
384     p->next = 0;
385     p->syntax = 0;
386     p->name = 0;
387     p->convert = 0;
388     p->type = type;
389     return p;
390 }
391
392 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
393                              const char *syntax, const char *name)
394 {
395     FOR_EACH_ELEMENT(ptr) {
396         if (!XML_STRCMP(ptr->name, "marc"))
397         {
398             yaz_iconv_t iconv = 0;
399             const char *input_charset = "marc-8";
400             struct _xmlAttr *attr;
401             
402             for (attr = ptr->properties; attr; attr = attr->next)
403             {
404                 if (attr_content(attr, "inputcharset", &input_charset))
405                     ;
406                 else
407                 {
408                     dom_log(YLOG_WARN, tinfo, ptr,
409                             "bad attribute @%s, expected @inputcharset",
410                             attr->name);
411                 }
412             }
413             iconv = yaz_iconv_open("utf-8", input_charset);
414             if (!iconv)
415             {
416                 dom_log(YLOG_WARN, tinfo, ptr, 
417                         "unsupported @charset '%s'", input_charset);
418                 return ZEBRA_FAIL;
419             }
420             else
421             {
422                 struct filter_input *p 
423                     = new_input(tinfo, DOM_INPUT_MARC);
424                 p->u.marc.handle = yaz_marc_create();
425                 p->u.marc.iconv = iconv;
426                 
427                 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
428                 
429                 ptr = ptr->next;
430                 
431                 parse_convert(tinfo, ptr, &p->convert);
432             }
433             break;
434
435         }
436         else if (!XML_STRCMP(ptr->name, "xmlreader"))
437         {
438             struct filter_input *p 
439                 = new_input(tinfo, DOM_INPUT_XMLREADER);
440             struct _xmlAttr *attr;
441             const char *level_str = 0;
442
443             p->u.xmlreader.split_level = 0;
444             p->u.xmlreader.reader = 0;
445
446             for (attr = ptr->properties; attr; attr = attr->next)
447             {
448                 if (attr_content(attr, "level", &level_str))
449                     ;
450                 else
451                 {
452                     dom_log(YLOG_WARN, tinfo, ptr,
453                             "bad attribute @%s, expected @level",
454                             attr->name);
455                 }
456             }
457             if (level_str)
458                 p->u.xmlreader.split_level = atoi(level_str);
459                 
460             ptr = ptr->next;
461
462             parse_convert(tinfo, ptr, &p->convert);
463             break;
464         }
465         else
466         {
467             dom_log(YLOG_WARN, tinfo, ptr,
468                     "bad element <%s>, expected <marc>|<xmlreader>",
469                     ptr->name);
470             return ZEBRA_FAIL;
471         }
472     }
473     return ZEBRA_OK;
474 }
475
476 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
477 {
478     char tmp_full_name[1024];
479     xmlNodePtr ptr;
480     xmlDocPtr doc;
481
482     tinfo->fname = odr_strdup(tinfo->odr_config, fname);
483     
484     if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path, 
485                              NULL, tmp_full_name))
486         tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
487     else
488         tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
489     
490     yaz_log(YLOG_LOG, "%s dom filter: "
491             "loading config file %s", tinfo->fname, tinfo->full_name);
492
493     doc = xmlParseFile(tinfo->full_name);
494     if (!doc)
495     {
496         yaz_log(YLOG_WARN, "%s: dom filter: "
497                 "failed to parse config file %s",
498                 tinfo->fname, tinfo->full_name);
499         return ZEBRA_FAIL;
500     }
501     /* save because we store ptrs to the content */ 
502     tinfo->doc_config = doc;
503     
504     ptr = xmlDocGetRootElement(doc);
505     if (!ptr || ptr->type != XML_ELEMENT_NODE 
506         || XML_STRCMP(ptr->name, "dom"))
507     {
508         dom_log(YLOG_WARN, tinfo, ptr,
509                 "bad root element <%s>, expected root element <dom>", 
510                 ptr->name);  
511         return ZEBRA_FAIL;
512     }
513
514     ptr = ptr->children;
515     FOR_EACH_ELEMENT(ptr) {
516         if (!XML_STRCMP(ptr->name, "extract"))
517         {
518             /*
519               <extract name="index">
520               <xslt stylesheet="first.xsl"/>
521               <xslt stylesheet="second.xsl"/>
522               </extract>
523             */
524             struct _xmlAttr *attr;
525             struct filter_extract *f =
526                 odr_malloc(tinfo->odr_config, sizeof(*f));
527             
528             tinfo->extract = f;
529             f->name = 0;
530             f->convert = 0;
531             for (attr = ptr->properties; attr; attr = attr->next)
532             {
533                 if (attr_content(attr, "name", &f->name))
534                     ;
535                 else
536                 {
537                     dom_log(YLOG_WARN, tinfo, ptr,
538                             "bad attribute @%s, expected @name",
539                             attr->name);
540                 }
541             }
542             parse_convert(tinfo, ptr->children, &f->convert);
543         }
544         else if (!XML_STRCMP(ptr->name, "retrieve"))
545         {  
546             /* 
547                <retrieve name="F">
548                <xslt stylesheet="some.xsl"/>
549                <xslt stylesheet="some.xsl"/>
550                </retrieve>
551             */
552             struct _xmlAttr *attr;
553             struct filter_retrieve **fp = &tinfo->retrieve_list;
554             struct filter_retrieve *f =
555                 odr_malloc(tinfo->odr_config, sizeof(*f));
556             
557             while (*fp)
558                 fp = &(*fp)->next;
559
560             *fp = f;
561             f->name = 0;
562             f->identifier = 0;
563             f->convert = 0;
564             f->next = 0;
565
566             for (attr = ptr->properties; attr; attr = attr->next)
567             {
568                 if (attr_content(attr, "identifier", 
569                                  &f->identifier))
570                     ;
571                 else if (attr_content(attr, "name", &f->name))
572                     ;
573                 else
574                 {
575                     dom_log(YLOG_WARN, tinfo, ptr,
576                             "bad attribute @%s,  expected @identifier|@name",
577                             attr->name);
578                 }
579             }
580             parse_convert(tinfo, ptr->children, &f->convert);
581         }
582         else if (!XML_STRCMP(ptr->name, "store"))
583         {
584             /*
585               <store name="F">
586               <xslt stylesheet="some.xsl"/>
587               <xslt stylesheet="some.xsl"/>
588               </retrieve>
589             */
590             struct filter_store *f =
591                 odr_malloc(tinfo->odr_config, sizeof(*f));
592             
593             tinfo->store = f;
594             f->convert = 0;
595             parse_convert(tinfo, ptr->children, &f->convert);
596         }
597         else if (!XML_STRCMP(ptr->name, "input"))
598         {
599             /*
600               <input syntax="xml">
601               <xmlreader level="1"/>
602               </input>
603               <input syntax="usmarc">
604               <marc inputcharset="marc-8"/>
605               </input>
606             */
607             struct _xmlAttr *attr;
608             const char  *syntax = 0;
609             const char *name = 0;
610             for (attr = ptr->properties; attr; attr = attr->next)
611             {
612                 if (attr_content(attr, "syntax", &syntax))
613                     ;
614                 else if (attr_content(attr, "name", &name))
615                     ;
616                 else
617                 {
618                     dom_log(YLOG_WARN, tinfo, ptr,
619                             "bad attribute @%s,  expected @syntax|@name",
620                             attr->name);
621                 }
622             }
623             parse_input(tinfo, ptr->children, syntax, name);
624         }
625         else
626         {
627             dom_log(YLOG_WARN, tinfo, ptr,
628                     "bad element <%s>, "
629                     "expected <extract>|<input>|<retrieve>|<store>",
630                     ptr->name);
631             return ZEBRA_FAIL;
632         }
633     }
634     if (!tinfo->input_list)
635     {
636         struct filter_input *p 
637             = new_input(tinfo, DOM_INPUT_XMLREADER);
638         p->u.xmlreader.split_level = 0;
639         p->u.xmlreader.reader = 0;
640     }
641     return ZEBRA_OK;
642 }
643
644 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
645                                                const char *est)
646 {
647     struct filter_retrieve *f = tinfo->retrieve_list;
648
649     /* return first schema if no est is provided */
650     if (!est)
651         return f;
652     for (; f; f = f->next)
653     { 
654         /* find requested schema */
655         if (est) 
656         {    
657             if (f->identifier && !strcmp(f->identifier, est))
658                 return f;
659             if (f->name && !strcmp(f->name, est))
660                 return f;
661         } 
662     }
663     return 0;
664 }
665
666 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
667 {
668     struct filter_info *tinfo = clientData;
669     if (!args || !*args)
670     {
671         yaz_log(YLOG_WARN, "dom filter: need config file");
672         return ZEBRA_FAIL;
673     }
674
675     if (tinfo->fname && !strcmp(args, tinfo->fname))
676         return ZEBRA_OK;
677     
678     tinfo->profile_path = res_get(res, "profilePath");
679
680     destroy_dom(tinfo);
681     return parse_dom(tinfo, args);
682 }
683
684 static void filter_destroy(void *clientData)
685 {
686     struct filter_info *tinfo = clientData;
687     destroy_dom(tinfo);
688     odr_destroy(tinfo->odr_config);
689     odr_destroy(tinfo->odr_record);
690     xfree(tinfo);
691 }
692
693 static int ioread_ex(void *context, char *buffer, int len)
694 {
695     struct recExtractCtrl *p = context;
696     return p->stream->readf(p->stream, buffer, len);
697 }
698
699 static int ioclose_ex(void *context)
700 {
701     return 0;
702 }
703
704
705 /* DOM filter style indexing */
706 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
707                             const char **dst_content)
708 {
709     if (0 == XML_STRCMP(attr->name, name) && attr->children 
710         && attr->children->type == XML_TEXT_NODE)
711     {
712         *dst_content = (const char *) (attr->children->content);
713         return 1;
714     }
715     return 0;
716 }
717
718
719 /* DOM filter style indexing */
720 static void index_value_of(struct filter_info *tinfo, 
721                            struct recExtractCtrl *extctr,
722                            RecWord* recword, 
723                            xmlNodePtr node, 
724                            const char *index_p)
725 {
726     if (tinfo->record_info_invoked == 1)
727     {
728         xmlChar *text = xmlNodeGetContent(node);
729         size_t text_len = strlen((const char *)text);
730         
731         /* if there is no text, we do not need to proceed */
732         if (text_len)
733         {            
734             const char *look = index_p;
735             const char *bval;
736             const char *eval;
737
738             xmlChar index[256];
739             xmlChar type[256];
740
741             /* assingning text to be indexed */
742             recword->term_buf = (const char *)text;
743             recword->term_len = text_len;
744
745             /* parsing all index name/type pairs */
746             /* may not start with ' ' or ':' */
747             while (*look && ' ' != *look && ':' != *look)
748             {
749                 /* setting name and type to zero */
750                 *index = '\0';
751                 *type = '\0';
752     
753                 /* parsing one index name */
754                 bval = look;
755                 while (*look && ':' != *look && ' ' != *look)
756                 {
757                     look++;
758                 }
759                 eval = look;
760                 strncpy((char *)index, (const char *)bval, eval - bval);
761                 index[eval - bval] = '\0';
762     
763     
764                 /* parsing one index type, if existing */
765                 if (':' == *look)
766                 {
767                     look++;
768       
769                     bval = look;
770                     while (*look && ' ' != *look)
771                     {
772                         look++;
773                     }
774                     eval = look;
775                     strncpy((char *)type, (const char *)bval, eval - bval);
776                     type[eval - bval] = '\0';
777                 }
778
779                 /* actually indexing the text given */
780                 dom_log(YLOG_DEBUG, tinfo, 0, 
781                         "INDEX '%s:%s' '%s'", 
782                         index ? (const char *) index : "null",
783                         type ? (const char *) type : "null", 
784                         text ? (const char *) text : "null");
785
786                 recword->index_name = (const char *)index;
787                 if (type && *type)
788                     recword->index_type = *type;
789
790                 /* writing debug out */
791                 if (extctr->flagShowRecords)
792                     dom_log(YLOG_LOG, tinfo, 0, 
793                             "INDEX '%s:%s' '%s'", 
794                             index ? (const char *) index : "null",
795                             type ? (const char *) type : "null", 
796                             text ? (const char *) text : "null");
797                 
798                 /* actually indexing the text given */
799                 recword->index_name = (const char *)index;
800                 if (type && *type)
801                     recword->index_type = *type;
802                 (extctr->tokenAdd)(recword);
803
804                 /* eat whitespaces */
805                 if (*look && ' ' == *look)
806                 {
807                     look++;
808                 } 
809             }
810         }
811         xmlFree(text); 
812     }
813 }
814
815
816 /* DOM filter style indexing */
817 static void set_record_info(struct filter_info *tinfo, 
818                             struct recExtractCtrl *extctr, 
819                             xmlNodePtr node, 
820                             const char * id_p, 
821                             const char * rank_p, 
822                             const char * type_p)
823 {
824     /* writing debug info out */
825     if (extctr && extctr->flagShowRecords)
826         dom_log(YLOG_LOG, tinfo, node,
827                 "RECORD id=%s rank=%s type=%s", 
828                 id_p ? (const char *) id_p : "(null)",
829                 rank_p ? (const char *) rank_p : "(null)",
830                 type_p ? (const char *) type_p : "(null)");
831     
832
833     if (id_p && *id_p)
834         sscanf((const char *)id_p, "%255s", extctr->match_criteria);
835
836     if (rank_p && *rank_p)
837         extctr->staticrank = atozint((const char *)rank_p);
838
839     if (type_p && *type_p)
840     {
841         enum zebra_recctrl_action_t action = action_update;
842         if (!strcmp(type_p, "insert"))
843             action = action_insert;
844         else if (!strcmp(type_p, "delete"))
845             action = action_delete;
846         else if (!strcmp(type_p, "replace"))
847             action = action_replace;
848         else if (!strcmp(type_p, "update"))
849             action = action_update;
850         else
851             dom_log(YLOG_WARN, tinfo, node, "bad @type value: %s", type_p);
852         extctr->action = action;
853         yaz_log(YLOG_LOG, "In mod_dom.c: setting action to %d", action);
854     }
855
856     if (tinfo->record_info_invoked == 1)
857     {
858         /* warn about multiple only once */
859         dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
860     }
861     tinfo->record_info_invoked++;
862
863 }
864
865
866 /* DOM filter style indexing */
867 static void process_xml_element_zebra_node(struct filter_info *tinfo, 
868                                            struct recExtractCtrl *extctr, 
869                                            RecWord* recword, 
870                                            xmlNodePtr node)
871 {
872     if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
873         && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
874     {
875          if (0 == XML_STRCMP(node->name, "index"))
876          {
877             const char *index_p = 0;
878
879             struct _xmlAttr *attr;      
880             for (attr = node->properties; attr; attr = attr->next)
881             {
882                 if (attr_content_xml(attr, "name", &index_p))
883                 {
884                     index_value_of(tinfo, extctr, recword, node, index_p);
885                 }  
886                 else
887                 {
888                     dom_log(YLOG_WARN, tinfo, node,
889                             "bad attribute @%s, expected @name",
890                             attr->name);
891                 }
892             }
893         }
894         else if (0 == XML_STRCMP(node->name, "record"))
895         {
896             const char *id_p = 0;
897             const char *rank_p = 0;
898             const char *type_p = 0;
899
900             struct _xmlAttr *attr;
901             for (attr = node->properties; attr; attr = attr->next)
902             {
903                 if (attr_content_xml(attr, "id", &id_p))
904                     ;
905                 else if (attr_content_xml(attr, "rank", &rank_p))
906                     ;
907                 else if (attr_content_xml(attr, "type", &type_p))
908                     ;
909                 else
910                 {
911                     dom_log(YLOG_WARN, tinfo, node,
912                             "bad attribute @%s, expected @id|@rank|@type",
913                             attr->name);
914                 }
915             }
916             set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
917         } 
918         else
919         {
920             dom_log(YLOG_WARN, tinfo, node,
921                     "bad element <%s>,"
922                     " expected <record>|<index> in namespace '%s'",
923                     node->name, zebra_dom_ns);
924         }
925     }
926 }
927
928 static int attr_content_pi(const char **c_ptr, const char *name,
929                            char *value, size_t value_max)
930 {
931     size_t name_len = strlen(name);
932     const char *look = *c_ptr;
933     int ret = 0;
934
935     *value = '\0';
936     while (*look && ' ' == *look)
937         look++;
938     if (strlen(look) > name_len)
939     {
940         if (look[name_len] == '=' && !memcmp(look, name, name_len))
941         {
942             size_t i = 0;
943             look += name_len+1;
944             while (*look && ' ' != *look)
945             {
946                 if (i < value_max-1)
947                     value[i++] = *look;
948                 look++;
949             }
950             value[i] = '\0';
951             ret = 1;
952         }
953     }
954     while (*look && ' ' == *look)
955         look++;
956     *c_ptr = look;
957     return ret;
958 }
959
960 /* DOM filter style indexing */
961 static void process_xml_pi_node(struct filter_info *tinfo, 
962                                 struct recExtractCtrl *extctr, 
963                                 xmlNodePtr node,
964                                 const char **index_pp)
965 {
966     /* if right PI name, continue parsing PI */
967     if (0 == strcmp(zebra_pi_name, (const char *)node->name))
968     {
969         xmlChar *pi_p =  node->content;
970         const char *look = (const char *) node->content;
971     
972         /* parsing PI record instructions */
973         if (0 == strncmp((const char *)look, "record", 6))
974         {
975             char id[256];
976             char rank[256];
977             char type[256];
978             
979             *id = '\0';
980             *rank = '\0';
981             *type = '\0';
982             look += 6;
983             while (*look)
984                 if (attr_content_pi(&look, "id", id, sizeof(id)))
985                     ;
986                 else if (attr_content_pi(&look, "rank", rank, sizeof(rank)))
987                     ;
988                 else if (attr_content_pi(&look, "type", type, sizeof(type)))
989                 {
990                     dom_log(YLOG_WARN, tinfo, node,
991                             "content '%s', can not parse '%s'",
992                             pi_p, look);
993                     break;
994                 }
995             set_record_info(tinfo, extctr, node, id, rank, type);
996         } 
997         /* parsing index instruction */
998         else if (0 == strncmp((const char *)look, "index", 5))
999         {
1000             look += 5;
1001       
1002             /* eat whitespace */
1003             while (*look && ' ' == *look)
1004                 look++;
1005
1006             /* export index instructions to outside */
1007             *index_pp = look;
1008         } 
1009         else 
1010         {
1011             dom_log(YLOG_WARN, tinfo, node,
1012                     "content '%s', can not parse '%s'",
1013                     pi_p, look);
1014         }
1015     }
1016 }
1017
1018 /* DOM filter style indexing */
1019 static void process_xml_element_node(struct filter_info *tinfo, 
1020                                      struct recExtractCtrl *extctr, 
1021                                      RecWord* recword, 
1022                                      xmlNodePtr node)
1023 {
1024     /* remember indexing instruction from PI to next element node */
1025     const char *index_p = 0;
1026
1027     /* check if we are an element node in the special zebra namespace 
1028        and either set record data or index value-of node content*/
1029     process_xml_element_zebra_node(tinfo, extctr, recword, node);
1030   
1031     /* loop through kid nodes */
1032     for (node = node->children; node; node = node->next)
1033     {
1034         /* check and set PI record and index index instructions */
1035         if (node->type == XML_PI_NODE)
1036         {
1037             process_xml_pi_node(tinfo, extctr, node, &index_p);
1038         }
1039         else if (node->type == XML_ELEMENT_NODE)
1040         {
1041             /* if there was a PI index instruction before this element */
1042             if (index_p)
1043             {
1044                 index_value_of(tinfo, extctr, recword, node, index_p);
1045                 index_p = 0;
1046             }
1047             process_xml_element_node(tinfo, extctr, recword,node);
1048         }
1049         else
1050             continue;
1051     }
1052 }
1053
1054
1055 /* DOM filter style indexing */
1056 static void extract_dom_doc_node(struct filter_info *tinfo, 
1057                                  struct recExtractCtrl *extctr, 
1058                                  xmlDocPtr doc)
1059 {
1060     /* only need to do the initialization once, reuse recword for all terms */
1061     RecWord recword;
1062     (*extctr->init)(extctr, &recword);
1063
1064     process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1065 }
1066
1067
1068
1069
1070 static int convert_extract_doc(struct filter_info *tinfo, 
1071                                struct filter_input *input,
1072                                struct recExtractCtrl *p, 
1073                                xmlDocPtr doc)
1074
1075 {
1076     xmlChar *buf_out;
1077     int len_out;
1078     const char *params[10];
1079     xsltStylesheetPtr last_xsp = 0;
1080     xmlDocPtr store_doc = 0;
1081
1082     /* per default do not ingest record */
1083     tinfo->record_info_invoked = 0;
1084
1085     /* exit if empty document given */
1086     if (!doc)
1087         return RECCTRL_EXTRACT_SKIP;
1088
1089     /* we actuallu have a document which needs to be processed further */
1090     params[0] = 0;
1091     set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1092
1093     /* input conversion */
1094     perform_convert(tinfo, p, input->convert, params, &doc, 0);
1095
1096     if (tinfo->store)
1097     {
1098         /* store conversion */
1099         store_doc = xmlCopyDoc(doc, 1);
1100         perform_convert(tinfo, p, tinfo->store->convert,
1101                         params, &store_doc, &last_xsp);
1102     }
1103     
1104     /* saving either store doc or original doc in case no store doc exists */
1105     if (last_xsp)
1106         xsltSaveResultToString(&buf_out, &len_out, 
1107                                store_doc ? store_doc : doc, last_xsp);
1108     else
1109         xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1110
1111     (*p->setStoreData)(p, buf_out, len_out);
1112     xmlFree(buf_out);
1113
1114     if (store_doc)
1115         xmlFreeDoc(store_doc);
1116
1117     /* extract conversion */
1118     perform_convert(tinfo, p, tinfo->extract->convert, params, &doc, 0);
1119
1120
1121     /* finally, do the indexing */
1122     if (doc){
1123         extract_dom_doc_node(tinfo, p, doc);
1124         xmlFreeDoc(doc);
1125     }
1126     
1127     /* there was nothing to index, so there is no inserted/updated record */
1128     if (tinfo->record_info_invoked == 0)
1129         return RECCTRL_EXTRACT_SKIP;
1130
1131     return RECCTRL_EXTRACT_OK;
1132 }
1133
1134 static int extract_xml_split(struct filter_info *tinfo,
1135                              struct filter_input *input,
1136                              struct recExtractCtrl *p)
1137 {
1138     int ret;
1139
1140     if (p->first_record)
1141     {
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 */,
1146                                                    0 /* URL */, 
1147                                                    0 /* encoding */,
1148                                                    XML_PARSE_XINCLUDE
1149                                                    | XML_PARSE_NOENT
1150                                                    | XML_PARSE_NONET);
1151     }
1152     if (!input->u.xmlreader.reader)
1153         return RECCTRL_EXTRACT_ERROR_GENERIC;
1154
1155     ret = xmlTextReaderRead(input->u.xmlreader.reader);
1156     while (ret == 1)
1157     {
1158         int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1159         int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1160
1161         if (type == XML_READER_TYPE_ELEMENT && 
1162             input->u.xmlreader.split_level == depth)
1163         {
1164             xmlNodePtr ptr;
1165
1166             /* per default do not ingest record */
1167             tinfo->record_info_invoked = 0;
1168             
1169             ptr = xmlTextReaderExpand(input->u.xmlreader.reader);
1170             if (ptr)
1171                 {
1172                 /* we have a new document */
1173
1174                 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1175                 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1176                 
1177                 xmlDocSetRootElement(doc, ptr2);
1178                 
1179                 /* writing debug info out */
1180                 if (p->flagShowRecords)
1181                 {
1182                     xmlChar *buf_out = 0;
1183                     int len_out = 0;
1184                     xmlDocDumpMemory(doc, &buf_out, &len_out);
1185                     yaz_log(YLOG_LOG, "%s: XMLREADER level: %i\n%.*s", 
1186                             tinfo->fname ? tinfo->fname : "(none)",
1187                             depth, len_out, buf_out); 
1188                     xmlFree(buf_out);
1189                 }
1190                 
1191                 return convert_extract_doc(tinfo, input, p, doc);
1192             }
1193             else
1194             {
1195                 xmlFreeTextReader(input->u.xmlreader.reader);
1196                 input->u.xmlreader.reader = 0;
1197                 return RECCTRL_EXTRACT_ERROR_GENERIC;
1198             }
1199         }
1200         ret = xmlTextReaderRead(input->u.xmlreader.reader);
1201     }
1202     xmlFreeTextReader(input->u.xmlreader.reader);
1203     input->u.xmlreader.reader = 0;
1204     return RECCTRL_EXTRACT_EOF;
1205 }
1206
1207 static int extract_xml_full(struct filter_info *tinfo, 
1208                             struct filter_input *input,
1209                             struct recExtractCtrl *p)
1210 {
1211     if (p->first_record) /* only one record per stream */
1212     {
1213         xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, 
1214                                   p /* I/O handler */,
1215                                   0 /* URL */,
1216                                   0 /* encoding */,
1217                                   XML_PARSE_XINCLUDE
1218                                   | XML_PARSE_NOENT
1219                                   | XML_PARSE_NONET);
1220         if (!doc)
1221         {
1222             return RECCTRL_EXTRACT_ERROR_GENERIC;
1223         }
1224         return convert_extract_doc(tinfo, input, p, doc);
1225     }
1226     else
1227         return RECCTRL_EXTRACT_EOF;
1228 }
1229
1230 static int extract_iso2709(struct filter_info *tinfo,
1231                            struct filter_input *input,
1232                            struct recExtractCtrl *p)
1233 {
1234     char buf[100000];
1235     int record_length;
1236     int read_bytes, r;
1237
1238     if (p->stream->readf(p->stream, buf, 5) != 5)
1239         return RECCTRL_EXTRACT_EOF;
1240     while (*buf < '0' || *buf > '9')
1241     {
1242         int i;
1243
1244         dom_log(YLOG_WARN, tinfo, 0,
1245                 "MARC: Skipping bad byte %d (0x%02X)",
1246                 *buf & 0xff, *buf & 0xff);
1247         for (i = 0; i<4; i++)
1248             buf[i] = buf[i+1];
1249
1250         if (p->stream->readf(p->stream, buf+4, 1) != 1)
1251             return RECCTRL_EXTRACT_EOF;
1252     }
1253     record_length = atoi_n (buf, 5);
1254     if (record_length < 25)
1255     {
1256         dom_log(YLOG_WARN, tinfo, 0,
1257                 "MARC record length < 25, is %d",  record_length);
1258         return RECCTRL_EXTRACT_ERROR_GENERIC;
1259     }
1260     read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1261     if (read_bytes < record_length-5)
1262     {
1263         dom_log(YLOG_WARN, tinfo, 0,
1264                 "couldn't read whole MARC record");
1265         return RECCTRL_EXTRACT_ERROR_GENERIC;
1266     }
1267     r = yaz_marc_read_iso2709(input->u.marc.handle,  buf, record_length);
1268     if (r < record_length)
1269     {
1270         dom_log (YLOG_WARN, tinfo, 0,
1271                  "parsing of MARC record failed r=%d length=%d",
1272                  r, record_length);
1273         return RECCTRL_EXTRACT_ERROR_GENERIC;
1274     }
1275     else
1276     {
1277         xmlDocPtr rdoc;
1278         xmlNode *root_ptr;
1279         yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1280         rdoc = xmlNewDoc((const xmlChar*) "1.0");
1281         xmlDocSetRootElement(rdoc, root_ptr);
1282         return convert_extract_doc(tinfo, input, p, rdoc);        
1283     }
1284     return RECCTRL_EXTRACT_OK;
1285 }
1286
1287 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1288 {
1289     struct filter_info *tinfo = clientData;
1290     struct filter_input *input = tinfo->input_list;
1291
1292     if (!input)
1293         return RECCTRL_EXTRACT_ERROR_GENERIC;
1294     
1295     odr_reset(tinfo->odr_record);
1296     switch(input->type)
1297     {
1298     case DOM_INPUT_XMLREADER:
1299         if (input->u.xmlreader.split_level == 0)
1300             return extract_xml_full(tinfo, input, p);
1301         else
1302             return extract_xml_split(tinfo, input, p);
1303         break;
1304     case DOM_INPUT_MARC:
1305         return extract_iso2709(tinfo, input, p);
1306     }
1307     return RECCTRL_EXTRACT_ERROR_GENERIC;
1308 }
1309
1310 static int ioread_ret(void *context, char *buffer, int len)
1311 {
1312     struct recRetrieveCtrl *p = context;
1313     return p->stream->readf(p->stream, buffer, len);
1314 }
1315
1316 static int ioclose_ret(void *context)
1317 {
1318     return 0;
1319 }
1320
1321 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1322 {
1323     /* const char *esn = zebra_dom_ns; */
1324     const char *esn = 0;
1325     const char *params[32];
1326     struct filter_info *tinfo = clientData;
1327     xmlDocPtr doc;
1328     struct filter_retrieve *retrieve;
1329     xsltStylesheetPtr last_xsp = 0;
1330
1331     if (p->comp)
1332     {
1333         if (p->comp->which == Z_RecordComp_simple
1334             && p->comp->u.simple->which == Z_ElementSetNames_generic)
1335         {
1336             esn = p->comp->u.simple->u.generic;
1337         }
1338         else if (p->comp->which == Z_RecordComp_complex 
1339                  && p->comp->u.complex->generic->elementSpec
1340                  && p->comp->u.complex->generic->elementSpec->which ==
1341                  Z_ElementSpec_elementSetName)
1342         {
1343             esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1344         }
1345     }
1346     retrieve = lookup_retrieve(tinfo, esn);
1347     if (!retrieve)
1348     {
1349         p->diagnostic =
1350             YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1351         return 0;
1352     }
1353
1354     params[0] = 0;
1355     set_param_int(params, "id", p->localno, p->odr);
1356     if (p->fname)
1357         set_param_str(params, "filename", p->fname, p->odr);
1358     if (p->staticrank >= 0)
1359         set_param_int(params, "rank", p->staticrank, p->odr);
1360
1361     if (esn)
1362         set_param_str(params, "schema", esn, p->odr);
1363     else
1364         if (retrieve->name)
1365             set_param_str(params, "schema", retrieve->name, p->odr);
1366         else if (retrieve->identifier)
1367             set_param_str(params, "schema", retrieve->identifier, p->odr);
1368         else
1369             set_param_str(params, "schema", "", p->odr);
1370
1371     if (p->score >= 0)
1372         set_param_int(params, "score", p->score, p->odr);
1373     set_param_int(params, "size", p->recordSize, p->odr);
1374
1375     doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1376                     0 /* URL */,
1377                     0 /* encoding */,
1378                     XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
1379     if (!doc)
1380     {
1381         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1382         return 0;
1383     }
1384
1385     /* retrieve conversion */
1386     perform_convert(tinfo, 0, retrieve->convert, params, &doc, &last_xsp);
1387     if (!doc)
1388     {
1389         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1390     }
1391     else if (!p->input_format
1392              || !oid_oidcmp(p->input_format, yaz_oid_recsyn_xml))
1393     {
1394         xmlChar *buf_out;
1395         int len_out;
1396
1397         if (last_xsp)
1398             xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1399         else
1400             xmlDocDumpMemory(doc, &buf_out, &len_out);            
1401
1402         p->output_format = yaz_oid_recsyn_xml;
1403         p->rec_len = len_out;
1404         p->rec_buf = odr_malloc(p->odr, p->rec_len);
1405         memcpy(p->rec_buf, buf_out, p->rec_len);
1406         xmlFree(buf_out);
1407     }
1408     else if (!oid_oidcmp(p->output_format, yaz_oid_recsyn_sutrs))
1409     {
1410         xmlChar *buf_out;
1411         int len_out;
1412
1413         if (last_xsp)
1414             xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1415         else
1416             xmlDocDumpMemory(doc, &buf_out, &len_out);            
1417         
1418         p->output_format = yaz_oid_recsyn_sutrs;
1419         p->rec_len = len_out;
1420         p->rec_buf = odr_malloc(p->odr, p->rec_len);
1421         memcpy(p->rec_buf, buf_out, p->rec_len);
1422         
1423         xmlFree(buf_out);
1424     }
1425     else
1426     {
1427         p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1428     }
1429     xmlFreeDoc(doc);
1430     return 0;
1431 }
1432
1433 static struct recType filter_type = {
1434     0,
1435     "dom",
1436     filter_init,
1437     filter_config,
1438     filter_destroy,
1439     filter_extract,
1440     filter_retrieve
1441 };
1442
1443 RecType
1444 #ifdef IDZEBRA_STATIC_DOM
1445 idzebra_filter_dom
1446 #else
1447 idzebra_filter
1448 #endif
1449
1450 [] = {
1451     &filter_type,
1452     0,
1453 };
1454 /*
1455  * Local variables:
1456  * c-basic-offset: 4
1457  * indent-tabs-mode: nil
1458  * End:
1459  * vim: shiftwidth=4 tabstop=8 expandtab
1460  */
1461