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