1 /* $Id: d1_doespec.c,v 1.3 2004-09-28 10:15:03 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
27 #include <yaz/proto.h>
28 #include <idzebra/data1.h>
30 static int match_children(data1_handle dh, data1_node *n,
31 Z_Espec1 *e, int i, Z_ETagUnit **t,
34 static int match_children_wildpath(data1_handle dh, data1_node *n,
36 Z_ETagUnit **t, int num)
42 * Locate a specific triple within a variant.
43 * set is the set to look for, universal set is the set that applies to a
44 * triple with an unknown set.
46 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
47 oid_value set, int zclass, int type)
50 oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
51 oid_value defaultset = defaultsetent ? defaultsetent->value :
54 for (i = 0; i < var->num_triples; i++)
57 oid_getentbyoid(var->triples[i]->variantSetId);
58 oid_value curset = cursetent ? cursetent->value : defaultset;
61 *var->triples[i]->zclass == zclass &&
62 *var->triples[i]->type == type)
63 return var->triples[i];
68 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
69 int get_bytes, Z_Variant *vreq)
74 if (n->which == DATA1N_tag)
76 if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
78 * This seems to cause multi-level elements to fall out when only a
79 * top-level elementRequest has been given... Problem is, I can't figure
80 * out what it was supposed to ACHIEVE.... delete when code has been
85 n->u.tag.node_selected = 1;
86 n->u.tag.make_variantlist = make_variantlist;
87 n->u.tag.no_data_requested = no_data;
88 n->u.tag.get_bytes = get_bytes;
91 for (c = n->child; c; c = c->next)
93 if (c->which == DATA1N_tag && (!n->child ||
94 n->child->which != DATA1N_tag))
96 c->u.tag.node_selected = 1;
97 c->u.tag.make_variantlist = make_variantlist;
98 c->u.tag.no_data_requested = no_data;
99 c->u.tag.get_bytes = get_bytes;
101 mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
106 static void match_triple (data1_handle dh, Z_Variant *vreq,
108 oid_value var1, data1_node *n)
114 if (n->which != DATA1N_variant)
122 assert ((*c)->which == DATA1N_variant);
124 if ((*c)->u.variant.type->zclass->zclass == 4 &&
125 (*c)->u.variant.type->type == 1)
127 if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
128 (r->which == Z_Triple_internationalString))
130 const char *string_value =
131 r->value.internationalString;
132 if (strcmp ((*c)->u.variant.value, string_value))
138 data1_free_tree (dh, *c);
143 match_triple (dh, vreq, defsetval, var1, *c);
149 static int match_children_here (data1_handle dh, data1_node *n,
151 Z_ETagUnit **t, int num)
153 int counter = 0, hits = 0;
156 Z_Occurrences *occur;
158 for (c = n->child; c ; c = c->next)
162 if (c->which != DATA1N_tag)
165 if (tp->which == Z_ETagUnit_specificTag)
167 Z_SpecificTag *want = tp->u.specificTag;
168 occur = want->occurrences;
169 if (c->u.tag.element)
170 tag = c->u.tag.element->tag;
171 if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
174 if (want->tagValue->which == Z_StringOrNumeric_numeric)
176 if (!tag || tag->which != DATA1T_numeric)
178 if (*want->tagValue->u.numeric != tag->value.numeric)
183 assert(want->tagValue->which == Z_StringOrNumeric_string);
184 if (tag && tag->which != DATA1T_string)
186 if (data1_matchstr(want->tagValue->u.string,
187 tag ? tag->value.string : c->u.tag.tag))
192 occur = tp->u.wildThing;
195 * Ok, so we have a matching tag. Are we within occurrences-range?
198 if (occur && occur->which == Z_Occurrences_last)
200 yaz_log(LOG_WARN, "Can't do occurrences=last (yet)");
203 if (!occur || occur->which == Z_Occurrences_all ||
204 (occur->which == Z_Occurrences_values && counter >=
205 *occur->u.values->start))
207 if (match_children(dh, c, e, i, t + 1, num - 1))
209 c->u.tag.node_selected = 1;
211 * Consider the variant specification if this is a complete
216 int show_variantlist = 0;
221 e->elements[i]->u.simpleElement->variantRequest;
222 oident *defset = oid_getentbyoid(e->defaultVariantSetId);
223 oid_value defsetval = defset ? defset->value : VAL_NONE;
224 oid_value var1 = oid_getvalbyname("Variant-1");
227 vreq = e->defaultVariantRequest;
234 * 6,5: meta-data requested, variant list.
236 if (find_triple(vreq, defsetval, var1, 6, 5))
237 show_variantlist = 1;
239 * 9,1: Miscellaneous, no data requested.
241 if (find_triple(vreq, defsetval, var1, 9, 1))
245 if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
246 if (r->which == Z_Triple_integer)
247 get_bytes = *r->value.integer;
249 if (!show_variantlist)
250 match_triple (dh, vreq, defsetval, var1, c);
252 mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
256 * have we looked at enough children?
258 if (!occur || (occur->which == Z_Occurrences_values &&
259 (!occur->u.values->howMany ||
260 counter - *occur->u.values->start >=
261 *occur->u.values->howMany - 1)))
269 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
270 int i, Z_ETagUnit **t, int num)
278 case Z_ETagUnit_wildThing:
279 case Z_ETagUnit_specificTag:
280 res = match_children_here(dh, n, e, i, t, num); break;
281 case Z_ETagUnit_wildPath:
282 res = match_children_wildpath(dh, n, e, i, t, num); break;
289 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
293 n = data1_get_root_tag (dh, n);
294 if (n && n->which == DATA1N_tag)
295 n->u.tag.node_selected = 1;
297 for (i = 0; i < e->num_elements; i++)
299 if (e->elements[i]->which != Z_ERequest_simpleElement)
301 match_children(dh, n, e, i,
302 e->elements[i]->u.simpleElement->path->tags,
303 e->elements[i]->u.simpleElement->path->num_tags);