XML reader for data1 (EXPAT)
[yaz-moved-to-github.git] / retrieval / d1_grs.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_grs.c,v $
7  * Revision 1.20  2002-05-13 14:13:37  adam
8  * XML reader for data1 (EXPAT)
9  *
10  * Revision 1.19  2002/04/15 09:06:30  adam
11  * Fix explain tags for XML writer
12  *
13  * Revision 1.18  2002/04/12 14:40:07  adam
14  * GRS-1 writer accepts non-abs
15  *
16  * Revision 1.17  1999/11/30 13:47:12  adam
17  * Improved installation. Moved header files to include/yaz.
18  *
19  * Revision 1.16  1999/08/27 09:40:32  adam
20  * Renamed logf function to yaz_log. Removed VC++ project files.
21  *
22  * Revision 1.15  1999/03/31 11:18:25  adam
23  * Implemented odr_strdup. Added Reference ID to backend server API.
24  *
25  * Revision 1.14  1998/03/16 12:21:15  adam
26  * Fixed problem with tag names that weren't set to the right value
27  * when wildcards were used.
28  *
29  * Revision 1.13  1998/02/11 11:53:35  adam
30  * Changed code so that it compiles as C++.
31  *
32  * Revision 1.12  1997/11/24 11:33:56  adam
33  * Using function odr_nullval() instead of global ODR_NULLVAL when
34  * appropriate.
35  *
36  * Revision 1.11  1997/11/18 09:51:09  adam
37  * Removed element num_children from data1_node. Minor changes in
38  * data1 to Explain.
39  *
40  * Revision 1.10  1997/09/17 12:10:36  adam
41  * YAZ version 1.4.
42  *
43  * Revision 1.9  1997/05/14 06:54:03  adam
44  * C++ support.
45  *
46  * Revision 1.8  1996/12/05 13:17:49  quinn
47  * Fixed GRS-1 null-ref
48  *
49  * Revision 1.7  1996/10/11  11:57:23  quinn
50  * Smallish
51  *
52  * Revision 1.6  1996/07/06  19:58:34  quinn
53  * System headerfiles gathered in yconfig
54  *
55  * Revision 1.5  1996/06/03  09:46:42  quinn
56  * Added OID data type.
57  *
58  * Revision 1.4  1996/05/01  12:45:30  quinn
59  * Support use of local tag names in abs file.
60  *
61  * Revision 1.3  1995/11/13  09:27:35  quinn
62  * Fiddling with the variant stuff.
63  *
64  * Revision 1.2  1995/11/01  13:54:46  quinn
65  * Minor adjustments
66  *
67  * Revision 1.1  1995/11/01  11:56:07  quinn
68  * Added Retrieval (data management) functions en masse.
69  *
70  *
71  */
72
73 #include <assert.h>
74 #include <stdlib.h>
75
76 #include <yaz/proto.h>
77 #include <yaz/log.h>
78 #include <yaz/data1.h>
79
80 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
81
82 static Z_ElementMetaData *get_ElementMetaData(ODR o)
83 {
84     Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
85
86     r->seriesOrder = 0;
87     r->usageRight = 0;
88     r->num_hits = 0;
89     r->hits = 0;
90     r->displayName = 0;
91     r->num_supportedVariants = 0;
92     r->supportedVariants = 0;
93     r->message = 0;
94     r->elementDescriptor = 0;
95     r->surrogateFor = 0;
96     r->surrogateElement = 0;
97     r->other = 0;
98
99     return r;
100 }
101
102 /*
103  * N should point to the *last* (leaf) triple in a sequence. Construct a variant
104  * from each of the triples beginning (ending) with 'n', up to the
105  * nearest parent tag. num should equal the number of triples in the
106  * sequence.
107  */
108 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
109 {
110     Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
111     data1_node *p;
112
113     v->globalVariantSetId = 0;
114     v->num_triples = num;
115     v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
116
117     /*
118      * cycle back up through the tree of variants
119      * (traversing exactly 'level' variants).
120      */
121     for (p = n, num--; p && num >= 0; p = p->parent, num--)
122     {
123         Z_Triple *t;
124
125         assert(p->which == DATA1N_variant);
126         t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
127         t->variantSetId = 0;
128         t->zclass = (int *)odr_malloc(o, sizeof(int));
129         *t->zclass = p->u.variant.type->zclass->zclass;
130         t->type = (int *)odr_malloc(o, sizeof(int));
131         *t->type = p->u.variant.type->type;
132
133         switch (p->u.variant.type->datatype)
134         {
135             case DATA1K_string:
136                 t->which = Z_Triple_internationalString;
137                 t->value.internationalString =
138                     odr_strdup(o, p->u.variant.value);
139                 break;
140             default:
141                 yaz_log(LOG_WARN, "Unable to handle value for variant %s",
142                         p->u.variant.type->name);
143                 return 0;
144         }
145     }
146     return v;
147 }
148
149 /*
150  * Traverse the variant children of n, constructing a supportedVariant list.
151  */
152 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
153     ODR o)
154 {
155     data1_node *c;
156     
157     for (c = n->child; c; c = c->next)
158         if (c->which == DATA1N_data && level)
159         {
160             if (!m->supportedVariants)
161                 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
162                     D1_VARIANTARRAY);
163             else if (m->num_supportedVariants >= D1_VARIANTARRAY)
164             {
165                 yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
166                         D1_VARIANTARRAY);
167                 return -1;
168             }
169
170             if (!(m->supportedVariants[m->num_supportedVariants++] =
171                 make_variant(n, level, o)))
172                 return -1;
173         }
174         else if (c->which == DATA1N_variant)
175             if (traverse_triples(c, level+1, m, o) < 0)
176                 return -1;
177     return 0;
178 }
179
180 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
181                                         int select, int leaf,
182                                         ODR o, int *len)
183 {
184     Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
185
186     if (!n)
187     {
188         res->which = Z_ElementData_elementNotThere;
189         res->u.elementNotThere = odr_nullval();
190     }
191     else if (n->which == DATA1N_data && (leaf || n->next == NULL))
192     {
193         char str[512];
194         int toget;
195         data1_node *p;
196
197         for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
198             ;
199
200         switch (n->u.data.what)
201         {
202             case DATA1I_num:
203                 res->which = Z_ElementData_numeric;
204                 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
205                 *res->u.numeric = atoi(n->u.data.data);
206                 *len += 4;
207                 break;
208             case DATA1I_text:
209                 toget = n->u.data.len;
210                 if (p && p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
211                     toget = p->u.tag.get_bytes;
212                 res->which = Z_ElementData_string;
213                 res->u.string = (char *)odr_malloc(o, toget+1);
214                 memcpy(res->u.string, n->u.data.data, toget);
215                 res->u.string[toget] = '\0';
216                 *len += toget;
217                 break;
218             case DATA1I_oid:
219                 res->which = Z_ElementData_oid;
220                 strncpy(str, n->u.data.data, n->u.data.len);
221                 str[n->u.data.len] = '\0';
222                 res->u.oid = odr_getoidbystr(o, str);
223                 *len += n->u.data.len;
224                 break;
225             default:
226                 yaz_log(LOG_WARN, "Can't handle datatype.");
227                 return 0;
228         }
229     }
230     else
231     {
232         res->which = Z_ElementData_subtree;
233         if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
234             return 0;
235     }
236     return res;
237 }
238
239 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
240                                             int select, ODR o,
241                                             int *len)
242 {
243     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
244     data1_tag *tag = 0;
245     data1_node *data;
246     int leaf;
247
248     if (n->which == DATA1N_tag)
249     {
250         if (n->u.tag.element)
251             tag = n->u.tag.element->tag;
252         data = n->child;
253         leaf = 0;
254     }
255     /*
256      * If we're a data element at this point, we need to insert a
257      * wellKnown tag to wrap us up.
258      */
259     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
260     {
261         if (n->root->u.root.absyn &&
262             !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
263                                         "wellKnown")))
264         {
265             yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
266             return 0;
267         }
268         data = n;
269         leaf = 1;
270     }
271     else
272     {
273         yaz_log(LOG_WARN, "Bad data.");
274         return 0;
275     }
276
277     res->tagType = (int *)odr_malloc(o, sizeof(int));
278     *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
279     res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
280     if (tag && tag->which == DATA1T_numeric)
281     {
282         res->tagValue->which = Z_StringOrNumeric_numeric;
283         res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
284         *res->tagValue->u.numeric = tag->value.numeric;
285     }
286     else
287     {
288         char *tagstr;
289
290         if (n->which == DATA1N_tag)      
291             tagstr = n->u.tag.tag;       /* tag at node */
292         else if (tag)                    
293             tagstr = tag->value.string;  /* no take from well-known */
294         else
295             tagstr = "???";                /* no tag at all! */
296         res->tagValue->which = Z_StringOrNumeric_string;
297         res->tagValue->u.string = odr_strdup(o, tagstr);
298     }
299     res->tagOccurrence = 0;
300     res->appliedVariant = 0;
301     res->metaData = 0;
302     if (n->which == DATA1N_variant || (data && data->which ==
303         DATA1N_variant && data->next == NULL))
304     {
305         int nvars = 0;
306
307         res->metaData = get_ElementMetaData(o);
308         if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
309             if (traverse_triples(data, 0, res->metaData, o) < 0)
310                 return 0;
311         while (data && data->which == DATA1N_variant)
312         {
313             nvars++;
314             data = data->child;
315         }
316         if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
317             res->appliedVariant = make_variant(data->parent, nvars-1, o);
318     }
319     if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
320     {
321         res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
322         res->content->which = Z_ElementData_noDataRequested;
323         res->content->u.noDataRequested = odr_nullval();
324     }
325     else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
326                                                  o, len)))
327         return 0;
328     *len += 10;
329     return res;
330 }
331
332 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
333                                 int select, ODR o, int *len)
334 {
335     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
336     data1_node *c;
337     int num_children = 0;
338     
339     for (c = n->child; c; c = c->next)
340         num_children++;
341
342     res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
343     res->num_elements = 0;
344     for (c = n->child; c; c = c->next)
345     {
346         if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
347             continue;
348         if (!(res->elements[res->num_elements++] =
349               nodetotaggedelement (dh, c, select, o, len)))
350             return 0;
351     }
352     return res;
353 }