2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-09-17 12:10:36 adam
10 * Revision 1.10 1997/09/05 09:50:56 adam
11 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
13 * Revision 1.9 1996/06/10 08:56:02 quinn
16 * Revision 1.8 1996/05/01 12:45:31 quinn
17 * Support use of local tag names in abs file.
19 * Revision 1.7 1995/12/13 13:44:31 quinn
20 * Modified Data1-system to use nmem
22 * Revision 1.6 1995/12/12 16:37:08 quinn
23 * Added destroy element to data1_node.
25 * Revision 1.5 1995/12/12 14:11:31 quinn
26 * More work on the large-record problem.
28 * Revision 1.4 1995/12/11 15:22:37 quinn
29 * Added last_child field to the node.
30 * Rewrote schema-mapping.
32 * Revision 1.3 1995/11/01 16:34:56 quinn
33 * Making data1 look for tables in data1_tabpath
35 * Revision 1.2 1995/11/01 13:54:46 quinn
38 * Revision 1.1 1995/11/01 11:56:08 quinn
39 * Added Retrieval (data management) functions en masse.
57 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
59 NMEM mem = data1_nmem_get (dh);
60 data1_maptab *res = nmem_malloc(mem, sizeof(*res));
63 char *argv[50], line[512];
65 int local_numeric = 0;
67 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
69 logf(LOG_WARN|LOG_ERRNO, "%s", file);
74 res->target_absyn_ref = ODR_NONE;
79 while ((argc = readconf_line(f, line, 512, argv, 50)))
80 if (!strcmp(argv[0], "targetref"))
84 logf(LOG_WARN, "%s: one argument required for targetref",
88 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
90 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
94 else if (!strcmp(argv[0], "targetname"))
98 logf(LOG_WARN, "%s: one argument required for targetref",
102 res->target_absyn_name = nmem_malloc(mem, strlen(argv[1])+1);
103 strcpy(res->target_absyn_name, argv[1]);
105 else if (!yaz_matchstr(argv[0], "localnumeric"))
107 else if (!strcmp(argv[0], "name"))
111 logf(LOG_WARN, "%s: one argument required for name",
115 res->name = nmem_malloc(mem, strlen(argv[1])+1);
116 strcpy(res->name, argv[1]);
118 else if (!strcmp(argv[0], "map"))
121 char *ep, *path = argv[2];
125 logf(LOG_WARN, "%s: At least 2 arguments required for map",
129 *mapp = nmem_malloc(mem, sizeof(**mapp));
131 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
132 (*mapp)->no_data = 1;
134 (*mapp)->no_data = 0;
135 (*mapp)->source_element_name = nmem_malloc(mem, strlen(argv[1])+1);
136 strcpy((*mapp)->source_element_name, argv[1]);
137 mtp = &(*mapp)->target_path;
140 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
141 (ep = strchr(path, '/'))))
144 char valstr[512], parm[512];
148 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
151 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
156 *mtp = nmem_malloc(mem, sizeof(**mtp));
159 if (np > 2 && !data1_matchstr(parm, "new"))
160 (*mtp)->new_field = 1;
162 (*mtp)->new_field = 0;
163 if ((type != 3 || local_numeric) && isdigit(*valstr))
165 (*mtp)->which = D1_MAPTAG_numeric;
166 (*mtp)->value.numeric = atoi(valstr);
170 (*mtp)->which = D1_MAPTAG_string;
171 (*mtp)->value.string = nmem_malloc(mem, strlen(valstr)+1);
172 strcpy((*mtp)->value.string, valstr);
176 mapp = &(*mapp)->next;
179 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
186 * Locate node with given elementname.
187 * NOTE: This is stupid - we don't find repeats this way.
189 static data1_node *find_node(data1_node *p, char *elementname)
193 for (c = p->child; c; c = c->next)
194 if (c->which == DATA1N_tag && c->u.tag.element &&
195 !data1_matchstr(c->u.tag.element->name, elementname))
197 else if ((r = find_node(c, elementname)))
203 * See if the node n is equivalent to the tag t.
205 static int tagmatch(data1_node *n, data1_maptag *t)
207 if (n->which != DATA1N_tag)
209 if (n->u.tag.element)
211 if (n->u.tag.element->tag->tagset)
213 if (n->u.tag.element->tag->tagset->type != t->type)
216 else if (t->type != 3)
218 if (n->u.tag.element->tag->which == DATA1T_numeric)
220 if (t->which != D1_MAPTAG_numeric)
222 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
227 if (t->which != D1_MAPTAG_string)
229 if (data1_matchstr(n->u.tag.element->tag->value.string,
240 if (t->which == D1_MAPTAG_numeric)
241 sprintf(str, "%d", t->value.numeric);
243 strcpy(str, t->value.string);
244 if (data1_matchstr(n->u.tag.tag, str))
250 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
251 data1_node *res, NMEM mem)
256 * locate each source element in turn.
258 for (c = n->child; c; c = c->next)
259 if (c->which == DATA1N_tag && c->u.tag.element)
261 for (m = map->map; m; m = m->next)
263 if (!data1_matchstr(m->source_element_name,
264 c->u.tag.element->name))
266 data1_node *pn = res;
267 data1_node *cur = pn->last_child;
271 * process the target path specification.
273 for (mt = m->target_path; mt; mt = mt->next)
275 if (!cur || mt->new_field || !tagmatch(cur, mt))
277 cur = data1_mk_node (dh, mem);
278 cur->which = DATA1N_tag;
279 cur->u.tag.element = 0;
280 cur->u.tag.tag = mt->value.string;
281 cur->u.tag.node_selected = 0;
283 cur->root = pn->root;
287 pn->last_child->next = cur;
288 pn->last_child = cur;
294 else if (!m->no_data)
296 cur->child = c->child;
297 cur->last_child = c->last_child;
298 cur->num_children = c->num_children;
307 if (map_children(dh, c, map, res, mem) < 0)
314 * Create a (possibly lossy) copy of the given record based on the
315 * table. The new copy will refer back to the data of the original record,
316 * which should not be discarded during the lifetime of the copy.
318 data1_node *data1_map_record (data1_handle dh, data1_node *n,
319 data1_maptab *map, NMEM m)
321 data1_node *res = data1_mk_node(dh, m);
323 res->which = DATA1N_root;
324 res->u.root.type = map->target_absyn_name;
325 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
327 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
328 map->name, map->target_absyn_name);
333 if (map_children(dh, n, map, res, m) < 0)
335 data1_free_tree(dh, res);