1 /* $Id: d1_grs.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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_ElementMetaData *get_ElementMetaData(ODR o)
36 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
43 r->num_supportedVariants = 0;
44 r->supportedVariants = 0;
46 r->elementDescriptor = 0;
48 r->surrogateElement = 0;
55 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
56 * from each of the triples beginning (ending) with 'n', up to the
57 * nearest parent tag. num should equal the number of triples in the
60 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
62 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
65 v->globalVariantSetId = 0;
67 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
70 * cycle back up through the tree of variants
71 * (traversing exactly 'level' variants).
73 for (p = n, num--; p && num >= 0; p = p->parent, num--)
77 assert(p->which == DATA1N_variant);
78 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
80 t->zclass = (int *)odr_malloc(o, sizeof(int));
81 *t->zclass = p->u.variant.type->zclass->zclass;
82 t->type = (int *)odr_malloc(o, sizeof(int));
83 *t->type = p->u.variant.type->type;
85 switch (p->u.variant.type->datatype)
88 t->which = Z_Triple_internationalString;
89 t->value.internationalString =
90 odr_strdup(o, p->u.variant.value);
93 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
94 p->u.variant.type->name);
102 * Traverse the variant children of n, constructing a supportedVariant list.
104 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
109 for (c = n->child; c; c = c->next)
110 if (c->which == DATA1N_data && level)
112 if (!m->supportedVariants)
113 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
115 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
117 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
122 if (!(m->supportedVariants[m->num_supportedVariants++] =
123 make_variant(n, level, o)))
126 else if (c->which == DATA1N_variant)
127 if (traverse_triples(c, level+1, m, o) < 0)
133 * Locate some data under this node. This routine should handle variants
136 static char *get_data(data1_node *n, int *len)
143 if (n->which == DATA1N_data)
146 *len = n->u.data.len;
148 for (i = 0; i<*len; i++)
149 if (!d1_isspace(n->u.data.data[i]))
151 while (*len && d1_isspace(n->u.data.data[*len - 1]))
155 return n->u.data.data + i;
157 if (n->which == DATA1N_tag)
171 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
172 int select, int leaf,
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 = (int *)odr_malloc(o, sizeof(int));
194 *res->u.numeric = atoi_n (cp, toget);
199 res->which = Z_ElementData_string;
200 res->u.string = (char *)odr_malloc(o, toget+1);
202 memcpy(res->u.string, cp, toget);
203 res->u.string[toget] = '\0';
207 res->which = Z_ElementData_oid;
210 memcpy (str, cp, toget);
212 res->u.oid = odr_getoidbystr(o, str);
213 *len += oid_oidlen(res->u.oid) * sizeof(int);
216 yaz_log(LOG_WARN, "Can't handle datatype.");
222 res->which = Z_ElementData_subtree;
223 if (!(res->u.subtree = data1_nodetogr (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 && strchr("\n ", n->u.data.data[i-1]))
245 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
249 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
254 if (n->which == DATA1N_tag)
256 if (n->u.tag.element)
257 tag = n->u.tag.element->tag;
260 /* skip empty data children */
261 while (is_empty_data(data))
266 { /* got one. see if this is the only non-empty one */
267 data1_node *sub = data->next;
268 while (sub && is_empty_data(sub))
271 leaf = 1; /* all empty. 'data' is the only child */
275 * If we're a data element at this point, we need to insert a
276 * wellKnown tag to wrap us up.
278 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
280 if (n->root->u.root.absyn &&
281 !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
284 yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
289 if (is_empty_data(data))
294 yaz_log(LOG_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,
353 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
354 int select, ODR o, int *len)
356 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
358 int num_children = 0;
360 if (n->which == DATA1N_root)
361 n = data1_get_root_tag (dh, n);
363 for (c = n->child; c; c = c->next)
366 res->elements = (Z_TaggedElement **)
367 odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
368 res->num_elements = 0;
369 for (c = n->child; c; c = c->next)
371 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
373 if ((res->elements[res->num_elements] =
374 nodetotaggedelement (dh, c, select, o, len)))