1 /* $Id: d1_doespec.c,v 1.12 2007-01-15 15:10:14 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
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
28 #include <yaz/proto.h>
29 #include <idzebra/data1.h>
31 static int match_children(data1_handle dh, data1_node *n,
32 Z_Espec1 *e, int i, Z_ETagUnit **t,
36 static int match_children_wildpath(data1_handle dh, data1_node *n,
38 Z_ETagUnit **t, int num)
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.
48 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
49 oid_value set, int zclass, int type)
52 oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
53 oid_value defaultset = defaultsetent ? defaultsetent->value :
56 for (i = 0; i < var->num_triples; i++)
59 oid_getentbyoid(var->triples[i]->variantSetId);
60 oid_value curset = cursetent ? cursetent->value : defaultset;
63 *var->triples[i]->zclass == zclass &&
64 *var->triples[i]->type == type)
65 return var->triples[i];
70 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
71 int get_bytes, Z_Variant *vreq, int select_flag)
76 if (n->which == DATA1N_tag)
78 if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
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
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;
93 for (c = n->child; c; c = c->next)
95 if (c->which == DATA1N_tag && (!n->child ||
96 n->child->which != DATA1N_tag))
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;
103 mark_subtree(c, make_variantlist, no_data, get_bytes, vreq,
109 static void match_triple (data1_handle dh, Z_Variant *vreq,
111 oid_value var1, data1_node *n)
117 if (n->which != DATA1N_variant)
125 assert ((*c)->which == DATA1N_variant);
127 if ((*c)->u.variant.type->zclass->zclass == 4 &&
128 (*c)->u.variant.type->type == 1)
130 if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
131 (r->which == Z_Triple_internationalString))
133 const char *string_value =
134 r->value.internationalString;
135 if (strcmp ((*c)->u.variant.value, string_value))
145 match_triple (dh, vreq, defsetval, var1, *c);
151 static int match_node_and_attr (data1_node *c, const char *spec)
161 if (c->u.tag.element)
162 tag = c->u.tag.element->tag;
165 sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
166 if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
169 if (*predicate == '\0')
171 else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
174 for (xa = c->u.tag.attributes; xa; xa = xa->next)
175 if (!strcmp(xa->name, attr) &&
176 !strcmp(xa->value, value))
180 else if (sscanf(predicate, "@%63s", attr) == 1)
183 for (xa = c->u.tag.attributes; xa; xa = xa->next)
184 if (!strcmp(xa->name, attr))
189 yaz_log(YLOG_WARN, "Bad simpleelement component: '%s'", spec);
194 static int match_children_here (data1_handle dh, data1_node *n,
196 Z_ETagUnit **t, int num,
199 int counter = 0, hits = 0;
202 Z_Occurrences *occur;
204 for (c = n->child; c ; c = c->next)
208 if (c->which != DATA1N_tag)
211 if (tp->which == Z_ETagUnit_specificTag)
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 :
220 if (want->tagValue->which == Z_StringOrNumeric_numeric)
222 if (!tag || tag->which != DATA1T_numeric)
224 if (*want->tagValue->u.numeric != tag->value.numeric)
227 else if (want->tagValue->which == Z_StringOrNumeric_string)
229 const char *str_val = want->tagValue->u.string;
230 if (str_val[0] == '!')
235 if (tag && tag->which != DATA1T_string)
238 if (!match_node_and_attr(c, str_val))
241 if (data1_matchstr(str_val,
242 tag ? tag->value.string : c->u.tag.tag))
248 yaz_log(YLOG_WARN, "Bad SpecificTag type: %d",
249 want->tagValue->which);
253 else if (tp->which == Z_ETagUnit_wildThing)
254 occur = tp->u.wildThing;
258 * Ok, so we have a matching tag. Are we within occurrences-range?
261 if (occur && occur->which == Z_Occurrences_last)
263 yaz_log(YLOG_WARN, "Can't do occurrences=last (yet)");
266 if (!occur || occur->which == Z_Occurrences_all ||
267 (occur->which == Z_Occurrences_values && counter >=
268 *occur->u.values->start))
270 if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
272 c->u.tag.node_selected = select_flag;
274 * Consider the variant specification if this is a complete
279 int show_variantlist = 0;
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");
290 vreq = e->defaultVariantRequest;
297 * 6,5: meta-data requested, variant list.
299 if (find_triple(vreq, defsetval, var1, 6, 5))
300 show_variantlist = 1;
302 * 9,1: Miscellaneous, no data requested.
304 if (find_triple(vreq, defsetval, var1, 9, 1))
308 if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
309 if (r->which == Z_Triple_integer)
310 get_bytes = *r->value.integer;
312 if (!show_variantlist)
313 match_triple (dh, vreq, defsetval, var1, c);
315 mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
320 * have we looked at enough children?
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)))
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)
342 case Z_ETagUnit_wildThing:
343 case Z_ETagUnit_specificTag:
344 res = match_children_here(dh, n, e, i, t, num, select_flag);
346 case Z_ETagUnit_wildPath:
347 res = match_children_wildpath(dh, n, e, i, t, num); break;
354 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
358 n = data1_get_root_tag (dh, n);
359 if (n && n->which == DATA1N_tag)
360 n->u.tag.node_selected = 1;
362 for (i = 0; i < e->num_elements; i++)
364 if (e->elements[i]->which != Z_ERequest_simpleElement)
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 */ );
376 * indent-tabs-mode: nil
378 * vim: shiftwidth=4 tabstop=8 expandtab