1 /* $Id: index_rules.c,v 1.2 2007-10-24 13:55:55 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 <yaz/match_glob.h>
30 #include <yaz/xmalloc.h>
31 #include <yaz/wrbuf.h>
34 struct zebra_index_rules_s {
36 struct zebra_index_rule *rules;
42 struct zebra_index_rule {
47 const char *alwaysmatches;
48 const char *firstinfield;
50 struct zebra_index_rule *next;
53 struct zebra_index_rule *parse_index_rule(const xmlNode *ptr)
55 struct _xmlAttr *attr;
56 struct zebra_index_rule *rule;
58 rule = xmalloc(sizeof(*rule));
64 rule->alwaysmatches = 0;
65 rule->firstinfield = 0;
67 for (attr = ptr->properties; attr; attr = attr->next)
69 if (attr->children && attr->children->type == XML_TEXT_NODE)
71 if (!strcmp((const char *) attr->name, "id"))
72 rule->id = (const char *) attr->children->content;
73 else if (!strcmp((const char *) attr->name, "locale"))
74 rule->locale = (const char *) attr->children->content;
75 else if (!strcmp((const char *) attr->name, "position"))
76 rule->position = (const char *) attr->children->content;
77 else if (!strcmp((const char *) attr->name, "alwaysmatches"))
78 rule->alwaysmatches = (const char *) attr->children->content;
79 else if (!strcmp((const char *) attr->name, "firstinfield"))
80 rule->firstinfield = (const char *) attr->children->content;
81 else if (!strcmp((const char *) attr->name, "sort"))
82 rule->sort = (const char *) attr->children->content;
85 yaz_log(YLOG_WARN, "Unsupport attribute '%s' for indexrule",
97 zebra_index_rules_t zebra_index_rules_create(const char *fname)
99 xmlDocPtr doc = xmlParseFile(fname);
102 return zebra_index_rules_create_doc(doc);
105 zebra_index_rules_t zebra_index_rules_create_doc(xmlDocPtr doc)
108 zebra_index_rules_t r = xmalloc(sizeof(*r));
109 struct zebra_index_rule **rp = &r->rules;
110 const xmlNode *top = xmlDocGetRootElement(doc);
114 if (top && top->type == XML_ELEMENT_NODE
115 && !strcmp((const char *) top->name, "indexrules"))
117 const xmlNode *ptr = top->children;
118 for (; ptr; ptr = ptr->next)
120 if (ptr->type == XML_ELEMENT_NODE
121 && !strcmp((const char *) ptr->name, "indexrule"))
123 *rp = parse_index_rule(ptr);
126 zebra_index_rules_destroy(r);
135 zebra_index_rules_destroy(r);
140 yaz_log(YLOG_WARN, "Cannot read index rules %s because YAZ is without XML "
147 void zebra_index_rules_destroy(zebra_index_rules_t r)
150 struct zebra_index_rule *rule;
154 r->rules = rule->next;
163 const char *zebra_index_rule_lookup_str(zebra_index_rules_t r, const char *id)
167 struct zebra_index_rule *rule = r->rules;
169 while (rule && !yaz_match_glob(rule->id, id))
180 * indent-tabs-mode: nil
182 * vim: shiftwidth=4 tabstop=8 expandtab