2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1998-02-11 11:53:35 adam
8 * Changed code so that it compiles as C++.
10 * Revision 1.12 1997/11/18 09:51:09 adam
11 * Removed element num_children from data1_node. Minor changes in
14 * Revision 1.11 1997/09/17 12:10:36 adam
17 * Revision 1.10 1997/09/05 09:50:56 adam
18 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
20 * Revision 1.9 1996/06/10 08:56:02 quinn
23 * Revision 1.8 1996/05/01 12:45:31 quinn
24 * Support use of local tag names in abs file.
26 * Revision 1.7 1995/12/13 13:44:31 quinn
27 * Modified Data1-system to use nmem
29 * Revision 1.6 1995/12/12 16:37:08 quinn
30 * Added destroy element to data1_node.
32 * Revision 1.5 1995/12/12 14:11:31 quinn
33 * More work on the large-record problem.
35 * Revision 1.4 1995/12/11 15:22:37 quinn
36 * Added last_child field to the node.
37 * Rewrote schema-mapping.
39 * Revision 1.3 1995/11/01 16:34:56 quinn
40 * Making data1 look for tables in data1_tabpath
42 * Revision 1.2 1995/11/01 13:54:46 quinn
45 * Revision 1.1 1995/11/01 11:56:08 quinn
46 * Added Retrieval (data management) functions en masse.
64 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
66 NMEM mem = data1_nmem_get (dh);
67 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
70 char *argv[50], line[512];
72 int local_numeric = 0;
74 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
76 logf(LOG_WARN|LOG_ERRNO, "%s", file);
81 res->target_absyn_ref = ODR_NONE;
86 while ((argc = readconf_line(f, line, 512, argv, 50)))
87 if (!strcmp(argv[0], "targetref"))
91 logf(LOG_WARN, "%s: one argument required for targetref",
95 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
97 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
101 else if (!strcmp(argv[0], "targetname"))
105 logf(LOG_WARN, "%s: one argument required for targetref",
109 res->target_absyn_name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
110 strcpy(res->target_absyn_name, argv[1]);
112 else if (!yaz_matchstr(argv[0], "localnumeric"))
114 else if (!strcmp(argv[0], "name"))
118 logf(LOG_WARN, "%s: one argument required for name",
122 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
123 strcpy(res->name, argv[1]);
125 else if (!strcmp(argv[0], "map"))
128 char *ep, *path = argv[2];
132 logf(LOG_WARN, "%s: At least 2 arguments required for map",
136 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
138 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
139 (*mapp)->no_data = 1;
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;
147 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
148 (ep = strchr(path, '/'))))
151 char valstr[512], parm[512];
155 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
158 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
163 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
166 if (np > 2 && !data1_matchstr(parm, "new"))
167 (*mtp)->new_field = 1;
169 (*mtp)->new_field = 0;
170 if ((type != 3 || local_numeric) && isdigit(*valstr))
172 (*mtp)->which = D1_MAPTAG_numeric;
173 (*mtp)->value.numeric = atoi(valstr);
177 (*mtp)->which = D1_MAPTAG_string;
178 (*mtp)->value.string = (char *)nmem_malloc(mem, strlen(valstr)+1);
179 strcpy((*mtp)->value.string, valstr);
183 mapp = &(*mapp)->next;
186 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
193 * Locate node with given elementname.
194 * NOTE: This is stupid - we don't find repeats this way.
196 static data1_node *find_node(data1_node *p, char *elementname)
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))
204 else if ((r = find_node(c, elementname)))
210 * See if the node n is equivalent to the tag t.
212 static int tagmatch(data1_node *n, data1_maptag *t)
214 if (n->which != DATA1N_tag)
216 if (n->u.tag.element)
218 if (n->u.tag.element->tag->tagset)
220 if (n->u.tag.element->tag->tagset->type != t->type)
223 else if (t->type != 3)
225 if (n->u.tag.element->tag->which == DATA1T_numeric)
227 if (t->which != D1_MAPTAG_numeric)
229 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
234 if (t->which != D1_MAPTAG_string)
236 if (data1_matchstr(n->u.tag.element->tag->value.string,
247 if (t->which == D1_MAPTAG_numeric)
248 sprintf(str, "%d", t->value.numeric);
250 strcpy(str, t->value.string);
251 if (data1_matchstr(n->u.tag.tag, str))
257 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
258 data1_node *res, NMEM mem)
263 * locate each source element in turn.
265 for (c = n->child; c; c = c->next)
266 if (c->which == DATA1N_tag && c->u.tag.element)
268 for (m = map->map; m; m = m->next)
270 if (!data1_matchstr(m->source_element_name,
271 c->u.tag.element->name))
273 data1_node *pn = res;
274 data1_node *cur = pn->last_child;
278 * process the target path specification.
280 for (mt = m->target_path; mt; mt = mt->next)
282 if (!cur || mt->new_field || !tagmatch(cur, mt))
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;
290 cur->root = pn->root;
294 pn->last_child->next = cur;
295 pn->last_child = cur;
300 else if (!m->no_data)
302 cur->child = c->child;
303 cur->last_child = c->last_child;
311 if (map_children(dh, c, map, res, mem) < 0)
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.
322 data1_node *data1_map_record (data1_handle dh, data1_node *n,
323 data1_maptab *map, NMEM m)
325 data1_node *res = data1_mk_node(dh, m);
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)))
331 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
332 map->name, map->target_absyn_name);
337 if (map_children(dh, n, map, res, m) < 0)
339 data1_free_tree(dh, res);