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