2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-12-12 14:11:31 quinn
8 * More work on the large-record problem.
10 * Revision 1.4 1995/12/11 15:22:37 quinn
11 * Added last_child field to the node.
12 * Rewrote schema-mapping.
14 * Revision 1.3 1995/11/01 16:34:56 quinn
15 * Making data1 look for tables in data1_tabpath
17 * Revision 1.2 1995/11/01 13:54:46 quinn
20 * Revision 1.1 1995/11/01 11:56:08 quinn
21 * Added Retrieval (data management) functions en masse.
39 data1_maptab *data1_read_maptab(char *file)
41 data1_maptab *res = xmalloc(sizeof(*res));
44 char *argv[50], line[512];
47 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
49 logf(LOG_WARN|LOG_ERRNO, "%s", file);
54 res->target_absyn_ref = ODR_NONE;
59 while ((argc = readconf_line(f, line, 512, argv, 50)))
60 if (!strcmp(argv[0], "targetref"))
64 logf(LOG_WARN, "%s: one argument required for targetref",
68 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
70 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
74 else if (!strcmp(argv[0], "targetname"))
78 logf(LOG_WARN, "%s: one argument required for targetref",
82 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
83 strcpy(res->target_absyn_name, argv[1]);
85 else if (!strcmp(argv[0], "name"))
89 logf(LOG_WARN, "%s: one argument required for name",
93 res->name = xmalloc(strlen(argv[1])+1);
94 strcpy(res->name, argv[1]);
96 else if (!strcmp(argv[0], "map"))
99 char *ep, *path = argv[2];
103 logf(LOG_WARN, "%s: At least 2 arguments required for map",
107 *mapp = xmalloc(sizeof(**mapp));
109 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
110 (*mapp)->no_data = 1;
112 (*mapp)->no_data = 0;
113 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
114 strcpy((*mapp)->source_element_name, argv[1]);
115 mtp = &(*mapp)->target_path;
118 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
119 (ep = strchr(path, '/'))))
122 char valstr[512], parm[512];
126 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
129 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
134 *mtp = xmalloc(sizeof(**mtp));
137 if (np > 2 && !data1_matchstr(parm, "new"))
138 (*mtp)->new_field = 1;
140 (*mtp)->new_field = 0;
142 if ((numval = atoi(valstr)))
144 (*mtp)->which = D1_MAPTAG_numeric;
145 (*mtp)->value.numeric = numval;
150 (*mtp)->which = D1_MAPTAG_string;
151 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
152 strcpy((*mtp)->value.string, valstr);
158 mapp = &(*mapp)->next;
161 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
170 * Locate node with givel elementname.
171 * NOTE: This is stupid - we don't find repeats this way.
173 static data1_node *find_node(data1_node *p, char *elementname)
177 for (c = p->child; c; c = c->next)
178 if (c->which == DATA1N_tag && c->u.tag.element &&
179 !data1_matchstr(c->u.tag.element->name, elementname))
181 else if ((r = find_node(c, elementname)))
187 * See if the node n is equivalent to the tag t.
189 static int tagmatch(data1_node *n, data1_maptag *t)
191 if (n->which != DATA1N_tag)
193 if (n->u.tag.element)
195 if (n->u.tag.element->tag->tagset->type != t->type)
197 if (n->u.tag.element->tag->which == DATA1T_numeric)
199 if (t->which != D1_MAPTAG_numeric)
201 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
206 if (t->which != D1_MAPTAG_string)
208 if (data1_matchstr(n->u.tag.element->tag->value.string,
219 if (t->which == D1_MAPTAG_numeric)
220 sprintf(str, "%d", t->value.numeric);
222 strcpy(str, t->value.string);
223 if (data1_matchstr(n->u.tag.tag, str))
229 static int map_children(data1_node *n, data1_maptab *map, data1_node *res)
234 * locate each source element in turn.
236 for (c = n->child; c; c = c->next)
237 if (c->which == DATA1N_tag && c->u.tag.element)
239 for (m = map->map; m; m = m->next)
241 if (!data1_matchstr(m->source_element_name,
242 c->u.tag.element->name))
244 data1_node *pn = res;
245 data1_node *cur = pn->last_child;
249 * process the target path specification.
251 for (mt = m->target_path; mt; mt = mt->next)
253 if (!cur || !tagmatch(cur, mt))
255 cur = data1_mk_node();
256 cur->which = DATA1N_tag;
257 cur->u.tag.element = 0;
258 cur->u.tag.tag = mt->value.string;
259 cur->u.tag.node_selected = 0;
261 cur->root = pn->root;
265 pn->last_child->next = cur;
266 pn->last_child = cur;
272 else if (!m->no_data)
274 cur->child = c->child;
275 cur->last_child = c->last_child;
276 cur->num_children = c->num_children;
285 if (map_children(c, map, res) < 0)
295 * See if the node n is equivalent to the tag t.
297 static int tagmatch(data1_node *n, data1_maptag *t)
299 if (n->which != DATA1N_tag)
301 if (n->u.tag.element)
303 if (n->u.tag.element->tag->tagset->type != t->type)
305 if (n->u.tag.element->tag->which == DATA1T_numeric)
307 if (t->which != D1_MAPTAG_numeric)
309 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
314 if (t->which != D1_MAPTAG_string)
316 if (data1_matchstr(n->u.tag.element->tag->value.string,
327 if (t->which == D1_MAPTAG_numeric)
328 sprintf(str, "%d", t->value.numeric);
330 strcpy(str, t->value.string);
331 if (data1_matchstr(n->u.tag.tag, str))
337 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
341 for (c = n->child; c; c = c->next)
343 if (c->which == DATA1N_tag)
345 if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
346 m->source_element_name))
348 /* Process target path specification */
350 data1_node *pn = res, *cur = pn->last_child;
352 for (mt = m->target_path; mt; mt = mt->next)
354 if (!cur || !tagmatch(cur, mt))
356 cur = data1_mk_node();
357 cur->which = DATA1N_tag;
358 cur->u.tag.element = 0;
359 cur->u.tag.tag = mt->value.string;
360 cur->u.tag.node_selected = 0;
362 cur->root = pn->root;
366 pn->last_child->next = cur;
367 pn->last_child = cur;
372 else if (!m->no_data)
374 cur->child = c->child;
375 cur->num_children = c->num_children;
381 else if (map_elements(res, c, m) < 0)
388 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
392 for (m = map->map; m; m = m->next)
393 if (map_elements(res, n, m) < 0)
401 * Create a (possibly lossy) copy of the given record based on the
402 * table. The new copy will refer back to the data of the original record,
403 * which should not be discarded during the lifetime of the copy.
405 data1_node *data1_map_record(data1_node *n, data1_maptab *map)
407 data1_node *res = data1_mk_node();
409 res->which = DATA1N_root;
410 res->u.root.type = map->target_absyn_name;
411 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
413 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
414 map->name, map->target_absyn_name);
419 if (map_children(n, map, res) < 0)
421 data1_free_tree(res);