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