Put local variables footer in all c, h files.
[idzebra-moved-to-github.git] / data1 / d1_doespec.c
1 /* $Id: d1_doespec.c,v 1.9 2006-05-10 08:13:18 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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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             data1_free_tree (dh, *c);
142             *c = (*c)->next;
143         }
144         else
145         {
146             match_triple (dh, vreq, defsetval, var1, *c);
147             c = &(*c)->next;
148         }
149     }
150 }
151
152 static int match_node_and_attr (data1_node *c, const char *spec)
153 {
154
155     char predicate[64];
156     char elem[64];
157     char attr[64];
158     char value[64];
159     char dummy_ch;
160
161     data1_tag *tag = 0;
162     if (c->u.tag.element)
163         tag = c->u.tag.element->tag;
164     
165     *predicate = '\0';
166     sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
167     if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
168         return 0;
169
170     if (*predicate == '\0')
171         return 1;
172     else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
173     {
174         data1_xattr *xa;
175         for (xa = c->u.tag.attributes; xa; xa = xa->next)
176             if (!strcmp(xa->name, attr) &&
177                 !strcmp(xa->value, value))
178                 return 1;
179         return 0;
180     }
181     else if (sscanf(predicate, "@%63s", attr) == 1)
182     {
183         data1_xattr *xa;
184         for (xa = c->u.tag.attributes; xa; xa = xa->next)
185             if (!strcmp(xa->name, attr))
186                 return 1;
187     }
188     else
189     {
190         yaz_log(YLOG_WARN, "Bad simpleelement component: '%s'", spec);
191     }
192     return 0;
193 }
194                                 
195 static int match_children_here (data1_handle dh, data1_node *n,
196                                 Z_Espec1 *e, int i,
197                                 Z_ETagUnit **t, int num,
198                                 int select_flag)
199 {
200     int counter = 0, hits = 0;
201     data1_node *c;
202     Z_ETagUnit *tp = *t;
203     Z_Occurrences *occur;
204
205     for (c = n->child; c ; c = c->next)
206     {
207         data1_tag *tag = 0;
208
209         if (c->which != DATA1N_tag)
210             continue;
211
212         if (tp->which == Z_ETagUnit_specificTag)
213         {
214             Z_SpecificTag *want = tp->u.specificTag;
215             occur = want->occurrences;
216             if (c->u.tag.element)
217                 tag = c->u.tag.element->tag;
218             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
219                 3))
220                 continue;
221             if (want->tagValue->which == Z_StringOrNumeric_numeric)
222             {
223                 if (!tag || tag->which != DATA1T_numeric)
224                     continue;
225                 if (*want->tagValue->u.numeric != tag->value.numeric)
226                     continue;
227             }
228             else if (want->tagValue->which == Z_StringOrNumeric_string)
229             {
230                 const char *str_val = want->tagValue->u.string;
231                 if (str_val[0] == '!')
232                 {
233                     str_val++;
234                     select_flag = 0;
235                 }
236                 if (tag && tag->which != DATA1T_string)
237                     continue;
238 #if 1
239                 if (!match_node_and_attr(c, str_val))
240                     continue;
241 #else      
242                 if (data1_matchstr(str_val,
243                                    tag ? tag->value.string : c->u.tag.tag))
244                     continue;
245 #endif
246             }
247             else
248             {
249                 yaz_log(YLOG_WARN, "Bad SpecificTag type: %d",
250                         want->tagValue->which);
251                 continue;
252             }
253         }
254         else if (tp->which == Z_ETagUnit_wildThing)
255             occur = tp->u.wildThing;
256         else
257             continue;
258         /*
259          * Ok, so we have a matching tag. Are we within occurrences-range?
260          */
261         counter++;
262         if (occur && occur->which == Z_Occurrences_last)
263         {
264             yaz_log(YLOG_WARN, "Can't do occurrences=last (yet)");
265             return 0;
266         }
267         if (!occur || occur->which == Z_Occurrences_all ||
268             (occur->which == Z_Occurrences_values && counter >=
269             *occur->u.values->start))
270         {
271             if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
272             {
273                 c->u.tag.node_selected = select_flag;
274                 /*
275                  * Consider the variant specification if this is a complete
276                  * match.
277                  */
278                 if (num == 1)
279                 {
280                     int show_variantlist = 0;
281                     int no_data = 0;
282                     int get_bytes = -1;
283
284                     Z_Variant *vreq =
285                         e->elements[i]->u.simpleElement->variantRequest;
286                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
287                     oid_value defsetval = defset ? defset->value : VAL_NONE;
288                     oid_value var1 = oid_getvalbyname("Variant-1");
289
290                     if (!vreq)
291                         vreq = e->defaultVariantRequest;
292
293                     if (vreq)
294                     {
295                         Z_Triple *r;
296
297                         /*
298                          * 6,5: meta-data requested, variant list.
299                          */
300                         if (find_triple(vreq, defsetval, var1, 6, 5))
301                             show_variantlist = 1;
302                         /*
303                          * 9,1: Miscellaneous, no data requested.
304                          */
305                         if (find_triple(vreq, defsetval, var1, 9, 1))
306                             no_data = 1;
307
308                         /* howmuch */
309                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
310                             if (r->which == Z_Triple_integer)
311                                 get_bytes = *r->value.integer;
312
313                         if (!show_variantlist)
314                             match_triple (dh, vreq, defsetval, var1, c);
315                     }
316                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
317                                  select_flag);
318                 }
319                 hits++;
320                 /*
321                  * have we looked at enough children?
322                  */
323                 if (!occur || (occur->which == Z_Occurrences_values &&
324                     (!occur->u.values->howMany ||
325                     counter - *occur->u.values->start >=
326                     *occur->u.values->howMany - 1)))
327                     return hits;
328             }
329         }
330     }
331     return hits;
332 }
333
334 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
335                           int i, Z_ETagUnit **t, int num, int select_flag)
336 {
337     int res;
338
339     if (!num)
340         return 1;
341     switch (t[0]->which)
342     {
343     case Z_ETagUnit_wildThing:
344     case Z_ETagUnit_specificTag:
345         res = match_children_here(dh, n, e, i, t, num, select_flag);
346         break;
347     case Z_ETagUnit_wildPath:
348         res = match_children_wildpath(dh, n, e, i, t, num); break;
349     default:
350         abort();
351     }
352     return res;
353 }
354
355 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
356 {
357     int i;
358
359     n = data1_get_root_tag (dh, n);
360     if (n && n->which == DATA1N_tag)
361         n->u.tag.node_selected = 1;
362     
363     for (i = 0; i < e->num_elements; i++)
364     {
365         if (e->elements[i]->which != Z_ERequest_simpleElement)
366             return 100;
367         match_children(dh, n, e, i,
368                        e->elements[i]->u.simpleElement->path->tags,
369                        e->elements[i]->u.simpleElement->path->num_tags,
370                        1 /* select (include) by default */ );
371     }
372     return 0;
373 }
374 /*
375  * Local variables:
376  * c-basic-offset: 4
377  * indent-tabs-mode: nil
378  * End:
379  * vim: shiftwidth=4 tabstop=8 expandtab
380  */
381