1 /* $Id: d1_map.c,v 1.8 2005-01-15 19:38:18 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")))
46 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", file);
51 res->target_absyn_ref = VAL_NONE;
56 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
57 if (!strcmp(argv[0], "targetref"))
61 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetref",
65 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
68 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
69 file, lineno, argv[1]);
73 else if (!strcmp(argv[0], "targetname"))
77 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetname",
81 res->target_absyn_name =
82 (char *)nmem_malloc(mem, strlen(argv[1])+1);
83 strcpy(res->target_absyn_name, argv[1]);
85 else if (!yaz_matchstr(argv[0], "localnumeric"))
87 else if (!strcmp(argv[0], "name"))
91 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name", file, lineno);
94 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
95 strcpy(res->name, argv[1]);
97 else if (!strcmp(argv[0], "map"))
100 char *ep, *path = argv[2];
104 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for map",
108 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
110 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
111 (*mapp)->no_data = 1;
113 (*mapp)->no_data = 0;
114 (*mapp)->source_element_name =
115 (char *)nmem_malloc(mem, strlen(argv[1])+1);
116 strcpy((*mapp)->source_element_name, argv[1]);
117 mtp = &(*mapp)->target_path;
120 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
121 (ep = strchr(path, '/'))))
124 char valstr[512], parm[512];
128 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
131 yaz_log(YLOG_WARN, "%s:%d: Syntax error in map "
132 "directive: %s", file, lineno, argv[2]);
136 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
139 if (np > 2 && !data1_matchstr(parm, "new"))
140 (*mtp)->new_field = 1;
142 (*mtp)->new_field = 0;
143 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
145 (*mtp)->which = D1_MAPTAG_numeric;
146 (*mtp)->value.numeric = atoi(valstr);
150 (*mtp)->which = D1_MAPTAG_string;
151 (*mtp)->value.string =
152 (char *)nmem_malloc(mem, strlen(valstr)+1);
153 strcpy((*mtp)->value.string, valstr);
157 mapp = &(*mapp)->next;
160 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
161 file, lineno, argv[0]);
168 * Locate node with given elementname.
169 * NOTE: This is stupid - we don't find repeats this way.
171 static data1_node *find_node(data1_node *p, char *elementname)
175 for (c = p->child; c; c = c->next)
176 if (c->which == DATA1N_tag && c->u.tag.element &&
177 !data1_matchstr(c->u.tag.element->name, elementname))
179 else if ((r = find_node(c, elementname)))
185 * See if the node n is equivalent to the tag t.
187 static int tagmatch(data1_node *n, data1_maptag *t)
189 if (n->which != DATA1N_tag)
191 if (n->u.tag.element)
193 if (n->u.tag.element->tag->tagset)
195 if (n->u.tag.element->tag->tagset->type != t->type)
198 else if (t->type != 3)
200 if (n->u.tag.element->tag->which == DATA1T_numeric)
202 if (t->which != D1_MAPTAG_numeric)
204 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
209 if (t->which != D1_MAPTAG_string)
211 if (data1_matchstr(n->u.tag.element->tag->value.string,
222 if (t->which == D1_MAPTAG_numeric)
223 sprintf(str, "%d", t->value.numeric);
225 strcpy(str, t->value.string);
226 if (data1_matchstr(n->u.tag.tag, str))
232 static data1_node *dup_child (data1_handle dh, data1_node *n,
233 data1_node **last, NMEM mem,
236 data1_node *first = 0;
237 data1_node **m = &first;
239 for (; n; n = n->next)
241 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
242 memcpy (*m, n, sizeof(**m));
244 (*m)->parent = parent;
245 (*m)->root = parent->root;
246 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
253 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
254 data1_node *res, NMEM mem)
259 * locate each source element in turn.
261 for (c = n->child; c; c = c->next)
262 if (c->which == DATA1N_tag && c->u.tag.element)
264 for (m = map->map; m; m = m->next)
266 if (!data1_matchstr(m->source_element_name,
267 c->u.tag.element->name))
269 data1_node *pn = res;
270 data1_node *cur = pn->last_child;
274 * process the target path specification.
276 for (mt = m->target_path; mt; mt = mt->next)
278 if (!cur || mt->new_field || !tagmatch(cur, mt))
280 if (mt->which == D1_MAPTAG_string)
282 cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
283 cur->u.tag.tag = mt->value.string;
285 else if (mt->which == D1_MAPTAG_numeric)
290 pn->root->u.root.absyn->tagset,
294 if (tag && tag->names->name)
297 dh, mem, tag->names->name, 0, pn);
305 else if (!m->no_data)
308 dup_child (dh, c->child,
309 &cur->last_child, mem, cur);
314 if (map_children(dh, c, map, res, mem) < 0)
321 * Create a (possibly lossy) copy of the given record based on the
322 * table. The new copy will refer back to the data of the original record,
323 * which should not be discarded during the lifetime of the copy.
325 data1_node *data1_map_record (data1_handle dh, data1_node *n,
326 data1_maptab *map, NMEM m)
328 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
330 res->which = DATA1N_root;
331 res->u.root.type = map->target_absyn_name;
332 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
334 yaz_log(YLOG_WARN, "%s: Failed to load target absyn '%s'",
335 map->name, map->target_absyn_name);
337 if (data1_is_xmlmode(dh))
342 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
347 if (map_children(dh, n, map, res1, m) < 0)
349 data1_free_tree(dh, res);