2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 2002-04-12 14:40:07 adam
8 * GRS-1 writer accepts non-abs
10 * Revision 1.17 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.16 1999/08/27 09:40:32 adam
14 * Renamed logf function to yaz_log. Removed VC++ project files.
16 * Revision 1.15 1999/03/31 11:18:25 adam
17 * Implemented odr_strdup. Added Reference ID to backend server API.
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.
23 * Revision 1.13 1998/02/11 11:53:35 adam
24 * Changed code so that it compiles as C++.
26 * Revision 1.12 1997/11/24 11:33:56 adam
27 * Using function odr_nullval() instead of global ODR_NULLVAL when
30 * Revision 1.11 1997/11/18 09:51:09 adam
31 * Removed element num_children from data1_node. Minor changes in
34 * Revision 1.10 1997/09/17 12:10:36 adam
37 * Revision 1.9 1997/05/14 06:54:03 adam
40 * Revision 1.8 1996/12/05 13:17:49 quinn
41 * Fixed GRS-1 null-ref
43 * Revision 1.7 1996/10/11 11:57:23 quinn
46 * Revision 1.6 1996/07/06 19:58:34 quinn
47 * System headerfiles gathered in yconfig
49 * Revision 1.5 1996/06/03 09:46:42 quinn
50 * Added OID data type.
52 * Revision 1.4 1996/05/01 12:45:30 quinn
53 * Support use of local tag names in abs file.
55 * Revision 1.3 1995/11/13 09:27:35 quinn
56 * Fiddling with the variant stuff.
58 * Revision 1.2 1995/11/01 13:54:46 quinn
61 * Revision 1.1 1995/11/01 11:56:07 quinn
62 * Added Retrieval (data management) functions en masse.
70 #include <yaz/proto.h>
72 #include <yaz/data1.h>
74 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
76 static Z_ElementMetaData *get_ElementMetaData(ODR o)
78 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
85 r->num_supportedVariants = 0;
86 r->supportedVariants = 0;
88 r->elementDescriptor = 0;
90 r->surrogateElement = 0;
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
102 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
104 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
107 v->globalVariantSetId = 0;
108 v->num_triples = num;
109 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
112 * cycle back up through the tree of variants
113 * (traversing exactly 'level' variants).
115 for (p = n, num--; p && num >= 0; p = p->parent, num--)
119 assert(p->which == DATA1N_variant);
120 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
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;
127 switch (p->u.variant.type->datatype)
130 t->which = Z_Triple_internationalString;
131 t->value.internationalString =
132 odr_strdup(o, p->u.variant.value);
135 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
136 p->u.variant.type->name);
144 * Traverse the variant children of n, constructing a supportedVariant list.
146 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
151 for (c = n->child; c; c = c->next)
152 if (c->which == DATA1N_data && level)
154 if (!m->supportedVariants)
155 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
157 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
159 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
164 if (!(m->supportedVariants[m->num_supportedVariants++] =
165 make_variant(n, level, o)))
168 else if (c->which == DATA1N_variant)
169 if (traverse_triples(c, level+1, m, o) < 0)
174 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
175 int select, int leaf,
178 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
182 res->which = Z_ElementData_elementNotThere;
183 res->u.elementNotThere = odr_nullval();
185 else if (n->which == DATA1N_data && (leaf || n->next == NULL))
191 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
194 switch (n->u.data.what)
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);
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';
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;
220 yaz_log(LOG_WARN, "Can't handle datatype.");
226 res->which = Z_ElementData_subtree;
227 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
233 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
237 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
242 if (n->which == DATA1N_tag)
244 if (n->u.tag.element)
245 tag = n->u.tag.element->tag;
250 * If we're a data element at this point, we need to insert a
251 * wellKnown tag to wrap us up.
253 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
255 if (n->root->u.root.absyn &&
256 !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
259 yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
267 yaz_log(LOG_WARN, "Bad data.");
271 res->tagType = (int *)odr_malloc(o, sizeof(int));
272 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
273 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
274 if (tag && tag->which == DATA1T_numeric)
276 res->tagValue->which = Z_StringOrNumeric_numeric;
277 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
278 *res->tagValue->u.numeric = tag->value.numeric;
284 if (n->which == DATA1N_tag)
285 tagstr = n->u.tag.tag; /* tag at node */
287 tagstr = tag->value.string; /* no take from well-known */
289 tagstr = "?"; /* no tag at all! */
290 res->tagValue->which = Z_StringOrNumeric_string;
291 res->tagValue->u.string = odr_strdup(o, tagstr);
293 res->tagOccurrence = 0;
294 res->appliedVariant = 0;
296 if (n->which == DATA1N_variant || (data && data->which ==
297 DATA1N_variant && data->next == NULL))
301 res->metaData = get_ElementMetaData(o);
302 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
303 if (traverse_triples(data, 0, res->metaData, o) < 0)
305 while (data && data->which == DATA1N_variant)
310 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
311 res->appliedVariant = make_variant(data->parent, nvars-1, o);
313 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
315 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
316 res->content->which = Z_ElementData_noDataRequested;
317 res->content->u.noDataRequested = odr_nullval();
319 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
326 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
327 int select, ODR o, int *len)
329 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
331 int num_children = 0;
333 for (c = n->child; c; c = c->next)
336 res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
337 res->num_elements = 0;
338 for (c = n->child; c; c = c->next)
340 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
342 if (!(res->elements[res->num_elements++] =
343 nodetotaggedelement (dh, c, select, o, len)))