2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-12-11 15:22:37 quinn
8 * Added last_child field to the node.
9 * Rewrote schema-mapping.
11 * Revision 1.3 1995/11/01 16:34:56 quinn
12 * Making data1 look for tables in data1_tabpath
14 * Revision 1.2 1995/11/01 13:54:46 quinn
17 * Revision 1.1 1995/11/01 11:56:08 quinn
18 * Added Retrieval (data management) functions en masse.
36 data1_maptab *data1_read_maptab(char *file)
38 data1_maptab *res = xmalloc(sizeof(*res));
41 char *argv[50], line[512];
44 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
46 logf(LOG_WARN|LOG_ERRNO, "%s", file);
51 res->target_absyn_ref = ODR_NONE;
56 while ((argc = readconf_line(f, line, 512, argv, 50)))
57 if (!strcmp(argv[0], "targetref"))
61 logf(LOG_WARN, "%s: one argument required for targetref",
65 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
67 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
71 else if (!strcmp(argv[0], "targetname"))
75 logf(LOG_WARN, "%s: one argument required for targetref",
79 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
80 strcpy(res->target_absyn_name, argv[1]);
82 else if (!strcmp(argv[0], "name"))
86 logf(LOG_WARN, "%s: one argument required for name",
90 res->name = xmalloc(strlen(argv[1])+1);
91 strcpy(res->name, argv[1]);
93 else if (!strcmp(argv[0], "map"))
96 char *ep, *path = argv[2];
100 logf(LOG_WARN, "%s: At least 2 arguments required for map",
104 *mapp = xmalloc(sizeof(**mapp));
106 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
107 (*mapp)->no_data = 1;
109 (*mapp)->no_data = 0;
110 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
111 strcpy((*mapp)->source_element_name, argv[1]);
112 mtp = &(*mapp)->target_path;
115 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
116 (ep = strchr(path, '/'))))
119 char valstr[512], parm[512];
123 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
126 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
131 *mtp = xmalloc(sizeof(**mtp));
134 if (np > 2 && !data1_matchstr(parm, "new"))
135 (*mtp)->new_field = 1;
137 (*mtp)->new_field = 0;
139 if ((numval = atoi(valstr)))
141 (*mtp)->which = D1_MAPTAG_numeric;
142 (*mtp)->value.numeric = numval;
147 (*mtp)->which = D1_MAPTAG_string;
148 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
149 strcpy((*mtp)->value.string, valstr);
155 mapp = &(*mapp)->next;
158 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
167 * Locate node with givel elementname.
168 * NOTE: This is stupid - we don't find repeats this way.
170 static data1_node *find_node(data1_node *p, char *elementname)
174 for (c = p->child; c; c = c->next)
175 if (c->which == DATA1N_tag && c->u.tag.element &&
176 !data1_matchstr(c->u.tag.element->name, elementname))
178 else if ((r = find_node(c, elementname)))
184 * See if the node n is equivalent to the tag t.
186 static int tagmatch(data1_node *n, data1_maptag *t)
188 if (n->which != DATA1N_tag)
190 if (n->u.tag.element)
192 if (n->u.tag.element->tag->tagset->type != t->type)
194 if (n->u.tag.element->tag->which == DATA1T_numeric)
196 if (t->which != D1_MAPTAG_numeric)
198 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
203 if (t->which != D1_MAPTAG_string)
205 if (data1_matchstr(n->u.tag.element->tag->value.string,
216 if (t->which == D1_MAPTAG_numeric)
217 sprintf(str, "%d", t->value.numeric);
219 strcpy(str, t->value.string);
220 if (data1_matchstr(n->u.tag.tag, str))
226 static int map_children(data1_node *n, data1_maptab *map, data1_node *res)
231 * locate each source element in turn.
233 for (c = n->child; c; c = c->next)
234 if (c->which == DATA1N_tag && c->u.tag.element)
236 for (m = map->map; m; m = m->next)
238 if (!data1_matchstr(m->source_element_name,
239 c->u.tag.element->name))
241 data1_node *pn = res;
246 * process the target path specification.
248 for (mt = m->target_path; mt; mt = mt->next)
254 for (l = pn->child, last = 0; l; last = l, l = l->next)
256 match = tagmatch(l, mt);
258 if (!tagmatch(l, mt))
260 if (!match || !mt->next || mt->new_field)
262 cur = data1_mk_node();
263 cur->which = DATA1N_tag;
264 cur->u.tag.element = 0;
265 cur->u.tag.tag = mt->value.string;
266 cur->u.tag.node_selected = 0;
268 cur->root = pn->root;
271 cur->next = pn->child;
276 cur->next = last->next;
282 cur = last ? last : pn->child;
286 else if (!m->no_data)
288 cur->child = c->child;
289 cur->num_children = c->num_children;
296 if (map_children(c, map, res) < 0)
306 * See if the node n is equivalent to the tag t.
308 static int tagmatch(data1_node *n, data1_maptag *t)
310 if (n->which != DATA1N_tag)
312 if (n->u.tag.element)
314 if (n->u.tag.element->tag->tagset->type != t->type)
316 if (n->u.tag.element->tag->which == DATA1T_numeric)
318 if (t->which != D1_MAPTAG_numeric)
320 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
325 if (t->which != D1_MAPTAG_string)
327 if (data1_matchstr(n->u.tag.element->tag->value.string,
338 if (t->which == D1_MAPTAG_numeric)
339 sprintf(str, "%d", t->value.numeric);
341 strcpy(str, t->value.string);
342 if (data1_matchstr(n->u.tag.tag, str))
348 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
352 for (c = n->child; c; c = c->next)
354 if (c->which == DATA1N_tag)
356 if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
357 m->source_element_name))
359 /* Process target path specification */
361 data1_node *pn = res, *cur = pn->last_child;
363 for (mt = m->target_path; mt; mt = mt->next)
365 if (!cur || !tagmatch(cur, mt))
367 cur = data1_mk_node();
368 cur->which = DATA1N_tag;
369 cur->u.tag.element = 0;
370 cur->u.tag.tag = mt->value.string;
371 cur->u.tag.node_selected = 0;
373 cur->root = pn->root;
377 pn->last_child->next = cur;
378 pn->last_child = cur;
383 else if (!m->no_data)
385 cur->child = c->child;
386 cur->num_children = c->num_children;
392 else if (map_elements(res, c, m) < 0)
399 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
403 for (m = map->map; m; m = m->next)
404 if (map_elements(res, n, m) < 0)
410 * Create a (possibly lossy) copy of the given record based on the
411 * table. The new copy will refer back to the data of the original record,
412 * which should not be discarded during the lifetime of the copy.
414 data1_node *data1_map_record(data1_node *n, data1_maptab *map)
416 data1_node *res = data1_mk_node();
418 res->which = DATA1N_root;
419 res->u.root.type = map->target_absyn_name;
420 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
422 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
423 map->name, map->target_absyn_name);
428 if (map_record(res, n, map) < 0)
430 data1_free_tree(res);