2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 2002-05-28 21:09:44 adam
8 * schema mapping supports duplicate maps (copy instead of pointer swap)
10 * Revision 1.20 2002/05/13 14:13:37 adam
11 * XML reader for data1 (EXPAT)
13 * Revision 1.19 2002/04/15 09:06:30 adam
14 * Fix explain tags for XML writer
16 * Revision 1.18 2002/04/12 14:40:07 adam
17 * GRS-1 writer accepts non-abs
19 * Revision 1.17 1999/11/30 13:47:12 adam
20 * Improved installation. Moved header files to include/yaz.
22 * Revision 1.16 1999/08/27 09:40:32 adam
23 * Renamed logf function to yaz_log. Removed VC++ project files.
25 * Revision 1.15 1999/03/31 11:18:25 adam
26 * Implemented odr_strdup. Added Reference ID to backend server API.
28 * Revision 1.14 1998/03/16 12:21:15 adam
29 * Fixed problem with tag names that weren't set to the right value
30 * when wildcards were used.
32 * Revision 1.13 1998/02/11 11:53:35 adam
33 * Changed code so that it compiles as C++.
35 * Revision 1.12 1997/11/24 11:33:56 adam
36 * Using function odr_nullval() instead of global ODR_NULLVAL when
39 * Revision 1.11 1997/11/18 09:51:09 adam
40 * Removed element num_children from data1_node. Minor changes in
43 * Revision 1.10 1997/09/17 12:10:36 adam
46 * Revision 1.9 1997/05/14 06:54:03 adam
49 * Revision 1.8 1996/12/05 13:17:49 quinn
50 * Fixed GRS-1 null-ref
52 * Revision 1.7 1996/10/11 11:57:23 quinn
55 * Revision 1.6 1996/07/06 19:58:34 quinn
56 * System headerfiles gathered in yconfig
58 * Revision 1.5 1996/06/03 09:46:42 quinn
59 * Added OID data type.
61 * Revision 1.4 1996/05/01 12:45:30 quinn
62 * Support use of local tag names in abs file.
64 * Revision 1.3 1995/11/13 09:27:35 quinn
65 * Fiddling with the variant stuff.
67 * Revision 1.2 1995/11/01 13:54:46 quinn
70 * Revision 1.1 1995/11/01 11:56:07 quinn
71 * Added Retrieval (data management) functions en masse.
79 #include <yaz/proto.h>
81 #include <yaz/data1.h>
83 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
85 static Z_ElementMetaData *get_ElementMetaData(ODR o)
87 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
94 r->num_supportedVariants = 0;
95 r->supportedVariants = 0;
97 r->elementDescriptor = 0;
99 r->surrogateElement = 0;
106 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
107 * from each of the triples beginning (ending) with 'n', up to the
108 * nearest parent tag. num should equal the number of triples in the
111 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
113 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
116 v->globalVariantSetId = 0;
117 v->num_triples = num;
118 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
121 * cycle back up through the tree of variants
122 * (traversing exactly 'level' variants).
124 for (p = n, num--; p && num >= 0; p = p->parent, num--)
128 assert(p->which == DATA1N_variant);
129 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
131 t->zclass = (int *)odr_malloc(o, sizeof(int));
132 *t->zclass = p->u.variant.type->zclass->zclass;
133 t->type = (int *)odr_malloc(o, sizeof(int));
134 *t->type = p->u.variant.type->type;
136 switch (p->u.variant.type->datatype)
139 t->which = Z_Triple_internationalString;
140 t->value.internationalString =
141 odr_strdup(o, p->u.variant.value);
144 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
145 p->u.variant.type->name);
153 * Traverse the variant children of n, constructing a supportedVariant list.
155 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
160 for (c = n->child; c; c = c->next)
161 if (c->which == DATA1N_data && level)
163 if (!m->supportedVariants)
164 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
166 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
168 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
173 if (!(m->supportedVariants[m->num_supportedVariants++] =
174 make_variant(n, level, o)))
177 else if (c->which == DATA1N_variant)
178 if (traverse_triples(c, level+1, m, o) < 0)
183 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
184 int select, int leaf,
187 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
191 res->which = Z_ElementData_elementNotThere;
192 res->u.elementNotThere = odr_nullval();
194 else if (n->which == DATA1N_data && (leaf || n->next == NULL))
200 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
203 switch (n->u.data.what)
206 res->which = Z_ElementData_numeric;
207 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
208 *res->u.numeric = atoi(n->u.data.data);
212 toget = n->u.data.len;
213 if (p && p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
214 toget = p->u.tag.get_bytes;
215 res->which = Z_ElementData_string;
216 res->u.string = (char *)odr_malloc(o, toget+1);
217 memcpy(res->u.string, n->u.data.data, toget);
218 res->u.string[toget] = '\0';
222 res->which = Z_ElementData_oid;
223 strncpy(str, n->u.data.data, n->u.data.len);
224 str[n->u.data.len] = '\0';
225 res->u.oid = odr_getoidbystr(o, str);
226 *len += n->u.data.len;
229 yaz_log(LOG_WARN, "Can't handle datatype.");
235 res->which = Z_ElementData_subtree;
236 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
242 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
246 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
251 if (n->which == DATA1N_tag)
253 if (n->u.tag.element)
254 tag = n->u.tag.element->tag;
259 * If we're a data element at this point, we need to insert a
260 * wellKnown tag to wrap us up.
262 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
264 if (n->root->u.root.absyn &&
265 !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
268 yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
276 yaz_log(LOG_WARN, "Bad data.");
280 res->tagType = (int *)odr_malloc(o, sizeof(int));
281 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
282 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
283 if (tag && tag->which == DATA1T_numeric)
285 res->tagValue->which = Z_StringOrNumeric_numeric;
286 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
287 *res->tagValue->u.numeric = tag->value.numeric;
293 if (n->which == DATA1N_tag)
294 tagstr = n->u.tag.tag; /* tag at node */
296 tagstr = tag->value.string; /* no take from well-known */
298 tagstr = "???"; /* no tag at all! */
299 res->tagValue->which = Z_StringOrNumeric_string;
300 res->tagValue->u.string = odr_strdup(o, tagstr);
302 res->tagOccurrence = 0;
303 res->appliedVariant = 0;
305 if (n->which == DATA1N_variant || (data && data->which ==
306 DATA1N_variant && data->next == NULL))
310 res->metaData = get_ElementMetaData(o);
311 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
312 if (traverse_triples(data, 0, res->metaData, o) < 0)
314 while (data && data->which == DATA1N_variant)
319 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
320 res->appliedVariant = make_variant(data->parent, nvars-1, o);
322 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
324 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
325 res->content->which = Z_ElementData_noDataRequested;
326 res->content->u.noDataRequested = odr_nullval();
328 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
335 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
336 int select, ODR o, int *len)
338 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
340 int num_children = 0;
342 for (c = n->child; c; c = c->next)
345 res->elements = (Z_TaggedElement **)
346 odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
347 res->num_elements = 0;
348 for (c = n->child; c; c = c->next)
350 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
352 if (!(res->elements[res->num_elements++] =
353 nodetotaggedelement (dh, c, select, o, len)))