KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > xmlrules > FromXmlRuleSet


1 /* $Id: FromXmlRuleSet.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.xmlrules;
20
21
22 import java.net.URL JavaDoc;
23
24 import org.apache.commons.digester.Digester;
25 import org.apache.commons.digester.RuleSetBase;
26
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * A Digester rule set where the rules come from an XML file.
31  *
32  * @since 1.2
33  */

34 public class FromXmlRuleSet extends RuleSetBase {
35
36     public static final String JavaDoc DIGESTER_DTD_PATH = "org/apache/commons/digester/xmlrules/digester-rules.dtd";
37
38     /**
39      * The file containing the Digester rules, in XML.
40      */

41     private XMLRulesLoader rulesLoader;
42
43     /**
44      * The rule set for parsing the Digester rules
45      */

46     private DigesterRuleParser parser;
47
48     /**
49         * The digester for loading the rules xml.
50         */

51     private Digester rulesDigester;
52
53     /**
54      * Constructs a FromXmlRuleSet using the default DigesterRuleParser and
55      * rulesDigester.
56      * @param rulesXml the path to the XML document defining the Digester rules
57      */

58     public FromXmlRuleSet(URL JavaDoc rulesXml) {
59         this(rulesXml, new DigesterRuleParser(), new Digester());
60     }
61
62     /**
63      * Constructs a FromXmlRuleSet using the default DigesterRuleParser and
64      * a ruleDigester for loading the rules xml.
65      * @param rulesXml the path to the XML document defining the Digester rules
66      * @param rulesDigester the digester to read the rules xml.
67      */

68     public FromXmlRuleSet(URL JavaDoc rulesXml, Digester rulesDigester) {
69         this(rulesXml, new DigesterRuleParser(), rulesDigester);
70     }
71
72     /**
73      * @param rulesXml the path to the XML document defining the Digester rules
74      * @param parser an instance of DigesterRuleParser, for parsing the rules from XML
75      */

76     public FromXmlRuleSet(URL JavaDoc rulesXml, DigesterRuleParser parser) {
77         this(rulesXml, parser, new Digester());
78     }
79
80     /**
81      * @param rulesXml the path to the XML document defining the Digester rules
82      * @param parser an instance of DigesterRuleParser, for parsing the rules from XML
83      * @param rulesDigester the digester used to load the Xml rules.
84      */

85     public FromXmlRuleSet(URL JavaDoc rulesXml, DigesterRuleParser parser, Digester rulesDigester) {
86         init(new URLXMLRulesLoader(rulesXml), parser, rulesDigester);
87     }
88
89     /**
90      * Constructs a FromXmlRuleSet using the default DigesterRuleParser and
91      * rulesDigester.
92      * @param inputSource load the xml rules from this InputSource
93      */

94     public FromXmlRuleSet(InputSource JavaDoc inputSource) {
95         this(inputSource, new DigesterRuleParser(), new Digester());
96     }
97     
98     /**
99      * Constructs a FromXmlRuleSet using the default DigesterRuleParser and
100      * a ruleDigester for loading the rules xml.
101      * @param inputSource load the xml rules from this InputSource
102      * @param rulesDigester the digester to read the rules xml.
103      */

104     public FromXmlRuleSet(InputSource JavaDoc inputSource, Digester rulesDigester) {
105         this(inputSource, new DigesterRuleParser(), rulesDigester);
106     }
107
108     /**
109      * @param inputSource load the xml rules from this InputSource
110      * @param parser an instance of DigesterRuleParser, for parsing the rules from XML
111      */

112     public FromXmlRuleSet(InputSource JavaDoc inputSource, DigesterRuleParser parser) {
113         this(inputSource, parser, new Digester());
114     }
115
116     /**
117      * @param inputSource load the xml rules from this InputSource
118      * @param parser an instance of DigesterRuleParser, for parsing the rules from XML
119      * @param rulesDigester the digester used to load the Xml rules.
120      */

121     public FromXmlRuleSet(InputSource JavaDoc inputSource, DigesterRuleParser parser, Digester rulesDigester) {
122         init(new InputSourceXMLRulesLoader(inputSource), parser, rulesDigester);
123     }
124     
125     /**
126      * Base constructor
127      */

128     private void init(XMLRulesLoader rulesLoader, DigesterRuleParser parser, Digester rulesDigester) {
129         this.rulesLoader = rulesLoader;
130         this.parser = parser;
131         this.rulesDigester = rulesDigester;
132     }
133     
134     /**
135      * Adds to the digester the set of Rule instances defined in the
136      * XML file for this rule set.
137      * @see org.apache.commons.digester.RuleSetBase
138      */

139     public void addRuleInstances(org.apache.commons.digester.Digester digester) throws XmlLoadException {
140         addRuleInstances(digester, null);
141     }
142     
143     /**
144      * Adds to the digester the set of Rule instances defined in the
145      * XML file for this rule set.
146      * <p>
147      * Note that this method doesn't have a matching one on the DigesterLoader
148      * class, because it is not expected to be widely used, and DigesterLoader's
149      * load method is already heavily overloaded.
150      *
151      * @param digester is the digester that rules will be added to.
152      * @param basePath is a path that will be prefixed to every
153      * pattern string defined in the xmlrules input file.
154      *
155      * @see org.apache.commons.digester.RuleSetBase
156      * @since 1.6
157      */

158     public void addRuleInstances(
159     org.apache.commons.digester.Digester digester,
160     String JavaDoc basePath)
161     throws XmlLoadException {
162         
163         URL JavaDoc dtdURL = getClass().getClassLoader().getResource(DIGESTER_DTD_PATH);
164         if (dtdURL == null) {
165             throw new XmlLoadException("Cannot find resource \"" +
166                     DIGESTER_DTD_PATH + "\"");
167         }
168         parser.setDigesterRulesDTD(dtdURL.toString());
169         parser.setTarget(digester);
170         parser.setBasePath(basePath);
171
172         rulesDigester.addRuleSet(parser);
173         rulesDigester.push(parser);
174
175         rulesLoader.loadRules();
176     }
177     
178     /**
179      * Worker class encapsulates loading mechanisms.
180      * Private until some reason is found to make it public.
181      */

182     private abstract static class XMLRulesLoader {
183         /** Load rules now */
184         public abstract void loadRules() throws XmlLoadException;
185     }
186     
187     /** Loads XMLRules from an URL */
188     private class URLXMLRulesLoader extends XMLRulesLoader {
189         private URL JavaDoc url;
190         public URLXMLRulesLoader(URL JavaDoc url) {
191             this.url = url;
192         }
193         
194         public void loadRules() throws XmlLoadException {
195             try {
196                 rulesDigester.parse(url.openStream());
197             } catch (Exception JavaDoc ex) {
198                 throw new XmlLoadException(ex);
199             }
200         }
201     }
202
203     /** Loads XMLRules from an InputSource */
204     private class InputSourceXMLRulesLoader extends XMLRulesLoader {
205         private InputSource JavaDoc inputSource;
206         public InputSourceXMLRulesLoader(InputSource JavaDoc inputSource) {
207             this.inputSource = inputSource;
208         }
209         
210         public void loadRules() throws XmlLoadException {
211             try {
212                 rulesDigester.parse(inputSource);
213             } catch (Exception JavaDoc ex) {
214                 throw new XmlLoadException(ex);
215             }
216         }
217     }
218 }
219
220
Popular Tags