View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.config;
17  
18  
19  import java.net.URL;
20  import org.apache.commons.chain.Catalog;
21  import org.apache.commons.digester.Digester;
22  import org.apache.commons.digester.RuleSet;
23  import org.xml.sax.InputSource;
24  
25  
26  /***
27   * <p>Class to parse the contents of an XML configuration file (using
28   * Commons Digester) that defines and configures commands and command chains
29   * to be registered in a {@link Catalog}.  Advanced users can configure the
30   * detailed parsing behavior by configuring the properties of an instance
31   * of this class prior to calling the <code>parse()</code> method.  It
32   * is legal to call the <code>parse()</code> method more than once, in order
33   * to parse more than one configuration document.</p>
34   *
35   * @author Craig R. McClanahan
36   * @version $Revision: 1.4 $ $Date: 2004/10/18 01:07:41 $
37   */
38  public class ConfigParser {
39  
40  
41      // ----------------------------------------------------- Instance Variables
42  
43  
44      /***
45       * <p>The <code>Digester</code> to be used for parsing.</p>
46       */
47      private Digester digester = null;
48  
49  
50      /***
51       * <p>The <code>RuleSet</code> to be used for configuring our Digester
52       * parsing rules.</p>
53       */
54      private RuleSet ruleSet = null;
55  
56  
57      /***
58       * <p>Should Digester use the context class loader?
59       */
60      private boolean useContextClassLoader = true;
61  
62  
63      // ------------------------------------------------------------- Properties
64  
65  
66      /***
67       * <p>Return the <code>Digester</code> instance to be used for
68       * parsing, creating one if necessary.</p>
69       */
70      public Digester getDigester() {
71  
72          if (digester == null) {
73              digester = new Digester();
74              RuleSet ruleSet = getRuleSet();
75              digester.setNamespaceAware(ruleSet.getNamespaceURI() != null);
76              digester.setUseContextClassLoader(getUseContextClassLoader());
77              digester.setValidating(false);
78              digester.addRuleSet(ruleSet);
79          }
80          return (digester);
81  
82      }
83  
84  
85      /***
86       * <p>Return the <code>RuleSet</code> to be used for configuring
87       * our <code>Digester</code> parsing rules, creating one if necessary.</p>
88       */
89      public RuleSet getRuleSet() {
90  
91          if (ruleSet == null) {
92              ruleSet = new ConfigRuleSet();
93          }
94          return (ruleSet);
95  
96      }
97  
98  
99      /***
100      * <p>Set the <code>RuleSet</code> to be used for configuring
101      * our <code>Digester</code> parsing rules.</p>
102      *
103      * @param ruleSet The new RuleSet to use
104      */
105     public void setRuleSet(RuleSet ruleSet) {
106 
107         this.digester = null;
108         this.ruleSet = ruleSet;
109 
110     }
111 
112 
113     /***
114      * <p>Return the "use context class loader" flag.  If set to
115      * <code>true</code>, Digester will attempt to instantiate new
116      * command and chain instances from the context class loader.</p>
117      */
118     public boolean getUseContextClassLoader() {
119 
120         return (this.useContextClassLoader);
121 
122     }
123 
124 
125     /***
126      * <p>Set the "use context class loader" flag.</p>
127      *
128      * @param useContextClassLoader The new flag value
129      */
130     public void setUseContextClassLoader(boolean useContextClassLoader) {
131 
132         this.useContextClassLoader = useContextClassLoader;
133 
134     }
135 
136 
137     // --------------------------------------------------------- Public Methods
138 
139 
140     /***
141      * <p>Parse the XML document at the specified URL, using the configured
142      * <code>RuleSet</code>, registering top level commands into the specified
143      * {@link Catalog}.  Use this method <strong>only</strong> if you have
144      * <strong>NOT</strong> included any <code>factory</code> element in your
145      * configuration resource, and wish to supply the catalog explictly.</p>
146      *
147      * @param catalog {@link Catalog} into which configured chains are
148      *  to be registered
149      * @param url <code>URL</code> of the XML document to be parsed
150      *
151      * @exception Exception if a parsing error occurs
152      *
153      * @deprecated Use parse(URL) on a configuration resource with "factory"
154      *  element(s) embedded
155      */
156     public void parse(Catalog catalog, URL url) throws Exception {
157 
158         // Prepare our Digester instance
159         Digester digester = getDigester();
160         digester.clear();
161         digester.push(catalog);
162 
163         // Prepare our InputSource
164         InputSource source = new InputSource(url.toExternalForm());
165         source.setByteStream(url.openStream());
166 
167         // Parse the configuration document
168         digester.parse(source);
169 
170     }
171 
172 
173     /***
174      * <p>Parse the XML document at the specified URL using the configured
175      * <code>RuleSet</code>, registering catalogs with nested chains and
176      * commands as they are encountered.  Use this method <strong>only</strong>
177      * if you have included one or more <code>factory</code> elements in your
178      * configuration resource.</p>
179      *
180      * @param url <code>URL</code> of the XML document to be parsed
181      *
182      * @exception Exception if a parsing error occurs
183      */
184     public void parse(URL url) throws Exception {
185 
186         // Prepare our Digester instance
187         Digester digester = getDigester();
188         digester.clear();
189 
190         // Prepare our InputSource
191         InputSource source = new InputSource(url.toExternalForm());
192         source.setByteStream(url.openStream());
193 
194         // Parse the configuration document
195         digester.parse(source);
196 
197     }
198 
199 
200 }