2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-05-01 12:45:31 quinn
8 * Support use of local tag names in abs file.
10 * Revision 1.7 1995/12/13 13:44:31 quinn
11 * Modified Data1-system to use nmem
13 * Revision 1.6 1995/12/12 16:37:08 quinn
14 * Added destroy element to data1_node.
16 * Revision 1.5 1995/12/12 14:11:31 quinn
17 * More work on the large-record problem.
19 * Revision 1.4 1995/12/11 15:22:37 quinn
20 * Added last_child field to the node.
21 * Rewrote schema-mapping.
23 * Revision 1.3 1995/11/01 16:34:56 quinn
24 * Making data1 look for tables in data1_tabpath
26 * Revision 1.2 1995/11/01 13:54:46 quinn
29 * Revision 1.1 1995/11/01 11:56:08 quinn
30 * Added Retrieval (data management) functions en masse.
48 data1_maptab *data1_read_maptab(char *file)
50 data1_maptab *res = xmalloc(sizeof(*res));
53 char *argv[50], line[512];
56 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
58 logf(LOG_WARN|LOG_ERRNO, "%s", file);
63 res->target_absyn_ref = ODR_NONE;
68 while ((argc = readconf_line(f, line, 512, argv, 50)))
69 if (!strcmp(argv[0], "targetref"))
73 logf(LOG_WARN, "%s: one argument required for targetref",
77 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
79 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
83 else if (!strcmp(argv[0], "targetname"))
87 logf(LOG_WARN, "%s: one argument required for targetref",
91 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
92 strcpy(res->target_absyn_name, argv[1]);
94 else if (!strcmp(argv[0], "name"))
98 logf(LOG_WARN, "%s: one argument required for name",
102 res->name = xmalloc(strlen(argv[1])+1);
103 strcpy(res->name, argv[1]);
105 else if (!strcmp(argv[0], "map"))
108 char *ep, *path = argv[2];
112 logf(LOG_WARN, "%s: At least 2 arguments required for map",
116 *mapp = xmalloc(sizeof(**mapp));
118 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
119 (*mapp)->no_data = 1;
121 (*mapp)->no_data = 0;
122 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
123 strcpy((*mapp)->source_element_name, argv[1]);
124 mtp = &(*mapp)->target_path;
127 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
128 (ep = strchr(path, '/'))))
131 char valstr[512], parm[512];
135 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
138 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
143 *mtp = xmalloc(sizeof(**mtp));
146 if (np > 2 && !data1_matchstr(parm, "new"))
147 (*mtp)->new_field = 1;
149 (*mtp)->new_field = 0;
151 if ((numval = atoi(valstr)))
153 (*mtp)->which = D1_MAPTAG_numeric;
154 (*mtp)->value.numeric = numval;
159 (*mtp)->which = D1_MAPTAG_string;
160 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
161 strcpy((*mtp)->value.string, valstr);
167 mapp = &(*mapp)->next;
170 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
179 * Locate node with givel elementname.
180 * NOTE: This is stupid - we don't find repeats this way.
182 static data1_node *find_node(data1_node *p, char *elementname)
186 for (c = p->child; c; c = c->next)
187 if (c->which == DATA1N_tag && c->u.tag.element &&
188 !data1_matchstr(c->u.tag.element->name, elementname))
190 else if ((r = find_node(c, elementname)))
196 * See if the node n is equivalent to the tag t.
198 static int tagmatch(data1_node *n, data1_maptag *t)
200 if (n->which != DATA1N_tag)
202 if (n->u.tag.element)
204 if (n->u.tag.element->tag->tagset)
206 if (n->u.tag.element->tag->tagset->type != t->type)
209 else if (t->type != 3)
211 if (n->u.tag.element->tag->which == DATA1T_numeric)
213 if (t->which != D1_MAPTAG_numeric)
215 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
220 if (t->which != D1_MAPTAG_string)
222 if (data1_matchstr(n->u.tag.element->tag->value.string,
233 if (t->which == D1_MAPTAG_numeric)
234 sprintf(str, "%d", t->value.numeric);
236 strcpy(str, t->value.string);
237 if (data1_matchstr(n->u.tag.tag, str))
243 static int map_children(data1_node *n, data1_maptab *map, data1_node *res,
249 * locate each source element in turn.
251 for (c = n->child; c; c = c->next)
252 if (c->which == DATA1N_tag && c->u.tag.element)
254 for (m = map->map; m; m = m->next)
256 if (!data1_matchstr(m->source_element_name,
257 c->u.tag.element->name))
259 data1_node *pn = res;
260 data1_node *cur = pn->last_child;
264 * process the target path specification.
266 for (mt = m->target_path; mt; mt = mt->next)
268 if (!cur || mt->new_field || !tagmatch(cur, mt))
270 cur = data1_mk_node(mem);
271 cur->which = DATA1N_tag;
272 cur->u.tag.element = 0;
273 cur->u.tag.tag = mt->value.string;
274 cur->u.tag.node_selected = 0;
276 cur->root = pn->root;
280 pn->last_child->next = cur;
281 pn->last_child = cur;
287 else if (!m->no_data)
289 cur->child = c->child;
290 cur->last_child = c->last_child;
291 cur->num_children = c->num_children;
300 if (map_children(c, map, res, mem) < 0)
310 * See if the node n is equivalent to the tag t.
312 static int tagmatch(data1_node *n, data1_maptag *t)
314 if (n->which != DATA1N_tag)
316 if (n->u.tag.element)
318 if (n->u.tag.element->tag->tagset->type != t->type)
320 if (n->u.tag.element->tag->which == DATA1T_numeric)
322 if (t->which != D1_MAPTAG_numeric)
324 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
329 if (t->which != D1_MAPTAG_string)
331 if (data1_matchstr(n->u.tag.element->tag->value.string,
342 if (t->which == D1_MAPTAG_numeric)
343 sprintf(str, "%d", t->value.numeric);
345 strcpy(str, t->value.string);
346 if (data1_matchstr(n->u.tag.tag, str))
352 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
356 for (c = n->child; c; c = c->next)
358 if (c->which == DATA1N_tag)
360 if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
361 m->source_element_name))
363 /* Process target path specification */
365 data1_node *pn = res, *cur = pn->last_child;
367 for (mt = m->target_path; mt; mt = mt->next)
369 if (!cur || !tagmatch(cur, mt))
371 cur = data1_mk_node();
372 cur->which = DATA1N_tag;
373 cur->u.tag.element = 0;
374 cur->u.tag.tag = mt->value.string;
375 cur->u.tag.node_selected = 0;
377 cur->root = pn->root;
381 pn->last_child->next = cur;
382 pn->last_child = cur;
387 else if (!m->no_data)
389 cur->child = c->child;
390 cur->num_children = c->num_children;
396 else if (map_elements(res, c, m) < 0)
403 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
407 for (m = map->map; m; m = m->next)
408 if (map_elements(res, n, m) < 0)
416 * Create a (possibly lossy) copy of the given record based on the
417 * table. The new copy will refer back to the data of the original record,
418 * which should not be discarded during the lifetime of the copy.
420 data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m)
422 data1_node *res = data1_mk_node(m);
424 res->which = DATA1N_root;
425 res->u.root.type = map->target_absyn_name;
426 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
428 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
429 map->name, map->target_absyn_name);
434 if (map_children(n, map, res, m) < 0)
436 data1_free_tree(res);