1 /* $Id: d1_doespec.c,v 1.9 2006-05-10 08:13:18 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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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))
141 data1_free_tree (dh, *c);
146 match_triple (dh, vreq, defsetval, var1, *c);
152 static int match_node_and_attr (data1_node *c, const char *spec)
162 if (c->u.tag.element)
163 tag = c->u.tag.element->tag;
166 sscanf(spec, "%63[^[]%c%63[^]]", elem, &dummy_ch, predicate);
167 if (data1_matchstr(elem, tag ? tag->value.string : c->u.tag.tag))
170 if (*predicate == '\0')
172 else if (sscanf(predicate, "@%63[^=]=%63s", attr, value) == 2)
175 for (xa = c->u.tag.attributes; xa; xa = xa->next)
176 if (!strcmp(xa->name, attr) &&
177 !strcmp(xa->value, value))
181 else if (sscanf(predicate, "@%63s", attr) == 1)
184 for (xa = c->u.tag.attributes; xa; xa = xa->next)
185 if (!strcmp(xa->name, attr))
190 yaz_log(YLOG_WARN, "Bad simpleelement component: '%s'", spec);
195 static int match_children_here (data1_handle dh, data1_node *n,
197 Z_ETagUnit **t, int num,
200 int counter = 0, hits = 0;
203 Z_Occurrences *occur;
205 for (c = n->child; c ; c = c->next)
209 if (c->which != DATA1N_tag)
212 if (tp->which == Z_ETagUnit_specificTag)
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 :
221 if (want->tagValue->which == Z_StringOrNumeric_numeric)
223 if (!tag || tag->which != DATA1T_numeric)
225 if (*want->tagValue->u.numeric != tag->value.numeric)
228 else if (want->tagValue->which == Z_StringOrNumeric_string)
230 const char *str_val = want->tagValue->u.string;
231 if (str_val[0] == '!')
236 if (tag && tag->which != DATA1T_string)
239 if (!match_node_and_attr(c, str_val))
242 if (data1_matchstr(str_val,
243 tag ? tag->value.string : c->u.tag.tag))
249 yaz_log(YLOG_WARN, "Bad SpecificTag type: %d",
250 want->tagValue->which);
254 else if (tp->which == Z_ETagUnit_wildThing)
255 occur = tp->u.wildThing;
259 * Ok, so we have a matching tag. Are we within occurrences-range?
262 if (occur && occur->which == Z_Occurrences_last)
264 yaz_log(YLOG_WARN, "Can't do occurrences=last (yet)");
267 if (!occur || occur->which == Z_Occurrences_all ||
268 (occur->which == Z_Occurrences_values && counter >=
269 *occur->u.values->start))
271 if (match_children(dh, c, e, i, t + 1, num - 1, select_flag))
273 c->u.tag.node_selected = select_flag;
275 * Consider the variant specification if this is a complete
280 int show_variantlist = 0;
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");
291 vreq = e->defaultVariantRequest;
298 * 6,5: meta-data requested, variant list.
300 if (find_triple(vreq, defsetval, var1, 6, 5))
301 show_variantlist = 1;
303 * 9,1: Miscellaneous, no data requested.
305 if (find_triple(vreq, defsetval, var1, 9, 1))
309 if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
310 if (r->which == Z_Triple_integer)
311 get_bytes = *r->value.integer;
313 if (!show_variantlist)
314 match_triple (dh, vreq, defsetval, var1, c);
316 mark_subtree(c, show_variantlist, no_data, get_bytes, vreq,
321 * have we looked at enough children?
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)))
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)
343 case Z_ETagUnit_wildThing:
344 case Z_ETagUnit_specificTag:
345 res = match_children_here(dh, n, e, i, t, num, select_flag);
347 case Z_ETagUnit_wildPath:
348 res = match_children_wildpath(dh, n, e, i, t, num); break;
355 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
359 n = data1_get_root_tag (dh, n);
360 if (n && n->which == DATA1N_tag)
361 n->u.tag.node_selected = 1;
363 for (i = 0; i < e->num_elements; i++)
365 if (e->elements[i]->which != Z_ERequest_simpleElement)
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 */ );
377 * indent-tabs-mode: nil
379 * vim: shiftwidth=4 tabstop=8 expandtab