First parts of index_rules system (ICU support).
[idzebra-moved-to-github.git] / util / index_rules.c
1 /* $Id: index_rules.c,v 1.1 2007-10-23 12:26:26 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5    This file is part of the Zebra server.
6
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
10    version.
11
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
15    for more details.
16
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
20    02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <string.h>
27
28 #include "index_rules.h"
29 #include "rob_regexp.h"
30 #include <yaz/xmalloc.h>
31 #include <yaz/wrbuf.h>
32 #include <yaz/log.h>
33
34 struct zebra_index_rules_s {
35     WRBUF last_id;
36 #if YAZ_HAVE_XML2
37     struct zebra_index_rule *rules;
38     struct zebra_index_rule *last_rule_match;
39     xmlDocPtr doc;
40 #endif
41 };
42
43 #if YAZ_HAVE_XML2
44 struct zebra_index_rule {
45     const xmlNode *ptr;
46     const char *id;
47     const char *locale;
48     const char *position;
49     const char *alwaysmatches;
50     const char *firstinfield;
51     const char *sort;
52     struct zebra_index_rule *next;
53 };
54
55 struct zebra_index_rule *parse_index_rule(const xmlNode *ptr)
56 {
57     struct _xmlAttr *attr;
58     struct zebra_index_rule *rule;
59     
60     rule = xmalloc(sizeof(*rule)); 
61     rule->next = 0;
62     rule->ptr = ptr;
63     rule->locale = 0;
64     rule->id = 0;
65     rule->position = 0;
66     rule->alwaysmatches = 0;
67     rule->firstinfield = 0;
68     rule->sort = 0;
69     for (attr = ptr->properties; attr; attr = attr->next)
70     {
71         if (attr->children && attr->children->type == XML_TEXT_NODE)
72         {
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;
85             else
86             {
87                 yaz_log(YLOG_WARN, "Unsupport attribute '%s' for indexrule",
88                         attr->name);
89                 xfree(rule);
90                 return 0;
91             }
92         }
93     }
94     return rule;
95 }
96 /* YAZ_HAVE_XML2 */
97 #endif
98
99 zebra_index_rules_t zebra_index_rules_create(const char *fname)
100 {
101     xmlDocPtr doc = xmlParseFile(fname);
102     if (!doc)
103         return 0;
104     return zebra_index_rules_create_doc(doc);
105 }
106
107 zebra_index_rules_t zebra_index_rules_create_doc(xmlDocPtr doc)
108 {
109 #if YAZ_HAVE_XML2
110     zebra_index_rules_t r = xmalloc(sizeof(*r));
111     struct zebra_index_rule **rp = &r->rules;
112     const xmlNode *top = xmlDocGetRootElement(doc);
113     
114     r->doc = doc;
115     r->last_rule_match = 0;
116     r->last_id = wrbuf_alloc();
117     *rp = 0;
118     if (top && top->type == XML_ELEMENT_NODE
119         && !strcmp((const char *) top->name, "indexrules"))
120     {
121         const xmlNode *ptr = top->children;
122         for (; ptr; ptr = ptr->next)
123         {
124             if (ptr->type == XML_ELEMENT_NODE
125                 && !strcmp((const char *) ptr->name, "indexrule"))
126             {
127                 *rp = parse_index_rule(ptr);
128                 if (!*rp)
129                 {
130                     zebra_index_rules_destroy(r);
131                     return 0;
132                 }
133                 rp = &(*rp)->next;
134             }
135         }
136     }
137     else
138     {
139         zebra_index_rules_destroy(r);
140         r = 0;
141     }
142     return r;
143 #else
144     yaz_log(YLOG_WARN, "Cannot read index rules %s because YAZ is without XML "
145             "support", fname);
146     return 0;
147 /* YAZ_HAVE_XML2 */
148 #endif
149 }
150
151 void zebra_index_rules_destroy(zebra_index_rules_t r)
152 {
153 #if YAZ_HAVE_XML2
154     struct zebra_index_rule *rule;
155     while (r->rules)
156     {
157         rule = r->rules;
158         r->rules = rule->next;
159         xfree(rule);
160     }
161     xmlFreeDoc(r->doc);
162
163 #endif
164     wrbuf_destroy(r->last_id);
165     xfree(r);
166 }
167
168 const char *zebra_index_rule_lookup_str(zebra_index_rules_t r, const char *id)
169 {
170 #if YAZ_HAVE_XML2
171     if (r->last_rule_match && !strcmp(wrbuf_cstr(r->last_id), id))
172         return r->last_rule_match->id;
173     else
174     {
175         struct zebra_index_rule *rule = r->rules;
176         
177         wrbuf_rewind(r->last_id);
178         wrbuf_puts(r->last_id, id);
179         while (rule && !zebra_rob_regexp(rule->id, id))
180             rule = rule->next;
181         r->last_rule_match = rule;
182         if (rule)
183             return rule->id;
184     }
185 #endif
186     return 0;
187 }
188
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * indent-tabs-mode: nil
193  * End:
194  * vim: shiftwidth=4 tabstop=8 expandtab
195  */
196