1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 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>
37 char *source_element_name;
38 data1_maptag *target_path;
39 struct data1_mapunit *next;
42 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
44 NMEM mem = data1_nmem_get (dh);
45 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
49 char *argv[50], line[512];
51 int local_numeric = 0;
53 if (!(f = data1_path_fopen(dh, file, "r")))
62 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
63 if (!strcmp(argv[0], "targetref"))
67 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetref",
71 res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
72 CLASS_RECSYN, argv[1], mem);
75 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
76 file, lineno, argv[1]);
80 else if (!strcmp(argv[0], "targetname"))
84 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetname",
88 res->target_absyn_name =
89 (char *)nmem_malloc(mem, strlen(argv[1])+1);
90 strcpy(res->target_absyn_name, argv[1]);
92 else if (!yaz_matchstr(argv[0], "localnumeric"))
94 else if (!strcmp(argv[0], "name"))
98 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name", file, lineno);
101 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
102 strcpy(res->name, argv[1]);
104 else if (!strcmp(argv[0], "map"))
107 char *ep, *path = argv[2];
111 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for map",
115 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
117 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
118 (*mapp)->no_data = 1;
120 (*mapp)->no_data = 0;
121 if (argc > 3 && !data1_matchstr(argv[3], "nochop"))
122 (*mapp)->no_chop = 1;
124 (*mapp)->no_chop = 0;
125 (*mapp)->source_element_name =
126 (char *)nmem_malloc(mem, strlen(argv[1])+1);
127 strcpy((*mapp)->source_element_name, argv[1]);
128 mtp = &(*mapp)->target_path;
131 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
132 (ep = strchr(path, '/'))))
135 char valstr[512], parm[512];
139 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
142 yaz_log(YLOG_WARN, "%s:%d: Syntax error in map "
143 "directive: %s", file, lineno, argv[2]);
147 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
150 if (np > 2 && !data1_matchstr(parm, "new"))
151 (*mtp)->new_field = 1;
153 (*mtp)->new_field = 0;
154 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
156 (*mtp)->which = D1_MAPTAG_numeric;
157 (*mtp)->value.numeric = atoi(valstr);
161 (*mtp)->which = D1_MAPTAG_string;
162 (*mtp)->value.string =
163 (char *)nmem_malloc(mem, strlen(valstr)+1);
164 strcpy((*mtp)->value.string, valstr);
168 mapp = &(*mapp)->next;
171 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
172 file, lineno, argv[0]);
179 * See if the node n is equivalent to the tag t.
181 static int tagmatch(data1_node *n, data1_maptag *t)
183 if (n->which != DATA1N_tag)
185 if (n->u.tag.element)
187 if (n->u.tag.element->tag->tagset)
189 if (n->u.tag.element->tag->tagset->type != t->type)
192 else if (t->type != 3)
194 if (n->u.tag.element->tag->which == DATA1T_numeric)
196 if (t->which != D1_MAPTAG_numeric)
198 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
203 if (t->which != D1_MAPTAG_string)
205 if (data1_matchstr(n->u.tag.element->tag->value.string,
216 if (t->which == D1_MAPTAG_numeric)
217 sprintf(str, "%d", t->value.numeric);
219 strcpy(str, t->value.string);
220 if (data1_matchstr(n->u.tag.tag, str))
226 static data1_node *dup_child (data1_handle dh, data1_node *n,
227 data1_node **last, NMEM mem,
230 data1_node *first = 0;
231 data1_node **m = &first;
233 for (; n; n = n->next)
235 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
236 memcpy (*m, n, sizeof(**m));
238 (*m)->parent = parent;
239 (*m)->root = parent->root;
240 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
247 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
248 data1_node *res, NMEM mem)
253 * locate each source element in turn.
255 for (c = n->child; c; c = c->next)
256 if (c->which == DATA1N_tag && c->u.tag.element)
258 for (m = map->map; m; m = m->next)
260 if (!data1_matchstr(m->source_element_name,
261 c->u.tag.element->name))
263 data1_node *pn = res;
264 data1_node *cur = pn->last_child;
268 * process the target path specification.
270 for (mt = m->target_path; mt; mt = mt->next)
272 if (!cur || mt->new_field || !tagmatch(cur, mt))
274 if (mt->which == D1_MAPTAG_string)
276 cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
277 cur->u.tag.tag = mt->value.string;
279 else if (mt->which == D1_MAPTAG_numeric)
284 pn->root->u.root.absyn->tagset,
288 if (tag && tag->names->name)
291 dh, mem, tag->names->name, 0, pn);
299 else if (!m->no_data)
302 dup_child (dh, c->child,
303 &cur->last_child, mem, cur);
306 data1_concat_text(dh, mem, cur->child);
307 data1_chop_text(dh, mem, cur->child);
313 if (map_children(dh, c, map, res, mem) < 0)
320 * Create a (possibly lossy) copy of the given record based on the
321 * table. The new copy will refer back to the data of the original record,
322 * which should not be discarded during the lifetime of the copy.
324 data1_node *data1_map_record (data1_handle dh, data1_node *n,
325 data1_maptab *map, NMEM m)
327 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
329 res->which = DATA1N_root;
330 res->u.root.type = map->target_absyn_name;
331 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name,
332 DATA1_XPATH_INDEXING_ENABLE)))
334 yaz_log(YLOG_WARN, "%s: Failed to load target absyn '%s'",
335 map->name, map->target_absyn_name);
340 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
341 while (n && n->which != DATA1N_tag)
343 if (map_children(dh, n, map, res1, m) < 0)
351 * c-file-style: "Stroustrup"
352 * indent-tabs-mode: nil
354 * vim: shiftwidth=4 tabstop=8 expandtab