1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* converts data1 tree to GRS-1 record */
25 #include <yaz/proto.h>
29 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
31 static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
32 int select, ODR o, int *len,
33 data1_tag *wellknown_tag);
35 static Z_ElementMetaData *get_ElementMetaData(ODR o)
37 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
44 r->num_supportedVariants = 0;
45 r->supportedVariants = 0;
47 r->elementDescriptor = 0;
49 r->surrogateElement = 0;
56 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
57 * from each of the triples beginning (ending) with 'n', up to the
58 * nearest parent tag. num should equal the number of triples in the
61 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
63 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
66 v->globalVariantSetId = 0;
68 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
71 * cycle back up through the tree of variants
72 * (traversing exactly 'level' variants).
74 for (p = n, num--; p && num >= 0; p = p->parent, num--)
78 assert(p->which == DATA1N_variant);
79 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
81 t->zclass = (int *)odr_malloc(o, sizeof(int));
82 *t->zclass = p->u.variant.type->zclass->zclass;
83 t->type = (int *)odr_malloc(o, sizeof(int));
84 *t->type = p->u.variant.type->type;
86 switch (p->u.variant.type->datatype)
89 t->which = Z_Triple_internationalString;
90 t->value.internationalString =
91 odr_strdup(o, p->u.variant.value);
94 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
95 p->u.variant.type->name);
103 * Traverse the variant children of n, constructing a supportedVariant list.
105 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
110 for (c = n->child; c; c = c->next)
111 if (c->which == DATA1N_data && level)
113 if (!m->supportedVariants)
114 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
116 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
118 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
123 if (!(m->supportedVariants[m->num_supportedVariants++] =
124 make_variant(n, level, o)))
127 else if (c->which == DATA1N_variant)
128 if (traverse_triples(c, level+1, m, o) < 0)
134 * Locate some data under this node. This routine should handle variants
137 static char *get_data(data1_node *n, int *len)
144 if (n->which == DATA1N_data)
147 *len = n->u.data.len;
149 for (i = 0; i<*len; i++)
150 if (!d1_isspace(n->u.data.data[i]))
152 while (*len && d1_isspace(n->u.data.data[*len - 1]))
156 return n->u.data.data + i;
158 if (n->which == DATA1N_tag)
172 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
173 int select, int leaf,
175 data1_tag *wellknown_tag)
177 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
181 res->which = Z_ElementData_elementNotThere;
182 res->u.elementNotThere = odr_nullval();
184 else if (n->which == DATA1N_data && leaf)
187 int toget = n->u.data.len;
189 cp = get_data (n, &toget);
191 switch (n->u.data.what)
194 res->which = Z_ElementData_numeric;
195 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
196 *res->u.numeric = atoi_n (cp, toget);
201 res->which = Z_ElementData_string;
202 res->u.string = (char *)odr_malloc(o, toget+1);
204 memcpy(res->u.string, cp, toget);
205 res->u.string[toget] = '\0';
209 res->which = Z_ElementData_oid;
212 memcpy (str, cp, toget);
214 res->u.oid = odr_getoidbystr(o, str);
215 *len += oid_oidlen(res->u.oid) * sizeof(int);
218 yaz_log(YLOG_WARN, "Can't handle datatype.");
224 res->which = Z_ElementData_subtree;
225 if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
232 static int is_empty_data (data1_node *n)
234 if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
235 || n->u.data.what == DATA1I_xmltext))
237 int i = n->u.data.len;
239 while (i > 0 && d1_isspace(n->u.data.data[i-1]))
248 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
251 data1_tag *wellknown_tag)
253 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
258 if (n->which == DATA1N_tag)
260 if (n->u.tag.element)
261 tag = n->u.tag.element->tag;
264 /* skip empty data children */
265 while (is_empty_data(data))
270 { /* got one. see if this is the only non-empty one */
271 data1_node *sub = data->next;
272 while (sub && is_empty_data(sub))
275 leaf = 1; /* all empty. 'data' is the only child */
279 * If we're a data element at this point, we need to insert a
280 * wellKnown tag to wrap us up.
282 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
289 if (is_empty_data(data))
294 yaz_log(YLOG_WARN, "Bad data.");
298 res->tagType = (int *)odr_malloc(o, sizeof(int));
299 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
300 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
301 if (tag && tag->which == DATA1T_numeric)
303 res->tagValue->which = Z_StringOrNumeric_numeric;
304 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
305 *res->tagValue->u.numeric = tag->value.numeric;
311 if (n->which == DATA1N_tag)
312 tagstr = n->u.tag.tag; /* tag at node */
314 tagstr = tag->value.string; /* no take from well-known */
317 res->tagValue->which = Z_StringOrNumeric_string;
318 res->tagValue->u.string = odr_strdup(o, tagstr);
320 res->tagOccurrence = 0;
321 res->appliedVariant = 0;
323 if (n->which == DATA1N_variant || (data && data->which ==
324 DATA1N_variant && data->next == NULL))
328 res->metaData = get_ElementMetaData(o);
329 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
330 if (traverse_triples(data, 0, res->metaData, o) < 0)
332 while (data && data->which == DATA1N_variant)
337 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
338 res->appliedVariant = make_variant(data->parent, nvars-1, o);
340 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
342 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
343 res->content->which = Z_ElementData_noDataRequested;
344 res->content->u.noDataRequested = odr_nullval();
346 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
347 o, len, wellknown_tag)))
353 static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
354 int select, ODR o, int *len,
355 data1_tag *wellknown_tag)
357 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
359 int num_children = 0;
361 for (c = n->child; c; c = c->next)
364 res->elements = (Z_TaggedElement **)
365 odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
366 res->num_elements = 0;
367 for (c = n->child; c; c = c->next)
369 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
371 if ((res->elements[res->num_elements] =
372 nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
378 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
379 int select, ODR o, int *len)
382 data1_tag *wellknown_tag = 0;
384 if (n->which == DATA1N_root)
385 n = data1_get_root_tag (dh, n);
387 if (n->root->u.root.absyn &&
389 data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
392 yaz_log(YLOG_WARN, "Unable to locate tag for 'wellKnown'");
393 wellknown_tag = odr_malloc(o, sizeof(*wellknown_tag));
394 wellknown_tag->which = DATA1T_numeric;
395 wellknown_tag->value.numeric = 19;
396 wellknown_tag->next = 0;
397 wellknown_tag->tagset = odr_malloc(o, sizeof(*wellknown_tag->tagset));
398 wellknown_tag->tagset->type = 1;
399 wellknown_tag->kind = DATA1K_string;
401 return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
406 * indent-tabs-mode: nil
408 * vim: shiftwidth=4 tabstop=8 expandtab