c9261296880b599c1bf7789d5f6eed03b261a86d
[idzebra-moved-to-github.git] / index / alvis.c
1 /* $Id: alvis.c,v 1.14 2007-03-07 14:18:35 marc 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
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
29
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>
37
38 #if YAZ_HAVE_EXSLT
39 #include <libexslt/exslt.h>
40 #endif
41
42 #include <idzebra/util.h>
43 #include <idzebra/recctrl.h>
44
45 struct filter_schema {
46     const char *name;
47     const char *identifier;
48     const char *stylesheet;
49     struct filter_schema *next;
50     const char *default_schema;
51     /* char default_schema; */
52     const char *include_snippet;
53     xsltStylesheetPtr stylesheet_xsp;
54 };
55
56 struct filter_info {
57     xmlDocPtr doc;
58     char *fname;
59     char *full_name;
60     const char *profile_path;
61     int split_level;
62     const char *split_path;
63     ODR odr;
64     struct filter_schema *schemas;
65     xmlTextReaderPtr reader;
66 };
67
68 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
69
70 #define XML_STRCMP(a,b)   strcmp((char*)a, b)
71 #define XML_STRLEN(a) strlen((char*)a)
72
73 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
74
75 static void set_param_xml(const char **params, const char *name,
76                           const char *value, ODR odr)
77 {
78     while (*params)
79         params++;
80     params[0] = name;
81     params[1] = value;
82     params[2] = 0;
83 }
84
85 static void set_param_str(const char **params, const char *name,
86                           const char *value, ODR odr)
87 {
88     char *quoted = odr_malloc(odr, 3 + strlen(value));
89     sprintf(quoted, "'%s'", value);
90     while (*params)
91         params++;
92     params[0] = name;
93     params[1] = quoted;
94     params[2] = 0;
95 }
96
97 static void set_param_int(const char **params, const char *name,
98                           zint value, ODR odr)
99 {
100     char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
101     while (*params)
102         params++;
103     sprintf(quoted, "'" ZINT_FORMAT "'", value);
104     params[0] = name;
105     params[1] = quoted;
106     params[2] = 0;
107 }
108
109 #define ENABLE_INPUT_CALLBACK 0
110
111 #if ENABLE_INPUT_CALLBACK
112 static int zebra_xmlInputMatchCallback (char const *filename)
113 {
114     yaz_log(YLOG_LOG, "match %s", filename);
115     return 0;
116 }
117
118 static void * zebra_xmlInputOpenCallback (char const *filename)
119 {
120     return 0;
121 }
122
123 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
124 {
125     return 0;
126 }
127
128 static int zebra_xmlInputCloseCallback (void * context)
129 {
130     return 0;
131 }
132 #endif
133
134 static void *filter_init(Res res, RecType recType)
135 {
136     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
137     tinfo->reader = 0;
138     tinfo->fname = 0;
139     tinfo->full_name = 0;
140     tinfo->profile_path = 0;
141     tinfo->split_level = 0;
142     tinfo->split_path = 0;
143     tinfo->odr = odr_createmem(ODR_ENCODE);
144     tinfo->doc = 0;
145     tinfo->schemas = 0;
146
147 #if YAZ_HAVE_EXSLT
148     exsltRegisterAll(); 
149 #endif
150
151 #if ENABLE_INPUT_CALLBACK
152     xmlRegisterDefaultInputCallbacks();
153     xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
154                               zebra_xmlInputOpenCallback,
155                               zebra_xmlInputReadCallback,
156                               zebra_xmlInputCloseCallback);
157 #endif
158     return tinfo;
159 }
160
161 static int attr_content(struct _xmlAttr *attr, const char *name,
162                         const char **dst_content)
163 {
164     if (!XML_STRCMP(attr->name, name) && attr->children 
165         && attr->children->type == XML_TEXT_NODE)
166     {
167         *dst_content = (const char *)(attr->children->content);
168         return 1;
169     }
170     return 0;
171 }
172
173 static void destroy_schemas(struct filter_info *tinfo)
174 {
175     struct filter_schema *schema = tinfo->schemas;
176     while (schema)
177     {
178         struct filter_schema *schema_next = schema->next;
179         if (schema->stylesheet_xsp)
180             xsltFreeStylesheet(schema->stylesheet_xsp);
181         xfree(schema);
182         schema = schema_next;
183     }
184     tinfo->schemas = 0;
185     xfree(tinfo->fname);
186     if (tinfo->doc)
187         xmlFreeDoc(tinfo->doc);    
188     tinfo->doc = 0;
189 }
190
191 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
192 {
193     char tmp_full_name[1024];
194     xmlNodePtr ptr;
195     tinfo->fname = xstrdup(fname);
196     
197     if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path, 
198                              NULL, tmp_full_name))
199         tinfo->full_name = xstrdup(tmp_full_name);
200     else
201         tinfo->full_name = xstrdup(tinfo->fname);
202     
203     yaz_log(YLOG_LOG, "alvis filter: loading config file %s", tinfo->full_name);
204     
205     tinfo->doc = xmlParseFile(tinfo->full_name);
206     
207     if (!tinfo->doc)
208     {
209         yaz_log(YLOG_WARN, "alvis filter: could not parse config file %s", 
210                 tinfo->full_name);
211         
212         return ZEBRA_FAIL;
213     }
214     
215     ptr = xmlDocGetRootElement(tinfo->doc);
216     if (!ptr || ptr->type != XML_ELEMENT_NODE 
217         || XML_STRCMP(ptr->name, "schemaInfo"))
218     {
219         yaz_log(YLOG_WARN, 
220                 "alvis filter:  config file %s :" 
221                 " expected root element <schemaInfo>", 
222                 tinfo->full_name);  
223         return ZEBRA_FAIL;
224     }
225
226     for (ptr = ptr->children; ptr; ptr = ptr->next)
227     {
228         if (ptr->type != XML_ELEMENT_NODE)
229             continue;
230         if (!XML_STRCMP(ptr->name, "schema"))
231         {  
232             struct _xmlAttr *attr;
233             struct filter_schema *schema = xmalloc(sizeof(*schema));
234             schema->name = 0;
235             schema->identifier = 0;
236             schema->stylesheet = 0;
237             schema->default_schema = 0;
238             schema->next = tinfo->schemas;
239             schema->stylesheet_xsp = 0;
240             schema->include_snippet = 0;
241             tinfo->schemas = schema;
242             for (attr = ptr->properties; attr; attr = attr->next)
243             {
244                 attr_content(attr, "identifier", &schema->identifier);
245                 attr_content(attr, "name", &schema->name);
246                 attr_content(attr, "stylesheet", &schema->stylesheet);
247                 attr_content(attr, "default", &schema->default_schema);
248                 attr_content(attr, "snippet", &schema->include_snippet);
249             }
250             /*yaz_log(YLOG_LOG, "XSLT add %s %s %s", 
251               schema->name, schema->identifier, schema->stylesheet); */
252
253             /* find requested schema */
254
255             if (schema->stylesheet)
256             {
257                 char tmp_xslt_full_name[1024];
258                 if (!yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path, 
259                                           NULL, tmp_xslt_full_name)) 
260                 {
261                     yaz_log(YLOG_WARN, 
262                             "alvis filter: stylesheet %s not found in path %s",
263                             schema->stylesheet, tinfo->profile_path);
264                     return ZEBRA_FAIL;
265                 }
266                 schema->stylesheet_xsp 
267                     = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
268                 if (!schema->stylesheet_xsp)
269                 {
270                     yaz_log(YLOG_WARN, 
271                             "alvis filter: could not parse xslt stylesheet %s", 
272                             tmp_xslt_full_name);
273                     return ZEBRA_FAIL;
274                 }
275             }
276         }
277         else if (!XML_STRCMP(ptr->name, "split"))
278         {
279             struct _xmlAttr *attr;
280             for (attr = ptr->properties; attr; attr = attr->next)
281             {
282                 const char *split_level_str = 0;
283                 attr_content(attr, "level", &split_level_str);
284                 tinfo->split_level = 
285                     split_level_str ? atoi(split_level_str) : 0;
286             }
287         }
288         else
289         {
290             yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
291             return ZEBRA_FAIL;
292         }
293     }
294     return ZEBRA_OK;
295 }
296
297 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
298                                            const char *est)
299 {
300     struct filter_schema *schema;
301
302     for (schema = tinfo->schemas; schema; schema = schema->next)
303     { 
304         /* find requested schema */
305         if (est) 
306         {    
307             if (schema->identifier && !strcmp(schema->identifier, est))
308                 return schema;
309             
310             if (schema->name && !strcmp(schema->name, est))
311                 return schema;
312         } 
313         /* or return default schema if defined */
314         else if (schema->default_schema)
315             return schema;
316     }
317
318     /* return first schema if no default schema defined */
319     if (tinfo->schemas)
320         return tinfo->schemas;
321     
322     return 0;
323 }
324
325 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
326 {
327     struct filter_info *tinfo = clientData;
328     if (!args || !*args)
329     {
330         yaz_log(YLOG_WARN, "alvis filter: need config file");
331         return ZEBRA_FAIL;
332     }
333
334     if (tinfo->fname && !strcmp(args, tinfo->fname))
335         return ZEBRA_OK;
336     
337     tinfo->profile_path = res_get(res, "profilePath");
338     yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
339
340     destroy_schemas(tinfo);
341     return create_schemas(tinfo, args);
342 }
343
344 static void filter_destroy(void *clientData)
345 {
346     struct filter_info *tinfo = clientData;
347     destroy_schemas(tinfo);
348     xfree(tinfo->full_name);
349     if (tinfo->reader)
350         xmlFreeTextReader(tinfo->reader);
351     odr_destroy(tinfo->odr);
352     xfree(tinfo);
353 }
354
355 static int ioread_ex(void *context, char *buffer, int len)
356 {
357     struct recExtractCtrl *p = context;
358     return p->stream->readf(p->stream, buffer, len);
359 }
360
361 static int ioclose_ex(void *context)
362 {
363     return 0;
364 }
365
366 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
367                         xmlNodePtr ptr, RecWord *recWord)
368 {
369     for(; ptr; ptr = ptr->next)
370     {
371         index_cdata(tinfo, ctrl, ptr->children, recWord);
372         if (ptr->type != XML_TEXT_NODE)
373             continue;
374         recWord->term_buf = (const char *)ptr->content;
375         recWord->term_len = XML_STRLEN(ptr->content);
376         (*ctrl->tokenAdd)(recWord);
377     }
378 }
379
380 static void index_node(struct filter_info *tinfo,  struct recExtractCtrl *ctrl,
381                        xmlNodePtr ptr, RecWord *recWord)
382 {
383     for(; ptr; ptr = ptr->next)
384     {
385         index_node(tinfo, ctrl, ptr->children, recWord);
386         if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
387             XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
388             continue;
389         if (!XML_STRCMP(ptr->name, "index"))
390         {
391             const char *name_str = 0;
392             const char *type_str = 0;
393             const char *xpath_str = 0;
394             struct _xmlAttr *attr;
395             for (attr = ptr->properties; attr; attr = attr->next)
396             {
397                 attr_content(attr, "name", &name_str);
398                 attr_content(attr, "xpath", &xpath_str);
399                 attr_content(attr, "type", &type_str);
400             }
401             if (name_str)
402             {
403                 int prev_type = recWord->index_type; /* save default type */
404
405                 if (type_str && *type_str)
406                     recWord->index_type = *type_str; /* type was given */
407                 recWord->index_name = name_str;
408                 index_cdata(tinfo, ctrl, ptr->children, recWord);
409
410                 recWord->index_type = prev_type;     /* restore it again */
411             }
412         }
413     }
414 }
415
416 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
417                          xmlNodePtr ptr, RecWord *recWord)
418 {
419     const char *type_str = "update";
420
421     if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
422         !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
423         && !XML_STRCMP(ptr->name, "record"))
424     {
425         const char *id_str = 0;
426         const char *rank_str = 0;
427         struct _xmlAttr *attr;
428         for (attr = ptr->properties; attr; attr = attr->next)
429         {
430             attr_content(attr, "type", &type_str);
431             attr_content(attr, "id", &id_str);
432             attr_content(attr, "rank", &rank_str);
433         }
434         if (id_str)
435             sscanf(id_str, "%255s", ctrl->match_criteria);
436
437         if (rank_str)
438             ctrl->staticrank = atozint(rank_str);
439         ptr = ptr->children;
440     }
441
442     if (!strcmp("update", type_str))
443         index_node(tinfo, ctrl, ptr, recWord);
444     else if (!strcmp("delete", type_str))
445          yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
446     else
447          yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'", 
448                  type_str);
449 }
450     
451 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
452                        xmlDocPtr doc)
453 {
454     RecWord recWord;
455     const char *params[10];
456     xmlChar *buf_out;
457     int len_out;
458
459     struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
460
461     params[0] = 0;
462     set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
463
464     (*p->init)(p, &recWord);
465
466     if (schema && schema->stylesheet_xsp)
467     {
468         xmlNodePtr root_ptr;
469         xmlDocPtr resDoc = 
470             xsltApplyStylesheet(schema->stylesheet_xsp,
471                                 doc, params);
472         if (p->flagShowRecords)
473         {
474             xmlDocDumpMemory(resDoc, &buf_out, &len_out);
475             fwrite(buf_out, len_out, 1, stdout);
476             xmlFree(buf_out);
477         }
478         root_ptr = xmlDocGetRootElement(resDoc);
479         if (root_ptr)
480             index_record(tinfo, p, root_ptr, &recWord);
481         else
482         {
483             yaz_log(YLOG_WARN, "No root for index XML record."
484                     " split_level=%d stylesheet=%s",
485                     tinfo->split_level, schema->stylesheet);
486         }
487         xmlFreeDoc(resDoc);
488     }
489     xmlDocDumpMemory(doc, &buf_out, &len_out);
490     if (p->flagShowRecords)
491         fwrite(buf_out, len_out, 1, stdout);
492     (*p->setStoreData)(p, buf_out, len_out);
493     xmlFree(buf_out);
494     
495     xmlFreeDoc(doc);
496     return RECCTRL_EXTRACT_OK;
497 }
498
499 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
500 {
501     int ret;
502
503     if (p->first_record)
504     {
505         if (tinfo->reader)
506             xmlFreeTextReader(tinfo->reader);
507         tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
508                                        p /* I/O handler */,
509                                        0 /* URL */, 
510                                        0 /* encoding */,
511                                        XML_PARSE_XINCLUDE
512                                        | XML_PARSE_NOENT
513                                        | XML_PARSE_NONET);
514     }
515     if (!tinfo->reader)
516         return RECCTRL_EXTRACT_ERROR_GENERIC;
517
518     ret = xmlTextReaderRead(tinfo->reader);
519     while (ret == 1)
520     {
521         int type = xmlTextReaderNodeType(tinfo->reader);
522         int depth = xmlTextReaderDepth(tinfo->reader);
523         if (type == XML_READER_TYPE_ELEMENT && tinfo->split_level == depth)
524         {
525             xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
526             if (ptr)
527             {
528                 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
529                 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
530                 
531                 xmlDocSetRootElement(doc, ptr2);
532                 
533                 return extract_doc(tinfo, p, doc);
534             }
535             else
536             {
537                 xmlFreeTextReader(tinfo->reader);
538                 tinfo->reader = 0;
539                 return RECCTRL_EXTRACT_ERROR_GENERIC;
540             }
541         }
542         ret = xmlTextReaderRead(tinfo->reader);
543     }
544     xmlFreeTextReader(tinfo->reader);
545     tinfo->reader = 0;
546     return RECCTRL_EXTRACT_EOF;
547 }
548
549 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
550 {
551     if (p->first_record) /* only one record per stream */
552     {
553        xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
554                                  0 /* URL */,
555                                  0 /* encoding */,
556                                  XML_PARSE_XINCLUDE
557                                  | XML_PARSE_NOENT
558                                  | XML_PARSE_NONET);
559        if (!doc)
560            return RECCTRL_EXTRACT_ERROR_GENERIC;
561        /* else {
562            xmlNodePtr root = xmlDocGetRootElement(doc);
563             if (!root)
564                 return RECCTRL_EXTRACT_ERROR_GENERIC;
565                 } */
566        
567        return extract_doc(tinfo, p, doc);
568     }
569     else
570        return RECCTRL_EXTRACT_EOF;
571 }
572
573 static int filter_extract(void *clientData, struct recExtractCtrl *p)
574 {
575     struct filter_info *tinfo = clientData;
576
577     odr_reset(tinfo->odr);
578     if (tinfo->split_level == 0)
579         return extract_full(tinfo, p);
580     else
581         return extract_split(tinfo, p);
582 }
583
584 static int ioread_ret(void *context, char *buffer, int len)
585 {
586     struct recRetrieveCtrl *p = context;
587     return p->stream->readf(p->stream, buffer, len);
588 }
589
590 static int ioclose_ret(void *context)
591 {
592     return 0;
593 }
594
595 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
596                                int window_size)
597 {
598     const char *xml_doc_str;
599     int ord = 0;
600     WRBUF wrbuf = wrbuf_alloc();
601     zebra_snippets *res = 
602         zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
603     zebra_snippet_word *w = zebra_snippets_list(res);
604
605     if (text_mode)
606         wrbuf_printf(wrbuf, "\'");
607     else
608         wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
609     for (; w; w = w->next)
610     {
611         if (ord == 0)
612             ord = w->ord;
613         else if (ord != w->ord)
614
615             break;
616         if (text_mode)
617             wrbuf_printf(wrbuf, "%s%s%s ", 
618                          w->match ? "*" : "",
619                          w->term,
620                          w->match ? "*" : "");
621         else
622         {
623             wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>", 
624                          w->ord, w->seqno,
625                          (w->match ? "match='1'" : ""));
626             wrbuf_xmlputs(wrbuf, w->term);
627             wrbuf_printf(wrbuf, "</term>\n");
628         }
629     }
630     if (text_mode)
631         wrbuf_printf(wrbuf, "\'");
632     else
633         wrbuf_printf(wrbuf, "</snippet>\n");
634
635     xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
636
637     zebra_snippets_destroy(res);
638     wrbuf_free(wrbuf, 1);
639     return xml_doc_str;
640 }
641
642 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
643 {
644     /* const char *esn = zebra_xslt_ns; */
645     const char *esn = 0;
646     const char *params[32];
647     struct filter_info *tinfo = clientData;
648     xmlDocPtr resDoc;
649     xmlDocPtr doc;
650     struct filter_schema *schema;
651     int window_size = -1;
652
653     if (p->comp)
654     {
655         if (p->comp->which == Z_RecordComp_simple
656             && p->comp->u.simple->which == Z_ElementSetNames_generic)
657         {
658             esn = p->comp->u.simple->u.generic;
659         }
660         else if (p->comp->which == Z_RecordComp_complex 
661                  && p->comp->u.complex->generic->elementSpec
662                  && p->comp->u.complex->generic->elementSpec->which ==
663                  Z_ElementSpec_elementSetName)
664         {
665             esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
666         }
667     }
668     schema = lookup_schema(tinfo, esn);
669     if (!schema)
670     {
671         p->diagnostic =
672             YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
673         return 0;
674     }
675
676     if (schema->include_snippet)
677         window_size = atoi(schema->include_snippet);
678
679     params[0] = 0;
680     set_param_int(params, "id", p->localno, p->odr);
681     if (p->fname)
682         set_param_str(params, "filename", p->fname, p->odr);
683     if (p->staticrank >= 0)
684         set_param_int(params, "rank", p->staticrank, p->odr);
685
686     if (esn)
687         set_param_str(params, "schema", esn, p->odr);
688     else
689         if (schema->name)
690             set_param_str(params, "schema", schema->name, p->odr);
691         else if (schema->identifier)
692             set_param_str(params, "schema", schema->identifier, p->odr);
693         else
694             set_param_str(params, "schema", "", p->odr);
695
696     if (p->score >= 0)
697         set_param_int(params, "score", p->score, p->odr);
698     set_param_int(params, "size", p->recordSize, p->odr);
699
700     if (window_size >= 0)
701         set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
702                       p->odr);
703     doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
704                     0 /* URL */,
705                     0 /* encoding */,
706                     XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
707     if (!doc)
708     {
709         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
710         return 0;
711     }
712
713     if (window_size >= 0)
714     {
715         xmlNodePtr node = xmlDocGetRootElement(doc);
716         const char *snippet_str = snippet_doc(p, 0, window_size);
717         xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
718         xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
719     }
720     if (!schema->stylesheet_xsp)
721         resDoc = doc;
722     else
723     {
724         resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
725                                      doc, params);
726         xmlFreeDoc(doc);
727     }
728     if (!resDoc)
729     {
730         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
731     }
732     else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
733     {
734         xmlChar *buf_out;
735         int len_out;
736
737         if (schema->stylesheet_xsp)
738             xsltSaveResultToString(&buf_out, &len_out, resDoc,
739                                    schema->stylesheet_xsp);     
740         else
741             xmlDocDumpMemory(resDoc, &buf_out, &len_out);            
742
743         p->output_format = VAL_TEXT_XML;
744         p->rec_len = len_out;
745         p->rec_buf = odr_malloc(p->odr, p->rec_len);
746         memcpy(p->rec_buf, buf_out, p->rec_len);
747         xmlFree(buf_out);
748     }
749     else if (p->output_format == VAL_SUTRS)
750     {
751         xmlChar *buf_out;
752         int len_out;
753
754         if (schema->stylesheet_xsp)
755             xsltSaveResultToString(&buf_out, &len_out, resDoc,
756                                    schema->stylesheet_xsp);
757         else
758             xmlDocDumpMemory(resDoc, &buf_out, &len_out);            
759
760         p->output_format = VAL_SUTRS;
761         p->rec_len = len_out;
762         p->rec_buf = odr_malloc(p->odr, p->rec_len);
763         memcpy(p->rec_buf, buf_out, p->rec_len);
764         
765         xmlFree(buf_out);
766     }
767     else
768     {
769         p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
770     }
771     xmlFreeDoc(resDoc);
772     return 0;
773 }
774
775 static struct recType filter_type = {
776     0,
777     "alvis",
778     filter_init,
779     filter_config,
780     filter_destroy,
781     filter_extract,
782     filter_retrieve
783 };
784
785 RecType
786 #ifdef IDZEBRA_STATIC_ALVIS
787 idzebra_filter_alvis
788 #else
789 idzebra_filter
790 #endif
791
792 [] = {
793     &filter_type,
794     0,
795 };
796 /*
797  * Local variables:
798  * c-basic-offset: 4
799  * indent-tabs-mode: nil
800  * End:
801  * vim: shiftwidth=4 tabstop=8 expandtab
802  */
803