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