Added support for string components in simpleelement specs. Supported
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 8 Feb 2005 00:36:08 +0000 (00:36 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 8 Feb 2005 00:36:08 +0000 (00:36 +0000)
formats are:
 element[@attr=value]  _or_ element[@attr] _or_  element
If a simpleelement begins with ! the nodes are excluded (instead
of included).  Exclusion only makes sense if a larger node set is
included _first_.

data1/d1_doespec.c
data1/d1_espec.c

index 57d260d..066952a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: d1_doespec.c,v 1.7 2005-01-16 23:14:56 adam Exp $
+/* $Id: d1_doespec.c,v 1.8 2005-02-08 00:36:08 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -30,7 +30,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 static int match_children(data1_handle dh, data1_node *n,
                          Z_Espec1 *e, int i, Z_ETagUnit **t,
-    int num);
+                         int num,
+                         int select_flag);
 
 static int match_children_wildpath(data1_handle dh, data1_node *n,
                                   Z_Espec1 *e, int i,
@@ -67,7 +68,7 @@ static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
 }
 
 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
-    int get_bytes, Z_Variant *vreq)
+                        int get_bytes, Z_Variant *vreq, int select_flag)
 {
     data1_node *c;
 
@@ -83,7 +84,7 @@ static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
      */
 #endif
     {
-       n->u.tag.node_selected = 1;
+       n->u.tag.node_selected = select_flag;
        n->u.tag.make_variantlist = make_variantlist;
        n->u.tag.no_data_requested = no_data;
        n->u.tag.get_bytes = get_bytes;
@@ -94,12 +95,13 @@ static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
        if (c->which == DATA1N_tag && (!n->child ||
            n->child->which != DATA1N_tag))
        {
-           c->u.tag.node_selected = 1;
+           c->u.tag.node_selected = select_flag;
            c->u.tag.make_variantlist = make_variantlist;
            c->u.tag.no_data_requested = no_data;
            c->u.tag.get_bytes = get_bytes;
        }
-       mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
+       mark_subtree(c, make_variantlist, no_data, get_bytes, vreq,
+                    select_flag);
     }
 }
 
@@ -146,10 +148,54 @@ static void match_triple (data1_handle dh, Z_Variant *vreq,
        }
     }
 }
+
+static int match_node_and_attr (data1_node *c, const char *spec)
+{
+
+    char predicate[64];
+    char elem[64];
+    char attr[64];
+    char value[64];
+    char dummy_ch;
+
+    data1_tag *tag = 0;
+    if (c->u.tag.element)
+       tag = c->u.tag.element->tag;
+    
+    *predicate = '\0';
+    sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
+    if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
+       return 0;
+
+    if (*predicate == '\0')
+       return 1;
+    else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
+    {
+       data1_xattr *xa;
+       for (xa = c->u.tag.attributes; xa; xa = xa->next)
+           if (!strcmp(xa->name, attr) &&
+               !strcmp(xa->value, value))
+               return 1;
+       return 0;
+    }
+    else if (sscanf(predicate, "@%63s", attr) == 1)
+    {
+       data1_xattr *xa;
+       for (xa = c->u.tag.attributes; xa; xa = xa->next)
+           if (!strcmp(xa->name, attr))
+               return 1;
+    }
+    else
+    {
+       yaz_log(YLOG_WARN, "Bad simpleelement component: '%s'", spec);
+    }
+    return 0;
+}
                                
 static int match_children_here (data1_handle dh, data1_node *n,
                                Z_Espec1 *e, int i,
-                               Z_ETagUnit **t, int num)
+                               Z_ETagUnit **t, int num,
+                               int select_flag)
 {
     int counter = 0, hits = 0;
     data1_node *c;
@@ -179,19 +225,36 @@ static int match_children_here (data1_handle dh, data1_node *n,
                if (*want->tagValue->u.numeric != tag->value.numeric)
                    continue;
            }
-           else
+           else if (want->tagValue->which == Z_StringOrNumeric_string)
            {
-               assert(want->tagValue->which == Z_StringOrNumeric_string);
+               const char *str_val = want->tagValue->u.string;
+               if (str_val[0] == '!')
+               {
+                   str_val++;
+                   select_flag = 0;
+               }
                if (tag && tag->which != DATA1T_string)
                    continue;
-               if (data1_matchstr(want->tagValue->u.string,
-                   tag ? tag->value.string : c->u.tag.tag))
+#if 1
+               if (!match_node_and_attr(c, str_val))
+                   continue;
+#else     
+               if (data1_matchstr(str_val,
+                                  tag ? tag->value.string : c->u.tag.tag))
                    continue;
+#endif
+           }
+           else
+           {
+               yaz_log(YLOG_WARN, "Bad SpecificTag type: %d",
+                       want->tagValue->which);
+               continue;
            }
        }
