Towards GPL
[idzebra-moved-to-github.git] / recctrl / recgrs.c
index 2bd0901..c41906f 100644 (file)
@@ -1,9 +1,26 @@
-/*
- * Copyright (C) 1994-2002, Index Data
- * All rights reserved.
- *
- * $Id: recgrs.c,v 1.50 2002-05-14 20:48:47 adam Exp $
- */
+/* $Id: recgrs.c,v 1.59 2002-08-02 19:26:56 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 
 #include <stdio.h>
 #include <assert.h>
@@ -105,6 +122,16 @@ static void grs_destroy(void *clientData)
     xfree (h);
 }
 
+/* use
+     1   start element (tag)
+     2   end element
+     3   start attr (and attr-exact)
+     4   end attr
+
+  1016   cdata
+  1015   attr data
+*/
+
 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
                          int level, RecWord *wrd, int use)
 {
@@ -146,20 +173,12 @@ static void index_xpath (data1_node *n, struct recExtractCtrl *p,
                 tag_path_full[flen++] = '/';
             }
             else if (nn->which == DATA1N_root)
-            {
-                size_t tlen = strlen(nn->u.root.type);
-                if (tlen + flen > (sizeof(tag_path_full)-2))
-                    return;
-                memcpy (tag_path_full + flen, nn->u.root.type, tlen);
-                flen += tlen;
-                tag_path_full[flen++] = '/';
                 break;
-            }
         }
         wrd->reg_type = '0';
         wrd->string = tag_path_full;
         wrd->length = flen;
-        wrd->attrSet = VAL_IDXPATH,
+        wrd->attrSet = VAL_IDXPATH;
         wrd->attrUse = use;
         if (p->flagShowRecords)
         {
@@ -172,9 +191,67 @@ static void index_xpath (data1_node *n, struct recExtractCtrl *p,
         }
         else
         {
-            (*p->tokenAdd)(wrd);
+            data1_xattr *xp;
+            (*p->tokenAdd)(wrd);   /* index element pag (AKA tag path) */
+            if (use == 1)
+            {
+                for (xp = n->u.tag.attributes; xp; xp = xp->next)
+                {
+                    char comb[512];
+                    /* attribute  (no value) */
+                    wrd->reg_type = '0';
+                    wrd->attrUse = 3;
+                    wrd->string = xp->name;
+                    wrd->length = strlen(xp->name);
+                    
+                    wrd->seqno--;
+                    (*p->tokenAdd)(wrd);
+
+                    if (xp->value &&
+                        strlen(xp->name) + strlen(xp->value) < sizeof(comb)-2)
+                    {
+                        /* attribute value exact */
+                        strcpy (comb, xp->name);
+                        strcat (comb, "=");
+                        strcat (comb, xp->value);
+                        
+                        wrd->attrUse = 3;
+                        wrd->reg_type = '0';
+                        wrd->string = comb;
+                        wrd->length = strlen(comb);
+                        wrd->seqno--;
+                        
+                        (*p->tokenAdd)(wrd);
+                    }
+                }                
+                for (xp = n->u.tag.attributes; xp; xp = xp->next)
+                {
+                    char attr_tag_path_full[1024];
+                    
+                    sprintf (attr_tag_path_full, "@%s/%.*s",
+                             xp->name, flen, tag_path_full);
+
+                    wrd->reg_type = '0';
+                    wrd->attrUse = 1;
+                    wrd->string = attr_tag_path_full;
+                    wrd->length = strlen(attr_tag_path_full);
+                    (*p->tokenAdd)(wrd);
+                    
+                    wrd->attrUse = 1015;
+                    wrd->reg_type = 'w';
+                    wrd->string = xp->value;
+                    wrd->length = strlen(xp->value);
+                    
+                    (*p->tokenAdd)(wrd);
+                    
+                    wrd->reg_type = '0';
+                    wrd->attrUse = 2;
+                    wrd->string = attr_tag_path_full;
+                    wrd->length = strlen(attr_tag_path_full);
+                    (*p->tokenAdd)(wrd);
+                }
+            }
         }
-        break;
     }
 }
 
@@ -366,6 +443,7 @@ int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
             (*p->schemaAdd)(p, oidtmp);
     }
     (*p->init)(p, &wrd);
+
     return dumpkeys(n, p, 0, &wrd);
 }
 
