Happy new year.
[idzebra-moved-to-github.git] / data1 / d1_grs.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 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 = odr_intdup(o, p->u.variant.type->zclass->zclass);
82         t->type = odr_intdup(o, p->u.variant.type->type);
83
84         switch (p->u.variant.type->datatype)
85         {
86             case DATA1K_string:
87                 t->which = Z_Triple_internationalString;
88                 t->value.internationalString =
89                     odr_strdup(o, p->u.variant.value);
90                 break;
91             default:
92                 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
93                         p->u.variant.type->name);
94                 return 0;
95         }
96     }
97     return v;
98 }
99
100 /*
101  * Traverse the variant children of n, constructing a supportedVariant list.
102  */
103 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
104     ODR o)
105 {
106     data1_node *c;
107     
108     for (c = n->child; c; c = c->next)
109         if (c->which == DATA1N_data && level)
110         {
111             if (!m->supportedVariants)
112                 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
113                     D1_VARIANTARRAY);
114             else if (m->num_supportedVariants >= D1_VARIANTARRAY)
115             {
116                 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
117                         D1_VARIANTARRAY);
118                 return -1;
119             }
120
121             if (!(m->supportedVariants[m->num_supportedVariants++] =
122                 make_variant(n, level, o)))
123                 return -1;
124         }
125         else if (c->which == DATA1N_variant)
126             if (traverse_triples(c, level+1, m, o) < 0)
127                 return -1;
128     return 0;
129 }
130
131 /*
132  * Locate some data under this node. This routine should handle variants
133  * prettily.
134  */
135 static char *get_data(data1_node *n, int *len)
136 {
137     char *r;
138     data1_node *np = 0;
139
140     while (n)
141     {
142         if (n->which == DATA1N_data)
143         {
144             int i;
145             *len = n->u.data.len;
146
147             for (i = 0; i<*len; i++)
148                 if (!d1_isspace(n->u.data.data[i]))
149                     break;
150             while (*len && d1_isspace(n->u.data.data[*len - 1]))
151                 (*len)--;
152             *len = *len - i;
153             if (*len > 0)
154                 return n->u.data.data + i;
155         }
156         if (n->which == DATA1N_tag)
157             np = n->child;
158         n = n->next;
159         if (!n)
160         {
161             n = np;
162             np = 0;
163         }
164     }
165     r = "";
166     *len = strlen(r);
167     return r;
168 }
169
170 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
171                                         int select, int leaf,
172                                         ODR o, int *len,
173                                         data1_tag *wellknown_tag)
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 = odr_intdup(o, atoi_n(cp, toget));
194             *len += 4;
195             break;
196         case DATA1I_text:
197         case DATA1I_xmltext:
198             res->which = Z_ElementData_string;
199             res->u.string = (char *)odr_malloc(o, toget+1);
200             if (toget)
201                 memcpy(res->u.string, cp, toget);
202             res->u.string[toget] = '\0';
203             *len += toget;
204             break;
205         case DATA1I_oid:
206             res->which = Z_ElementData_oid;
207             if (toget > 63)
208                 toget = 63;
209             memcpy (str, cp, toget);
210             str[toget] = '\0';
211             res->u.oid = odr_getoidbystr(o, str);
212             *len += oid_oidlen(res->u.oid) * sizeof(int);
213             break;
214         default:
215             yaz_log(YLOG_WARN, "Can't handle datatype.");
216             return 0;
217         }
218     }
219     else
220     {
221         res->which = Z_ElementData_subtree;
222         if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
223                                                  wellknown_tag )))
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 && d1_isspace(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                                             data1_tag *wellknown_tag)
249 {
250     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
251     data1_tag *tag = 0;
252     data1_node *data;
253     int leaf = 0;
254
255     if (n->which == DATA1N_tag)
256     {
257         if (n->u.tag.element)
258             tag = n->u.tag.element->tag;
259         data = n->child;
260
261         /* skip empty data children */
262         while (is_empty_data(data))
263             data = data->next;
264         if (!data)
265             data = n->child;
266         else
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))
270                 sub = sub->next;
271             if (!sub)
272                 leaf = 1;  /* all empty. 'data' is the only child */
273         }
274     }
275     /*
276      * If we're a data element at this point, we need to insert a
277      * wellKnown tag to wrap us up.
278      */
279     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
280     {
281         tag = wellknown_tag;
282         if (!tag)
283             return 0;
284         data = n;
285         leaf = 1;
286         if (is_empty_data(data))
287             return 0;
288     }
289     else
290     {
291         yaz_log(YLOG_WARN, "Bad data.");
292         return 0;
293     }
294
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)
298     {
299         res->tagValue->which = Z_StringOrNumeric_numeric;
300         res->tagValue->u.numeric = odr_intdup(o, tag->value.numeric);
301     }
302     else
303     {
304         char *tagstr;
305
306         if (n->which == DATA1N_tag)      
307             tagstr = n->u.tag.tag;       /* tag at node */
308         else if (tag)                    
309             tagstr = tag->value.string;  /* no take from well-known */
310         else
311             return 0;
312         res->tagValue->which = Z_StringOrNumeric_string;
313         res->tagValue->u.string = odr_strdup(o, tagstr);
314     }
315     res->tagOccurrence = 0;
316     res->appliedVariant = 0;
317     res->metaData = 0;
318     if (n->which == DATA1N_variant || (data && data->which ==
319         DATA1N_variant && data->next == NULL))
320     {
321         int nvars = 0;
322
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)
326                 return 0;
327         while (data && data->which == DATA1N_variant)
328         {
329             nvars++;
330             data = data->child;
331         }
332         if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
333             res->appliedVariant = make_variant(data->parent, nvars-1, o);
334     }
335     if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
336     {
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();
340     }
341     else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
342                                                  o, len, wellknown_tag)))
343         return 0;
344     *len += 10;
345     return res;
346 }
347
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)
351 {
352     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
353     data1_node *c;
354     int num_children = 0;
355
356     for (c = n->child; c; c = c->next)
357         num_children++;
358
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)
363     {
364         if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
365             continue;
366         if ((res->elements[res->num_elements] =
367              nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
368             res->num_elements++;
369     }
370     return res;
371 }
372
373 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
374                                 int select, ODR o, int *len)
375
376 {
377     data1_tag *wellknown_tag = 0;
378
379     if (n->which == DATA1N_root)
380         n = data1_get_root_tag (dh, n);
381
382     if (n->root->u.root.absyn &&
383         !(wellknown_tag =
384           data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
385                               "wellKnown")))
386     {
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;
395     }
396     return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
397 }
398 /*
399  * Local variables:
400  * c-basic-offset: 4
401  * c-file-style: "Stroustrup"
402  * indent-tabs-mode: nil
403  * End:
404  * vim: shiftwidth=4 tabstop=8 expandtab
405  */
406