2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-12-13 13:44:31 quinn
8 * Modified Data1-system to use nmem
10 * Revision 1.6 1995/12/12 16:37:08 quinn
11 * Added destroy element to data1_node.
13 * Revision 1.5 1995/12/12 14:11:31 quinn
14 * More work on the large-record problem.
16 * Revision 1.4 1995/12/11 15:22:37 quinn
17 * Added last_child field to the node.
18 * Rewrote schema-mapping.
20 * Revision 1.3 1995/11/01 16:34:56 quinn
21 * Making data1 look for tables in data1_tabpath
23 * Revision 1.2 1995/11/01 13:54:46 quinn
26 * Revision 1.1 1995/11/01 11:56:08 quinn
27 * Added Retrieval (data management) functions en masse.
45 data1_maptab *data1_read_maptab(char *file)
47 data1_maptab *res = xmalloc(sizeof(*res));
50 char *argv[50], line[512];
53 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
55 logf(LOG_WARN|LOG_ERRNO, "%s", file);
60 res->target_absyn_ref = ODR_NONE;
65 while ((argc = readconf_line(f, line, 512, argv, 50)))
66 if (!strcmp(argv[0], "targetref"))
70 logf(LOG_WARN, "%s: one argument required for targetref",
74 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
76 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
80 else if (!strcmp(argv[0], "targetname"))
84 logf(LOG_WARN, "%s: one argument required for targetref",
88 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
89 strcpy(res->target_absyn_name, argv[1]);
91 else if (!strcmp(argv[0], "name"))
95 logf(LOG_WARN, "%s: one argument required for name",
99 res->name = xmalloc(strlen(argv[1])+1);
100 strcpy(res->name, argv[1]);
102 else if (!strcmp(argv[0], "map"))
105 char *ep, *path = argv[2];
109 logf(LOG_WARN, "%s: At least 2 arguments required for map",
113 *mapp = xmalloc(sizeof(**mapp));
115 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
116 (*mapp)->no_data = 1;
118 (*mapp)->no_data = 0;
119 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
120 strcpy((*mapp)->source_element_name, argv[1]);
121 mtp = &(*mapp)->target_path;
124 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
125 (ep = strchr(path, '/'))))
128 char valstr[512], parm[512];
132 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
135 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
140 *mtp = xmalloc(sizeof(**mtp));
143 if (np > 2 && !data1_matchstr(parm, "new"))
144 (*mtp)->new_field = 1;
146 (*mtp)->new_field = 0;
148 if ((numval = atoi(valstr)))
150 (*mtp)->which = D1_MAPTAG_numeric;
151 (*mtp)->value.numeric = numval;
156 (*mtp)->which = D1_MAPTAG_string;
157 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
158 strcpy((*mtp)->value.string, valstr);
164 mapp = &(*mapp)->next;
167 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
176 * Locate node with givel elementname.
177 * NOTE: This is stupid - we don't find repeats this way.
179 static data1_node *find_node(data1_node *p, char *elementname)
183 for (c = p->child; c; c = c->next)
184 if (c->which == DATA1N_tag && c->u.tag.element &&
185 !data1_matchstr(c->u.tag.element->name, elementname))
187 else if ((r = find_node(c, elementname)))
193 * See if the node n is equivalent to the tag t.
195 static int tagmatch(data1_node *n, data1_maptag *t)
197 if (n->which != DATA1N_tag)
199 if (n->u.tag.element)
201 if (n->u.tag.element->tag->tagset->type != t->type)
203 if (n->u.tag.element->tag->which == DATA1T_numeric)
205 if (t->which != D1_MAPTAG_numeric)
207 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
212 if (t->which != D1_MAPTAG_string)
214 if (data1_matchstr(n->u.tag.element->tag->value.string,
225 if (t->which == D1_MAPTAG_numeric)
226 sprintf(str, "%d", t->value.numeric);
228 strcpy(str, t->value.string);
229 if (data1_matchstr(n->u.tag.tag, str))
235 static int map_children(data1_node *n, data1_maptab *map, data1_node *res,
241 * locate each source element in turn.
243 for (c = n->child; c; c = c->next)
244 if (c->which == DATA1N_tag && c->u.tag.element)
246 for (m = map->map; m; m = m->next)
248 if (!data1_matchstr(m->source_element_name,
249 c->u.tag.element->name))
251 data1_node *pn = res;
252 data1_node *cur = pn->last_child;
256 * process the target path specification.
258 for (mt = m->target_path; mt; mt = mt->next)
260 if (!cur || mt->new_field || !tagmatch(cur, mt))
262 cur = data1_mk_node(mem);
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;
272 pn->last_child->next = cur;
273 pn->last_child = cur;
279 else if (!m->no_data)
281 cur->child = c->child;
282 cur->last_child = c->last_child;
283 cur->num_children = c->num_children;
292 if (map_children(c, map, res, mem) < 0)
302 * See if the node n is equivalent to the tag t.
304 static int tagmatch(data1_node *n, data1_maptag *t)
306 if (n->which != DATA1N_tag)
308 if (n->u.tag.element)
310 if (n->u.tag.element->tag->tagset->type != t->type)
312 if (n->u.tag.element->tag->which == DATA1T_numeric)
314 if (t->which != D1_MAPTAG_numeric)
316 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
321 if (t->which != D1_MAPTAG_string)
323 if (data1_matchstr(n->u.tag.element->tag->value.string,
334 if (t->which == D1_MAPTAG_numeric)
335 sprintf(str, "%d", t->value.numeric);
337 strcpy(str, t->value.string);
338 if (data1_matchstr(n->u.tag.tag, str))
344 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
348 for (c = n->child; c; c = c->next)
350 if (c->which == DATA1N_tag)
352 if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
353 m->source_element_name))
355 /* Process target path specification */
357 data1_node *pn = res, *cur = pn->last_child;
359 for (mt = m->target_path; mt; mt = mt->next)
361 if (!cur || !tagmatch(cur, mt))
363 cur = data1_mk_node();
364 cur->which = DATA1N_tag;
365 cur->u.tag.element = 0;
366 cur->u.tag.tag = mt->value.string;
367 cur->u.tag.node_selected = 0;
369 cur->root = pn->root;
373 pn->last_child->next = cur;
374 pn->last_child = cur;
379 else if (!m->no_data)
381 cur->child = c->child;
382 cur->num_children = c->num_children;
388 else if (map_elements(res, c, m) < 0)
395 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
399 for (m = map->map; m; m = m->next)
400 if (map_elements(res, n, m) < 0)
408 * Create a (possibly lossy) copy of the given record based on the
409 * table. The new copy will refer back to the data of the original record,
410 * which should not be discarded during the lifetime of the copy.
412 data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m)
414 data1_node *res = data1_mk_node(m);
416 res->which = DATA1N_root;
417 res->u.root.type = map->target_absyn_name;
418 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
420 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
421 map->name, map->target_absyn_name);
426 if (map_children(n, map, res, m) < 0)
428 data1_free_tree(res);