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