2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 2002-04-15 09:06:30 adam
8 * Fix explain tags for XML writer
10 * Revision 1.18 2002/04/12 14:40:07 adam
11 * GRS-1 writer accepts non-abs
13 * Revision 1.17 1999/11/30 13:47:12 adam
14 * Improved installation. Moved header files to include/yaz.
16 * Revision 1.16 1999/08/27 09:40:32 adam
17 * Renamed logf function to yaz_log. Removed VC++ project files.
19 * Revision 1.15 1999/03/31 11:18:25 adam
20 * Implemented odr_strdup. Added Reference ID to backend server API.
22 * Revision 1.14 1998/03/16 12:21:15 adam
23 * Fixed problem with tag names that weren't set to the right value
24 * when wildcards were used.
26 * Revision 1.13 1998/02/11 11:53:35 adam
27 * Changed code so that it compiles as C++.
29 * Revision 1.12 1997/11/24 11:33:56 adam
30 * Using function odr_nullval() instead of global ODR_NULLVAL when
33 * Revision 1.11 1997/11/18 09:51:09 adam
34 * Removed element num_children from data1_node. Minor changes in
37 * Revision 1.10 1997/09/17 12:10:36 adam
40 * Revision 1.9 1997/05/14 06:54:03 adam
43 * Revision 1.8 1996/12/05 13:17:49 quinn
44 * Fixed GRS-1 null-ref
46 * Revision 1.7 1996/10/11 11:57:23 quinn
49 * Revision 1.6 1996/07/06 19:58:34 quinn
50 * System headerfiles gathered in yconfig
52 * Revision 1.5 1996/06/03 09:46:42 quinn
53 * Added OID data type.
55 * Revision 1.4 1996/05/01 12:45:30 quinn
56 * Support use of local tag names in abs file.
58 * Revision 1.3 1995/11/13 09:27:35 quinn
59 * Fiddling with the variant stuff.
61 * Revision 1.2 1995/11/01 13:54:46 quinn
64 * Revision 1.1 1995/11/01 11:56:07 quinn
65 * Added Retrieval (data management) functions en masse.
73 #include <yaz/proto.h>
75 #include <yaz/data1.h>
77 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
79 static Z_ElementMetaData *get_ElementMetaData(ODR o)
81 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
88 r->num_supportedVariants = 0;
89 r->supportedVariants = 0;
91 r->elementDescriptor = 0;
93 r->surrogateElement = 0;
100 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
101 * from each of the triples beginning (ending) with 'n', up to the
102 * nearest parent tag. num should equal the number of triples in the
105 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
107 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
110 v->globalVariantSetId = 0;
111 v->num_triples = num;
112 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
115 * cycle back up through the tree of variants
116 * (traversing exactly 'level' variants).
118 for (p = n, num--; p && num >= 0; p = p->parent, num--)
122 assert(p->which == DATA1N_variant);
123 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
125 t->zclass = (int *)odr_malloc(o, sizeof(int));
126 *t->zclass = p->u.variant.type->zclass->zclass;
127 t->type = (int *)odr_malloc(o, sizeof(int));
128 *t->type = p->u.variant.type->type;
130 switch (p->u.variant.type->datatype)
133 t->which = Z_Triple_internationalString;
134 t->value.internationalString =
135 odr_strdup(o, p->u.variant.value);
138 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
139 p->u.variant.type->name);
147 * Traverse the variant children of n, constructing a supportedVariant list.
149 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
154 for (c = n->child; c; c = c->next)
155 if (c->which == DATA1N_data && level)
157 if (!m->supportedVariants)
158 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
160 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
162 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
167 if (!(m->supportedVariants[m->num_supportedVariants++] =
168 make_variant(n, level, o)))
171 else if (c->which == DATA1N_variant)
172 if (traverse_triples(c, level+1, m, o) < 0)
177 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
178 int select, int leaf,
181 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
185 res->which = Z_ElementData_elementNotThere;
186 res->u.elementNotThere = odr_nullval();
188 else if (n->which == DATA1N_data && (leaf || n->next == NULL))
194 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
197 switch (n->u.data.what)
200 res->which = Z_ElementData_numeric;
201 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
202 *res->u.numeric = atoi(n->u.data.data);
206 toget = n->u.data.len;
207 if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
208 toget = p->u.tag.get_bytes;
209 res->which = Z_ElementData_string;
210 res->u.string = (char *)odr_malloc(o, toget+1);
211 memcpy(res->u.string, n->u.data.data, toget);
212 res->u.string[toget] = '\0';
216 res->which = Z_ElementData_oid;
217 strncpy(str, n->u.data.data, n->u.data.len);
218 str[n->u.data.len] = '\0';
219 res->u.oid = odr_getoidbystr(o, str);
220 *len += n->u.data.len;
223 yaz_log(LOG_WARN, "Can't handle datatype.");
229 res->which = Z_ElementData_subtree;
230 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
236 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
240 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
245 if (n->which == DATA1N_tag)
247 if (n->u.tag.element)
248 tag = n->u.tag.element->tag;
253 * If we're a data element at this point, we need to insert a
254 * wellKnown tag to wrap us up.
256 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
258 if (n->root->u.root.absyn &&
259 !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
262 yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
270 yaz_log(LOG_WARN, "Bad data.");
274 res->tagType = (int *)odr_malloc(o, sizeof(int));
275 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
276 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
277 if (tag && tag->which == DATA1T_numeric)
279 res->tagValue->which = Z_StringOrNumeric_numeric;
280 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
281 *res->tagValue->u.numeric = tag->value.numeric;
287 if (n->which == DATA1N_tag)
288 tagstr = n->u.tag.tag; /* tag at node */
290 tagstr = tag->value.string; /* no take from well-known */
292 tagstr = "???"; /* no tag at all! */
293 res->tagValue->which = Z_StringOrNumeric_string;
294 res->tagValue->u.string = odr_strdup(o, tagstr);
296 res->tagOccurrence = 0;
297 res->appliedVariant = 0;
299 if (n->which == DATA1N_variant || (data && data->which ==
300 DATA1N_variant && data->next == NULL))
304 res->metaData = get_ElementMetaData(o);
305 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
306 if (traverse_triples(data, 0, res->metaData, o) < 0)
308 while (data && data->which == DATA1N_variant)
313 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
314 res->appliedVariant = make_variant(data->parent, nvars-1, o);
316 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
318 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
319 res->content->which = Z_ElementData_noDataRequested;
320 res->content->u.noDataRequested = odr_nullval();
322 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
329 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
330 int select, ODR o, int *len)
332 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
334 int num_children = 0;
336 for (c = n->child; c; c = c->next)
339 res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
340 res->num_elements = 0;
341 for (c = n->child; c; c = c->next)
343 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
345 if (!(res->elements[res->num_elements++] =
346 nodetotaggedelement (dh, c, select, o, len)))