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