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