change license for data1 source
[idzebra-moved-to-github.git] / data1 / d1_grs.c
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
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 /* converts data1 tree to GRS-1 record */
24
25 #include <assert.h>
26 #include <stdlib.h>
27
28 #include <yaz/proto.h>
29 #include <yaz/log.h>
30 #include <data1.h>
31
32 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
33
34 static Z_ElementMetaData *get_ElementMetaData(ODR o)
35 {
36     Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
37
38     r->seriesOrder = 0;
39     r->usageRight = 0;
40     r->num_hits = 0;
41     r->hits = 0;
42     r->displayName = 0;
43     r->num_supportedVariants = 0;
44     r->supportedVariants = 0;
45     r->message = 0;
46     r->elementDescriptor = 0;
47     r->surrogateFor = 0;
48     r->surrogateElement = 0;
49     r->other = 0;
50
51     return r;
52 }
53
54 /*
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
58  * sequence.
59  */
60 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
61 {
62     Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
63     data1_node *p;
64
65     v->globalVariantSetId = 0;
66     v->num_triples = num;
67     v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
68
69     /*
70      * cycle back up through the tree of variants
71      * (traversing exactly 'level' variants).
72      */
73     for (p = n, num--; p && num >= 0; p = p->parent, num--)
74     {
75         Z_Triple *t;
76
77         assert(p->which == DATA1N_variant);
78         t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
79         t->variantSetId = 0;
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;
84
85         switch (p->u.variant.type->datatype)
86         {
87             case DATA1K_string:
88                 t->which = Z_Triple_internationalString;
89                 t->value.internationalString =
90                     odr_strdup(o, p->u.variant.value);
91                 break;
92             default:
93                 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
94                         p->u.variant.type->name);
95                 return 0;
96         }
97     }
98     return v;
99 }
100
101 /*
102  * Traverse the variant children of n, constructing a supportedVariant list.
103  */
104 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
105     ODR o)
106 {
107     data1_node *c;
108     
109     for (c = n->child; c; c = c->next)
110         if (c->which == DATA1N_data && level)
111         {
112             if (!m->supportedVariants)
113                 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
114                     D1_VARIANTARRAY);
115             else if (m->num_supportedVariants >= D1_VARIANTARRAY)
116             {
117                 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
118                         D1_VARIANTARRAY);
119                 return -1;
120             }
121
122             if (!(m->supportedVariants[m->num_supportedVariants++] =
123                 make_variant(n, level, o)))
124                 return -1;
125         }
126         else if (c->which == DATA1N_variant)
127             if (traverse_triples(c, level+1, m, o) < 0)
128                 return -1;
129     return 0;
130 }
131
132 /*
133  * Locate some data under this node. This routine should handle variants
134  * prettily.
135  */
136 static char *get_data(data1_node *n, int *len)
137 {
138     char *r;
139     data1_node *np = 0;
140
141     while (n)
142     {
143         if (n->which == DATA1N_data)
144         {
145             int i;
146             *len = n->u.data.len;
147
148             for (i = 0; i<*len; i++)
149                 if (!d1_isspace(n->u.data.data[i]))
150                     break;
151             while (*len && d1_isspace(n->u.data.data[*len - 1]))
152                 (*len)--;
153             *len = *len - i;
154             if (*len > 0)
155                 return n->u.data.data + i;
156         }
157         if (n->which == DATA1N_tag)
158             np = n->child;
159         n = n->next;
160         if (!n)
161         {
162             n = np;
163             np = 0;
164         }
165     }
166     r = "";
167     *len = strlen(r);
168     return r;
169 }
170
171 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
172                                         int select, int leaf,
173                                         ODR o, int *len)
174 {
175     Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
176
177     if (!n)
178     {
179         res->which = Z_ElementData_elementNotThere;
180         res->u.elementNotThere = odr_nullval();
181     }
182     else if (n->which == DATA1N_data && leaf)
183     {
184         char str[64], *cp;
185         int toget = n->u.data.len;
186
187         cp = get_data (n, &toget);
188
189         switch (n->u.data.what)
190         {
191         case DATA1I_num:
192             res->which = Z_ElementData_numeric;
193             res->u.numeric = (int *)odr_malloc(o, sizeof(int));
194             *res->u.numeric = atoi_n (cp, toget);
195             *len += 4;
196             break;
197         case DATA1I_text:
198         case DATA1I_xmltext:
199             res->which = Z_ElementData_string;
200             res->u.string = (char *)odr_malloc(o, toget+1);
201             if (toget)
202                 memcpy(res->u.string, cp, toget);
203             res->u.string[toget] = '\0';
204             *len += toget;
205             break;
206         case DATA1I_oid:
207             res->which = Z_ElementData_oid;
208             if (toget > 63)
209                 toget = 63;
210             memcpy (str, cp, toget);
211             str[toget] = '\0';
212             res->u.oid = odr_getoidbystr(o, str);
213             *len += oid_oidlen(res->u.oid) * sizeof(int);
214             break;
215         default:
216             yaz_log(LOG_WARN, "Can't handle datatype.");
217             return 0;
218         }
219     }
220     else
221     {
222         res->which = Z_ElementData_subtree;
223         if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
224             return 0;
225     }
226     return res;
227 }
228
229 static int is_empty_data (data1_node *n)
230 {
231     if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
232                                 || n->u.data.what == DATA1I_xmltext))
233     {
234         int i = n->u.data.len;
235         
236         while (i > 0 && strchr("\n ", n->u.data.data[i-1]))
237             i--;
238         if (i == 0)
239             return 1;
240     }
241     return 0;
242 }
243
244
245 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
246                                             int select, ODR o,
247                                             int *len)
248 {
249     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
250     data1_tag *tag = 0;
251     data1_node *data;
252     int leaf = 0;
253
254     if (n->which == DATA1N_tag)
255     {
256         if (n->u.tag.element)
257             tag = n->u.tag.element->tag;
258         data = n->child;
259
260         /* skip empty data children */
261         while (is_empty_data(data))
262             data = data->next;
263         if (!data)
264             data = n->child;
265         else
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))
269                 sub = sub->next;
270             if (!sub)
271                 leaf = 1;  /* all empty. 'data' is the only child */
272         }
273     }
274     /*
275      * If we're a data element at this point, we need to insert a
276      * wellKnown tag to wrap us up.
277      */
278     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
279     {
280         if (n->root->u.root.absyn &&
281             !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
282                                         "wellKnown")))
283         {
284             yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
285             return 0;
286         }
287         data = n;
288         leaf = 1;
289         if (is_empty_data(data))
290             return 0;
291     }
292     else
293     {
294         yaz_log(LOG_WARN, "Bad data.");
295         return 0;
296     }
297
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)
302     {
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;
306     }
307     else
308     {
309         char *tagstr;
310
311         if (n->which == DATA1N_tag)      
312             tagstr = n->u.tag.tag;       /* tag at node */
313         else if (tag)                    
314             tagstr = tag->value.string;  /* no take from well-known */
315         else
316             return 0;
317         res->tagValue->which = Z_StringOrNumeric_string;
318         res->tagValue->u.string = odr_strdup(o, tagstr);
319     }
320     res->tagOccurrence = 0;
321     res->appliedVariant = 0;
322     res->metaData = 0;
323     if (n->which == DATA1N_variant || (data && data->which ==
324         DATA1N_variant && data->next == NULL))
325     {
326         int nvars = 0;
327
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)
331                 return 0;
332         while (data && data->which == DATA1N_variant)
333         {
334             nvars++;
335             data = data->child;
336         }
337         if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
338             res->appliedVariant = make_variant(data->parent, nvars-1, o);
339     }
340     if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
341     {
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();
345     }
346     else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
347                                                  o, len)))
348         return 0;
349     *len += 10;
350     return res;
351 }
352
353 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
354                                 int select, ODR o, int *len)
355 {
356     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
357     data1_node *c;
358     int num_children = 0;
359
360     if (n->which == DATA1N_root)
361         n = data1_get_root_tag (dh, n);
362         
363     for (c = n->child; c; c = c->next)
364         num_children++;
365
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)
370     {
371         if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
372             continue;
373         if ((res->elements[res->num_elements] =
374              nodetotaggedelement (dh, c, select, o, len)))
375             res->num_elements++;
376     }
377     return res;
378 }