YAZ version 1.4.
[yaz-moved-to-github.git] / retrieval / d1_map.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_map.c,v $
7  * Revision 1.11  1997-09-17 12:10:36  adam
8  * YAZ version 1.4.
9  *
10  * Revision 1.10  1997/09/05 09:50:56  adam
11  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
12  *
13  * Revision 1.9  1996/06/10 08:56:02  quinn
14  * Work on Summary.
15  *
16  * Revision 1.8  1996/05/01  12:45:31  quinn
17  * Support use of local tag names in abs file.
18  *
19  * Revision 1.7  1995/12/13  13:44:31  quinn
20  * Modified Data1-system to use nmem
21  *
22  * Revision 1.6  1995/12/12  16:37:08  quinn
23  * Added destroy element to data1_node.
24  *
25  * Revision 1.5  1995/12/12  14:11:31  quinn
26  * More work on the large-record problem.
27  *
28  * Revision 1.4  1995/12/11  15:22:37  quinn
29  * Added last_child field to the node.
30  * Rewrote schema-mapping.
31  *
32  * Revision 1.3  1995/11/01  16:34:56  quinn
33  * Making data1 look for tables in data1_tabpath
34  *
35  * Revision 1.2  1995/11/01  13:54:46  quinn
36  * Minor adjustments
37  *
38  * Revision 1.1  1995/11/01  11:56:08  quinn
39  * Added Retrieval (data management) functions en masse.
40  *
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48
49 #include <oid.h>
50 #include <log.h>
51 #include <readconf.h>
52
53 #include <tpath.h>
54 #include <data1.h>
55 #include <d1_map.h>
56
57 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
58 {
59     NMEM mem = data1_nmem_get (dh);
60     data1_maptab *res = nmem_malloc(mem, sizeof(*res));
61     FILE *f;
62     int argc;
63     char *argv[50], line[512];
64     data1_mapunit **mapp;
65     int local_numeric = 0;
66
67     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
68     {
69         logf(LOG_WARN|LOG_ERRNO, "%s", file);
70         return 0;
71     }
72
73     res->name = 0;
74     res->target_absyn_ref = ODR_NONE;
75     res->map = 0;
76     mapp = &res->map;
77     res->next = 0;
78
79     while ((argc = readconf_line(f, line, 512, argv, 50)))
80         if (!strcmp(argv[0], "targetref"))
81         {
82             if (argc != 2)
83             {
84                 logf(LOG_WARN, "%s: one argument required for targetref",
85                     file);
86                 continue;
87             }
88             if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
89             {
90                 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
91                 continue;
92             }
93         }
94         else if (!strcmp(argv[0], "targetname"))
95         {
96             if (argc != 2)
97             {
98                 logf(LOG_WARN, "%s: one argument required for targetref",
99                     file);
100                 continue;
101             }
102             res->target_absyn_name = nmem_malloc(mem, strlen(argv[1])+1);
103             strcpy(res->target_absyn_name, argv[1]);
104         }
105         else if (!yaz_matchstr(argv[0], "localnumeric"))
106             local_numeric = 1;
107         else if (!strcmp(argv[0], "name"))
108         {
109             if (argc != 2)
110             {
111                 logf(LOG_WARN, "%s: one argument required for name",
112                     file);
113                 continue;
114             }
115             res->name = nmem_malloc(mem, strlen(argv[1])+1);
116             strcpy(res->name, argv[1]);
117         }
118         else if (!strcmp(argv[0], "map"))
119         {
120             data1_maptag **mtp;
121             char *ep, *path = argv[2];
122
123             if (argc < 3)
124             {
125                 logf(LOG_WARN, "%s: At least 2 arguments required for map",
126                     file);
127                 continue;
128             }
129             *mapp = nmem_malloc(mem, sizeof(**mapp));
130             (*mapp)->next = 0;
131             if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
132                 (*mapp)->no_data = 1;
133             else
134                 (*mapp)->no_data = 0;
135             (*mapp)->source_element_name = nmem_malloc(mem, strlen(argv[1])+1);
136             strcpy((*mapp)->source_element_name, argv[1]);
137             mtp = &(*mapp)->target_path;
138             if (*path == '/')
139                 path++;
140             for (ep = strchr(path, '/'); path; (void)((path = ep) &&
141                 (ep = strchr(path, '/'))))
142             {
143                 int type, np;
144                 char valstr[512], parm[512];
145
146                 if (ep)
147                     ep++;
148                 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
149                     parm)) < 2)
150                 {
151                     logf(LOG_WARN, "%s: Syntax error in map directive: %s",
152                         file, argv[2]);
153                     fclose(f);
154                     return 0;
155                 }
156                 *mtp = nmem_malloc(mem, sizeof(**mtp));
157                 (*mtp)->next = 0;
158                 (*mtp)->type = type;
159                 if (np > 2 && !data1_matchstr(parm, "new"))
160                     (*mtp)->new_field = 1;
161                 else
162                     (*mtp)->new_field = 0;
163                 if ((type != 3 || local_numeric) && isdigit(*valstr))
164                 {
165                     (*mtp)->which = D1_MAPTAG_numeric;
166                     (*mtp)->value.numeric = atoi(valstr);
167                 }
168                 else
169                 {
170                     (*mtp)->which = D1_MAPTAG_string;
171                     (*mtp)->value.string = nmem_malloc(mem, strlen(valstr)+1);
172                     strcpy((*mtp)->value.string, valstr);
173                 }
174                 mtp = &(*mtp)->next;
175             }
176             mapp = &(*mapp)->next;
177         }
178         else 
179             logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
180
181     fclose(f);
182     return res;
183 }
184
185 /*
186  * Locate node with given elementname.
187  * NOTE: This is stupid - we don't find repeats this way.
188  */
189 static data1_node *find_node(data1_node *p, char *elementname)
190 {
191     data1_node *c, *r;
192
193     for (c = p->child; c; c = c->next)
194         if (c->which == DATA1N_tag && c->u.tag.element &&
195             !data1_matchstr(c->u.tag.element->name, elementname))
196             return c;
197         else if ((r = find_node(c, elementname)))
198             return r;
199     return 0;
200 }
201
202 /*
203  * See if the node n is equivalent to the tag t.
204  */
205 static int tagmatch(data1_node *n, data1_maptag *t)
206 {
207     if (n->which != DATA1N_tag)
208         return 0;
209     if (n->u.tag.element)
210     {
211         if (n->u.tag.element->tag->tagset)
212         {
213             if (n->u.tag.element->tag->tagset->type != t->type)
214                 return 0;
215         }
216         else if (t->type != 3)
217             return 0;
218         if (n->u.tag.element->tag->which == DATA1T_numeric)
219         {
220             if (t->which != D1_MAPTAG_numeric)
221                 return 0;
222             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
223                 return 0;
224         }
225         else
226         {
227             if (t->which != D1_MAPTAG_string)
228                 return 0;
229             if (data1_matchstr(n->u.tag.element->tag->value.string,
230                 t->value.string))
231                 return 0;
232         }
233     }
234     else /* local tag */
235     {
236         char str[10];
237
238         if (t->type != 3)
239             return 0;
240         if (t->which == D1_MAPTAG_numeric)
241             sprintf(str, "%d", t->value.numeric);
242         else
243             strcpy(str, t->value.string);
244         if (data1_matchstr(n->u.tag.tag, str))
245             return 0;
246     }
247     return 1;
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                             cur = data1_mk_node (dh, mem);
278                             cur->which = DATA1N_tag;
279                             cur->u.tag.element = 0;
280                             cur->u.tag.tag = mt->value.string;
281                             cur->u.tag.node_selected = 0;
282                             cur->parent = pn;
283                             cur->root = pn->root;
284                             if (!pn->child)
285                                 pn->child = cur;
286                             if (pn->last_child)
287                                 pn->last_child->next = cur;
288                             pn->last_child = cur;
289                             pn->num_children++;
290                         }
291                         
292                         if (mt->next)
293                             pn = cur;
294                         else if (!m->no_data)
295                         {
296                             cur->child = c->child;
297                             cur->last_child = c->last_child;
298                             cur->num_children = c->num_children;
299                             c->child = 0;
300                             c->last_child = 0;
301                             c->num_children = 0;
302                         }
303                     }
304                     break;
305                 }
306             }
307             if (map_children(dh, c, map, res, mem) < 0)
308                 return -1;
309         }
310     return 0;
311 }
312
313 /*
314  * Create a (possibly lossy) copy of the given record based on the
315  * table. The new copy will refer back to the data of the original record,
316  * which should not be discarded during the lifetime of the copy.
317  */
318 data1_node *data1_map_record (data1_handle dh, data1_node *n,
319                               data1_maptab *map, NMEM m)
320 {
321     data1_node *res = data1_mk_node(dh, m);
322
323     res->which = DATA1N_root;
324     res->u.root.type = map->target_absyn_name;
325     if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
326     {
327         logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
328             map->name, map->target_absyn_name);
329     }
330     res->parent = 0;
331     res->root = res;
332
333     if (map_children(dh, n, map, res, m) < 0)
334     {
335         data1_free_tree(dh, res);
336         return 0;
337     }
338     return res;
339 }
340