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