1 /* $Id: d1_doespec.c,v 1.13 2007-04-16 08:44:31 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
23 /** \file d1_doespec.c
24 * \brief handle Z39.50 variant-1 specs
26 * See http://www.loc.gov/z3950/agency/defns/variant1.html
32 #include <yaz/proto.h>
33 #include <yaz/oid_db.h>
34 #include <idzebra/data1.h>
36 static int match_children(data1_handle dh, data1_node *n,
37 Z_Espec1 *e, int i, Z_ETagUnit **t,
41 static int match_children_wildpath(data1_handle dh, data1_node *n,
43 Z_ETagUnit **t, int num)
49 * Locate a specific triple within a variant.
50 * set is the set to look for, universal set is the set that applies to a
51 * triple with an unknown set.
53 static Z_Triple *find_triple(Z_Variant *var, const int *universal_oid,
54 const int *var_oid, int zclass, int type)
58 for (i = 0; i < var->num_triples; i++)
60 const int *cur_oid = var->triples[i]->variantSetId;
62 cur_oid = var->globalVariantSetId;
63 if (cur_oid && var_oid
64 && !oid_oidcmp(var_oid, cur_oid) && *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 const int *var_oid, 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, def_oid, var_oid, 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, def_oid, var_oid, *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;
286 const int *var_oid = yaz_oid_variant1();
288 vreq = e->defaultVariantRequest;
295 * 6,5: meta-data requested, variant list.
297 if (find_triple(vreq, e->defaultVariantSetId,
299 show_variantlist = 1;
301 * 9,1: Miscellaneous, no data requested.
303 if (find_triple(vreq, e->defaultVariantSetId,
308 if ((r = find_triple(vreq, e->defaultVariantSetId,
310 if (r->which == Z_Triple_integer)
311 get_bytes = *r->value.integer;
313 if (!show_variantlist)
314 match_triple(dh, vreq, e->defaultVariantSetId,
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 */ );
378 * indent-tabs-mode: nil
380 * vim: shiftwidth=4 tabstop=8 expandtab