1 /* $Id: d1_map.c,v 1.16 2007-04-16 08:44:31 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/oid_db.h>
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")))
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 res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
63 CLASS_RECSYN, argv[1], mem);
66 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
67 file, lineno, argv[1]);
71 else if (!strcmp(argv[0], "targetname"))
75 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetname",
79 res->target_absyn_name =
80 (char *)nmem_malloc(mem, strlen(argv[1])+1);
81 strcpy(res->target_absyn_name, argv[1]);
83 else if (!yaz_matchstr(argv[0], "localnumeric"))
85 else if (!strcmp(argv[0], "name"))
89 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name", file, lineno);
92 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
93 strcpy(res->name, argv[1]);
95 else if (!strcmp(argv[0], "map"))
98 char *ep, *path = argv[2];
102 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for map",
106 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
108 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
109 (*mapp)->no_data = 1;
111 (*mapp)->no_data = 0;
112 (*mapp)->source_element_name =
113 (char *)nmem_malloc(mem, strlen(argv[1])+1);
114 strcpy((*mapp)->source_element_name, argv[1]);
115 mtp = &(*mapp)->target_path;
118 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
119 (ep = strchr(path, '/'))))
122 char valstr[512], parm[512];
126 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
129 yaz_log(YLOG_WARN, "%s:%d: Syntax error in map "
130 "directive: %s", file, lineno, argv[2]);
134 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
137 if (np > 2 && !data1_matchstr(parm, "new"))
138 (*mtp)->new_field = 1;
140 (*mtp)->new_field = 0;
141 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
143 (*mtp)->which = D1_MAPTAG_numeric;
144 (*mtp)->value.numeric = atoi(valstr);
148 (*mtp)->which = D1_MAPTAG_string;
149 (*mtp)->value.string =
150 (char *)nmem_malloc(mem, strlen(valstr)+1);
151 strcpy((*mtp)->value.string, valstr);
155 mapp = &(*mapp)->next;
158 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
159 file, lineno, argv[0]);
166 * Locate node with given elementname.
167 * NOTE: This is stupid - we don't find repeats this way.
169 static data1_node *find_node(data1_node *p, char *elementname)
173 for (c = p->child; c; c = c->next)
174 if (c->which == DATA1N_tag && c->u.tag.element &&
175 !data1_matchstr(c->u.tag.element->name, elementname))
177 else if ((r = find_node(c, elementname)))
183 * See if the node n is equivalent to the tag t.
185 static int tagmatch(data1_node *n, data1_maptag *t)
187 if (n->which != DATA1N_tag)
189 if (n->u.tag.element)
191 if (n->u.tag.element->tag->tagset)
193 if (n->u.tag.element->tag->tagset->type != t->type)
196 else if (t->type != 3)
198 if (n->u.tag.element->tag->which == DATA1T_numeric)
200 if (t->which != D1_MAPTAG_numeric)
202 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
207 if (t->which != D1_MAPTAG_string)
209 if (data1_matchstr(n->u.tag.element->tag->value.string,
220 if (t->which == D1_MAPTAG_numeric)
221 sprintf(str, "%d", t->value.numeric);
223 strcpy(str, t->value.string);
224 if (data1_matchstr(n->u.tag.tag, str))
230 static data1_node *dup_child (data1_handle dh, data1_node *n,
231 data1_node **last, NMEM mem,
234 data1_node *first = 0;
235 data1_node **m = &first;
237 for (; n; n = n->next)
239 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
240 memcpy (*m, n, sizeof(**m));
242 (*m)->parent = parent;
243 (*m)->root = parent->root;
244 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
251 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
252 data1_node *res, NMEM mem)
257 * locate each source element in turn.
259 for (c = n->child; c; c = c->next)
260 if (c->which == DATA1N_tag && c->u.tag.element)
262 for (m = map->map; m; m = m->next)
264 if (!data1_matchstr(m->source_element_name,
265 c->u.tag.element->name))
267 data1_node *pn = res;
268 data1_node *cur = pn->last_child;
272 * process the target path specification.
274 for (mt = m->target_path; mt; mt = mt->next)
276 if (!cur || mt->new_field || !tagmatch(cur, mt))
278 if (mt->which == D1_MAPTAG_string)
280 cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
281 cur->u.tag.tag = mt->value.string;
283 else if (mt->which == D1_MAPTAG_numeric)
288 pn->root->u.root.absyn->tagset,
292 if (tag && tag->names->name)
295 dh, mem, tag->names->name, 0, pn);
303 else if (!m->no_data)
306 dup_child (dh, c->child,
307 &cur->last_child, mem, cur);
312 if (map_children(dh, c, map, res, mem) < 0)
319 * Create a (possibly lossy) copy of the given record based on the
320 * table. The new copy will refer back to the data of the original record,
321 * which should not be discarded during the lifetime of the copy.
323 data1_node *data1_map_record (data1_handle dh, data1_node *n,
324 data1_maptab *map, NMEM m)
326 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
328 res->which = DATA1N_root;
329 res->u.root.type = map->target_absyn_name;
330 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name,
331 DATA1_XPATH_INDEXING_ENABLE)))
333 yaz_log(YLOG_WARN, "%s: Failed to load target absyn '%s'",
334 map->name, map->target_absyn_name);
339 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
340 while (n && n->which != DATA1N_tag)
342 if (map_children(dh, n, map, res1, m) < 0)
350 * indent-tabs-mode: nil
352 * vim: shiftwidth=4 tabstop=8 expandtab