1 /* $Id: index_rules.c,v 1.1 2007-10-23 12:26:26 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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include "index_rules.h"
29 #include "rob_regexp.h"
30 #include <yaz/xmalloc.h>
31 #include <yaz/wrbuf.h>
34 struct zebra_index_rules_s {
37 struct zebra_index_rule *rules;
38 struct zebra_index_rule *last_rule_match;
44 struct zebra_index_rule {
49 const char *alwaysmatches;
50 const char *firstinfield;
52 struct zebra_index_rule *next;
55 struct zebra_index_rule *parse_index_rule(const xmlNode *ptr)
57 struct _xmlAttr *attr;
58 struct zebra_index_rule *rule;
60 rule = xmalloc(sizeof(*rule));
66 rule->alwaysmatches = 0;
67 rule->firstinfield = 0;
69 for (attr = ptr->properties; attr; attr = attr->next)
71 if (attr->children && attr->children->type == XML_TEXT_NODE)
73 if (!strcmp((const char *) attr->name, "id"))
74 rule->id = (const char *) attr->children->content;
75 else if (!strcmp((const char *) attr->name, "locale"))
76 rule->locale = (const char *) attr->children->content;
77 else if (!strcmp((const char *) attr->name, "position"))
78 rule->position = (const char *) attr->children->content;
79 else if (!strcmp((const char *) attr->name, "alwaysmatches"))
80 rule->alwaysmatches = (const char *) attr->children->content;
81 else if (!strcmp((const char *) attr->name, "firstinfield"))
82 rule->firstinfield = (const char *) attr->children->content;
83 else if (!strcmp((const char *) attr->name, "sort"))
84 rule->sort = (const char *) attr->children->content;
87 yaz_log(YLOG_WARN, "Unsupport attribute '%s' for indexrule",
99 zebra_index_rules_t zebra_index_rules_create(const char *fname)
101 xmlDocPtr doc = xmlParseFile(fname);
104 return zebra_index_rules_create_doc(doc);
107 zebra_index_rules_t zebra_index_rules_create_doc(xmlDocPtr doc)
110 zebra_index_rules_t r = xmalloc(sizeof(*r));
111 struct zebra_index_rule **rp = &r->rules;
112 const xmlNode *top = xmlDocGetRootElement(doc);
115 r->last_rule_match = 0;
116 r->last_id = wrbuf_alloc();
118 if (top && top->type == XML_ELEMENT_NODE
119 && !strcmp((const char *) top->name, "indexrules"))
121 const xmlNode *ptr = top->children;
122 for (; ptr; ptr = ptr->next)
124 if (ptr->type == XML_ELEMENT_NODE
125 && !strcmp((const char *) ptr->name, "indexrule"))
127 *rp = parse_index_rule(ptr);
130 zebra_index_rules_destroy(r);
139 zebra_index_rules_destroy(r);
144 yaz_log(YLOG_WARN, "Cannot read index rules %s because YAZ is without XML "
151 void zebra_index_rules_destroy(zebra_index_rules_t r)
154 struct zebra_index_rule *rule;
158 r->rules = rule->next;
164 wrbuf_destroy(r->last_id);
168 const char *zebra_index_rule_lookup_str(zebra_index_rules_t r, const char *id)
171 if (r->last_rule_match && !strcmp(wrbuf_cstr(r->last_id), id))
172 return r->last_rule_match->id;
175 struct zebra_index_rule *rule = r->rules;
177 wrbuf_rewind(r->last_id);
178 wrbuf_puts(r->last_id, id);
179 while (rule && !zebra_rob_regexp(rule->id, id))
181 r->last_rule_match = rule;
192 * indent-tabs-mode: nil
194 * vim: shiftwidth=4 tabstop=8 expandtab