View Javadoc

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  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //digester
28  import org.apache.commons.digester.Digester;
29  //tubo-configuration
30  import org.tubo.configuration.def.Configuration;
31  //tubo-configuration-digester
32  import org.tubo.configuration.def.digester.*;
33  //tubo-configuration-default
34  import org.tubo.configuration.def.baseimpl.BaseIncludeDefImpl;
35  //tubo-exception
36  import org.tubo.exception.TuboConfigurationException;
37  //sax
38  import org.xml.sax.SAXException;
39  //io
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          // create xml parser
74          Digester digester = crateDigester();
75          //
76          // configuring
77          configureDigester(digester,config);
78          //
79          // Pushing Configuration on top of queue
80          digester.push(config);
81          //
82          // run parsing
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          // return configuration
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         // validation configuration
105         digester.setValidating(false);
106         //
107         // add include rules
108         addBasicRules(digester);
109         //
110         // add include rules
111         addIncludeRules(digester);
112         //
113         // add Rules to understand other rules (meta-meta rules)
114         addMetaMetaRules(digester);
115         //
116         // add all Meta-Meta Rules like Digester rules
117         addMetaRules(digester,config);
118     }
119 
120     private void addBasicRules(Digester digester) {
121         //
122         // set root properties to configuration object, if any
123         digester.addSetProperties(ROOT_TAG);
124         //
125         // set builder-class
126         digester.addCallMethod("tubo-configuration/kernel-builder-class","setKernelBuilderClassName",0);
127         //
128         // set builder-properties to configuration object, if any
129         digester.addSetProperties("tubo-configuration/kernel-builder-properties");
130     }
131 
132     private void addIncludeRules(Digester digester) {
133         //
134         // Create a new IncludeDef object if this tag occurs
135         digester.addObjectCreate(INCLUDE_TAG, "defclassname", BaseIncludeDefImpl.class);
136         //
137         // set tag properties to new def object
138         digester.addSetProperties(INCLUDE_TAG);
139         //
140         // Add IncludeDef to Configuration
141         digester.addSetNext(
142                 INCLUDE_TAG,
143                 "add",
144                 "org.tubo.configuration.def.IncludeDef");
145     }
146 
147     private void addMetaMetaRules(Digester digester) {
148         //
149         // Create a new ObjectCreate object if this tag occurs
150         digester.addObjectCreate(META_OBJECT_CREATE_RULE_TAG, "defclassname", DigesterObjectCreateRuleDef.class);
151         //
152         // set tag properties to new def object
153         digester.addSetProperties(META_OBJECT_CREATE_RULE_TAG);
154         //
155         // Add ObjectCreate to Configuration
156         digester.addSetNext(
157                 META_OBJECT_CREATE_RULE_TAG,
158                 "add",
159                 "org.tubo.configuration.def.digester.DigesterRuleDef");
160 
161         //
162         // Create a new SetProperties object if this tag occurs
163         digester.addObjectCreate(META_SET_PROPERTIES_RULE_TAG, "defclassname", DigesterSetPropertiesRuleDef.class);
164         //
165         // set tag properties to new def object
166         digester.addSetProperties(META_SET_PROPERTIES_RULE_TAG);
167         //
168         // Add SetProperties to Configuration
169         digester.addSetNext(
170                 META_SET_PROPERTIES_RULE_TAG,
171                 "add",
172                 "org.tubo.configuration.def.digester.DigesterRuleDef");
173 
174         //
175         // Create a new SetNext object if this tag occurs
176         digester.addObjectCreate(META_SET_NEXT_RULE_TAG, "defclassname", DigesterSetNextRuleDef.class);
177         //
178         // set tag properties to new def object
179         digester.addSetProperties(META_SET_NEXT_RULE_TAG);
180         //
181         // Add SetNext to Configuration
182         digester.addSetNext(
183                 META_SET_NEXT_RULE_TAG,
184                 "add",
185                 "org.tubo.configuration.def.digester.DigesterRuleDef");
186 
187         //
188         // Create a new CallMethod object if this tag occurs
189         digester.addObjectCreate(META_CALL_METHOD_RULE_TAG, "defclassname", DigesterCallMethodRuleDef.class);
190         //
191         // set tag properties to new def object
192         digester.addSetProperties(META_CALL_METHOD_RULE_TAG);
193         //
194         // Add SetNext to Configuration
195         digester.addSetNext(
196                 META_CALL_METHOD_RULE_TAG,
197                 "add",
198                 "org.tubo.configuration.def.digester.DigesterRuleDef");
199 
200         //
201         // Create a new CallMethod object if this tag occurs
202         digester.addObjectCreate(META_CALL_PARAM_RULE_TAG, "defclassname", DigesterCallParamRuleDef.class);
203         //
204         // set tag properties to new def object
205         digester.addSetProperties(META_CALL_PARAM_RULE_TAG);
206         //
207         // Add SetNext to Configuration
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         // check first if a Digester configuration
218         if (config instanceof DigesterConfigurationImpl) {
219             //
220             // get rule definitions
221             List ruleDefs = ((DigesterConfigurationImpl)config).getDigesterRuleDefs();
222             //
223             // iterate all
224             for (Iterator it=ruleDefs.iterator(); it.hasNext(); ) {
225                 //
226                 // get a rule def
227                 DigesterRuleDef ruleDef = (DigesterRuleDef)it.next();
228                 //
229                 // add this rule into digester
230                 digester.addRule(ruleDef.getPattern(),ruleDef.getDigesterRule());
231             }
232         }
233     }
234 
235 }