1 /**
2 * $Id: XMLDigesterConfigurationReader.java 17 2006-10-19 15:11:35Z maldito_orco $
3 * $Revision: 17 $
4 * $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
5 *
6 * =========================================================================
7 *
8 * Copyright 2005 Tubo
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package org.tubo.configuration.reader;
23
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.commons.digester.Digester;
29
30 import org.tubo.configuration.def.Configuration;
31
32 import org.tubo.configuration.def.digester.*;
33
34 import org.tubo.configuration.def.baseimpl.BaseIncludeDefImpl;
35
36 import org.tubo.exception.TuboConfigurationException;
37
38 import org.xml.sax.SAXException;
39
40 import java.io.Reader;
41 import java.io.IOException;
42 import java.util.List;
43 import java.util.Iterator;
44
45 /**
46 * Created: Jun 26, 2005 11:03:11 PM
47 * Last Modification Date: $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
48 *
49 * @author jvlio (jvlio@users.sourceforge.net)
50 * @version $Revision: 17 $
51 */
52 public class XMLDigesterConfigurationReader extends AbstractConfigurationReader {
53 public static final String RCS_ID = "$Id: XMLDigesterConfigurationReader.java 17 2006-10-19 15:11:35Z maldito_orco $";
54 private static Log log = LogFactory.getLog(XMLDigesterConfigurationReader.class);
55
56 public static String ROOT_TAG = "tubo-configuration";
57 public static String INCLUDE_TAG = ROOT_TAG+"/includes/include";
58
59 public static String META_OBJECT_CREATE_RULE_TAG = ROOT_TAG+"/digester-rule-set/object-create-rule";
60 public static String META_SET_PROPERTIES_RULE_TAG = ROOT_TAG+"/digester-rule-set/set-properties-rule";
61 public static String META_SET_NEXT_RULE_TAG = ROOT_TAG+"/digester-rule-set/set-next-rule";
62 public static String META_CALL_METHOD_RULE_TAG = ROOT_TAG+"/digester-rule-set/call-method-rule";
63 public static String META_CALL_PARAM_RULE_TAG = ROOT_TAG+"/digester-rule-set/call-param-rule";
64
65
66 protected Configuration createEmptyConfigurationObject() {
67 Configuration config = new DigesterConfigurationImpl();
68 return config;
69 }
70
71 protected Configuration parse(Reader reader, Configuration config) throws TuboConfigurationException {
72
73
74 Digester digester = crateDigester();
75
76
77 configureDigester(digester,config);
78
79
80 digester.push(config);
81
82
83 try {
84 config = (Configuration) digester.parse(reader);
85 } catch (IOException e) {
86 TuboConfigurationException tce = new TuboConfigurationException("IO Error tring to parse configuration.",e);
87 throw tce;
88 } catch (SAXException e) {
89 TuboConfigurationException tce = new TuboConfigurationException("XML Error while parse configuration.",e);
90 throw tce;
91 }
92
93
94 return config;
95 }
96
97 private Digester crateDigester() {
98 Digester digester = new Digester();
99 return digester;
100 }
101
102 private void configureDigester(Digester digester, Configuration config) {
103
104
105 digester.setValidating(false);
106
107
108 addBasicRules(digester);
109
110
111 addIncludeRules(digester);
112
113
114 addMetaMetaRules(digester);
115
116
117 addMetaRules(digester,config);
118 }
119
120 private void addBasicRules(Digester digester) {
121
122
123 digester.addSetProperties(ROOT_TAG);
124
125
126 digester.addCallMethod("tubo-configuration/kernel-builder-class","setKernelBuilderClassName",0);
127
128
129 digester.addSetProperties("tubo-configuration/kernel-builder-properties");
130 }
131
132 private void addIncludeRules(Digester digester) {
133
134
135 digester.addObjectCreate(INCLUDE_TAG, "defclassname", BaseIncludeDefImpl.class);
136
137
138 digester.addSetProperties(INCLUDE_TAG);
139
140
141 digester.addSetNext(
142 INCLUDE_TAG,
143 "add",
144 "org.tubo.configuration.def.IncludeDef");
145 }
146
147 private void addMetaMetaRules(Digester digester) {
148
149
150 digester.addObjectCreate(META_OBJECT_CREATE_RULE_TAG, "defclassname", DigesterObjectCreateRuleDef.class);
151
152
153 digester.addSetProperties(META_OBJECT_CREATE_RULE_TAG);
154
155
156 digester.addSetNext(
157 META_OBJECT_CREATE_RULE_TAG,
158 "add",
159 "org.tubo.configuration.def.digester.DigesterRuleDef");
160
161
162
163 digester.addObjectCreate(META_SET_PROPERTIES_RULE_TAG, "defclassname", DigesterSetPropertiesRuleDef.class);
164
165
166 digester.addSetProperties(META_SET_PROPERTIES_RULE_TAG);
167
168
169 digester.addSetNext(
170 META_SET_PROPERTIES_RULE_TAG,
171 "add",
172 "org.tubo.configuration.def.digester.DigesterRuleDef");
173
174
175
176 digester.addObjectCreate(META_SET_NEXT_RULE_TAG, "defclassname", DigesterSetNextRuleDef.class);
177
178
179 digester.addSetProperties(META_SET_NEXT_RULE_TAG);
180
181
182 digester.addSetNext(
183 META_SET_NEXT_RULE_TAG,
184 "add",
185 "org.tubo.configuration.def.digester.DigesterRuleDef");
186
187
188
189 digester.addObjectCreate(META_CALL_METHOD_RULE_TAG, "defclassname", DigesterCallMethodRuleDef.class);
190
191
192 digester.addSetProperties(META_CALL_METHOD_RULE_TAG);
193
194
195 digester.addSetNext(
196 META_CALL_METHOD_RULE_TAG,
197 "add",
198 "org.tubo.configuration.def.digester.DigesterRuleDef");
199
200
201
202 digester.addObjectCreate(META_CALL_PARAM_RULE_TAG, "defclassname", DigesterCallParamRuleDef.class);
203
204
205 digester.addSetProperties(META_CALL_PARAM_RULE_TAG);
206
207
208 digester.addSetNext(
209 META_CALL_PARAM_RULE_TAG,
210 "add",
211 "org.tubo.configuration.def.digester.DigesterRuleDef");
212
213 }
214
215 private void addMetaRules(Digester digester, Configuration config) {
216
217
218 if (config instanceof DigesterConfigurationImpl) {
219
220
221 List ruleDefs = ((DigesterConfigurationImpl)config).getDigesterRuleDefs();
222
223
224 for (Iterator it=ruleDefs.iterator(); it.hasNext(); ) {
225
226
227 DigesterRuleDef ruleDef = (DigesterRuleDef)it.next();
228
229
230 digester.addRule(ruleDef.getPattern(),ruleDef.getDigesterRule());
231 }
232 }
233 }
234
235 }