1 | /*
|
---|
2 | * Copyright (c) 1995-1999, Index Data.
|
---|
3 | * See the file LICENSE for details.
|
---|
4 | * Sebastian Hammer, Adam Dickmeiss
|
---|
5 | *
|
---|
6 | * $Log$
|
---|
7 | * Revision 1.1 2000/08/03 03:11:29 johnmcp
|
---|
8 | * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
|
---|
9 | *
|
---|
10 | * Revision 1.14 1999/11/30 13:47:12 adam
|
---|
11 | * Improved installation. Moved header files to include/yaz.
|
---|
12 | *
|
---|
13 | * Revision 1.13 1999/08/27 09:40:32 adam
|
---|
14 | * Renamed logf function to yaz_log. Removed VC++ project files.
|
---|
15 | *
|
---|
16 | * Revision 1.12 1999/04/23 13:34:33 adam
|
---|
17 | * Fixed bug in match_triple. Thanks to Franck Falcoz <[email protected]>.
|
---|
18 | *
|
---|
19 | * Revision 1.11 1997/11/06 11:36:44 adam
|
---|
20 | * Implemented variant match on simple elements -data1 tree and Espec-1.
|
---|
21 | *
|
---|
22 | * Revision 1.10 1997/10/02 12:10:24 quinn
|
---|
23 | * Attempt to fix bug in especs
|
---|
24 | *
|
---|
25 | * Revision 1.9 1997/09/17 12:10:35 adam
|
---|
26 | * YAZ version 1.4.
|
---|
27 | *
|
---|
28 | * Revision 1.8 1997/05/14 06:54:02 adam
|
---|
29 | * C++ support.
|
---|
30 | *
|
---|
31 | * Revision 1.7 1997/04/30 08:52:11 quinn
|
---|
32 | * Null
|
---|
33 | *
|
---|
34 | * Revision 1.6 1996/10/11 11:57:22 quinn
|
---|
35 | * Smallish
|
---|
36 | *
|
---|
37 | * Revision 1.5 1996/07/06 19:58:34 quinn
|
---|
38 | * System headerfiles gathered in yconfig
|
---|
39 | *
|
---|
40 | * Revision 1.4 1996/06/07 11:04:32 quinn
|
---|
41 | * Fixed tag->tagset dependency
|
---|
42 | *
|
---|
43 | * Revision 1.3 1995/11/13 09:27:33 quinn
|
---|
44 | * Fiddling with the variant stuff.
|
---|
45 | *
|
---|
46 | * Revision 1.2 1995/11/01 13:54:45 quinn
|
---|
47 | * Minor adjustments
|
---|
48 | *
|
---|
49 | * Revision 1.1 1995/11/01 11:56:07 quinn
|
---|
50 | * Added Retrieval (data management) functions en masse.
|
---|
51 | *
|
---|
52 | *
|
---|
53 | */
|
---|
54 |
|
---|
55 |
|
---|
56 | #include <assert.h>
|
---|
57 |
|
---|
58 | #include <yaz/oid.h>
|
---|
59 | #include <yaz/log.h>
|
---|
60 | #include <yaz/proto.h>
|
---|
61 | #include <yaz/data1.h>
|
---|
62 |
|
---|
63 | static int match_children(data1_handle dh, data1_node *n,
|
---|
64 | Z_Espec1 *e, int i, Z_ETagUnit **t,
|
---|
65 | int num);
|
---|
66 |
|
---|
67 | static int match_children_wildpath(data1_handle dh, data1_node *n,
|
---|
68 | Z_Espec1 *e, int i,
|
---|
69 | Z_ETagUnit **t, int num)
|
---|
70 | {
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Locate a specific triple within a variant.
|
---|
76 | * set is the set to look for, universal set is the set that applies to a
|
---|
77 | * triple with an unknown set.
|
---|
78 | */
|
---|
79 | static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
|
---|
80 | oid_value set, int zclass, int type)
|
---|
81 | {
|
---|
82 | int i;
|
---|
83 | oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
|
---|
84 | oid_value defaultset = defaultsetent ? defaultsetent->value :
|
---|
85 | universalset;
|
---|
86 |
|
---|
87 | for (i = 0; i < var->num_triples; i++)
|
---|
88 | {
|
---|
89 | oident *cursetent =
|
---|
90 | oid_getentbyoid(var->triples[i]->variantSetId);
|
---|
91 | oid_value curset = cursetent ? cursetent->value : defaultset;
|
---|
92 |
|
---|
93 | if (set == curset &&
|
---|
94 | *var->triples[i]->zclass == zclass &&
|
---|
95 | *var->triples[i]->type == type)
|
---|
96 | return var->triples[i];
|
---|
97 | }
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
|
---|
102 | int get_bytes, Z_Variant *vreq)
|
---|
103 | {
|
---|
104 | data1_node *c;
|
---|
105 |
|
---|
106 | #if 1
|
---|
107 | if (n->which == DATA1N_tag)
|
---|
108 | #else
|
---|
109 | if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
|
---|
110 | /*
|
---|
111 | * This seems to cause multi-level elements to fall out when only a
|
---|
112 | * top-level elementRequest has been given... Problem is, I can't figure
|
---|
113 | * out what it was supposed to ACHIEVE.... delete when code has been
|
---|
114 | * verified.
|
---|
115 | */
|
---|
116 | #endif
|
---|
117 | {
|
---|
118 | n->u.tag.node_selected = 1;
|
---|
119 | n->u.tag.make_variantlist = make_variantlist;
|
---|
120 | n->u.tag.no_data_requested = no_data;
|
---|
121 | n->u.tag.get_bytes = get_bytes;
|
---|
122 | }
|
---|
123 |
|
---|
124 | for (c = n->child; c; c = c->next)
|
---|
125 | {
|
---|
126 | if (c->which == DATA1N_tag && (!n->child ||
|
---|
127 | n->child->which != DATA1N_tag))
|
---|
128 | {
|
---|
129 | c->u.tag.node_selected = 1;
|
---|
130 | c->u.tag.make_variantlist = make_variantlist;
|
---|
131 | c->u.tag.no_data_requested = no_data;
|
---|
132 | c->u.tag.get_bytes = get_bytes;
|
---|
133 | }
|
---|
134 | mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | static void match_triple (data1_handle dh, Z_Variant *vreq,
|
---|
140 | oid_value defsetval,
|
---|
141 | oid_value var1, data1_node *n)
|
---|
142 | {
|
---|
143 | data1_node **c;
|
---|
144 |
|
---|
145 | if (!(n = n->child))
|
---|
146 | return;
|
---|
147 | if (n->which != DATA1N_variant)
|
---|
148 | return;
|
---|
149 | c = &n->child;
|
---|
150 | while (*c)
|
---|
151 | {
|
---|
152 | int remove_flag = 0;
|
---|
153 | Z_Triple *r;
|
---|
154 |
|
---|
155 | assert ((*c)->which == DATA1N_variant);
|
---|
156 |
|
---|
157 | if ((*c)->u.variant.type->zclass->zclass == 4 &&
|
---|
158 | (*c)->u.variant.type->type == 1)
|
---|
159 | {
|
---|
160 | if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
|
---|
161 | (r->which == Z_Triple_internationalString))
|
---|
162 | {
|
---|
163 | const char *string_value =
|
---|
164 | r->value.internationalString;
|
---|
165 | if (strcmp ((*c)->u.variant.value, string_value))
|
---|
166 | remove_flag = 1;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | if (remove_flag)
|
---|
170 | {
|
---|
171 | data1_free_tree (dh, *c);
|
---|
172 | *c = (*c)->next;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | match_triple (dh, vreq, defsetval, var1, *c);
|
---|
177 | c = &(*c)->next;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int match_children_here (data1_handle dh, data1_node *n,
|
---|
183 | Z_Espec1 *e, int i,
|
---|
184 | Z_ETagUnit **t, int num)
|
---|
185 | {
|
---|
186 | int counter = 0, hits = 0;
|
---|
187 | data1_node *c;
|
---|
188 | Z_ETagUnit *tp = *t;
|
---|
189 | Z_Occurrences *occur;
|
---|
190 |
|
---|
191 | for (c = n->child; c ; c = c->next)
|
---|
192 | {
|
---|
193 | data1_tag *tag = 0;
|
---|
194 | if (c->which != DATA1N_tag)
|
---|
195 | return 0;
|
---|
196 |
|
---|
197 | if (tp->which == Z_ETagUnit_specificTag)
|
---|
198 | {
|
---|
199 | Z_SpecificTag *want = tp->u.specificTag;
|
---|
200 | occur = want->occurrences;
|
---|
201 | if (c->u.tag.element)
|
---|
202 | tag = c->u.tag.element->tag;
|
---|
203 | if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
|
---|
204 | 3))
|
---|
205 | continue;
|
---|
206 | if (want->tagValue->which == Z_StringOrNumeric_numeric)
|
---|
207 | {
|
---|
208 | if (!tag || tag->which != DATA1T_numeric)
|
---|
209 | continue;
|
---|
210 | if (*want->tagValue->u.numeric != tag->value.numeric)
|
---|
211 | continue;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | assert(want->tagValue->which == Z_StringOrNumeric_string);
|
---|
216 | if (tag && tag->which != DATA1T_string)
|
---|
217 | continue;
|
---|
218 | if (data1_matchstr(want->tagValue->u.string,
|
---|
219 | tag ? tag->value.string : c->u.tag.tag))
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | else
|
---|
224 | occur = tp->u.wildThing;
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Ok, so we have a matching tag. Are we within occurrences-range?
|
---|
228 | */
|
---|
229 | counter++;
|
---|
230 | if (occur && occur->which == Z_Occurrences_last)
|
---|
231 | {
|
---|
232 | yaz_log(LOG_WARN, "Can't do occurrences=last (yet)");
|
---|
233 | return 0;
|
---|
234 | }
|
---|
235 | if (!occur || occur->which == Z_Occurrences_all ||
|
---|
236 | (occur->which == Z_Occurrences_values && counter >=
|
---|
237 | *occur->u.values->start))
|
---|
238 | {
|
---|
239 | if (match_children(dh, c, e, i, t + 1, num - 1))
|
---|
240 | {
|
---|
241 | c->u.tag.node_selected = 1;
|
---|
242 | /*
|
---|
243 | * Consider the variant specification if this is a complete
|
---|
244 | * match.
|
---|
245 | */
|
---|
246 | if (num == 1)
|
---|
247 | {
|
---|
248 | int show_variantlist = 0;
|
---|
249 | int no_data = 0;
|
---|
250 | int get_bytes = -1;
|
---|
251 |
|
---|
252 | Z_Variant *vreq =
|
---|
253 | e->elements[i]->u.simpleElement->variantRequest;
|
---|
254 | oident *defset = oid_getentbyoid(e->defaultVariantSetId);
|
---|
255 | oid_value defsetval = defset ? defset->value : VAL_NONE;
|
---|
256 | oid_value var1 = oid_getvalbyname("Variant-1");
|
---|
257 |
|
---|
258 | if (!vreq)
|
---|
259 | vreq = e->defaultVariantRequest;
|
---|
260 |
|
---|
261 | if (vreq)
|
---|
262 | {
|
---|
263 | Z_Triple *r;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * 6,5: meta-data requested, variant list.
|
---|
267 | */
|
---|
268 | if (find_triple(vreq, defsetval, var1, 6, 5))
|
---|
269 | show_variantlist = 1;
|
---|
270 | /*
|
---|
271 | * 9,1: Miscellaneous, no data requested.
|
---|
272 | */
|
---|
273 | if (find_triple(vreq, defsetval, var1, 9, 1))
|
---|
274 | no_data = 1;
|
---|
275 |
|
---|
276 | /* howmuch */
|
---|
277 | if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
|
---|
278 | if (r->which == Z_Triple_integer)
|
---|
279 | get_bytes = *r->value.integer;
|
---|
280 |
|
---|
281 | if (!show_variantlist)
|
---|
282 | match_triple (dh, vreq, defsetval, var1, c);
|
---|
283 | }
|
---|
284 | mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
|
---|
285 | }
|
---|
286 | hits++;
|
---|
287 | /*
|
---|
288 | * have we looked at enough children?
|
---|
289 | */
|
---|
290 | if (!occur || (occur->which == Z_Occurrences_values &&
|
---|
291 | (!occur->u.values->howMany ||
|
---|
292 | counter - *occur->u.values->start >=
|
---|
293 | *occur->u.values->howMany - 1)))
|
---|
294 | return hits;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | return hits;
|
---|
299 | }
|
---|
300 |
|
---|
301 | static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
|
---|
302 | int i, Z_ETagUnit **t, int num)
|
---|
303 | {
|
---|
304 | int res;
|
---|
305 |
|
---|
306 | if (!num)
|
---|
307 | return 1;
|
---|
308 | switch (t[0]->which)
|
---|
309 | {
|
---|
310 | case Z_ETagUnit_wildThing:
|
---|
311 | case Z_ETagUnit_specificTag:
|
---|
312 | res = match_children_here(dh, n, e, i, t, num); break;
|
---|
313 | case Z_ETagUnit_wildPath:
|
---|
314 | res = match_children_wildpath(dh, n, e, i, t, num); break;
|
---|
315 | default:
|
---|
316 | abort();
|
---|
317 | }
|
---|
318 | return res;
|
---|
319 | }
|
---|
320 |
|
---|
321 | int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
|
---|
322 | {
|
---|
323 | int i;
|
---|
324 |
|
---|
325 | for (i = 0; i < e->num_elements; i++)
|
---|
326 | {
|
---|
327 | if (e->elements[i]->which != Z_ERequest_simpleElement)
|
---|
328 | return 100;
|
---|
329 | match_children(dh, n, e, i,
|
---|
330 | e->elements[i]->u.simpleElement->path->tags,
|
---|
331 | e->elements[i]->u.simpleElement->path->num_tags);
|
---|
332 | }
|
---|
333 | return 0;
|
---|
334 | }
|
---|