1 /* $Id: d1_map.c,v 1.10 2006-06-08 10:33:19 adam Exp $
2 Copyright (C) 1995-2005
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>
33 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
35 NMEM mem = data1_nmem_get (dh);
36 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
40 char *argv[50], line[512];
42 int local_numeric = 0;
44 if (!(f = data1_path_fopen(dh, file, "r")))
48 res->target_absyn_ref = VAL_NONE;
53 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
54 if (!strcmp(argv[0], "targetref"))
58 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetref",
62 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
65 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
66 file, lineno, argv[1]);
70 else if (!strcmp(argv[0], "targetname"))
74 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetname",
78 res->target_absyn_name =
79 (char *)nmem_malloc(mem, strlen(argv[1])+1);
80 strcpy(res->target_absyn_name, argv[1]);
82 else if (!yaz_matchstr(argv[0], "localnumeric"))
84 else if (!strcmp(argv[0], "name"))
88 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name", file, lineno);
91 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
92 strcpy(res->name, argv[1]);
94 else if (!strcmp(argv[0], "map"))
97 char *ep, *path = argv[2];
101 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for map",
105 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
107 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
108 (*mapp)->no_data = 1;
110 (*mapp)->no_data = 0;
111 (*mapp)->source_element_name =
112 (char *)nmem_malloc(mem, strlen(argv[1])+1);
113 strcpy((*mapp)->source_element_name, argv[1]);
114 mtp = &(*mapp)->target_path;
117 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
118 (ep = strchr(path, '/'))))
121 char valstr[512], parm[512];
125 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
128 yaz_log(YLOG_WARN, "%s:%d: Syntax error in map "
129 "directive: %s", file, lineno, argv[2]);
133 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
136 if (np > 2 && !data1_matchstr(parm, "new"))
137 (*mtp)->new_field = 1;
139 (*mtp)->new_field = 0;
140 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
142 (*mtp)->which = D1_MAPTAG_numeric;
143 (*mtp)->value.numeric = atoi(valstr);
147 (*mtp)->which = D1_MAPTAG_string;
148 (*mtp)->value.string =
149 (char *)nmem_malloc(mem, strlen(valstr)+1);
150 strcpy((*mtp)->value.string, valstr);
154 mapp = &(*mapp)->next;
157 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
158 file, lineno, argv[0]);
165 * Locate node with given elementname.
166 * NOTE: This is stupid - we don't find repeats this way.
168 static data1_node *find_node(data1_node *p, char *elementname)
172 for (c = p->child; c; c = c->next)
173 if (c->which == DATA1N_tag && c->u.tag.element &&
174 !data1_matchstr(c->u.tag.element->name, elementname))
176 else if ((r = find_node(c, elementname)))
182 * See if the node n is equivalent to the tag t.
184 static int tagmatch(data1_node *n, data1_maptag *t)
186 if (n->which != DATA1N_tag)
188 if (n->u.tag.element)
190 if (n->u.tag.element->tag->tagset)
192 if (n->u.tag.element->tag->tagset->type != t->type)
195 else if (t->type != 3)
197 if (n->u.tag.element->tag->which == DATA1T_numeric)
199 if (t->which != D1_MAPTAG_numeric)
201 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
206 if (t->which != D1_MAPTAG_string)
208 if (data1_matchstr(n->u.tag.element->tag->value.string,
219 if (t->which == D1_MAPTAG_numeric)
220 sprintf(str, "%d", t->value.numeric);
222 strcpy(str, t->value.string);
223 if (data1_matchstr(n->u.tag.tag, str))
229 static data1_node *dup_child (data1_handle dh, data1_node *n,
230 data1_node **last, NMEM mem,
233 data1_node *first = 0;
234 data1_node **m = &first;
236 for (; n; n = n->next)
238 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
239 memcpy (*m, n, sizeof(**m));
241 (*m)->parent = parent;
242 (*m)->root = parent->root;
243 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
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 if (mt->which == D1_MAPTAG_string)
279 cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
280 cur->u.tag.tag = mt->value.string;
282 else if (mt->which == D1_MAPTAG_numeric)
287 pn->root->u.root.absyn->tagset,
291 if (tag && tag->names->name)
294 dh, mem, tag->names->name, 0, pn);
302 else if (!m->no_data)
305 dup_child (dh, c->child,
306 &cur->last_child, mem, cur);
311 if (map_children(dh, c, map, res, mem) < 0)
318 * Create a (possibly lossy) copy of the given record based on the
319 * table. The new copy will refer back to the data of the original record,
320 * which should not be discarded during the lifetime of the copy.
322 data1_node *data1_map_record (data1_handle dh, data1_node *n,
323 data1_maptab *map, NMEM m)
325 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
327 res->which = DATA1N_root;
328 res->u.root.type = map->target_absyn_name;
329 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
331 yaz_log(YLOG_WARN, "%s: Failed to load target absyn '%s'",
332 map->name, map->target_absyn_name);
334 if (data1_is_xmlmode(dh))
339 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
344 if (map_children(dh, n, map, res1, m) < 0)
346 data1_free_tree(dh, res);
355 * indent-tabs-mode: nil
357 * vim: shiftwidth=4 tabstop=8 expandtab