-       else
+       else if (tp->which == Z_ETagUnit_wildThing)
            occur = tp->u.wildThing;
-
+       else
+           continue;
        /*
         * Ok, so we have a matching tag. Are we within occurrences-range?
         */
@@ -205,9 +268,9 @@ static int match_children_here (data1_handle dh, data1_node *n,
            (occur->which == Z_Occurrences_values && counter >=
            *occur->u.values->start))
        {
-           if (match_children(dh, c, e, i, t + 1, num - 1))
+           if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
            {
-               c->u.tag.node_selected = 1;
+               c->u.tag.node_selected = select_flag;
                /*
                 * Consider the variant specification if this is a complete
                 * match.
@@ -250,7 +313,8 @@ static int match_children_here (data1_handle dh, data1_node *n,
                        if (!show_variantlist)
                            match_triple (dh, vreq, defsetval, var1, c);
                    }
-                   mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
+                   mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
+                                select_flag);
                }
                hits++;
                /*
@@ -268,7 +332,7 @@ static int match_children_here (data1_handle dh, data1_node *n,
 }
 
 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
-                         int i, Z_ETagUnit **t, int num)
+                         int i, Z_ETagUnit **t, int num, int select_flag)
 {
     int res;
 
@@ -278,7 +342,8 @@ static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
     {
     case Z_ETagUnit_wildThing:
     case Z_ETagUnit_specificTag:
-        res = match_children_here(dh, n, e, i, t, num); break;
+        res = match_children_here(dh, n, e, i, t, num, select_flag);
+       break;
     case Z_ETagUnit_wildPath:
         res = match_children_wildpath(dh, n, e, i, t, num); break;
     default:
@@ -301,7 +366,8 @@ int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
             return 100;
         match_children(dh, n, e, i,
                        e->elements[i]->u.simpleElement->path->tags,
-                       e->elements[i]->u.simpleElement->path->num_tags);
+                       e->elements[i]->u.simpleElement->path->num_tags,
+                      1 /* select (include) by default */ );
     }
     return 0;
 }
