2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-11-18 09:51:09 adam
8 * Removed element num_children from data1_node. Minor changes in
11 * Revision 1.11 1997/09/17 12:10:36 adam
14 * Revision 1.10 1997/09/05 09:50:56 adam
15 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
17 * Revision 1.9 1996/06/10 08:56:02 quinn
20 * Revision 1.8 1996/05/01 12:45:31 quinn
21 * Support use of local tag names in abs file.
23 * Revision 1.7 1995/12/13 13:44:31 quinn
24 * Modified Data1-system to use nmem
26 * Revision 1.6 1995/12/12 16:37:08 quinn
27 * Added destroy element to data1_node.
29 * Revision 1.5 1995/12/12 14:11:31 quinn
30 * More work on the large-record problem.
32 * Revision 1.4 1995/12/11 15:22:37 quinn
33 * Added last_child field to the node.
34 * Rewrote schema-mapping.
36 * Revision 1.3 1995/11/01 16:34:56 quinn
37 * Making data1 look for tables in data1_tabpath
39 * Revision 1.2 1995/11/01 13:54:46 quinn
42 * Revision 1.1 1995/11/01 11:56:08 quinn
43 * Added Retrieval (data management) functions en masse.
61 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
63 NMEM mem = data1_nmem_get (dh);
64 data1_maptab *res = nmem_malloc(mem, sizeof(*res));
67 char *argv[50], line[512];
69 int local_numeric = 0;
71 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
73 logf(LOG_WARN|LOG_ERRNO, "%s", file);
78 res->target_absyn_ref = ODR_NONE;
83 while ((argc = readconf_line(f, line, 512, argv, 50)))
84 if (!strcmp(argv[0], "targetref"))
88 logf(LOG_WARN, "%s: one argument required for targetref",
92 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
94 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
98 else if (!strcmp(argv[0], "targetname"))
102 logf(LOG_WARN, "%s: one argument required for targetref",
106 res->target_absyn_name = nmem_malloc(mem, strlen(argv[1])+1);
107 strcpy(res->target_absyn_name, argv[1]);
109 else if (!yaz_matchstr(argv[0], "localnumeric"))
111 else if (!strcmp(argv[0], "name"))
115 logf(LOG_WARN, "%s: one argument required for name",
119 res->name = nmem_malloc(mem, strlen(argv[1])+1);
120 strcpy(res->name, argv[1]);
122 else if (!strcmp(argv[0], "map"))
125 char *ep, *path = argv[2];
129 logf(LOG_WARN, "%s: At least 2 arguments required for map",
133 *mapp = nmem_malloc(mem, sizeof(**mapp));
135 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
136 (*mapp)->no_data = 1;
138 (*mapp)->no_data = 0;
139 (*mapp)->source_element_name = nmem_malloc(mem, strlen(argv[1])+1);
140 strcpy((*mapp)->source_element_name, argv[1]);
141 mtp = &(*mapp)->target_path;
144 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
145 (ep = strchr(path, '/'))))
148 char valstr[512], parm[512];
152 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
155 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
160 *mtp = nmem_malloc(mem, sizeof(**mtp));
163 if (np > 2 && !data1_matchstr(parm, "new"))
164 (*mtp)->new_field = 1;
166 (*mtp)->new_field = 0;
167 if ((type != 3 || local_numeric) && isdigit(*valstr))
169 (*mtp)->which = D1_MAPTAG_numeric;
170 (*mtp)->value.numeric = atoi(valstr);
174 (*mtp)->which = D1_MAPTAG_string;
175 (*mtp)->value.string = nmem_malloc(mem, strlen(valstr)+1);
176 strcpy((*mtp)->value.string, valstr);
180 mapp = &(*mapp)->next;
183 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
190 * Locate node with given elementname.
191 * NOTE: This is stupid - we don't find repeats this way.
193 static data1_node *find_node(data1_node *p, char *elementname)
197 for (c = p->child; c; c = c->next)
198 if (c->which == DATA1N_tag && c->u.tag.element &&
199 !data1_matchstr(c->u.tag.element->name, elementname))
201 else if ((r = find_node(c, elementname)))
207 * See if the node n is equivalent to the tag t.
209 static int tagmatch(data1_node *n, data1_maptag *t)
211 if (n->which != DATA1N_tag)
213 if (n->u.tag.element)
215 if (n->u.tag.element->tag->tagset)
217 if (n->u.tag.element->tag->tagset->type != t->type)
220 else if (t->type != 3)
222 if (n->u.tag.element->tag->which == DATA1T_numeric)
224 if (t->which != D1_MAPTAG_numeric)
226 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
231 if (t->which != D1_MAPTAG_string)
233 if (data1_matchstr(n->u.tag.element->tag->value.string,
244 if (t->which == D1_MAPTAG_numeric)
245 sprintf(str, "%d", t->value.numeric);
247 strcpy(str, t->value.string);
248 if (data1_matchstr(n->u.tag.tag, str))
254 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
255 data1_node *res, NMEM mem)
260 * locate each source element in turn.
262 for (c = n->child; c; c = c->next)
263 if (c->which == DATA1N_tag && c->u.tag.element)
265 for (m = map->map; m; m = m->next)
267 if (!data1_matchstr(m->source_element_name,
268 c->u.tag.element->name))
270 data1_node *pn = res;
271 data1_node *cur = pn->last_child;
275 * process the target path specification.
277 for (mt = m->target_path; mt; mt = mt->next)
279 if (!cur || mt->new_field || !tagmatch(cur, mt))
281 cur = data1_mk_node (dh, mem);
282 cur->which = DATA1N_tag;
283 cur->u.tag.element = 0;
284 cur->u.tag.tag = mt->value.string;
285 cur->u.tag.node_selected = 0;
287 cur->root = pn->root;
291 pn->last_child->next = cur;
292 pn->last_child = cur;
297 else if (!m->no_data)
299 cur->child = c->child;
300 cur->last_child = c->last_child;
308 if (map_children(dh, c, map, res, mem) < 0)
315 * Create a (possibly lossy) copy of the given record based on the
316 * table. The new copy will refer back to the data of the original record,
317 * which should not be discarded during the lifetime of the copy.
319 data1_node *data1_map_record (data1_handle dh, data1_node *n,
320 data1_maptab *map, NMEM m)
322 data1_node *res = data1_mk_node(dh, m);
324 res->which = DATA1N_root;
325 res->u.root.type = map->target_absyn_name;
326 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
328 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
329 map->name, map->target_absyn_name);
334 if (map_children(dh, n, map, res, m) < 0)
336 data1_free_tree(dh, res);