7acb26bbbbadc398210fd2b3eab0680c3c77ba34
[idzebra-moved-to-github.git] / data1 / d1_grs.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 /* converts data1 tree to GRS-1 record */
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #include <yaz/proto.h>
26 #include <yaz/log.h>
27 #include <d1_absyn.h>
28
29 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
30
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);
34
35 static Z_ElementMetaData *get_ElementMetaData(ODR o)
36 {
37     Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
38
39     r->seriesOrder = 0;
40     r->usageRight = 0;
41     r->num_hits = 0;
42     r->hits = 0;
43     r->displayName = 0;
44     r->num_supportedVariants = 0;
45     r->supportedVariants = 0;
46     r->message = 0;
47     r->elementDescriptor = 0;
48     r->surrogateFor = 0;
49     r->surrogateElement = 0;
50     r->other = 0;
51
52     return r;
53 }
54
55 /*
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
59  * sequence.
60  */
61 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
62 {
63     Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
64     data1_node *p;
65
66     v->globalVariantSetId = 0;
67     v->num_triples = num;
68     v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
69
70     /*
71      * cycle back up through the tree of variants
72      * (traversing exactly 'level' variants).
73      */
74     for (p = n, num--; p && num >= 0; p = p->parent, num--)
75     {
76         Z_Triple *t;
77
78         assert(p->which == DATA1N_variant);
79         t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
80         t->variantSetId = 0;
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;
85
86         switch (p->u.variant.type->datatype)
87         {
88             case DATA1K_string:
89                 t->which = Z_Triple_internationalString;
90                 t->value.internationalString =
91                     odr_strdup(o, p->u.variant.value);
92                 break;
93             default:
94                 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
95                         p->u.variant.type->name);
96                 return 0;
97         }
98     }
99     return v;
100 }
101
102 /*
103  * Traverse the variant children of n, constructing a supportedVariant list.
104  */
105 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
106     ODR o)
107 {
108     data1_node *c;
109     
110     for (c = n->child; c; c = c->next)
111         if (c->which == DATA1N_data && level)
112         {
113             if (!m->supportedVariants)
114                 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
115                     D1_VARIANTARRAY);
116             else if (m->num_supportedVariants >= D1_VARIANTARRAY)
117             {
118                 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
119                         D1_VARIANTARRAY);
120                 return -1;
121             }
122
123             if (!(m->supportedVariants[m->num_supportedVariants++] =
124                 make_variant(n, level, o)))
125                 return -1;
126         }
127         else if (c->which == DATA1N_variant)
128             if (traverse_triples(c, level+1, m, o) < 0)
129                 return -1;
130     return 0;
131 }
132
133 /*
134  * Locate some data under this node. This routine should handle variants
135  * prettily.
136  */
137 static char *get_data(data1_node *n, int *len)
138 {
139     char *r;
140     data1_node *np = 0;
141
142     while (n)
143     {
144         if (n->which == DATA1N_data)
145         {
146             int i;
147             *len = n->u.data.len;
148
149             for (i = 0; i<*len; i++)
150                 if (!d1_isspace(n->u.data.data[i]))
151                     break;
152             while (*len && d1_isspace(n->u.data.data[*len - 1]))
153                 (*len)--;
154             *len = *len - i;
155             if (*len > 0)
156                 return n->u.data.data + i;
157         }
158         if (n->which == DATA1N_tag)
159             np = n->child;
160         n = n->next;
161         if (!n)
162         {
163             n = np;
164             np = 0;
165         }
166     }
167     r = "";
168     *len = strlen(r);
169     return r;
170 }
171
172 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
173                                         int select, int leaf,
174                                         ODR o, int *len,
175                                         data1_tag *wellknown_tag)
176 {
177     Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
178
179     if (!n)
180     {
181         res->which = Z_ElementData_elementNotThere;
182         res->u.elementNotThere = odr_nullval();
183     }
184     else if (n->which == DATA1N_data && leaf)
185     {
186         char str[64], *cp;
187         int toget = n->u.data.len;
188
189         cp = get_data (n, &toget);
190
191         switch (n->u.data.what)
192         {
193         case DATA1I_num:
194             res->which = Z_ElementData_numeric;
195             res->u.numeric = (int *)odr_malloc(o, sizeof(int));
196             *res->u.numeric = atoi_n (cp, toget);
197             *len += 4;
198             break;
199         case DATA1I_text:
200         case DATA1I_xmltext:
201             res->which = Z_ElementData_string;
202             res->u.string = (char *)odr_malloc(o, toget+1);
203             if (toget)
204                 memcpy(res->u.string, cp, toget);
205             res->u.string[toget] = '\0';
206             *len += toget;
207             break;
208         case DATA1I_oid:
209             res->which = Z_ElementData_oid;
210             if (toget > 63)
211                 toget = 63;
212             memcpy (str, cp, toget);
213             str[toget] = '\0';
214             res->u.oid = odr_getoidbystr(o, str);
215             *len += oid_oidlen(res->u.oid) * sizeof(int);
216             break;
217         default:
218             yaz_log(YLOG_WARN, "Can't handle datatype.");
219             return 0;
220         }
221     }
222     else
223     {
224         res->which = Z_ElementData_subtree;
225         if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
226                                                  wellknown_tag )))
227             return 0;
228     }
229     return res;
230 }
231
232 static int is_empty_data (data1_node *n)
233 {
234     if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
235                                 || n->u.data.what == DATA1I_xmltext))
236     {
237         int i = n->u.data.len;
238         
239         while (i > 0 && d1_isspace(n->u.data.data[i-1]))
240             i--;
241         if (i == 0)
242             return 1;
243     }
244     return 0;
245 }
246
247
248 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
249                                             int select, ODR o,
250                                             int *len,
251                                             data1_tag *wellknown_tag)
252 {
253     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
254     data1_tag *tag = 0;
255     data1_node *data;
256     int leaf = 0;
257
258     if (n->which == DATA1N_tag)
259     {
260         if (n->u.tag.element)
261             tag = n->u.tag.element->tag;
262         data = n->child;
263
264         /* skip empty data children */
265         while (is_empty_data(data))
266             data = data->next;
267         if (!data)
268             data = n->child;
269         else
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))
273                 sub = sub->next;
274             if (!sub)
275                 leaf = 1;  /* all empty. 'data' is the only child */
276         }
277     }
278     /*
279      * If we're a data element at this point, we need to insert a
280      * wellKnown tag to wrap us up.
281      */
282     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
283     {
284         tag = wellknown_tag;
285         if (!tag)
286             return 0;
287         data = n;
288         leaf = 1;
289         if (is_empty_data(data))
290             return 0;
291     }
292     else
293     {
294         yaz_log(YLOG_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, wellknown_tag)))
348         return 0;
349     *len += 10;
350     return res;
351 }
352
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)
356 {
357     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
358     data1_node *c;
359     int num_children = 0;
360
361     for (c = n->child; c; c = c->next)
362         num_children++;
363
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)
368     {
369         if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
370             continue;
371         if ((res->elements[res->num_elements] =
372              nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
373             res->num_elements++;
374     }
375     return res;
376 }
377
378 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
379                                 int select, ODR o, int *len)
380
381 {
382     data1_tag *wellknown_tag = 0;
383
384     if (n->which == DATA1N_root)
385         n = data1_get_root_tag (dh, n);
386
387     if (n->root->u.root.absyn &&
388         !(wellknown_tag =
389           data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
390                               "wellKnown")))
391     {
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;
400     }
401     return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
402 }
403 /*
404  * Local variables:
405  * c-basic-offset: 4
406  * indent-tabs-mode: nil
407  * End:
408  * vim: shiftwidth=4 tabstop=8 expandtab
409  */
410