index 9e6f3b6..3f24b48 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: d1_espec.c,v 1.7 2005-01-15 19:38:18 adam Exp $
+/* $Id: d1_espec.c,v 1.8 2005-02-08 00:36:08 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -61,10 +61,8 @@ static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
        }
        t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
        t->variantSetId = 0;
-       t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
-       *t->zclass = zclass;
-       t->type = (int *)nmem_malloc(nmem, sizeof(int));
-       *t->type = type;
+       t->zclass = nmem_intdup(nmem, zclass);
+       t->type = nmem_intdup(nmem, type);
        /*
         * This is wrong.. we gotta look up the correct type for the
         * variant, I guess... damn this stuff.
@@ -77,16 +75,12 @@ static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
        else if (d1_isdigit(*value))
        {
            t->which = Z_Triple_integer;
-           t->value.integer = (int *)
-               nmem_malloc(nmem, sizeof(*t->value.integer));
-           *t->value.integer = atoi(value);
+           t->value.integer = nmem_intdup(nmem, atoi(value));
        }
        else
        {
            t->which = Z_Triple_internationalString;
-           t->value.internationalString = (char *)
-               nmem_malloc(nmem, strlen(value)+1);
-           strcpy(t->value.internationalString, value);
+           t->value.internationalString = nmem_strdup(nmem, value);
        }
     }
     return r;
@@ -103,8 +97,7 @@ static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
        op->which = Z_Occurrences_values;
        op->u.values = (Z_OccurValues *)
            nmem_malloc(nmem, sizeof(Z_OccurValues));
-       op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
-       *op->u.values->start = 1;
+       op->u.values->start = nmem_intdup(nmem, 1);
        op->u.values->howMany = 0;
     }
     else if (!strcmp(occ, "all"))
@@ -129,13 +122,9 @@ static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
        }
        op->which = Z_Occurrences_values;
        op->u.values = ov;
-       ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
-       *ov->start = atoi(occ);
+       ov->start = nmem_intdup(nmem, atoi(occ));
        if ((p = strchr(occ, '+')))
-       {
-           ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
-           *ov->howMany = atoi(p + 1);
-       }
+           ov->howMany = nmem_intdup(nmem, atoi(p + 1));
        else
            ov->howMany = 0;
     }
@@ -176,30 +165,45 @@ static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
        {
            valp++;
            force_string = 1;
+           if (*valp && valp[strlen(valp)-1] == '\'')
+               *valp = '\0';
        }
        u->which = Z_ETagUnit_specificTag;
        u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
-       t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
-       *t->tagType = type;
+       t->tagType = nmem_intdup(nmem, type);
        t->tagValue = (Z_StringOrNumeric *)
            nmem_malloc(nmem, sizeof(*t->tagValue));
-       if (!force_string && (numval = atoi(valp)))
+       if (!force_string && isdigit(*(unsigned char *)valp))
        {
+           numval = atoi(valp);
            t->tagValue->which = Z_StringOrNumeric_numeric;
-           t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
-           *t->tagValue->u.numeric = numval;
+           t->tagValue->u.numeric = nmem_intdup(nmem, numval);
        }
        else
        {
            t->tagValue->which = Z_StringOrNumeric_string;
-           t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
-           strcpy(t->tagValue->u.string, valp);
+           t->tagValue->u.string = nmem_strdup(nmem, valp);
        }
        if (terms > 2) /* an occurrences-spec exists */
            t->occurrences = read_occurrences(occ, nmem, file, lineno);
        else
            t->occurrences = 0;
     }
+    else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
+    {
+       int numval;
+       Z_SpecificTag *t;
+       char *valp = value;
+
+       u->which = Z_ETagUnit_specificTag;
+       u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
+       t->tagType = nmem_intdup(nmem, 3);
+       t->tagValue = (Z_StringOrNumeric *)
+           nmem_malloc(nmem, sizeof(*t->tagValue));
+       t->tagValue->which = Z_StringOrNumeric_string;
+       t->tagValue->u.string = nmem_strdup(nmem, valp);
+       t->occurrences = read_occurrences("all", nmem, file, lineno);
+    }
     else
     {
        return 0;
@@ -250,9 +254,7 @@ Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
                (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
            for (i = 0; i < nnames; i++)
            {
-               res->elementSetNames[i] = (char *)
-                   nmem_malloc(nmem, strlen(argv[i+1])+1);
-               strcpy(res->elementSetNames[i], argv[i+1]);
+               res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
            }
            res->num_elementSetNames = nnames;
        }
@@ -280,8 +282,7 @@ Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
                        file, lineno, argv[0]);
                continue;
            }
-           res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
-           *res->defaultTagType = atoi(argv[1]);
+           res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
        }
        else if (!strcmp(argv[0], "defaultvariantrequest"))
        {