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