2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-06-10 08:56:02 quinn
10 * Revision 1.8 1996/05/01 12:45:31 quinn
11 * Support use of local tag names in abs file.
13 * Revision 1.7 1995/12/13 13:44:31 quinn
14 * Modified Data1-system to use nmem
16 * Revision 1.6 1995/12/12 16:37:08 quinn
17 * Added destroy element to data1_node.
19 * Revision 1.5 1995/12/12 14:11:31 quinn
20 * More work on the large-record problem.
22 * Revision 1.4 1995/12/11 15:22:37 quinn
23 * Added last_child field to the node.
24 * Rewrote schema-mapping.
26 * Revision 1.3 1995/11/01 16:34:56 quinn
27 * Making data1 look for tables in data1_tabpath
29 * Revision 1.2 1995/11/01 13:54:46 quinn
32 * Revision 1.1 1995/11/01 11:56:08 quinn
33 * Added Retrieval (data management) functions en masse.
52 data1_maptab *data1_read_maptab(char *file)
54 data1_maptab *res = xmalloc(sizeof(*res));
57 char *argv[50], line[512];
59 int local_numeric = 0;
61 if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
63 logf(LOG_WARN|LOG_ERRNO, "%s", file);
68 res->target_absyn_ref = ODR_NONE;
73 while ((argc = readconf_line(f, line, 512, argv, 50)))
74 if (!strcmp(argv[0], "targetref"))
78 logf(LOG_WARN, "%s: one argument required for targetref",
82 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
84 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
88 else if (!strcmp(argv[0], "targetname"))
92 logf(LOG_WARN, "%s: one argument required for targetref",
96 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
97 strcpy(res->target_absyn_name, argv[1]);
99 else if (!yaz_matchstr(argv[0], "localnumeric"))
101 else if (!strcmp(argv[0], "name"))
105 logf(LOG_WARN, "%s: one argument required for name",
109 res->name = xmalloc(strlen(argv[1])+1);
110 strcpy(res->name, argv[1]);
112 else if (!strcmp(argv[0], "map"))
115 char *ep, *path = argv[2];
119 logf(LOG_WARN, "%s: At least 2 arguments required for map",
123 *mapp = xmalloc(sizeof(**mapp));
125 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
126 (*mapp)->no_data = 1;
128 (*mapp)->no_data = 0;
129 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
130 strcpy((*mapp)->source_element_name, argv[1]);
131 mtp = &(*mapp)->target_path;
134 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
135 (ep = strchr(path, '/'))))
138 char valstr[512], parm[512];
142 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
145 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
150 *mtp = xmalloc(sizeof(**mtp));
153 if (np > 2 && !data1_matchstr(parm, "new"))
154 (*mtp)->new_field = 1;
156 (*mtp)->new_field = 0;
157 if ((type != 3 || local_numeric) && isdigit(*valstr))
159 (*mtp)->which = D1_MAPTAG_numeric;
160 (*mtp)->value.numeric = atoi(valstr);
164 (*mtp)->which = D1_MAPTAG_string;
165 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
166 strcpy((*mtp)->value.string, valstr);
170 mapp = &(*mapp)->next;
173 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
180 * Locate node with given elementname.
181 * NOTE: This is stupid - we don't find repeats this way.
183 static data1_node *find_node(data1_node *p, char *elementname)
187 for (c = p->child; c; c = c->next)
188 if (c->which == DATA1N_tag && c->u.tag.element &&
189 !data1_matchstr(c->u.tag.element->name, elementname))
191 else if ((r = find_node(c, elementname)))
197 * See if the node n is equivalent to the tag t.
199 static int tagmatch(data1_node *n, data1_maptag *t)
201 if (n->which != DATA1N_tag)
203 if (n->u.tag.element)
205 if (n->u.tag.element->tag->tagset)
207 if (n->u.tag.element->tag->tagset->type != t->type)
210 else if (t->type != 3)
212 if (n->u.tag.element->tag->which == DATA1T_numeric)
214 if (t->which != D1_MAPTAG_numeric)
216 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
221 if (t->which != D1_MAPTAG_string)
223 if (data1_matchstr(n->u.tag.element->tag->value.string,
234 if (t->which == D1_MAPTAG_numeric)
235 sprintf(str, "%d", t->value.numeric);
237 strcpy(str, t->value.string);
238 if (data1_matchstr(n->u.tag.tag, str))
244 static int map_children(data1_node *n, data1_maptab *map, data1_node *res,
250 * locate each source element in turn.
252 for (c = n->child; c; c = c->next)
253 if (c->which == DATA1N_tag && c->u.tag.element)
255 for (m = map->map; m; m = m->next)
257 if (!data1_matchstr(m->source_element_name,
258 c->u.tag.element->name))
260 data1_node *pn = res;
261 data1_node *cur = pn->last_child;
265 * process the target path specification.
267 for (mt = m->target_path; mt; mt = mt->next)
269 if (!cur || mt->new_field || !tagmatch(cur, mt))
271 cur = data1_mk_node(mem);
272 cur->which = DATA1N_tag;
273 cur->u.tag.element = 0;
274 cur->u.tag.tag = mt->value.string;
275 cur->u.tag.node_selected = 0;
277 cur->root = pn->root;
281 pn->last_child->next = cur;
282 pn->last_child = cur;
288 else if (!m->no_data)
290 cur->child = c->child;
291 cur->last_child = c->last_child;
292 cur->num_children = c->num_children;
301 if (map_children(c, map, res, mem) < 0)
308 * Create a (possibly lossy) copy of the given record based on the
309 * table. The new copy will refer back to the data of the original record,
310 * which should not be discarded during the lifetime of the copy.
312 data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m)
314 data1_node *res = data1_mk_node(m);
316 res->which = DATA1N_root;
317 res->u.root.type = map->target_absyn_name;
318 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
320 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
321 map->name, map->target_absyn_name);
326 if (map_children(n, map, res, m) < 0)
328 data1_free_tree(res);