2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-12-12 16:37:08 quinn
8 * Added destroy element to data1_node.
10 * Revision 1.5 1995/12/12 14:11:31 quinn
11 * More work on the large-record problem.
13 * Revision 1.4 1995/12/11 15:22:37 quinn
14 * Added last_child field to the node.
15 * Rewrote schema-mapping.
17 * Revision 1.3 1995/11/01 16:34:56 quinn
18 * Making data1 look for tables in data1_tabpath
20 * Revision 1.2 1995/11/01 13:54:46 quinn
23 * Revision 1.1 1995/11/01 11:56:08 quinn
24 * Added Retrieval (data management) functions en masse.
42 data1_maptab *data1_read_maptab(char *file)
44 data1_maptab *res = xmalloc(sizeof(*res));
47 char *argv[50], line[512];
50 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
52 logf(LOG_WARN|LOG_ERRNO, "%s", file);
57 res->target_absyn_ref = ODR_NONE;
62 while ((argc = readconf_line(f, line, 512, argv, 50)))
63 if (!strcmp(argv[0], "targetref"))
67 logf(LOG_WARN, "%s: one argument required for targetref",
71 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
73 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
77 else if (!strcmp(argv[0], "targetname"))
81 logf(LOG_WARN, "%s: one argument required for targetref",
85 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
86 strcpy(res->target_absyn_name, argv[1]);
88 else if (!strcmp(argv[0], "name"))
92 logf(LOG_WARN, "%s: one argument required for name",
96 res->name = xmalloc(strlen(argv[1])+1);
97 strcpy(res->name, argv[1]);
99 else if (!strcmp(argv[0], "map"))
102 char *ep, *path = argv[2];
106 logf(LOG_WARN, "%s: At least 2 arguments required for map",
110 *mapp = xmalloc(sizeof(**mapp));
112 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
113 (*mapp)->no_data = 1;
115 (*mapp)->no_data = 0;
116 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
117 strcpy((*mapp)->source_element_name, argv[1]);
118 mtp = &(*mapp)->target_path;
121 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
122 (ep = strchr(path, '/'))))
125 char valstr[512], parm[512];
129 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
132 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
137 *mtp = xmalloc(sizeof(**mtp));
140 if (np > 2 && !data1_matchstr(parm, "new"))
141 (*mtp)->new_field = 1;
143 (*mtp)->new_field = 0;
145 if ((numval = atoi(valstr)))
147 (*mtp)->which = D1_MAPTAG_numeric;
148 (*mtp)->value.numeric = numval;
153 (*mtp)->which = D1_MAPTAG_string;
154 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
155 strcpy((*mtp)->value.string, valstr);
161 mapp = &(*mapp)->next;
164 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
173 * Locate node with givel elementname.
174 * NOTE: This is stupid - we don't find repeats this way.
176 static data1_node *find_node(data1_node *p, char *elementname)
180 for (c = p->child; c; c = c->next)
181 if (c->which == DATA1N_tag && c->u.tag.element &&
182 !data1_matchstr(c->u.tag.element->name, elementname))
184 else if ((r = find_node(c, elementname)))
190 * See if the node n is equivalent to the tag t.
192 static int tagmatch(data1_node *n, data1_maptag *t)
194 if (n->which != DATA1N_tag)
196 if (n->u.tag.element)
198 if (n->u.tag.element->tag->tagset->type != t->type)
200 if (n->u.tag.element->tag->which == DATA1T_numeric)
202 if (t->which != D1_MAPTAG_numeric)
204 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
209 if (t->which != D1_MAPTAG_string)
211 if (data1_matchstr(n->u.tag.element->tag->value.string,
222 if (t->which == D1_MAPTAG_numeric)
223 sprintf(str, "%d", t->value.numeric);
225 strcpy(str, t->value.string);
226 if (data1_matchstr(n->u.tag.tag, str))
232 static int map_children(data1_node *n, data1_maptab *map, data1_node *res)
237 * locate each source element in turn.
239 for (c = n->child; c; c = c->next)
240 if (c->which == DATA1N_tag && c->u.tag.element)
242 for (m = map->map; m; m = m->next)
244 if (!data1_matchstr(m->source_element_name,
245 c->u.tag.element->name))
247 data1_node *pn = res;
248 data1_node *cur = pn->last_child;
252 * process the target path specification.
254 for (mt = m->target_path; mt; mt = mt->next)
256 if (!cur || mt->new_field || !tagmatch(cur, mt))
258 cur = data1_mk_node();
259 cur->which = DATA1N_tag;
260 cur->u.tag.element = 0;
261 cur->u.tag.tag = mt->value.string;
262 cur->u.tag.node_selected = 0;
264 cur->root = pn->root;
268 pn->last_child->next = cur;
269 pn->last_child = cur;
275 else if (!m->no_data)
277 cur->child = c->child;
278 cur->last_child = c->last_child;
279 cur->num_children = c->num_children;
288 if (map_children(c, map, res) < 0)
298 * See if the node n is equivalent to the tag t.
300 static int tagmatch(data1_node *n, data1_maptag *t)
302 if (n->which != DATA1N_tag)
304 if (n->u.tag.element)
306 if (n->u.tag.element->tag->tagset->type != t->type)
308 if (n->u.tag.element->tag->which == DATA1T_numeric)
310 if (t->which != D1_MAPTAG_numeric)
312 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
317 if (t->which != D1_MAPTAG_string)
319 if (data1_matchstr(n->u.tag.element->tag->value.string,
330 if (t->which == D1_MAPTAG_numeric)
331 sprintf(str, "%d", t->value.numeric);
333 strcpy(str, t->value.string);
334 if (data1_matchstr(n->u.tag.tag, str))
340 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
344 for (c = n->child; c; c = c->next)
346 if (c->which == DATA1N_tag)
348 if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
349 m->source_element_name))
351 /* Process target path specification */
353 data1_node *pn = res, *cur = pn->last_child;
355 for (mt = m->target_path; mt; mt = mt->next)
357 if (!cur || !tagmatch(cur, mt))
359 cur = data1_mk_node();
360 cur->which = DATA1N_tag;
361 cur->u.tag.element = 0;
362 cur->u.tag.tag = mt->value.string;
363 cur->u.tag.node_selected = 0;
365 cur->root = pn->root;
369 pn->last_child->next = cur;
370 pn->last_child = cur;
375 else if (!m->no_data)
377 cur->child = c->child;
378 cur->num_children = c->num_children;
384 else if (map_elements(res, c, m) < 0)
391 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
395 for (m = map->map; m; m = m->next)
396 if (map_elements(res, n, m) < 0)
404 * Create a (possibly lossy) copy of the given record based on the
405 * table. The new copy will refer back to the data of the original record,
406 * which should not be discarded during the lifetime of the copy.
408 data1_node *data1_map_record(data1_node *n, data1_maptab *map)
410 data1_node *res = data1_mk_node();
412 res->which = DATA1N_root;
413 res->u.root.type = map->target_absyn_name;
414 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
416 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
417 map->name, map->target_absyn_name);
422 if (map_children(n, map, res) < 0)
424 data1_free_tree(res);