@@ -403,9 +481,14 @@ static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
         if ((oid_ent_to_oid (&oe, oidtmp)))
             (*p->schemaAdd)(p, oidtmp);
     }
+
+    /* ensure our data1 tree is UTF-8 */
+    data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
+
 #if 0
     data1_pr_tree (p->dh, n, stdout);
 #endif
+
     (*p->init)(p, &wrd);
     if (dumpkeys(n, p, 0, &wrd) < 0)
     {
@@ -502,9 +585,33 @@ static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
     }
 }
 
+static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
+                              NMEM mem)
+{
+    const char *idzebra_ns[7];
+
+    idzebra_ns[0] = "xmlns:idzebra";
+    idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
+    idzebra_ns[2] = 0;
+
+    data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
+
+    data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
+                           mem);
+    if (p->score != -1)
+        data1_mk_tag_data_int (p->dh, top, "idzebra:score",
+                               p->score, mem);
+    
+    data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
+                           mem);
+    if (p->fname)
+        data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
+                               p->fname, mem);
+}
+
 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
 {
-    data1_node *node = 0, *onode = 0;
+    data1_node *node = 0, *onode = 0, *top;
     data1_node *dnew;
     data1_maptab *map;
     int res, selected = 0;
@@ -513,6 +620,8 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
     char *tagname;
     struct grs_handlers *h = (struct grs_handlers *) clientData;
     int requested_schema = VAL_NONE;
+    data1_marctab *marctab;
+    int dummy;
     
     mem = nmem_create();
     gri.readf = p->readf;
@@ -537,11 +646,16 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
         nmem_destroy (mem);
        return 0;
     }
+    /* ensure our data1 tree is UTF-8 */
+    data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
+
 #if 0
     data1_pr_tree (p->dh, node, stdout);
 #endif
+    top = data1_get_root_tag (p->dh, node);
+
     logf (LOG_DEBUG, "grs_retrieve: size");
-    if ((dnew = data1_mk_tag_data_wd(p->dh, node, "size", mem)))
+    if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
     {
        dnew->u.data.what = DATA1I_text;
        dnew->u.data.data = dnew->lbuf;
@@ -551,7 +665,7 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
 
     tagname = res_get_def(p->res, "tagrank", "rank");
     if (strcmp(tagname, "0") && p->score >= 0 &&
-       (dnew = data1_mk_tag_data_wd(p->dh, node, tagname, mem)))
+       (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
     {
         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
        dnew->u.data.what = DATA1I_num;
@@ -562,11 +676,12 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
 
     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
     if (strcmp(tagname, "0") && p->localno > 0 &&
-        (dnew = data1_mk_tag_data_wd(p->dh, node, tagname, mem)))
+        (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
     {
         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
        dnew->u.data.what = DATA1I_text;
        dnew->u.data.data = dnew->lbuf;
+
        sprintf(dnew->u.data.data, "%d", p->localno);
        dnew->u.data.len = strlen(dnew->u.data.data);
     }
@@ -615,7 +730,7 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
      * the overlap of schema and formatting which is inherent in the MARC
      * family)
      */
-    logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
+    yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
     if (node->u.root.absyn)
         for (map = node->u.root.absyn->maptabs; map; map = map->next)
         {
@@ -631,7 +746,7 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
                 break;
             }
         }
-    logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
+    yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
     if (node->u.root.absyn &&
        node->u.root.absyn->reference != VAL_NONE &&
        p->input_format == VAL_GRS1)
@@ -691,10 +806,12 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
     switch (p->output_format = (p->input_format != VAL_NONE ?
                                p->input_format : VAL_SUTRS))
     {
-       data1_marctab *marctab;
-        int dummy;
-       
     case VAL_TEXT_XML:
+        add_idzebra_info (p, top, mem);
+
+        if (p->encoding)
+            data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
+
        if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
                                              &p->rec_len)))
            p->diagnostic = 238;
@@ -728,6 +845,8 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
            p->rec_len = (size_t) (-1);
        break;
     case VAL_SUTRS:
+        if (p->encoding)
+            data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
        if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
                                           &p->rec_len)))
            p->diagnostic = 238;
@@ -764,6 +883,8 @@ static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
            p->diagnostic = 238;
            break;
        }
+        if (p->encoding)
+            data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
        if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
                                        selected, &p->rec_len)))
            p->diagnostic = 238;