1 /* $Id: d1_map.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include <yaz/readconf.h>
30 #include <yaz/tpath.h>
34 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
36 NMEM mem = data1_nmem_get (dh);
37 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
41 char *argv[50], line[512];
43 int local_numeric = 0;
45 if (!(f = data1_path_fopen(dh, file, "r")))
47 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
52 res->target_absyn_ref = VAL_NONE;
57 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
58 if (!strcmp(argv[0], "targetref"))
62 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
66 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
69 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
70 file, lineno, argv[1]);
74 else if (!strcmp(argv[0], "targetname"))
78 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
82 res->target_absyn_name =
83 (char *)nmem_malloc(mem, strlen(argv[1])+1);
84 strcpy(res->target_absyn_name, argv[1]);
86 else if (!yaz_matchstr(argv[0], "localnumeric"))
88 else if (!strcmp(argv[0], "name"))
92 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
95 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
96 strcpy(res->name, argv[1]);
98 else if (!strcmp(argv[0], "map"))
101 char *ep, *path = argv[2];
105 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
109 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
111 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
112 (*mapp)->no_data = 1;
114 (*mapp)->no_data = 0;
115 (*mapp)->source_element_name =
116 (char *)nmem_malloc(mem, strlen(argv[1])+1);
117 strcpy((*mapp)->source_element_name, argv[1]);
118 mtp = &(*mapp)->target_path;
121 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
122 (ep = strchr(path, '/'))))
125 char valstr[512], parm[512];
129 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
132 yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
133 "directive: %s", file, lineno, argv[2]);
137 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
140 if (np > 2 && !data1_matchstr(parm, "new"))
141 (*mtp)->new_field = 1;
143 (*mtp)->new_field = 0;
144 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
146 (*mtp)->which = D1_MAPTAG_numeric;
147 (*mtp)->value.numeric = atoi(valstr);
151 (*mtp)->which = D1_MAPTAG_string;
152 (*mtp)->value.string =
153 (char *)nmem_malloc(mem, strlen(valstr)+1);
154 strcpy((*mtp)->value.string, valstr);
158 mapp = &(*mapp)->next;
161 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
162 file, lineno, argv[0]);
169 * Locate node with given elementname.
170 * NOTE: This is stupid - we don't find repeats this way.
172 static data1_node *find_node(data1_node *p, char *elementname)
176 for (c = p->child; c; c = c->next)
177 if (c->which == DATA1N_tag && c->u.tag.element &&
178 !data1_matchstr(c->u.tag.element->name, elementname))
180 else if ((r = find_node(c, elementname)))
186 * See if the node n is equivalent to the tag t.
188 static int tagmatch(data1_node *n, data1_maptag *t)
190 if (n->which != DATA1N_tag)
192 if (n->u.tag.element)
194 if (n->u.tag.element->tag->tagset)
196 if (n->u.tag.element->tag->tagset->type != t->type)
199 else if (t->type != 3)
201 if (n->u.tag.element->tag->which == DATA1T_numeric)
203 if (t->which != D1_MAPTAG_numeric)
205 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
210 if (t->which != D1_MAPTAG_string)
212 if (data1_matchstr(n->u.tag.element->tag->value.string,
223 if (t->which == D1_MAPTAG_numeric)
224 sprintf(str, "%d", t->value.numeric);
226 strcpy(str, t->value.string);
227 if (data1_matchstr(n->u.tag.tag, str))
233 static data1_node *dup_child (data1_handle dh, data1_node *n,
234 data1_node **last, NMEM mem,
237 data1_node *first = 0;
238 data1_node **m = &first;
240 for (; n; n = n->next)
242 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
243 memcpy (*m, n, sizeof(**m));
245 (*m)->parent = parent;
246 (*m)->root = parent->root;
247 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
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_node2 (dh, mem, DATA1N_tag, pn);
282 cur->u.tag.tag = mt->value.string;
287 else if (!m->no_data)
290 dup_child (dh, c->child,
291 &cur->last_child, mem, cur);
296 if (map_children(dh, c, map, res, mem) < 0)
303 * Create a (possibly lossy) copy of the given record based on the
304 * table. The new copy will refer back to the data of the original record,
305 * which should not be discarded during the lifetime of the copy.
307 data1_node *data1_map_record (data1_handle dh, data1_node *n,
308 data1_maptab *map, NMEM m)
310 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
312 res->which = DATA1N_root;
313 res->u.root.type = map->target_absyn_name;
314 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
316 yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
317 map->name, map->target_absyn_name);
319 if (data1_is_xmlmode(dh))
324 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
329 if (map_children(dh, n, map, res1, m) < 0)
331 data1_free_tree(dh, res);