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