1 /* $Id: d1_doespec.c,v 1.7 2005-01-16 23:14:56 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,
35 static int match_children_wildpath(data1_handle dh, data1_node *n,
37 Z_ETagUnit **t, int num)
43 * Locate a specific triple within a variant.
44 * set is the set to look for, universal set is the set that applies to a
45 * triple with an unknown set.
47 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
48 oid_value set, int zclass, int type)
51 oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
52 oid_value defaultset = defaultsetent ? defaultsetent->value :
55 for (i = 0; i < var->num_triples; i++)
58 oid_getentbyoid(var->triples[i]->variantSetId);
59 oid_value curset = cursetent ? cursetent->value : defaultset;
62 *var->triples[i]->zclass == zclass &&
63 *var->triples[i]->type == type)
64 return var->triples[i];
69 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
70 int get_bytes, Z_Variant *vreq)
75 if (n->which == DATA1N_tag)
77 if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
79 * This seems to cause multi-level elements to fall out when only a
80 * top-level elementRequest has been given... Problem is, I can't figure
81 * out what it was supposed to ACHIEVE.... delete when code has been
86 n->u.tag.node_selected = 1;
87 n->u.tag.make_variantlist = make_variantlist;
88 n->u.tag.no_data_requested = no_data;
89 n->u.tag.get_bytes = get_bytes;
92 for (c = n->child; c; c = c->next)
94 if (c->which == DATA1N_tag && (!n->child ||
95 n->child->which != DATA1N_tag))
97 c->u.tag.node_selected = 1;
98 c->u.tag.make_variantlist = make_variantlist;
99 c->u.tag.no_data_requested = no_data;
100 c->u.tag.get_bytes = get_bytes;
102 mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
107 static void match_triple (data1_handle dh, Z_Variant *vreq,
109 oid_value var1, data1_node *n)
115 if (n->which != DATA1N_variant)
123 assert ((*c)->which == DATA1N_variant);
125 if ((*c)->u.variant.type->zclass->zclass == 4 &&
126 (*c)->u.variant.type->type == 1)
128 if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
129 (r->which == Z_Triple_internationalString))
131 const char *string_value =
132 r->value.internationalString;
133 if (strcmp ((*c)->u.variant.value, string_value))
139 data1_free_tree (dh, *c);
144 match_triple (dh, vreq, defsetval, var1, *c);
150 static int match_children_here (data1_handle dh, data1_node *n,
152 Z_ETagUnit **t, int num)
154 int counter = 0, hits = 0;
157 Z_Occurrences *occur;
159 for (c = n->child; c ; c = c->next)
163 if (c->which != DATA1N_tag)
166 if (tp->which == Z_ETagUnit_specificTag)
168 Z_SpecificTag *want = tp->u.specificTag;
169 occur = want->occurrences;
170 if (c->u.tag.element)
171 tag = c->u.tag.element->tag;
172 if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
175 if (want->tagValue->which == Z_StringOrNumeric_numeric)
177 if (!tag || tag->which != DATA1T_numeric)
179 if (*want->tagValue->u.numeric != tag->value.numeric)
184 assert(want->tagValue->which == Z_StringOrNumeric_string);
185 if (tag && tag->which != DATA1T_string)
187 if (data1_matchstr(want->tagValue->u.string,
188 tag ? tag->value.string : c->u.tag.tag))
193 occur = tp->u.wildThing;
196 * Ok, so we have a matching tag. Are we within occurrences-range?
199 if (occur && occur->which == Z_Occurrences_last)
201 yaz_log(YLOG_WARN, "Can't do occurrences=last (yet)");
204 if (!occur || occur->which == Z_Occurrences_all ||
205 (occur->which == Z_Occurrences_values && counter >=
206 *occur->u.values->start))
208 if (match_children(dh, c, e, i, t + 1, num - 1))
210 c->u.tag.node_selected = 1;
212 * Consider the variant specification if this is a complete
217 int show_variantlist = 0;
222 e->elements[i]->u.simpleElement->variantRequest;
223 oident *defset = oid_getentbyoid(e->defaultVariantSetId);
224 oid_value defsetval = defset ? defset->value : VAL_NONE;
225 oid_value var1 = oid_getvalbyname("Variant-1");
228 vreq = e->defaultVariantRequest;
235 * 6,5: meta-data requested, variant list.
237 if (find_triple(vreq, defsetval, var1, 6, 5))
238 show_variantlist = 1;
240 * 9,1: Miscellaneous, no data requested.
242 if (find_triple(vreq, defsetval, var1, 9, 1))
246 if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
247 if (r->which == Z_Triple_integer)
248 get_bytes = *r->value.integer;
250 if (!show_variantlist)
251 match_triple (dh, vreq, defsetval, var1, c);
253 mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
257 * have we looked at enough children?
259 if (!occur || (occur->which == Z_Occurrences_values &&
260 (!occur->u.values->howMany ||
261 counter - *occur->u.values->start >=
262 *occur->u.values->howMany - 1)))
270 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
271 int i, Z_ETagUnit **t, int num)
279 case Z_ETagUnit_wildThing:
280 case Z_ETagUnit_specificTag:
281 res = match_children_here(dh, n, e, i, t, num); break;
282 case Z_ETagUnit_wildPath:
283 res = match_children_wildpath(dh, n, e, i, t, num); break;
290 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
294 n = data1_get_root_tag (dh, n);
295 if (n && n->which == DATA1N_tag)
296 n->u.tag.node_selected = 1;
298 for (i = 0; i < e->num_elements; i++)
300 if (e->elements[i]->which != Z_ERequest_simpleElement)
302 match_children(dh, n, e, i,
303 e->elements[i]->u.simpleElement->path->tags,
304 e->elements[i]->u.simpleElement->path->num_tags);