Fixed bug #594: Zebra pollutes XSLT output? For alvis and xslt
[idzebra-moved-to-github.git] / recctrl / alvis.c
index c25b5ce..ec22ed7 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: alvis.c,v 1.7 2005-08-22 14:39:47 adam Exp $
+/* $Id: alvis.c,v 1.15 2006-05-24 18:31:33 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -41,6 +41,7 @@ struct filter_schema {
     const char *stylesheet;
     struct filter_schema *next;
     const char *default_schema;
+    /* char default_schema; */
     const char *include_snippet;
     xsltStylesheetPtr stylesheet_xsp;
 };
@@ -207,10 +208,17 @@ static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
                attr_content(attr, "default", &schema->default_schema);
                attr_content(attr, "snippet", &schema->include_snippet);
            }
+            /*yaz_log(YLOG_LOG, "XSLT add %s %s %s", 
+              schema->name, schema->identifier, schema->stylesheet); */
+
+            /* find requested schema */
+
            if (schema->stylesheet)
                schema->stylesheet_xsp =
                    xsltParseStylesheetFile(
                        (const xmlChar*) schema->stylesheet);
+
+                
        }
        else if (!XML_STRCMP(ptr->name, "split"))
        {
@@ -234,30 +242,40 @@ static struct filter_schema *lookup_schema(struct filter_info *tinfo,
                                           const char *est)
 {
     struct filter_schema *schema;
+
     for (schema = tinfo->schemas; schema; schema = schema->next)
-    {
-       if (est)
-       {
+    { 
+        /* find requested schema */
+       if (est) 
+       {    
            if (schema->identifier && !strcmp(schema->identifier, est))
-               return schema;
+                return schema;
+            
            if (schema->name && !strcmp(schema->name, est))
                return schema;
-       }
-       if (schema->default_schema)
+       } 
+        /* or return default schema if defined */
+        else if (schema->default_schema)
            return schema;
     }
+
+    /* return first schema if no default schema defined */
+    if (tinfo->schemas)
+        return tinfo->schemas;
+    
     return 0;
 }
 
-static void filter_config(void *clientData, Res res, const char *args)
+static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
 {
     struct filter_info *tinfo = clientData;
     if (!args || !*args)
-       args = "xsltfilter.xml";
+       return ZEBRA_FAIL;
     if (tinfo->fname && !strcmp(args, tinfo->fname))
-       return;
+       return ZEBRA_OK;
     destroy_schemas(tinfo);
     create_schemas(tinfo, args);
+    return ZEBRA_OK;
 }
 
 static void filter_destroy(void *clientData)
@@ -546,8 +564,9 @@ static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
 
 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
 {
-    const char *esn = zebra_xslt_ns;
-    const char *params[20];
+    /* const char *esn = zebra_xslt_ns; */
+    const char *esn = 0;
+    const char *params[32];
     struct filter_info *tinfo = clientData;
     xmlDocPtr resDoc;
     xmlDocPtr doc;
@@ -556,13 +575,18 @@ static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
 
     if (p->comp)
     {
-       if (p->comp->which != Z_RecordComp_simple
-           || p->comp->u.simple->which != Z_ElementSetNames_generic)
+       if (p->comp->which == Z_RecordComp_simple
+           && p->comp->u.simple->which == Z_ElementSetNames_generic)
        {
-           p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
-           return 0;
+           esn = p->comp->u.simple->u.generic;
+       }
+       else if (p->comp->which == Z_RecordComp_complex 
+                && p->comp->u.complex->generic->elementSpec
+                && p->comp->u.complex->generic->elementSpec->which ==
+                Z_ElementSpec_elementSetName)
+       {
+           esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
        }
-       esn = p->comp->u.simple->u.generic;
     }
     schema = lookup_schema(tinfo, esn);
     if (!schema)
@@ -576,13 +600,25 @@ static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
        window_size = atoi(schema->include_snippet);
 
     params[0] = 0;
-    set_param_str(params, "schema", esn, p->odr);
+    set_param_int(params, "id", p->localno, p->odr);
     if (p->fname)
        set_param_str(params, "filename", p->fname, p->odr);
+    if (p->staticrank >= 0)
+       set_param_int(params, "rank", p->staticrank, p->odr);
+
+    if (esn)
+        set_param_str(params, "schema", esn, p->odr);
+    else
+        if (schema->name)
+            set_param_str(params, "schema", schema->name, p->odr);
+        else if (schema->identifier)
+            set_param_str(params, "schema", schema->identifier, p->odr);
+        else
+            set_param_str(params, "schema", "", p->odr);
+
     if (p->score >= 0)
        set_param_int(params, "score", p->score, p->odr);
     set_param_int(params, "size", p->recordSize, p->odr);
-    set_param_int(params, "id", p->localno, p->odr);
 
     if (window_size >= 0)
        set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
@@ -620,20 +656,23 @@ static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
     {
        xmlChar *buf_out;
        int len_out;
-       xmlDocDumpMemory(resDoc, &buf_out, &len_out);
+
+        xsltSaveResultToString(&buf_out, &len_out, resDoc,
+                               schema->stylesheet_xsp);        
 
        p->output_format = VAL_TEXT_XML;
        p->rec_len = len_out;
        p->rec_buf = odr_malloc(p->odr, p->rec_len);
        memcpy(p->rec_buf, buf_out, p->rec_len);
-       
        xmlFree(buf_out);
     }
     else if (p->output_format == VAL_SUTRS)
     {
        xmlChar *buf_out;
        int len_out;
-       xmlDocDumpMemory(resDoc, &buf_out, &len_out);
+
+        xsltSaveResultToString(&buf_out, &len_out, resDoc,
+                               schema->stylesheet_xsp);        
 
        p->output_format = VAL_SUTRS;
        p->rec_len = len_out;
@@ -671,3 +710,11 @@ idzebra_filter
     &filter_type,
     0,
 };
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+