1 /* $Id: d1_grs.c,v 1.7 2005-01-15 19:38:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* converts data1 tree to GRS-1 record */
28 #include <yaz/proto.h>
32 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
34 static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
35 int select, ODR o, int *len,
36 data1_tag *wellknown_tag);
38 static Z_ElementMetaData *get_ElementMetaData(ODR o)
40 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
47 r->num_supportedVariants = 0;
48 r->supportedVariants = 0;
50 r->elementDescriptor = 0;
52 r->surrogateElement = 0;
59 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
60 * from each of the triples beginning (ending) with 'n', up to the
61 * nearest parent tag. num should equal the number of triples in the
64 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
66 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
69 v->globalVariantSetId = 0;
71 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
74 * cycle back up through the tree of variants
75 * (traversing exactly 'level' variants).
77 for (p = n, num--; p && num >= 0; p = p->parent, num--)
81 assert(p->which == DATA1N_variant);
82 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
84 t->zclass = (int *)odr_malloc(o, sizeof(int));
85 *t->zclass = p->u.variant.type->zclass->zclass;
86 t->type = (int *)odr_malloc(o, sizeof(int));
87 *t->type = p->u.variant.type->type;
89 switch (p->u.variant.type->datatype)
92 t->which = Z_Triple_internationalString;
93 t->value.internationalString =
94 odr_strdup(o, p->u.variant.value);
97 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
98 p->u.variant.type->name);
106 * Traverse the variant children of n, constructing a supportedVariant list.
108 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
113 for (c = n->child; c; c = c->next)
114 if (c->which == DATA1N_data && level)
116 if (!m->supportedVariants)
117 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
119 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
121 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
126 if (!(m->supportedVariants[m->num_supportedVariants++] =
127 make_variant(n, level, o)))
130 else if (c->which == DATA1N_variant)
131 if (traverse_triples(c, level+1, m, o) < 0)
137 * Locate some data under this node. This routine should handle variants
140 static char *get_data(data1_node *n, int *len)
147 if (n->which == DATA1N_data)
150 *len = n->u.data.len;
152 for (i = 0; i<*len; i++)
153 if (!d1_isspace(n->u.data.data[i]))
155 while (*len && d1_isspace(n->u.data.data[*len - 1]))
159 return n->u.data.data + i;
161 if (n->which == DATA1N_tag)
175 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
176 int select, int leaf,
178 data1_tag *wellknown_tag)
180 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
184 res->which = Z_ElementData_elementNotThere;
185 res->u.elementNotThere = odr_nullval();
187 else if (n->which == DATA1N_data && leaf)
190 int toget = n->u.data.len;
192 cp = get_data (n, &toget);
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 (cp, toget);
204 res->which = Z_ElementData_string;
205 res->u.string = (char *)odr_malloc(o, toget+1);
207 memcpy(res->u.string, cp, toget);
208 res->u.string[toget] = '\0';
212 res->which = Z_ElementData_oid;
215 memcpy (str, cp, toget);
217 res->u.oid = odr_getoidbystr(o, str);
218 *len += oid_oidlen(res->u.oid) * sizeof(int);
221 yaz_log(YLOG_WARN, "Can't handle datatype.");
227 res->which = Z_ElementData_subtree;
228 if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
235 static int is_empty_data (data1_node *n)
237 if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
238 || n->u.data.what == DATA1I_xmltext))
240 int i = n->u.data.len;
242 while (i > 0 && d1_isspace(n->u.data.data[i-1]))
251 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
254 data1_tag *wellknown_tag)
256 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
261 if (n->which == DATA1N_tag)
263 if (n->u.tag.element)
264 tag = n->u.tag.element->tag;
267 /* skip empty data children */
268 while (is_empty_data(data))
273 { /* got one. see if this is the only non-empty one */
274 data1_node *sub = data->next;
275 while (sub && is_empty_data(sub))
278 leaf = 1; /* all empty. 'data' is the only child */
282 * If we're a data element at this point, we need to insert a
283 * wellKnown tag to wrap us up.
285 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
292 if (is_empty_data(data))
297 yaz_log(YLOG_WARN, "Bad data.");
301 res->tagType = (int *)odr_malloc(o, sizeof(int));
302 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
303 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
304 if (tag && tag->which == DATA1T_numeric)
306 res->tagValue->which = Z_StringOrNumeric_numeric;
307 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
308 *res->tagValue->u.numeric = tag->value.numeric;
314 if (n->which == DATA1N_tag)
315 tagstr = n->u.tag.tag; /* tag at node */
317 tagstr = tag->value.string; /* no take from well-known */
320 res->tagValue->which = Z_StringOrNumeric_string;
321 res->tagValue->u.string = odr_strdup(o, tagstr);
323 res->tagOccurrence = 0;
324 res->appliedVariant = 0;
326 if (n->which == DATA1N_variant || (data && data->which ==
327 DATA1N_variant && data->next == NULL))
331 res->metaData = get_ElementMetaData(o);
332 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
333 if (traverse_triples(data, 0, res->metaData, o) < 0)
335 while (data && data->which == DATA1N_variant)
340 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
341 res->appliedVariant = make_variant(data->parent, nvars-1, o);
343 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
345 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
346 res->content->which = Z_ElementData_noDataRequested;
347 res->content->u.noDataRequested = odr_nullval();
349 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
350 o, len, wellknown_tag)))
356 static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
357 int select, ODR o, int *len,
358 data1_tag *wellknown_tag)
360 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
362 int num_children = 0;
364 for (c = n->child; c; c = c->next)
367 res->elements = (Z_TaggedElement **)
368 odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
369 res->num_elements = 0;
370 for (c = n->child; c; c = c->next)
372 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
374 if ((res->elements[res->num_elements] =
375 nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
381 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
382 int select, ODR o, int *len)
385 data1_tag *wellknown_tag = 0;
387 if (n->which == DATA1N_root)
388 n = data1_get_root_tag (dh, n);
390 if (n->root->u.root.absyn &&
392 data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
395 yaz_log(YLOG_WARN, "Unable to locate tag for 'wellKnown'");
396 wellknown_tag = odr_malloc(o, sizeof(*wellknown_tag));
397 wellknown_tag->which = DATA1T_numeric;
398 wellknown_tag->value.numeric = 19;
399 wellknown_tag->next = 0;
400 wellknown_tag->tagset = odr_malloc(o, sizeof(*wellknown_tag->tagset));
401 wellknown_tag->tagset->type = 1;
402 wellknown_tag->kind = DATA1K_string;
404 return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);