-/* $Id: d1_absyn.c,v 1.27 2006-06-13 12:02:02 adam Exp $
- Copyright (C) 1995-2006
- Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+ Copyright (C) 1994-2011 Index Data
Zebra is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
for more details.
You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra. If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
*/
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <yaz/log.h>
-#include <yaz/oid.h>
+#include <yaz/oid_db.h>
#include <idzebra/data1.h>
#include <idzebra/recctrl.h>
#include <zebra_xpath.h>
void *data1_hash_lookup(struct data1_hash_table *ht, const char *str)
{
struct data1_hash_entry **he = &ht->ar[data1_hash_calc(ht, str)];
-
+
for (; *he && yaz_matchstr(str, (*he)->str); he = &(*he)->next)
;
if (*he)
struct data1_systag *next;
};
-struct data1_absyn_cache_info
+struct data1_absyn_cache_info
{
char *name;
data1_absyn *absyn;
data1_absyn_cache next;
};
-struct data1_attset_cache_info
+struct data1_attset_cache_info
{
char *name;
data1_attset *attset;
return 0;
}
/* *ostrich*
- We need to destroy DFAs, in xp_element (xelm) definitions
+ We need to destroy DFAs, in xp_element (xelm) definitions
pop, 2002-12-13
*/
void data1_absyn_destroy (data1_handle dh)
{
data1_absyn_cache p = *data1_absyn_cache_get (dh);
-
+
while (p)
{
data1_absyn *abs = p->absyn;
data1_xpelement *xpe = abs->xp_elements;
while (xpe) {
yaz_log (YLOG_DEBUG,"Destroy xp element %s",xpe->xpath_expr);
- if (xpe->dfa) { dfa_delete (&xpe->dfa); }
+ if (xpe->dfa)
+ dfa_delete (&xpe->dfa);
xpe = xpe->next;
- }
+ }
}
p = p->next;
}
return 0;
}
-data1_attset *data1_attset_search_id (data1_handle dh, int id)
+data1_attset *data1_attset_search_id(data1_handle dh, const Odr_oid *oid)
{
data1_attset_cache p = *data1_attset_cache_get (dh);
while (p)
{
- if (id == p->attset->reference)
+ if (p->attset->oid && !oid_oidcmp(oid, p->attset->oid))
return p->attset;
p = p->next;
}
{
NMEM mem = data1_nmem_get (dh);
data1_attset *attset;
-
+
attset = data1_read_attset (dh, name);
if (!attset)
yaz_log (YLOG_WARN|YLOG_ERRNO, "Couldn't load attribute set %s", name);
data1_attset_cache p = (data1_attset_cache)
nmem_malloc (mem, sizeof(*p));
data1_attset_cache *pp = data1_attset_cache_get (dh);
-
+
attset->name = p->name = nmem_strdup(mem, name);
p->attset = attset;
p->next = *pp;
/* we have multiple versions of data1_getelementbyname */
#define DATA1_GETELEMENTBYTAGNAME_VERSION 1
-#if DATA1_GETELEMENTBYTAGNAME_VERSION==0
-/* straight linear search */
-data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
- data1_element *parent,
- const char *tagname)
-{
- data1_element *r;
-
- /* It's now possible to have a data1 tree with no abstract syntax */
- if ( !abs )
- return 0;
-
- if (!parent)
- r = abs->main_elements;
- else
- r = parent->children;
-
- for (; r; r = r->next)
- {
- data1_name *n;
-
- for (n = r->tag->names; n; n = n->next)
- if (!data1_matchstr(tagname, n->name))
- return r;
- }
- return 0;
-}
-#endif
-
-#if DATA1_GETELEMENTBYTAGNAME_VERSION==1
-/* using hash search */
data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
data1_element *parent,
const char *tagname)
else
r = parent->children;
+#if DATA1_GETELEMENTBYTAGNAME_VERSION==1
+ /* using hash search */
if (!r)
return 0;
ht = r->hash;
if (!ht)
{
+ /* build hash table (the first time) */
ht = r->hash = data1_hash_open(29, data1_nmem_get(dh));
for (; r; r = r->next)
{
data1_name *n;
-
+
for (n = r->tag->names; n; n = n->next)
data1_hash_insert(ht, n->name, r, 0);
}
}
return data1_hash_lookup(ht, tagname);
-}
+#else
+ /* using linear search */
+ for (; r; r = r->next)
+ {
+ data1_name *n;
+
+ for (n = r->tag->names; n; n = n->next)
+ if (!data1_matchstr(tagname, n->name))
+ return r;
+ }
+ return 0;
#endif
+}
data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
const char *name)
*/
-static const char * mk_xpath_regexp (data1_handle dh, const char *expr)
+static const char * mk_xpath_regexp (data1_handle dh, const char *expr)
{
const char *p = expr;
int abs = 1;
char *stack[32];
char *res_p, *res = 0;
size_t res_size = 1;
-
+
if (*p != '/')
return ("");
p++;
- if (*p == '/')
- {
+ if (*p == '/')
+ {
abs =0;
p++;
}
is_predicate=1;
else if (*pp == ']')
is_predicate=0;
- else
+ else
{
if (!is_predicate) {
- if (*pp == '*')
+ if (*pp == '*')
*s++ = '.';
*s++ = *pp;
}
}
if (!abs)
{
- sprintf(res_p, ".*");
+ sprintf(res_p, ".*");
res_p += 2;
}
sprintf (res_p, "$");
*tp = (data1_termlist *)
nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
(*tp)->next = 0;
-
+
if (*attname == '!')
{
if (!xpelement && element_name)
source = "data"; /* ok: default is leaf data */
(*tp)->source = (char *)
nmem_strdup (data1_nmem_get (dh), source);
-
+
if (r < 2) /* is the structure qualified? */
(*tp)->structure = "w";
- else
+ else
(*tp)->structure = (char *)
nmem_strdup (data1_nmem_get (dh), structure);
tp = &(*tp)->next;
else
fieldtype = "datafield";
sprintf(buf, "/*/%s[@tag=\"%s\"]", fieldtype, field);
- if (*subfield)
+ if (*subfield)
sprintf(buf + strlen(buf), "/subfield[@code=\"%s\"]", subfield);
else if (field[0] != '0' || field[1] != '0')
strcat(buf, "/subfield");
char *p;
int argc;
int quoted = 0;
-
+
while ((p = fgets(line, len, f)))
{
(*lineno)++;
}
if (!p)
return 0;
-
+
for (argc = 0; *p ; argc++)
{
if (*p == '#') /* trailing comment */
return argc;
}
-data1_marctab *data1_absyn_getmarctab(data1_handle dh, data1_absyn *absyn)
+data1_marctab *data1_absyn_getmarctab(data1_handle dh, data1_node *root)
{
- return absyn->marc;
+ if (root->u.root.absyn)
+ return root->u.root.absyn->marc;
+ return 0;
}
-YAZ_EXPORT data1_element *data1_absyn_getelements(data1_handle dh,
- data1_absyn *absyn)
+data1_element *data1_absyn_getelements(data1_handle dh,
+ data1_node *root)
{
- return absyn->main_elements;
+ if (root->u.root.absyn)
+ return root->u.root.absyn->main_elements;
+ return 0;
}
static data1_absyn *data1_read_absyn(data1_handle dh, const char *file,
enum DATA1_XPATH_INDEXING default_xpath)
{
data1_sub_elements *cur_elements = NULL;
- data1_xpelement *cur_xpelement = NULL;
+ data1_xpelement **cur_xpelement = NULL;
data1_attset *attset_list = data1_empty_attset(dh);
data1_attset_child **attset_childp = &attset_list->children;
char *argv[50], line[512];
f = data1_path_fopen(dh, file, "r");
-
+
res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
res->name = 0;
- res->reference = VAL_NONE;
+ res->oid = 0;
res->tagset = 0;
res->encoding = 0;
- res->xpath_indexing =
+ res->xpath_indexing =
(f ? DATA1_XPATH_INDEXING_DISABLE : default_xpath);
res->systags = 0;
systagsp = &res->systags;
res->sub_elements = NULL;
res->main_elements = NULL;
res->xp_elements = NULL;
+ cur_xpelement = &res->xp_elements;
while (f && (argc = read_absyn_line(f, &lineno, line, 512, argv, 50)))
{
cur_elements->elements = NULL;
cur_elements->name = "main";
res->sub_elements = cur_elements;
-
+
level = 0;
ppl[level] = &cur_elements->elements;
}
}
level = i;
new_element = *ppl[level-1] = data1_mk_element(dh);
-
+
tp = &new_element->termlists;
ppl[level-1] = &new_element->next;
ppl[level] = &new_element->children;
-
+
/* consider subtree (if any) ... */
if ((sub_p = strchr (p, ':')) && sub_p[1])
{
*sub_p++ = '\0';
new_element->sub_name =
- nmem_strdup (data1_nmem_get(dh), sub_p);
+ nmem_strdup (data1_nmem_get(dh), sub_p);
}
/* well-defined tag */
if (sscanf(p, "(%d,%d)", &type, &value) == 2)
nt->which = DATA1T_string;
nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
nt->names = (data1_name *)
- nmem_malloc(data1_nmem_get(dh),
+ nmem_malloc(data1_nmem_get(dh),
sizeof(*new_element->tag->names));
nt->names->name = nt->value.string;
nt->names->next = 0;
/* *ostrich*
New code to support xelm directive
for each xelm a dfa is built. xelms are stored in res->xp_elements
-
+
maybe we should use a simple sscanf instead of dfa?
-
+
pop, 2002-12-13
Now [] predicates are supported. regexps and xpath structure is
int i;
char *p, *xpath_expr, *termlists;
const char *regexp;
- struct DFA *dfa = dfa = dfa_init();
+ struct DFA *dfa = 0;
data1_termlist **tp;
char melm_xpath[128];
-
- if (argc < 3)
+ data1_xpelement *xp_ele = 0;
+ data1_xpelement *last_match = 0;
+
+ if (argc != 3)
{
- yaz_log(YLOG_WARN, "%s:%d: Bad # of args to xelm", file, lineno);
+ yaz_log(YLOG_WARN, "%s:%d: Bad # of args to %s",
+ file, lineno, cmd);
continue;
}
}
termlists = argv[2];
regexp = mk_xpath_regexp(dh, xpath_expr);
- i = dfa_parse (dfa, ®exp);
- if (i || *regexp) {
- yaz_log(YLOG_WARN, "%s:%d: Bad xpath to xelm", file, lineno);
- dfa_delete (&dfa);
- continue;
- }
-
- if (!cur_xpelement)
- {
- cur_xpelement = (data1_xpelement *)
- nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
- res->xp_elements = cur_xpelement;
- } else {
- cur_xpelement->next = (data1_xpelement *)
- nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
- cur_xpelement = cur_xpelement->next;
- }
- cur_xpelement->next = NULL;
- cur_xpelement->xpath_expr = nmem_strdup(data1_nmem_get (dh),
- xpath_expr);
-
- dfa_mkstate (dfa);
- cur_xpelement->dfa = dfa;
-
-#ifdef ENHANCED_XELM
- cur_xpelement->xpath_len =
- zebra_parse_xpath_str(xpath_expr,
- cur_xpelement->xpath, XPATH_STEP_COUNT,
- data1_nmem_get(dh));
-
- /*
- dump_xp_steps(cur_xpelement->xpath,cur_xpelement->xpath_len);
- */
+
+#if OPTIMIZE_MELM
+ /* get last of existing regulars with same regexp */
+ for (xp_ele = res->xp_elements; xp_ele; xp_ele = xp_ele->next)
+ if (!strcmp(xp_ele->regexp, regexp))
+ last_match = xp_ele;
#endif
- cur_xpelement->termlists = 0;
- tp = &cur_xpelement->termlists;
-
+ if (!last_match)
+ {
+ /* new regular expression . Parse + generate */
+ const char *regexp_ptr = regexp;
+
+ dfa = dfa_init();
+ i = dfa_parse (dfa, ®exp_ptr);
+ if (i || *regexp_ptr) {
+ yaz_log(YLOG_WARN, "%s:%d: Bad xpath to xelm", file, lineno);
+ dfa_delete (&dfa);
+ continue;
+ }
+ }
+ *cur_xpelement = (data1_xpelement *)
+ nmem_malloc(data1_nmem_get(dh), sizeof(**cur_xpelement));
+ (*cur_xpelement)->next = 0;
+ (*cur_xpelement)->match_next = 0;
+ if (last_match)
+ last_match->match_next = *cur_xpelement;
+#if OPTIMIZE_MELM
+ (*cur_xpelement)->regexp = regexp;
+#endif
+ (*cur_xpelement)->xpath_expr = nmem_strdup(data1_nmem_get (dh),
+ xpath_expr);
+
+ if (dfa)
+ dfa_mkstate (dfa);
+ (*cur_xpelement)->dfa = dfa;
+
+#ifdef ENHANCED_XELM
+ (*cur_xpelement)->xpath_len =
+ zebra_parse_xpath_str(
+ xpath_expr,
+ (*cur_xpelement)->xpath, XPATH_STEP_COUNT,
+ data1_nmem_get(dh));
+#endif
+ (*cur_xpelement)->termlists = 0;
+ tp = &(*cur_xpelement)->termlists;
+
/* parse termList definitions */
p = termlists;
if (*p != '-')
}
*tp = all; /* append any ALL entries to the list */
}
+ cur_xpelement = &(*cur_xpelement)->next;
}
else if (!strcmp(cmd, "section"))
{
char *name;
-
+
if (argc < 2)
{
yaz_log(YLOG_WARN, "%s:%d: Bad # of args to section",
continue;
}
name = argv[1];
-
+
cur_elements = (data1_sub_elements *)
nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
cur_elements->next = res->sub_elements;
cur_elements->elements = NULL;
cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
res->sub_elements = cur_elements;
-
+
level = 0;
ppl[level] = &cur_elements->elements;
}
else if (!strcmp(cmd, "reference"))
{
char *name;
-
+
if (argc != 2)
{
yaz_log(YLOG_WARN, "%s:%d: Bad # of args to reference",
continue;
}
name = argv[1];
- if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
+ res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
+ CLASS_SCHEMA, name,
+ data1_nmem_get(dh));
+ if (!res->oid)
{
- yaz_log(YLOG_WARN, "%s:%d: Unknown tagset ref '%s'",
+ yaz_log(YLOG_WARN, "%s:%d: Unknown tagset ref '%s'",
file, lineno, name);
continue;
}
{
char *name;
data1_attset *attset;
-
+
if (argc != 2)
{
yaz_log(YLOG_WARN, "%s:%d: Bad # of args to attset",
}
name = argv[1];
fname = argv[2];
-
+
*esetpp = (data1_esetname *)
nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
(*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
else if (!strcmp(cmd, "maptab"))
{
char *name;
-
+
if (argc != 2)
{
yaz_log(YLOG_WARN, "%s:%d: Bad # of args for maptab",
else if (!strcmp(cmd, "marc"))
{
char *name;
-
+
if (argc != 2)
{
yaz_log(YLOG_WARN, "%s:%d: Bad # or args for marc",
}
else
{
- yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file,
+ yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file,
lineno, cmd);
continue;
}
}
if (f)
fclose(f);
-
+
for (cur_elements = res->sub_elements; cur_elements;
cur_elements = cur_elements->next)
{
*systagsp = 0;
return res;
}
+
/*
* Local variables:
* c-basic-offset: 4
+ * c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab