1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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
25 #include <yaz/oid_db.h>
26 #include <yaz/readconf.h>
27 #include <yaz/tpath.h>
34 char *source_element_name;
35 data1_maptag *target_path;
36 struct data1_mapunit *next;
39 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
41 NMEM mem = data1_nmem_get (dh);
42 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
46 char *argv[50], line[512];
48 int local_numeric = 0;
50 if (!(f = data1_path_fopen(dh, file, "r")))
59 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
60 if (!strcmp(argv[0], "targetref"))
64 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetref",
68 res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
69 CLASS_RECSYN, argv[1], mem);
72 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
73 file, lineno, argv[1]);
77 else if (!strcmp(argv[0], "targetname"))
81 yaz_log(YLOG_WARN, "%s:%d: Bad # args for targetname",
85 res->target_absyn_name =
86 (char *)nmem_malloc(mem, strlen(argv[1])+1);
87 strcpy(res->target_absyn_name, argv[1]);
89 else if (!yaz_matchstr(argv[0], "localnumeric"))
91 else if (!strcmp(argv[0], "name"))
95 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name", file, lineno);
98 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
99 strcpy(res->name, argv[1]);
101 else if (!strcmp(argv[0], "map"))
104 char *ep, *path = argv[2];
108 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for map",
112 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
114 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
115 (*mapp)->no_data = 1;
117 (*mapp)->no_data = 0;
118 if (argc > 3 && !data1_matchstr(argv[3], "nochop"))
119 (*mapp)->no_chop = 1;
121 (*mapp)->no_chop = 0;
122 (*mapp)->source_element_name =
123 (char *)nmem_malloc(mem, strlen(argv[1])+1);
124 strcpy((*mapp)->source_element_name, argv[1]);
125 mtp = &(*mapp)->target_path;
128 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
129 (ep = strchr(path, '/'))))
132 char valstr[512], parm[512];
136 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
139 yaz_log(YLOG_WARN, "%s:%d: Syntax error in map "
140 "directive: %s", file, lineno, argv[2]);
144 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
147 if (np > 2 && !data1_matchstr(parm, "new"))
148 (*mtp)->new_field = 1;
150 (*mtp)->new_field = 0;
151 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
153 (*mtp)->which = D1_MAPTAG_numeric;
154 (*mtp)->value.numeric = atoi(valstr);
158 (*mtp)->which = D1_MAPTAG_string;
159 (*mtp)->value.string =
160 (char *)nmem_malloc(mem, strlen(valstr)+1);
161 strcpy((*mtp)->value.string, valstr);
165 mapp = &(*mapp)->next;
168 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
169 file, lineno, argv[0]);
176 * See if the node n is equivalent to the tag t.
178 static int tagmatch(data1_node *n, data1_maptag *t)
180 if (n->which != DATA1N_tag)
182 if (n->u.tag.element)
184 if (n->u.tag.element->tag->tagset)
186 if (n->u.tag.element->tag->tagset->type != t->type)
189 else if (t->type != 3)
191 if (n->u.tag.element->tag->which == DATA1T_numeric)
193 if (t->which != D1_MAPTAG_numeric)
195 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
200 if (t->which != D1_MAPTAG_string)
202 if (data1_matchstr(n->u.tag.element->tag->value.string,
213 if (t->which == D1_MAPTAG_numeric)
214 sprintf(str, "%d", t->value.numeric);
216 strcpy(str, t->value.string);
217 if (data1_matchstr(n->u.tag.tag, str))
223 static data1_node *dup_child (data1_handle dh, data1_node *n,
224 data1_node **last, NMEM mem,
227 data1_node *first = 0;
228 data1_node **m = &first;
230 for (; n; n = n->next)
232 *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
233 memcpy (*m, n, sizeof(**m));
235 (*m)->parent = parent;
236 (*m)->root = parent->root;
237 (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
244 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
245 data1_node *res, NMEM mem)
250 * locate each source element in turn.
252 for (c = n->child; c; c = c->next)
253 if (c->which == DATA1N_tag && c->u.tag.element)
255 for (m = map->map; m; m = m->next)
257 if (!data1_matchstr(m->source_element_name,
258 c->u.tag.element->name))
260 data1_node *pn = res;
261 data1_node *cur = pn->last_child;
265 * process the target path specification.
267 for (mt = m->target_path; mt; mt = mt->next)
269 if (!cur || mt->new_field || !tagmatch(cur, mt))
271 if (mt->which == D1_MAPTAG_string)
273 cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
274 cur->u.tag.tag = mt->value.string;
276 else if (mt->which == D1_MAPTAG_numeric)
281 pn->root->u.root.absyn->tagset,
285 if (tag && tag->names->name)
288 dh, mem, tag->names->name, 0, pn);
296 else if (!m->no_data)
299 dup_child (dh, c->child,
300 &cur->last_child, mem, cur);
303 data1_concat_text(dh, mem, cur->child);
304 data1_chop_text(dh, mem, cur->child);
310 if (map_children(dh, c, map, res, mem) < 0)
317 * Create a (possibly lossy) copy of the given record based on the
318 * table. The new copy will refer back to the data of the original record,
319 * which should not be discarded during the lifetime of the copy.
321 data1_node *data1_map_record (data1_handle dh, data1_node *n,
322 data1_maptab *map, NMEM m)
324 data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
326 res->which = DATA1N_root;
327 res->u.root.type = map->target_absyn_name;
328 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name,
329 DATA1_XPATH_INDEXING_ENABLE)))
331 yaz_log(YLOG_WARN, "%s: Failed to load target absyn '%s'",
332 map->name, map->target_absyn_name);
337 res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
338 while (n && n->which != DATA1N_tag)
340 if (map_children(dh, n, map, res1, m) < 0)
348 * c-file-style: "Stroustrup"
349 * indent-tabs-mode: nil
351 * vim: shiftwidth=4 tabstop=8 expandtab