source: trunk/gsdl/packages/yaz/retrieval/d1_grs.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
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:31 johnmcp
8 * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
9 *
10 * Revision 1.17 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
12 *
13 * Revision 1.16 1999/08/27 09:40:32 adam
14 * Renamed logf function to yaz_log. Removed VC++ project files.
15 *
16 * Revision 1.15 1999/03/31 11:18:25 adam
17 * Implemented odr_strdup. Added Reference ID to backend server API.
18 *
19 * Revision 1.14 1998/03/16 12:21:15 adam
20 * Fixed problem with tag names that weren't set to the right value
21 * when wildcards were used.
22 *
23 * Revision 1.13 1998/02/11 11:53:35 adam
24 * Changed code so that it compiles as C++.
25 *
26 * Revision 1.12 1997/11/24 11:33:56 adam
27 * Using function odr_nullval() instead of global ODR_NULLVAL when
28 * appropriate.
29 *
30 * Revision 1.11 1997/11/18 09:51:09 adam
31 * Removed element num_children from data1_node. Minor changes in
32 * data1 to Explain.
33 *
34 * Revision 1.10 1997/09/17 12:10:36 adam
35 * YAZ version 1.4.
36 *
37 * Revision 1.9 1997/05/14 06:54:03 adam
38 * C++ support.
39 *
40 * Revision 1.8 1996/12/05 13:17:49 quinn
41 * Fixed GRS-1 null-ref
42 *
43 * Revision 1.7 1996/10/11 11:57:23 quinn
44 * Smallish
45 *
46 * Revision 1.6 1996/07/06 19:58:34 quinn
47 * System headerfiles gathered in yconfig
48 *
49 * Revision 1.5 1996/06/03 09:46:42 quinn
50 * Added OID data type.
51 *
52 * Revision 1.4 1996/05/01 12:45:30 quinn
53 * Support use of local tag names in abs file.
54 *
55 * Revision 1.3 1995/11/13 09:27:35 quinn
56 * Fiddling with the variant stuff.
57 *
58 * Revision 1.2 1995/11/01 13:54:46 quinn
59 * Minor adjustments
60 *
61 * Revision 1.1 1995/11/01 11:56:07 quinn
62 * Added Retrieval (data management) functions en masse.
63 *
64 *
65 */
66
67#include <assert.h>
68#include <stdlib.h>
69
70#include <yaz/proto.h>
71#include <yaz/log.h>
72#include <yaz/data1.h>
73
74#define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
75
76static Z_ElementMetaData *get_ElementMetaData(ODR o)
77{
78 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
79
80 r->seriesOrder = 0;
81 r->usageRight = 0;
82 r->num_hits = 0;
83 r->hits = 0;
84 r->displayName = 0;
85 r->num_supportedVariants = 0;
86 r->supportedVariants = 0;
87 r->message = 0;
88 r->elementDescriptor = 0;
89 r->surrogateFor = 0;
90 r->surrogateElement = 0;
91 r->other = 0;
92
93 return r;
94}
95
96/*
97 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
98 * from each of the triples beginning (ending) with 'n', up to the
99 * nearest parent tag. num should equal the number of triples in the
100 * sequence.
101 */
102static Z_Variant *make_variant(data1_node *n, int num, ODR o)
103{
104 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
105 data1_node *p;
106
107 v->globalVariantSetId = 0;
108 v->num_triples = num;
109 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
110
111 /*
112 * cycle back up through the tree of variants
113 * (traversing exactly 'level' variants).
114 */
115 for (p = n, num--; p && num >= 0; p = p->parent, num--)
116 {
117 Z_Triple *t;
118
119 assert(p->which == DATA1N_variant);
120 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
121 t->variantSetId = 0;
122 t->zclass = (int *)odr_malloc(o, sizeof(int));
123 *t->zclass = p->u.variant.type->zclass->zclass;
124 t->type = (int *)odr_malloc(o, sizeof(int));
125 *t->type = p->u.variant.type->type;
126
127 switch (p->u.variant.type->datatype)
128 {
129 case DATA1K_string:
130 t->which = Z_Triple_internationalString;
131 t->value.internationalString =
132 odr_strdup(o, p->u.variant.value);
133 break;
134 default:
135 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
136 p->u.variant.type->name);
137 return 0;
138 }
139 }
140 return v;
141}
142
143/*
144 * Traverse the variant children of n, constructing a supportedVariant list.
145 */
146static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
147 ODR o)
148{
149 data1_node *c;
150
151 for (c = n->child; c; c = c->next)
152 if (c->which == DATA1N_data && level)
153 {
154 if (!m->supportedVariants)
155 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
156 D1_VARIANTARRAY);
157 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
158 {
159 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
160 D1_VARIANTARRAY);
161 return -1;
162 }
163
164 if (!(m->supportedVariants[m->num_supportedVariants++] =
165 make_variant(n, level, o)))
166 return -1;
167 }
168 else if (c->which == DATA1N_variant)
169 if (traverse_triples(c, level+1, m, o) < 0)
170 return -1;
171 return 0;
172}
173
174static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
175 int select, int leaf,
176 ODR o, int *len)
177{
178 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
179
180 if (!n)
181 {
182 res->which = Z_ElementData_elementNotThere;
183 res->u.elementNotThere = odr_nullval();
184 }
185 else if (n->which == DATA1N_data && (leaf || n->next == NULL))
186 {
187 char str[512];
188 int toget;
189 data1_node *p;
190
191 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
192 ;
193
194 switch (n->u.data.what)
195 {
196 case DATA1I_num:
197 res->which = Z_ElementData_numeric;
198 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
199 *res->u.numeric = atoi(n->u.data.data);
200 *len += 4;
201 break;
202 case DATA1I_text:
203 toget = n->u.data.len;
204 if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
205 toget = p->u.tag.get_bytes;
206 res->which = Z_ElementData_string;
207 res->u.string = (char *)odr_malloc(o, toget+1);
208 memcpy(res->u.string, n->u.data.data, toget);
209 res->u.string[toget] = '\0';
210 *len += toget;
211 break;
212 case DATA1I_oid:
213 res->which = Z_ElementData_oid;
214 strncpy(str, n->u.data.data, n->u.data.len);
215 str[n->u.data.len] = '\0';
216 res->u.oid = odr_getoidbystr(o, str);
217 *len += n->u.data.len;
218 break;
219 default:
220 yaz_log(LOG_WARN, "Can't handle datatype.");
221 return 0;
222 }
223 }
224 else
225 {
226 res->which = Z_ElementData_subtree;
227 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
228 return 0;
229 }
230 return res;
231}
232
233static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
234 int select, ODR o,
235 int *len)
236{
237 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
238 data1_tag *tag = 0;
239 data1_node *data;
240 int leaf;
241
242 if (n->which == DATA1N_tag)
243 {
244 if (n->u.tag.element)
245 tag = n->u.tag.element->tag;
246 data = n->child;
247 leaf = 0;
248 }
249 /*
250 * If we're a data element at this point, we need to insert a
251 * wellKnown tag to wrap us up.
252 */
253 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
254 {
255 if (!(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
256 "wellKnown")))
257 {
258 yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
259 return 0;
260 }
261 data = n;
262 leaf = 1;
263 }
264 else
265 {
266 yaz_log(LOG_WARN, "Bad data.");
267 return 0;
268 }
269
270 res->tagType = (int *)odr_malloc(o, sizeof(int));
271 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
272 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
273 if (tag && tag->which == DATA1T_numeric)
274 {
275 res->tagValue->which = Z_StringOrNumeric_numeric;
276 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
277 *res->tagValue->u.numeric = tag->value.numeric;
278 }
279 else
280 {
281 char *tagstr;
282
283 if (n->which == DATA1N_tag)
284 tagstr = n->u.tag.tag; /* tag at node */
285 else if (tag)
286 tagstr = tag->value.string; /* no take from well-known */
287 else
288 tagstr = "?"; /* no tag at all! */
289 res->tagValue->which = Z_StringOrNumeric_string;
290 res->tagValue->u.string = odr_strdup(o, tagstr);
291 }
292 res->tagOccurrence = 0;
293 res->appliedVariant = 0;
294 res->metaData = 0;
295 if (n->which == DATA1N_variant || (data && data->which ==
296 DATA1N_variant && data->next == NULL))
297 {
298 int nvars = 0;
299
300 res->metaData = get_ElementMetaData(o);
301 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
302 if (traverse_triples(data, 0, res->metaData, o) < 0)
303 return 0;
304 while (data && data->which == DATA1N_variant)
305 {
306 nvars++;
307 data = data->child;
308 }
309 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
310 res->appliedVariant = make_variant(data->parent, nvars-1, o);
311 }
312 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
313 {
314 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
315 res->content->which = Z_ElementData_noDataRequested;
316 res->content->u.noDataRequested = odr_nullval();
317 }
318 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
319 o, len)))
320 return 0;
321 *len += 10;
322 return res;
323}
324
325Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
326 int select, ODR o, int *len)
327{
328 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
329 data1_node *c;
330 int num_children = 0;
331
332 for (c = n->child; c; c = c->next)
333 num_children++;
334
335 res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
336 res->num_elements = 0;
337 for (c = n->child; c; c = c->next)
338 {
339 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
340 continue;
341 if (!(res->elements[res->num_elements++] =
342 nodetotaggedelement (dh, c, select, o, len)))
343 return 0;
344 }
345 return res;
346}
Note: See TracBrowser for help on using the repository browser.