1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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 = odr_intdup(o, p->u.variant.type->zclass->zclass);
82 t->type = odr_intdup(o, p->u.variant.type->type);
84 switch (p->u.variant.type->datatype)
87 t->which = Z_Triple_internationalString;
88 t->value.internationalString =
89 odr_strdup(o, p->u.variant.value);
92 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
93 p->u.variant.type->name);
101 * Traverse the variant children of n, constructing a supportedVariant list.
103 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
108 for (c = n->child; c; c = c->next)
109 if (c->which == DATA1N_data && level)
111 if (!m->supportedVariants)
112 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
114 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
116 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
121 if (!(m->supportedVariants[m->num_supportedVariants++] =
122 make_variant(n, level, o)))
125 else if (c->which == DATA1N_variant)
126 if (traverse_triples(c, level+1, m, o) < 0)
132 * Locate some data under this node. This routine should handle variants
135 static char *get_data(data1_node *n, int *len)
142 if (n->which == DATA1N_data)
145 *len = n->u.data.len;
147 for (i = 0; i<*len; i++)
148 if (!d1_isspace(n->u.data.data[i]))
150 while (*len && d1_isspace(n->u.data.data[*len - 1]))
154 return n->u.data.data + i;
156 if (n->which == DATA1N_tag)
170 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
171 int select, int leaf,
173 data1_tag *wellknown_tag)
175 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
179 res->which = Z_ElementData_elementNotThere;
180 res->u.elementNotThere = odr_nullval();
182 else if (n->which == DATA1N_data && leaf)
185 int toget = n->u.data.len;
187 cp = get_data (n, &toget);
189 switch (n->u.data.what)
192 res->which = Z_ElementData_numeric;
193 res->u.numeric = odr_intdup(o, atoi_n(cp, toget));
198 res->which = Z_ElementData_string;
199 res->u.string = (char *)odr_malloc(o, toget+1);
201 memcpy(res->u.string, cp, toget);
202 res->u.string[toget] = '\0';
206 res->which = Z_ElementData_oid;
209 memcpy (str, cp, toget);
211 res->u.oid = odr_getoidbystr(o, str);
212 *len += oid_oidlen(res->u.oid) * sizeof(int);
215 yaz_log(YLOG_WARN, "Can't handle datatype.");
221 res->which = Z_ElementData_subtree;
222 if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
229 static int is_empty_data (data1_node *n)
231 if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
232 || n->u.data.what == DATA1I_xmltext))
234 int i = n->u.data.len;
236 while (i > 0 && d1_isspace(n->u.data.data[i-1]))
245 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
248 data1_tag *wellknown_tag)
250 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
255 if (n->which == DATA1N_tag)
257 if (n->u.tag.element)
258 tag = n->u.tag.element->tag;
261 /* skip empty data children */
262 while (is_empty_data(data))
267 { /* got one. see if this is the only non-empty one */
268 data1_node *sub = data->next;
269 while (sub && is_empty_data(sub))
272 leaf = 1; /* all empty. 'data' is the only child */
276 * If we're a data element at this point, we need to insert a
277 * wellKnown tag to wrap us up.
279 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
286 if (is_empty_data(data))
291 yaz_log(YLOG_WARN, "Bad data.");
295 res->tagType = odr_intdup(o, (tag && tag->tagset) ? tag->tagset->type : 3);
296 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
297 if (tag && tag->which == DATA1T_numeric)
299 res->tagValue->which = Z_StringOrNumeric_numeric;
300 res->tagValue->u.numeric = odr_intdup(o, tag->value.numeric);
306 if (n->which == DATA1N_tag)
307 tagstr = n->u.tag.tag; /* tag at node */
309 tagstr = tag->value.string; /* no take from well-known */
312 res->tagValue->which = Z_StringOrNumeric_string;
313 res->tagValue->u.string = odr_strdup(o, tagstr);
315 res->tagOccurrence = 0;
316 res->appliedVariant = 0;
318 if (n->which == DATA1N_variant || (data && data->which ==
319 DATA1N_variant && data->next == NULL))
323 res->metaData = get_ElementMetaData(o);
324 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
325 if (traverse_triples(data, 0, res->metaData, o) < 0)
327 while (data && data->which == DATA1N_variant)
332 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
333 res->appliedVariant = make_variant(data->parent, nvars-1, o);
335 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
337 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
338 res->content->which = Z_ElementData_noDataRequested;
339 res->content->u.noDataRequested = odr_nullval();
341 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
342 o, len, wellknown_tag)))
348 static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
349 int select, ODR o, int *len,
350 data1_tag *wellknown_tag)
352 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
354 int num_children = 0;
356 for (c = n->child; c; c = c->next)
359 res->elements = (Z_TaggedElement **)
360 odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
361 res->num_elements = 0;
362 for (c = n->child; c; c = c->next)
364 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
366 if ((res->elements[res->num_elements] =
367 nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
373 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
374 int select, ODR o, int *len)
377 data1_tag *wellknown_tag = 0;
379 if (n->which == DATA1N_root)
380 n = data1_get_root_tag (dh, n);
382 if (n->root->u.root.absyn &&
384 data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
387 yaz_log(YLOG_WARN, "Unable to locate tag for 'wellKnown'");
388 wellknown_tag = odr_malloc(o, sizeof(*wellknown_tag));
389 wellknown_tag->which = DATA1T_numeric;
390 wellknown_tag->value.numeric = 19;
391 wellknown_tag->next = 0;
392 wellknown_tag->tagset = odr_malloc(o, sizeof(*wellknown_tag->tagset));
393 wellknown_tag->tagset->type = 1;
394 wellknown_tag->kind = DATA1K_string;
396 return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
401 * c-file-style: "Stroustrup"
402 * indent-tabs-mode: nil
404 * vim: shiftwidth=4 tabstop=8 expandtab