1 /* $Id: d1_doespec.c,v 1.2.2.3 2006-08-14 10:38:51 adam Exp $
2 Copyright (C) 1995-2005
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
29 #include <yaz/proto.h>
32 static int match_children(data1_handle dh, data1_node *n,
33 Z_Espec1 *e, int i, Z_ETagUnit **t,
37 static int match_children_wildpath(data1_handle dh, data1_node *n,
39 Z_ETagUnit **t, int num)
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.
49 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
50 oid_value set, int zclass, int type)
53 oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
54 oid_value defaultset = defaultsetent ? defaultsetent->value :
57 for (i = 0; i < var->num_triples; i++)
60 oid_getentbyoid(var->triples[i]->variantSetId);
61 oid_value curset = cursetent ? cursetent->value : defaultset;
64 *var->triples[i]->zclass == zclass &&
65 *var->triples[i]->type == type)
66 return var->triples[i];
71 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
72 int get_bytes, Z_Variant *vreq, int select_flag)
77 if (n->which == DATA1N_tag)
79 if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
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
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;
94 for (c = n->child; c; c = c->next)
96 if (c->which == DATA1N_tag && (!n->child ||
97 n->child->which != DATA1N_tag))
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;
104 mark_subtree(c, make_variantlist, no_data, get_bytes, vreq,
110 static void match_triple (data1_handle dh, Z_Variant *vreq,
112 oid_value var1, data1_node *n)
118 if (n->which != DATA1N_variant)
126 assert ((*c)->which == DATA1N_variant);
128 if ((*c)->u.variant.type->zclass->zclass == 4 &&
129 (*c)->u.variant.type->type == 1)
131 if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
132 (r->which == Z_Triple_internationalString))
134 const char *string_value =
135 r->value.internationalString;
136 if (strcmp ((*c)->u.variant.value, string_value))
142 data1_free_tree (dh, *c);
147 match_triple (dh, vreq, defsetval, var1, *c);
153 static int match_node_and_attr (data1_node *c, const char *spec)
163 if (c->u.tag.element)
164 tag = c->u.tag.element->tag;
167 sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
168 if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
171 if (*predicate == '\0')
173 else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
176 for (xa = c->u.tag.attributes; xa; xa = xa->next)
177 if (!strcmp(xa->name, attr) &&
178 !strcmp(xa->value, value))
182 else if (sscanf(predicate, "@%63s", attr) == 1)
185 for (xa = c->u.tag.attributes; xa; xa = xa->next)
186 if (!strcmp(xa->name, attr))
191 yaz_log(LOG_WARN, "Bad simpleelement component: '%s'", spec);
196 static int match_children_here (data1_handle dh, data1_node *n,
198 Z_ETagUnit **t, int num,
201 int counter = 0, hits = 0;
204 Z_Occurrences *occur;
206 for (c = n->child; c ; c = c->next)
210 if (c->which != DATA1N_tag)
213 if (tp->which == Z_ETagUnit_specificTag)
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 :
222 if (want->tagValue->which == Z_StringOrNumeric_numeric)
224 if (!tag || tag->which != DATA1T_numeric)
226 if (*want->tagValue->u.numeric != tag->value.numeric)
229 else if (want->tagValue->which == Z_StringOrNumeric_string)
231 const char *str_val = want->tagValue->u.string;
232 if (str_val[0] == '!')
237 if (tag && tag->which != DATA1T_string)
240 if (!match_node_and_attr(c, str_val))
243 if (data1_matchstr(str_val,
244 tag ? tag->value.string : c->u.tag.tag))
250 yaz_log(LOG_WARN, "Bad SpecificTag type: %d",
251 want->tagValue->which);
255 else if (tp->which == Z_ETagUnit_wildThing)
256 occur = tp->u.wildThing;
260 * Ok, so we have a matching tag. Are we within occurrences-range?
263 if (occur && occur->which == Z_Occurrences_last)
265 yaz_log(LOG_WARN, "Can't do occurrences=last (yet)");
268 if (!occur || occur->which == Z_Occurrences_all ||
269 (occur->which == Z_Occurrences_values && counter >=
270 *occur->u.values->start))
272 if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
274 c->u.tag.node_selected = select_flag;
276 * Consider the variant specification if this is a complete
281 int show_variantlist = 0;
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");
292 vreq = e->defaultVariantRequest;
299 * 6,5: meta-data requested, variant list.
301 if (find_triple(vreq, defsetval, var1, 6, 5))
302 show_variantlist = 1;
304 * 9,1: Miscellaneous, no data requested.
306 if (find_triple(vreq, defsetval, var1, 9, 1))
310 if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
311 if (r->which == Z_Triple_integer)
312 get_bytes = *r->value.integer;
314 if (!show_variantlist)
315 match_triple (dh, vreq, defsetval, var1, c);
317 mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
322 * have we looked at enough children?
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)))
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)
344 case Z_ETagUnit_wildThing:
345 case Z_ETagUnit_specificTag:
346 res = match_children_here(dh, n, e, i, t, num, select_flag);
348 case Z_ETagUnit_wildPath:
349 res = match_children_wildpath(dh, n, e, i, t, num); break;
356 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
360 n = data1_get_root_tag (dh, n);
361 if (n && n->which == DATA1N_tag)
362 n->u.tag.node_selected = 1;
364 for (i = 0; i < e->num_elements; i++)
366 if (e->elements[i]->which != Z_ERequest_simpleElement)
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 */ );