Do not write zebrasrv.pid in daemon mode
[idzebra-moved-to-github.git] / util / index_rules.c
1 /* $Id: index_rules.c,v 1.2 2007-10-24 13:55:55 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 <yaz/match_glob.h>
30 #include <yaz/xmalloc.h>
31 #include <yaz/wrbuf.h>
32 #include <yaz/log.h>
33
34 struct zebra_index_rules_s {
35 #if YAZ_HAVE_XML2
36     struct zebra_index_rule *rules;
37     xmlDocPtr doc;
38 #endif
39 };
40
41 #if YAZ_HAVE_XML2
42 struct zebra_index_rule {
43     const xmlNode *ptr;
44     const char *id;
45     const char *locale;
46     const char *position;
47     const char *alwaysmatches;
48     const char *firstinfield;
49     const char *sort;
50     struct zebra_index_rule *next;
51 };
52
53 struct zebra_index_rule *parse_index_rule(const xmlNode *ptr)
54 {
55     struct _xmlAttr *attr;
56     struct zebra_index_rule *rule;
57     
58     rule = xmalloc(sizeof(*rule)); 
59     rule->next = 0;
60     rule->ptr = ptr;
61     rule->locale = 0;
62     rule->id = 0;
63     rule->position = 0;
64     rule->alwaysmatches = 0;
65     rule->firstinfield = 0;
66     rule->sort = 0;
67     for (attr = ptr->properties; attr; attr = attr->next)
68     {
69         if (attr->children && attr->children->type == XML_TEXT_NODE)
70         {
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;
83             else
84             {
85                 yaz_log(YLOG_WARN, "Unsupport attribute '%s' for indexrule",
86                         attr->name);
87                 xfree(rule);
88                 return 0;
89             }
90         }
91     }
92     return rule;
93 }
94 /* YAZ_HAVE_XML2 */
95 #endif
96
97 zebra_index_rules_t zebra_index_rules_create(const char *fname)
98 {
99     xmlDocPtr doc = xmlParseFile(fname);
100     if (!doc)
101         return 0;
102     return zebra_index_rules_create_doc(doc);
103 }
104
105 zebra_index_rules_t zebra_index_rules_create_doc(xmlDocPtr doc)
106 {
107 #if YAZ_HAVE_XML2
108     zebra_index_rules_t r = xmalloc(sizeof(*r));
109     struct zebra_index_rule **rp = &r->rules;
110     const xmlNode *top = xmlDocGetRootElement(doc);
111     
112     r->doc = doc;
113     *rp = 0;
114     if (top && top->type == XML_ELEMENT_NODE
115         && !strcmp((const char *) top->name, "indexrules"))
116     {
117         const xmlNode *ptr = top->children;
118         for (; ptr; ptr = ptr->next)
119         {
120             if (ptr->type == XML_ELEMENT_NODE
121                 && !strcmp((const char *) ptr->name, "indexrule"))
122             {
123                 *rp = parse_index_rule(ptr);
124                 if (!*rp)
125                 {
126                     zebra_index_rules_destroy(r);
127                     return 0;
128                 }
129                 rp = &(*rp)->next;
130             }
131         }
132     }
133     else
134     {
135         zebra_index_rules_destroy(r);
136         r = 0;
137     }
138     return r;
139 #else
140     yaz_log(YLOG_WARN, "Cannot read index rules %s because YAZ is without XML "
141             "support", fname);
142     return 0;
143 /* YAZ_HAVE_XML2 */
144 #endif
145 }
146
147 void zebra_index_rules_destroy(zebra_index_rules_t r)
148 {
149 #if YAZ_HAVE_XML2
150     struct zebra_index_rule *rule;
151     while (r->rules)
152     {
153         rule = r->rules;
154         r->rules = rule->next;
155         xfree(rule);
156     }
157     xmlFreeDoc(r->doc);
158
159 #endif
160     xfree(r);
161 }
162
163 const char *zebra_index_rule_lookup_str(zebra_index_rules_t r, const char *id)
164 {
165 #if YAZ_HAVE_XML2
166
167     struct zebra_index_rule *rule = r->rules;
168         
169     while (rule && !yaz_match_glob(rule->id, id))
170         rule = rule->next;
171     if (rule)
172         return rule->id;
173 #endif
174     return 0;
175 }
176
177 /*
178  * Local variables:
179  * c-basic-offset: 4
180  * indent-tabs-mode: nil
181  * End:
182  * vim: shiftwidth=4 tabstop=8 expandtab
183  */
184