Towards 1.3.42
[idzebra-moved-to-github.git] / data1 / d1_doespec.c
1 /* $Id: d1_doespec.c,v 1.2.2.3 2006-08-14 10:38:51 adam Exp $
2    Copyright (C) 1995-2005
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 <assert.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include <yaz/log.h>
28 #include <yaz/oid.h>
29 #include <yaz/proto.h>
30 #include <data1.h>
31
32 static int match_children(data1_handle dh, data1_node *n,
33                           Z_Espec1 *e, int i, Z_ETagUnit **t,
34                           int num,
35                           int select_flag);
36
37 static int match_children_wildpath(data1_handle dh, data1_node *n,
38                                    Z_Espec1 *e, int i,
39                                    Z_ETagUnit **t, int num)
40 {
41     return 0;
42 }
43
44 /*
45  * Locate a specific triple within a variant.
46  * set is the set to look for, universal set is the set that applies to a
47  * triple with an unknown set.
48  */
49 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
50     oid_value set, int zclass, int type)
51 {
52     int i;
53     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
54     oid_value defaultset = defaultsetent ? defaultsetent->value :
55         universalset;
56
57     for (i = 0; i < var->num_triples; i++)
58     {
59         oident *cursetent =
60             oid_getentbyoid(var->triples[i]->variantSetId);
61         oid_value curset = cursetent ? cursetent->value : defaultset;
62
63         if (set == curset &&
64             *var->triples[i]->zclass == zclass &&
65             *var->triples[i]->type == type)
66             return var->triples[i];
67     }
68     return 0;
69 }
70
71 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
72                          int get_bytes, Z_Variant *vreq, int select_flag)
73 {
74     data1_node *c;
75
76 #if 1
77     if (n->which == DATA1N_tag)
78 #else
79     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
80     /*
81      * This seems to cause multi-level elements to fall out when only a
82      * top-level elementRequest has been given... Problem is, I can't figure
83      * out what it was supposed to ACHIEVE.... delete when code has been
84      * verified.
85      */
86 #endif
87     {
88         n->u.tag.node_selected = select_flag;
89         n->u.tag.make_variantlist = make_variantlist;
90         n->u.tag.no_data_requested = no_data;
91         n->u.tag.get_bytes = get_bytes;
92     }
93
94     for (c = n->child; c; c = c->next)
95     {
96         if (c->which == DATA1N_tag && (!n->child ||
97             n->child->which != DATA1N_tag))
98         {
99             c->u.tag.node_selected = select_flag;
100             c->u.tag.make_variantlist = make_variantlist;
101             c->u.tag.no_data_requested = no_data;
102             c->u.tag.get_bytes = get_bytes;
103         }
104         mark_subtree(c, make_variantlist, no_data, get_bytes, vreq,
105                      select_flag);
106     }
107 }
108
109
110 static void match_triple (data1_handle dh, Z_Variant *vreq,
111                           oid_value defsetval,
112                           oid_value var1, data1_node *n)
113 {
114     data1_node **c;
115
116     if (!(n = n->child))
117         return;
118     if (n->which != DATA1N_variant)
119         return;
120     c = &n->child;
121     while (*c)
122     {
123         int remove_flag = 0;
124         Z_Triple *r;
125         
126         assert ((*c)->which == DATA1N_variant);
127         
128         if ((*c)->u.variant.type->zclass->zclass == 4 &&
129             (*c)->u.variant.type->type == 1)
130         {
131             if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
132                 (r->which == Z_Triple_internationalString))
133             {
134                 const char *string_value =
135                     r->value.internationalString;
136                 if (strcmp ((*c)->u.variant.value, string_value))
137                     remove_flag = 1;
138             }
139         }
140         if (remove_flag)
141         {
142             data1_free_tree (dh, *c);
143             *c = (*c)->next;
144         }
145         else
146         {
147             match_triple (dh, vreq, defsetval, var1, *c);
148             c = &(*c)->next;
149         }
150     }
151 }
152
153 static int match_node_and_attr (data1_node *c, const char *spec)
154 {
155
156     char predicate[64];
157     char elem[64];
158     char attr[64];
159     char value[64];
160     char dummy_ch;
161
162     data1_tag *tag = 0;
163     if (c->u.tag.element)
164         tag = c->u.tag.element->tag;
165     
166     *predicate = '\0';
167     sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
168     if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
169         return 0;
170
171     if (*predicate == '\0')
172         return 1;
173     else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
174     {
175         data1_xattr *xa;
176         for (xa = c->u.tag.attributes; xa; xa = xa->next)
177             if (!strcmp(xa->name, attr) &&
178                 !strcmp(xa->value, value))
179                 return 1;
180         return 0;
181     }
182     else if (sscanf(predicate, "@%63s", attr) == 1)
183     {
184         data1_xattr *xa;
185         for (xa = c->u.tag.attributes; xa; xa = xa->next)
186             if (!strcmp(xa->name, attr))
187                 return 1;
188     }
189     else
190     {
191         yaz_log(LOG_WARN, "Bad simpleelement component: '%s'", spec);
192     }
193     return 0;
194 }
195                                 
196 static int match_children_here (data1_handle dh, data1_node *n,
197                                 Z_Espec1 *e, int i,
198                                 Z_ETagUnit **t, int num,
199                                 int select_flag)
200 {
201     int counter = 0, hits = 0;
202     data1_node *c;
203     Z_ETagUnit *tp = *t;
204     Z_Occurrences *occur;
205
206     for (c = n->child; c ; c = c->next)
207     {
208         data1_tag *tag = 0;
209
210         if (c->which != DATA1N_tag)
211             continue;
212
213         if (tp->which == Z_ETagUnit_specificTag)
214         {
215             Z_SpecificTag *want = tp->u.specificTag;
216             occur = want->occurrences;
217             if (c->u.tag.element)
218                 tag = c->u.tag.element->tag;
219             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
220                 3))
221                 continue;
222             if (want->tagValue->which == Z_StringOrNumeric_numeric)
223             {
224                 if (!tag || tag->which != DATA1T_numeric)
225                     continue;
226                 if (*want->tagValue->u.numeric != tag->value.numeric)
227                     continue;
228             }
229             else if (want->tagValue->which == Z_StringOrNumeric_string)
230             {
231                 const char *str_val = want->tagValue->u.string;
232                 if (str_val[0] == '!')
233                 {
234                     str_val++;
235                     select_flag = 0;
236                 }
237                 if (tag && tag->which != DATA1T_string)
238                     continue;
239 #if 1
240                 if (!match_node_and_attr(c, str_val))
241                     continue;
242 #else      
243                 if (data1_matchstr(str_val,
244                                    tag ? tag->value.string : c->u.tag.tag))
245                     continue;
246 #endif
247             }
248             else
249             {
250                 yaz_log(LOG_WARN, "Bad SpecificTag type: %d",
251                         want->tagValue->which);
252                 continue;
253             }
254         }
255         else if (tp->which == Z_ETagUnit_wildThing)
256             occur = tp->u.wildThing;
257         else
258             continue;
259         /*
260          * Ok, so we have a matching tag. Are we within occurrences-range?
261          */
262         counter++;
263         if (occur && occur->which == Z_Occurrences_last)
264         {
265             yaz_log(LOG_WARN, "Can't do occurrences=last (yet)");
266             return 0;
267         }
268         if (!occur || occur->which == Z_Occurrences_all ||
269             (occur->which == Z_Occurrences_values && counter >=
270             *occur->u.values->start))
271         {
272             if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
273             {
274                 c->u.tag.node_selected = select_flag;
275                 /*
276                  * Consider the variant specification if this is a complete
277                  * match.
278                  */
279                 if (num == 1)
280                 {
281                     int show_variantlist = 0;
282                     int no_data = 0;
283                     int get_bytes = -1;
284
285                     Z_Variant *vreq =
286                         e->elements[i]->u.simpleElement->variantRequest;
287                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
288                     oid_value defsetval = defset ? defset->value : VAL_NONE;
289                     oid_value var1 = oid_getvalbyname("Variant-1");
290
291                     if (!vreq)
292                         vreq = e->defaultVariantRequest;
293
294                     if (vreq)
295                     {
296                         Z_Triple *r;
297
298                         /*
299                          * 6,5: meta-data requested, variant list.
300                          */
301                         if (find_triple(vreq, defsetval, var1, 6, 5))
302                             show_variantlist = 1;
303                         /*
304                          * 9,1: Miscellaneous, no data requested.
305                          */
306                         if (find_triple(vreq, defsetval, var1, 9, 1))
307                             no_data = 1;
308
309                         /* howmuch */
310                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
311                             if (r->which == Z_Triple_integer)
312                                 get_bytes = *r->value.integer;
313
314                         if (!show_variantlist)
315                             match_triple (dh, vreq, defsetval, var1, c);
316                     }
317                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
318                                  select_flag);
319                 }
320                 hits++;
321                 /*
322                  * have we looked at enough children?
323                  */
324                 if (!occur || (occur->which == Z_Occurrences_values &&
325                     (!occur->u.values->howMany ||
326                     counter - *occur->u.values->start >=
327                     *occur->u.values->howMany - 1)))
328                     return hits;
329             }
330         }
331     }
332     return hits;
333 }
334
335 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
336                           int i, Z_ETagUnit **t, int num, int select_flag)
337 {
338     int res;
339
340     if (!num)
341         return 1;
342     switch (t[0]->which)
343     {
344     case Z_ETagUnit_wildThing:
345     case Z_ETagUnit_specificTag:
346         res = match_children_here(dh, n, e, i, t, num, select_flag);
347         break;
348     case Z_ETagUnit_wildPath:
349         res = match_children_wildpath(dh, n, e, i, t, num); break;
350     default:
351         abort();
352     }
353     return res;
354 }
355
356 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
357 {
358     int i;
359
360     n = data1_get_root_tag (dh, n);
361     if (n && n->which == DATA1N_tag)
362         n->u.tag.node_selected = 1;
363     
364     for (i = 0; i < e->num_elements; i++)
365     {
366         if (e->elements[i]->which != Z_ERequest_simpleElement)
367             return 100;
368         match_children(dh, n, e, i,
369                        e->elements[i]->u.simpleElement->path->tags,
370                        e->elements[i]->u.simpleElement->path->num_tags,
371                        1 /* select (include) by default */ );
372     }
373     return 0;
374 }