2007.
[idzebra-moved-to-github.git] / data1 / d1_grs.c
1 /* $Id: d1_grs.c,v 1.10 2007-01-15 15:10:14 adam Exp $
2    Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
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 <d1_absyn.h>
31
32 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
33
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);
37
38 static Z_ElementMetaData *get_ElementMetaData(ODR o)
39 {
40     Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
41
42     r->seriesOrder = 0;
43     r->usageRight = 0;
44     r->num_hits = 0;
45     r->hits = 0;
46     r->displayName = 0;
47     r->num_supportedVariants = 0;
48     r->supportedVariants = 0;
49     r->message = 0;
50     r->elementDescriptor = 0;
51     r->surrogateFor = 0;
52     r->surrogateElement = 0;
53     r->other = 0;
54
55     return r;
56 }
57
58 /*
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
62  * sequence.
63  */
64 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
65 {
66     Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
67     data1_node *p;
68
69     v->globalVariantSetId = 0;
70     v->num_triples = num;
71     v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
72
73     /*
74      * cycle back up through the tree of variants
75      * (traversing exactly 'level' variants).
76      */
77     for (p = n, num--; p && num >= 0; p = p->parent, num--)
78     {
79         Z_Triple *t;
80
81         assert(p->which == DATA1N_variant);
82         t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
83         t->variantSetId = 0;
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;
88
89         switch (p->u.variant.type->datatype)
90         {
91             case DATA1K_string:
92                 t->which = Z_Triple_internationalString;
93                 t->value.internationalString =
94                     odr_strdup(o, p->u.variant.value);
95                 break;
96             default:
97                 yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
98                         p->u.variant.type->name);
99                 return 0;
100         }
101     }
102     return v;
103 }
104
105 /*
106  * Traverse the variant children of n, constructing a supportedVariant list.
107  */
108 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
109     ODR o)
110 {
111     data1_node *c;
112     
113     for (c = n->child; c; c = c->next)
114         if (c->which == DATA1N_data && level)
115         {
116             if (!m->supportedVariants)
117                 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
118                     D1_VARIANTARRAY);
119             else if (m->num_supportedVariants >= D1_VARIANTARRAY)
120             {
121                 yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
122                         D1_VARIANTARRAY);
123                 return -1;
124             }
125
126             if (!(m->supportedVariants[m->num_supportedVariants++] =
127                 make_variant(n, level, o)))
128                 return -1;
129         }
130         else if (c->which == DATA1N_variant)
131             if (traverse_triples(c, level+1, m, o) < 0)
132                 return -1;
133     return 0;
134 }
135
136 /*
137  * Locate some data under this node. This routine should handle variants
138  * prettily.
139  */
140 static char *get_data(data1_node *n, int *len)
141 {
142     char *r;
143     data1_node *np = 0;
144
145     while (n)
146     {
147         if (n->which == DATA1N_data)
148         {
149             int i;
150             *len = n->u.data.len;
151
152             for (i = 0; i<*len; i++)
153                 if (!d1_isspace(n->u.data.data[i]))
154                     break;
155             while (*len && d1_isspace(n->u.data.data[*len - 1]))
156                 (*len)--;
157             *len = *len - i;
158             if (*len > 0)
159                 return n->u.data.data + i;
160         }
161         if (n->which == DATA1N_tag)
162             np = n->child;
163         n = n->next;
164         if (!n)
165         {
166             n = np;
167             np = 0;
168         }
169     }
170     r = "";
171     *len = strlen(r);
172     return r;
173 }
174
175 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
176                                         int select, int leaf,
177                                         ODR o, int *len,
178                                         data1_tag *wellknown_tag)
179 {
180     Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
181
182     if (!n)
183     {
184         res->which = Z_ElementData_elementNotThere;
185         res->u.elementNotThere = odr_nullval();
186     }
187     else if (n->which == DATA1N_data && leaf)
188     {
189         char str[64], *cp;
190         int toget = n->u.data.len;
191
192         cp = get_data (n, &toget);
193
194         switch (n->u.data.what)
195         {
196         case DATA1I_num:
197             res->which = Z_ElementData_numeric;
198             res->u.numeric = (int *)odr_malloc(o, sizeof(int));
199             *res->u.numeric = atoi_n (cp, toget);
200             *len += 4;
201             break;
202         case DATA1I_text:
203         case DATA1I_xmltext:
204             res->which = Z_ElementData_string;
205             res->u.string = (char *)odr_malloc(o, toget+1);
206             if (toget)
207                 memcpy(res->u.string, cp, toget);
208             res->u.string[toget] = '\0';
209             *len += toget;
210             break;
211         case DATA1I_oid:
212             res->which = Z_ElementData_oid;
213             if (toget > 63)
214                 toget = 63;
215             memcpy (str, cp, toget);
216             str[toget] = '\0';
217             res->u.oid = odr_getoidbystr(o, str);
218             *len += oid_oidlen(res->u.oid) * sizeof(int);
219             break;
220         default:
221             yaz_log(YLOG_WARN, "Can't handle datatype.");
222             return 0;
223         }
224     }
225     else
226     {
227         res->which = Z_ElementData_subtree;
228         if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
229                                                  wellknown_tag )))
230             return 0;
231     }
232     return res;
233 }
234
235 static int is_empty_data (data1_node *n)
236 {
237     if (n && n->which == DATA1N_data && (n->u.data.what == DATA1I_text
238                                 || n->u.data.what == DATA1I_xmltext))
239     {
240         int i = n->u.data.len;
241         
242         while (i > 0 && d1_isspace(n->u.data.data[i-1]))
243             i--;
244         if (i == 0)
245             return 1;
246     }
247     return 0;
248 }
249
250
251 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
252                                             int select, ODR o,
253                                             int *len,
254                                             data1_tag *wellknown_tag)
255 {
256     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
257     data1_tag *tag = 0;
258     data1_node *data;
259     int leaf = 0;
260
261     if (n->which == DATA1N_tag)
262     {
263         if (n->u.tag.element)
264             tag = n->u.tag.element->tag;
265         data = n->child;
266
267         /* skip empty data children */
268         while (is_empty_data(data))
269             data = data->next;
270         if (!data)
271             data = n->child;
272         else
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))
276                 sub = sub->next;
277             if (!sub)
278                 leaf = 1;  /* all empty. 'data' is the only child */
279         }
280     }
281     /*
282      * If we're a data element at this point, we need to insert a
283      * wellKnown tag to wrap us up.
284      */
285     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
286     {
287         tag = wellknown_tag;
288         if (!tag)
289             return 0;
290         data = n;
291         leaf = 1;
292         if (is_empty_data(data))
293             return 0;
294     }
295     else
296     {
297         yaz_log(YLOG_WARN, "Bad data.");
298         return 0;
299     }
300
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)
305     {
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;
309     }
310     else
311     {
312         char *tagstr;
313
314         if (n->which == DATA1N_tag)      
315             tagstr = n->u.tag.tag;       /* tag at node */
316         else if (tag)                    
317             tagstr = tag->value.string;  /* no take from well-known */
318         else
319             return 0;
320         res->tagValue->which = Z_StringOrNumeric_string;
321         res->tagValue->u.string = odr_strdup(o, tagstr);
322     }
323     res->tagOccurrence = 0;
324     res->appliedVariant = 0;
325     res->metaData = 0;
326     if (n->which == DATA1N_variant || (data && data->which ==
327         DATA1N_variant && data->next == NULL))
328     {
329         int nvars = 0;
330
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)
334                 return 0;
335         while (data && data->which == DATA1N_variant)
336         {
337             nvars++;
338             data = data->child;
339         }
340         if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
341             res->appliedVariant = make_variant(data->parent, nvars-1, o);
342     }
343     if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
344     {
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();
348     }
349     else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
350                                                  o, len, wellknown_tag)))
351         return 0;
352     *len += 10;
353     return res;
354 }
355
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)
359 {
360     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
361     data1_node *c;
362     int num_children = 0;
363
364     for (c = n->child; c; c = c->next)
365         num_children++;
366
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)
371     {
372         if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
373             continue;
374         if ((res->elements[res->num_elements] =
375              nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
376             res->num_elements++;
377     }
378     return res;
379 }
380
381 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
382                                 int select, ODR o, int *len)
383
384 {
385     data1_tag *wellknown_tag = 0;
386
387     if (n->which == DATA1N_root)
388         n = data1_get_root_tag (dh, n);
389
390     if (n->root->u.root.absyn &&
391         !(wellknown_tag =
392           data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
393                               "wellKnown")))
394     {
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;
403     }
404     return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
405 }
406 /*
407  * Local variables:
408  * c-basic-offset: 4
409  * indent-tabs-mode: nil
410  * End:
411  * vim: shiftwidth=4 tabstop=8 expandtab
412  */
413