New functions yaz_is_abspath, yaz_path_fopen_base
[yaz-moved-to-github.git] / retrieval / d1_map.c
1 /*
2  * Copyright (c) 1995-2000, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_map.c,v $
7  * Revision 1.19  2002-04-04 20:49:46  adam
8  * New functions yaz_is_abspath, yaz_path_fopen_base
9  *
10  * Revision 1.18  2000/11/29 14:22:47  adam
11  * Implemented XML/SGML attributes for data1 so that d1_read reads them
12  * and d1_write generates proper attributes for XML/SGML records. Added
13  * register locking for threaded version.
14  *
15  * Revision 1.17  1999/11/30 13:47:12  adam
16  * Improved installation. Moved header files to include/yaz.
17  *
18  * Revision 1.16  1999/10/21 12:06:29  adam
19  * Retrieval module no longer uses ctype.h - functions.
20  *
21  * Revision 1.15  1999/08/27 09:40:32  adam
22  * Renamed logf function to yaz_log. Removed VC++ project files.
23  *
24  * Revision 1.14  1998/10/13 16:09:50  adam
25  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
26  * Added support for multiple attribute set references and tagset references
27  * from an abstract syntax file.
28  * Fixed many bad logs-calls in routines that read the various
29  * specifications regarding data1 (*.abs,*.att,...) and made the messages
30  * consistent whenever possible.
31  * Added extra 'lineno' argument to function readconf_line.
32  *
33  * Revision 1.13  1998/02/11 11:53:35  adam
34  * Changed code so that it compiles as C++.
35  *
36  * Revision 1.12  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.11  1997/09/17 12:10:36  adam
41  * YAZ version 1.4.
42  *
43  * Revision 1.10  1997/09/05 09:50:56  adam
44  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
45  *
46  * Revision 1.9  1996/06/10 08:56:02  quinn
47  * Work on Summary.
48  *
49  * Revision 1.8  1996/05/01  12:45:31  quinn
50  * Support use of local tag names in abs file.
51  *
52  * Revision 1.7  1995/12/13  13:44:31  quinn
53  * Modified Data1-system to use nmem
54  *
55  * Revision 1.6  1995/12/12  16:37:08  quinn
56  * Added destroy element to data1_node.
57  *
58  * Revision 1.5  1995/12/12  14:11:31  quinn
59  * More work on the large-record problem.
60  *
61  * Revision 1.4  1995/12/11  15:22:37  quinn
62  * Added last_child field to the node.
63  * Rewrote schema-mapping.
64  *
65  * Revision 1.3  1995/11/01  16:34:56  quinn
66  * Making data1 look for tables in data1_tabpath
67  *
68  * Revision 1.2  1995/11/01  13:54:46  quinn
69  * Minor adjustments
70  *
71  * Revision 1.1  1995/11/01  11:56:08  quinn
72  * Added Retrieval (data management) functions en masse.
73  *
74  *
75  */
76
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80
81 #include <yaz/oid.h>
82 #include <yaz/log.h>
83 #include <yaz/readconf.h>
84 #include <yaz/tpath.h>
85 #include <yaz/data1.h>
86 #include <yaz/d1_map.h>
87
88 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
89 {
90     NMEM mem = data1_nmem_get (dh);
91     data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
92     FILE *f;
93     int lineno = 0;
94     int argc;
95     char *argv[50], line[512];
96     data1_mapunit **mapp;
97     int local_numeric = 0;
98
99     if (!(f = data1_path_fopen(dh, file, "r")))
100     {
101         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
102         return 0;
103     }
104
105     res->name = 0;
106     res->target_absyn_ref = VAL_NONE;
107     res->map = 0;
108     mapp = &res->map;
109     res->next = 0;
110
111     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
112         if (!strcmp(argv[0], "targetref"))
113         {
114             if (argc != 2)
115             {
116                 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
117                         file, lineno);
118                 continue;
119             }
120             if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
121                 == VAL_NONE)
122             {
123                 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
124                         file, lineno, argv[1]);
125                 continue;
126             }
127         }
128         else if (!strcmp(argv[0], "targetname"))
129         {
130             if (argc != 2)
131             {
132                 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
133                         file, lineno);
134                 continue;
135             }
136             res->target_absyn_name =
137                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
138             strcpy(res->target_absyn_name, argv[1]);
139         }
140         else if (!yaz_matchstr(argv[0], "localnumeric"))
141             local_numeric = 1;
142         else if (!strcmp(argv[0], "name"))
143         {
144             if (argc != 2)
145             {
146                 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
147                 continue;
148             }
149             res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
150             strcpy(res->name, argv[1]);
151         }
152         else if (!strcmp(argv[0], "map"))
153         {
154             data1_maptag **mtp;
155             char *ep, *path = argv[2];
156
157             if (argc < 3)
158             {
159                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
160                         file, lineno);
161                 continue;
162             }
163             *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
164             (*mapp)->next = 0;
165             if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
166                 (*mapp)->no_data = 1;
167             else
168                 (*mapp)->no_data = 0;
169             (*mapp)->source_element_name =
170                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
171             strcpy((*mapp)->source_element_name, argv[1]);
172             mtp = &(*mapp)->target_path;
173             if (*path == '/')
174                 path++;
175             for (ep = strchr(path, '/'); path; (void)((path = ep) &&
176                 (ep = strchr(path, '/'))))
177             {
178                 int type, np;
179                 char valstr[512], parm[512];
180
181                 if (ep)
182                     ep++;
183                 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
184                     parm)) < 2)
185                 {
186                     yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
187                             "directive: %s", file, lineno, argv[2]);
188                     fclose(f);
189                     return 0;
190                 }
191                 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
192                 (*mtp)->next = 0;
193                 (*mtp)->type = type;
194                 if (np > 2 && !data1_matchstr(parm, "new"))
195                     (*mtp)->new_field = 1;
196                 else
197                     (*mtp)->new_field = 0;
198                 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
199                 {
200                     (*mtp)->which = D1_MAPTAG_numeric;
201                     (*mtp)->value.numeric = atoi(valstr);
202                 }
203                 else
204                 {
205                     (*mtp)->which = D1_MAPTAG_string;
206                     (*mtp)->value.string =
207                         (char *)nmem_malloc(mem, strlen(valstr)+1);
208                     strcpy((*mtp)->value.string, valstr);
209                 }
210                 mtp = &(*mtp)->next;
211             }
212             mapp = &(*mapp)->next;
213         }
214         else 
215             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
216                     file, lineno, argv[0]);
217
218     fclose(f);
219     return res;
220 }
221
222 /*
223  * Locate node with given elementname.
224  * NOTE: This is stupid - we don't find repeats this way.
225  */
226 static data1_node *find_node(data1_node *p, char *elementname)
227 {
228     data1_node *c, *r;
229
230     for (c = p->child; c; c = c->next)
231         if (c->which == DATA1N_tag && c->u.tag.element &&
232             !data1_matchstr(c->u.tag.element->name, elementname))
233             return c;
234         else if ((r = find_node(c, elementname)))
235             return r;
236     return 0;
237 }
238
239 /*
240  * See if the node n is equivalent to the tag t.
241  */
242 static int tagmatch(data1_node *n, data1_maptag *t)
243 {
244     if (n->which != DATA1N_tag)
245         return 0;
246     if (n->u.tag.element)
247     {
248         if (n->u.tag.element->tag->tagset)
249         {
250             if (n->u.tag.element->tag->tagset->type != t->type)
251                 return 0;
252         }
253         else if (t->type != 3)
254             return 0;
255         if (n->u.tag.element->tag->which == DATA1T_numeric)
256         {
257             if (t->which != D1_MAPTAG_numeric)
258                 return 0;
259             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
260                 return 0;
261         }
262         else
263         {
264             if (t->which != D1_MAPTAG_string)
265                 return 0;
266             if (data1_matchstr(n->u.tag.element->tag->value.string,
267                 t->value.string))
268                 return 0;
269         }
270     }
271     else /* local tag */
272     {
273         char str[10];
274
275         if (t->type != 3)
276             return 0;
277         if (t->which == D1_MAPTAG_numeric)
278             sprintf(str, "%d", t->value.numeric);
279         else
280             strcpy(str, t->value.string);
281         if (data1_matchstr(n->u.tag.tag, str))
282             return 0;
283     }
284     return 1;
285 }
286
287 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
288                         data1_node *res, NMEM mem)
289 {
290     data1_node *c;
291     data1_mapunit *m;
292     /*
293      * locate each source element in turn.
294      */
295     for (c = n->child; c; c = c->next)
296         if (c->which == DATA1N_tag && c->u.tag.element)
297         {
298             for (m = map->map; m; m = m->next)
299             {
300                 if (!data1_matchstr(m->source_element_name,
301                     c->u.tag.element->name))
302                 {
303                     data1_node *pn = res;
304                     data1_node *cur = pn->last_child;
305                     data1_maptag *mt;
306
307                     /*
308                      * process the target path specification.
309                      */
310                     for (mt = m->target_path; mt; mt = mt->next)
311                     {
312                         if (!cur || mt->new_field || !tagmatch(cur, mt))
313                         {
314                             cur = data1_mk_node_type (dh, mem, DATA1N_tag);
315                             cur->u.tag.tag = mt->value.string;
316
317                             cur->parent = pn;
318                             cur->root = pn->root;
319                             if (!pn->child)
320                                 pn->child = cur;
321                             if (pn->last_child)
322                                 pn->last_child->next = cur;
323                             pn->last_child = cur;
324                         }
325                         
326                         if (mt->next)
327                             pn = cur;
328                         else if (!m->no_data)
329                         {
330                             cur->child = c->child;
331                             cur->last_child = c->last_child;
332                             c->child = 0;
333                             c->last_child = 0;
334                         }
335                     }
336                     break;
337                 }
338             }
339             if (map_children(dh, c, map, res, mem) < 0)
340                 return -1;
341         }
342     return 0;
343 }
344
345 /*
346  * Create a (possibly lossy) copy of the given record based on the
347  * table. The new copy will refer back to the data of the original record,
348  * which should not be discarded during the lifetime of the copy.
349  */
350 data1_node *data1_map_record (data1_handle dh, data1_node *n,
351                               data1_maptab *map, NMEM m)
352 {
353     data1_node *res = data1_mk_node(dh, m);
354
355     res->which = DATA1N_root;
356     res->u.root.type = map->target_absyn_name;
357     if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
358     {
359         yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
360                 map->name, map->target_absyn_name);
361     }
362     res->parent = 0;
363     res->root = res;
364
365     if (map_children(dh, n, map, res, m) < 0)
366     {
367         data1_free_tree(dh, res);
368         return 0;
369     }
370     return res;
371 }
372