change license for data1 source
[idzebra-moved-to-github.git] / data1 / d1_map.c
1 /* $Id: d1_map.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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <yaz/oid.h>
28 #include <yaz/log.h>
29 #include <yaz/readconf.h>
30 #include <yaz/tpath.h>
31 #include <data1.h>
32 #include <d1_map.h>
33
34 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
35 {
36     NMEM mem = data1_nmem_get (dh);
37     data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
38     FILE *f;
39     int lineno = 0;
40     int argc;
41     char *argv[50], line[512];
42     data1_mapunit **mapp;
43     int local_numeric = 0;
44
45     if (!(f = data1_path_fopen(dh, file, "r")))
46     {
47         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
48         return 0;
49     }
50
51     res->name = 0;
52     res->target_absyn_ref = VAL_NONE;
53     res->map = 0;
54     mapp = &res->map;
55     res->next = 0;
56
57     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
58         if (!strcmp(argv[0], "targetref"))
59         {
60             if (argc != 2)
61             {
62                 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
63                         file, lineno);
64                 continue;
65             }
66             if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
67                 == VAL_NONE)
68             {
69                 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
70                         file, lineno, argv[1]);
71                 continue;
72             }
73         }
74         else if (!strcmp(argv[0], "targetname"))
75         {
76             if (argc != 2)
77             {
78                 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
79                         file, lineno);
80                 continue;
81             }
82             res->target_absyn_name =
83                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
84             strcpy(res->target_absyn_name, argv[1]);
85         }
86         else if (!yaz_matchstr(argv[0], "localnumeric"))
87             local_numeric = 1;
88         else if (!strcmp(argv[0], "name"))
89         {
90             if (argc != 2)
91             {
92                 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
93                 continue;
94             }
95             res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
96             strcpy(res->name, argv[1]);
97         }
98         else if (!strcmp(argv[0], "map"))
99         {
100             data1_maptag **mtp;
101             char *ep, *path = argv[2];
102
103             if (argc < 3)
104             {
105                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
106                         file, lineno);
107                 continue;
108             }
109             *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
110             (*mapp)->next = 0;
111             if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
112                 (*mapp)->no_data = 1;
113             else
114                 (*mapp)->no_data = 0;
115             (*mapp)->source_element_name =
116                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
117             strcpy((*mapp)->source_element_name, argv[1]);
118             mtp = &(*mapp)->target_path;
119             if (*path == '/')
120                 path++;
121             for (ep = strchr(path, '/'); path; (void)((path = ep) &&
122                 (ep = strchr(path, '/'))))
123             {
124                 int type, np;
125                 char valstr[512], parm[512];
126
127                 if (ep)
128                     ep++;
129                 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
130                     parm)) < 2)
131                 {
132                     yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
133                             "directive: %s", file, lineno, argv[2]);
134                     fclose(f);
135                     return 0;
136                 }
137                 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
138                 (*mtp)->next = 0;
139                 (*mtp)->type = type;
140                 if (np > 2 && !data1_matchstr(parm, "new"))
141                     (*mtp)->new_field = 1;
142                 else
143                     (*mtp)->new_field = 0;
144                 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
145                 {
146                     (*mtp)->which = D1_MAPTAG_numeric;
147                     (*mtp)->value.numeric = atoi(valstr);
148                 }
149                 else
150                 {
151                     (*mtp)->which = D1_MAPTAG_string;
152                     (*mtp)->value.string =
153                         (char *)nmem_malloc(mem, strlen(valstr)+1);
154                     strcpy((*mtp)->value.string, valstr);
155                 }
156                 mtp = &(*mtp)->next;
157             }
158             mapp = &(*mapp)->next;
159         }
160         else 
161             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
162                     file, lineno, argv[0]);
163
164     fclose(f);
165     return res;
166 }
167
168 /*
169  * Locate node with given elementname.
170  * NOTE: This is stupid - we don't find repeats this way.
171  */
172 static data1_node *find_node(data1_node *p, char *elementname)
173 {
174     data1_node *c, *r;
175
176     for (c = p->child; c; c = c->next)
177         if (c->which == DATA1N_tag && c->u.tag.element &&
178             !data1_matchstr(c->u.tag.element->name, elementname))
179             return c;
180         else if ((r = find_node(c, elementname)))
181             return r;
182     return 0;
183 }
184
185 /*
186  * See if the node n is equivalent to the tag t.
187  */
188 static int tagmatch(data1_node *n, data1_maptag *t)
189 {
190     if (n->which != DATA1N_tag)
191         return 0;
192     if (n->u.tag.element)
193     {
194         if (n->u.tag.element->tag->tagset)
195         {
196             if (n->u.tag.element->tag->tagset->type != t->type)
197                 return 0;
198         }
199         else if (t->type != 3)
200             return 0;
201         if (n->u.tag.element->tag->which == DATA1T_numeric)
202         {
203             if (t->which != D1_MAPTAG_numeric)
204                 return 0;
205             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
206                 return 0;
207         }
208         else
209         {
210             if (t->which != D1_MAPTAG_string)
211                 return 0;
212             if (data1_matchstr(n->u.tag.element->tag->value.string,
213                 t->value.string))
214                 return 0;
215         }
216     }
217     else /* local tag */
218     {
219         char str[10];
220
221         if (t->type != 3)
222             return 0;
223         if (t->which == D1_MAPTAG_numeric)
224             sprintf(str, "%d", t->value.numeric);
225         else
226             strcpy(str, t->value.string);
227         if (data1_matchstr(n->u.tag.tag, str))
228             return 0;
229     }
230     return 1;
231 }
232
233 static data1_node *dup_child (data1_handle dh, data1_node *n,
234                               data1_node **last, NMEM mem,
235                               data1_node *parent)
236 {
237     data1_node *first = 0;
238     data1_node **m = &first;
239
240     for (; n; n = n->next)
241     {
242         *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
243         memcpy (*m, n, sizeof(**m));
244         
245         (*m)->parent = parent;
246         (*m)->root = parent->root;
247         (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
248         m = &(*m)->next;
249     }
250     *m = 0;
251     return first;
252 }
253
254 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
255                         data1_node *res, NMEM mem)
256 {
257     data1_node *c;
258     data1_mapunit *m;
259     /*
260      * locate each source element in turn.
261      */
262     for (c = n->child; c; c = c->next)
263         if (c->which == DATA1N_tag && c->u.tag.element)
264         {
265             for (m = map->map; m; m = m->next)
266             {
267                 if (!data1_matchstr(m->source_element_name,
268                     c->u.tag.element->name))
269                 {
270                     data1_node *pn = res;
271                     data1_node *cur = pn->last_child;
272                     data1_maptag *mt;
273
274                     /*
275                      * process the target path specification.
276                      */
277                     for (mt = m->target_path; mt; mt = mt->next)
278                     {
279                         if (!cur || mt->new_field || !tagmatch(cur, mt))
280                         {
281                             cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
282                             cur->u.tag.tag = mt->value.string;
283                         }
284                         
285                         if (mt->next)
286                             pn = cur;
287                         else if (!m->no_data)
288                         {
289                             cur->child =
290                                 dup_child (dh, c->child,
291                                            &cur->last_child, mem, cur);
292                         }
293                     }
294                 }
295             }
296             if (map_children(dh, c, map, res, mem) < 0)
297                 return -1;
298         }
299     return 0;
300 }
301
302 /*
303  * Create a (possibly lossy) copy of the given record based on the
304  * table. The new copy will refer back to the data of the original record,
305  * which should not be discarded during the lifetime of the copy.
306  */
307 data1_node *data1_map_record (data1_handle dh, data1_node *n,
308                               data1_maptab *map, NMEM m)
309 {
310     data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
311
312     res->which = DATA1N_root;
313     res->u.root.type = map->target_absyn_name;
314     if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
315     {
316         yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
317                 map->name, map->target_absyn_name);
318     }
319     if (data1_is_xmlmode(dh))
320     {
321         n = n->child;
322         if (!n)
323             return 0;
324         res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
325     }
326     else
327         res1 = res;
328
329     if (map_children(dh, n, map, res1, m) < 0)
330     {
331         data1_free_tree(dh, res);
332         return 0;
333     }
334     return res;
335 }
336