+ if (sp->lookahead != 't')
+ return 0;
+ if (sp->len == 4 && !memcmp(sp->tok, "data", sp->len))
+ {
+ if (n->which == DATA1N_data)
+ {
+ wrd->term_buf = n->u.data.data;
+ wrd->term_len = n->u.data.len;
+ }
+ sp_lex(sp);
+ }
+ else if (sp->len == 3 && !memcmp(sp->tok, "tag", sp->len))
+ {
+ if (n->which == DATA1N_tag)
+ {
+ wrd->term_buf = n->u.tag.tag;
+ wrd->term_len = strlen(n->u.tag.tag);
+ }
+ sp_lex(sp);
+ }
+ else if (sp->len == 4 && !memcmp(sp->tok, "attr", sp->len))
+ {
+ RecWord tmp_w;
+ sp_lex(sp);
+ if (sp->lookahead != '(')
+ return 0;
+ sp_lex(sp);
+
+ if (!sp_expr(sp, n, &tmp_w))
+ return 0;
+
+ wrd->term_buf = "";
+ wrd->term_len = 0;
+ if (n->which == DATA1N_tag)
+ {
+ data1_xattr *p = n->u.tag.attributes;
+ while (p && strlen(p->name) != tmp_w.term_len &&
+ memcmp (p->name, tmp_w.term_buf, tmp_w.term_len))
+ p = p->next;
+ if (p)
+ {
+ wrd->term_buf = p->value;
+ wrd->term_len = strlen(p->value);
+ }
+ }
+ if (sp->lookahead != ')')
+ return 0;
+ sp_lex(sp);
+ }
+ else if (sp->len == 5 && !memcmp(sp->tok, "first", sp->len))
+ {
+ return sp_first(sp, n, wrd);
+ }
+ else if (sp->len == 5 && !memcmp(sp->tok, "range", sp->len))
+ {
+ return sp_range(sp, n, wrd);
+ }
+ else if (sp->len > 0 && isdigit(*(unsigned char *)sp->tok))
+ {
+ wrd->term_buf = nmem_malloc(sp->nmem, sp->len);
+ memcpy(wrd->term_buf, sp->tok, sp->len);
+ wrd->term_len = sp->len;
+ sp_lex(sp);
+ }
+ else if (sp->len > 2 && sp->tok[0] == '\'' && sp->tok[sp->len-1] == '\'')
+ {
+ wrd->term_len = sp->len - 2;
+ wrd->term_buf = nmem_malloc(sp->nmem, wrd->term_len);
+ memcpy(wrd->term_buf, sp->tok+1, wrd->term_len);
+ sp_lex(sp);
+ }
+ else
+ {
+ wrd->term_buf = "";
+ wrd->term_len = 0;
+ sp_lex(sp);
+ }
+ return 1;
+}
+
+static struct source_parser *source_parser_create()
+{
+ struct source_parser *sp = xmalloc(sizeof(*sp));
+
+ sp->nmem = nmem_create();
+ return sp;
+}
+
+static void source_parser_destroy(struct source_parser *sp)
+{
+ if (!sp)
+ return;
+ nmem_destroy(sp->nmem);
+ xfree(sp);
+}
+
+static int sp_parse(struct source_parser *sp,
+ data1_node *n, RecWord *wrd, const char *src)
+{
+ sp->len = 0;
+ sp->tok = 0;
+ sp->src = src;
+ sp->lookahead = 0;
+ nmem_reset(sp->nmem);
+
+ sp_lex(sp);
+ return sp_expr(sp, n, wrd);
+}
+
+int d1_check_xpath_predicate(data1_node *n, struct xpath_predicate *p)
+{
+ int res = 1;
+ char *attname;
+ data1_xattr *attr;
+
+ if (!p) {
+ return 1;
+ } else {
+ if (p->which == XPATH_PREDICATE_RELATION) {
+ if (p->u.relation.name[0]) {
+ if (*p->u.relation.name != '@') {
+ yaz_log(YLOG_WARN,
+ " Only attributes (@) are supported in xelm xpath predicates");
+ yaz_log(YLOG_WARN, "predicate %s ignored", p->u.relation.name);
+ return 1;
+ }
+ attname = p->u.relation.name + 1;
+ res = 0;
+ /* looking for the attribute with a specified name */
+ for (attr = n->u.tag.attributes; attr; attr = attr->next) {
+ yaz_log(YLOG_DEBUG," - attribute %s <-> %s", attname, attr->name );
+
+ if (!strcmp(attr->name, attname)) {
+ if (p->u.relation.op[0]) {
+ if (*p->u.relation.op != '=') {
+ yaz_log(YLOG_WARN,
+ "Only '=' relation is supported (%s)",p->u.relation.op);
+ yaz_log(YLOG_WARN, "predicate %s ignored", p->u.relation.name);
+ res = 1; break;
+ } else {
+ yaz_log(YLOG_DEBUG," - value %s <-> %s",
+ p->u.relation.value, attr->value );
+ if (!strcmp(attr->value, p->u.relation.value)) {
+ res = 1; break;
+ }
+ }
+ } else {
+ /* attribute exists, no value specified */
+ res = 1; break;
+ }
+ }
+ }
+ yaz_log(YLOG_DEBUG, "return %d", res);
+ return res;
+ } else {
+ return 1;
+ }
+ }
+ else if (p->which == XPATH_PREDICATE_BOOLEAN) {
+ if (!strcmp(p->u.boolean.op,"and")) {
+ return d1_check_xpath_predicate(n, p->u.boolean.left)
+ && d1_check_xpath_predicate(n, p->u.boolean.right);
+ }
+ else if (!strcmp(p->u.boolean.op,"or")) {
+ return (d1_check_xpath_predicate(n, p->u.boolean.left)
+ || d1_check_xpath_predicate(n, p->u.boolean.right));
+ } else {
+ yaz_log(YLOG_WARN, "Unknown boolean relation %s, ignored",p->u.boolean.op);
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+
+/* *ostrich*
+
+New function, looking for xpath "element" definitions in abs, by
+tagpath, using a kind of ugly regxp search.The DFA was built while
+parsing abs, so here we just go trough them and try to match
+against the given tagpath. The first matching entry is returned.