KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: DigesterLoader.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.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.apache.commons.digester.Digester;
28 import org.apache.commons.digester.RuleSet;
29
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32
33
34 /**
35  * This class manages the creation of Digester instances from XML digester
36  * rules files.
37  *
38  * @since 1.2
39  */

40
41 public class DigesterLoader {
42
43     /**
44      * Creates a new digester and initializes it from the specified InputSource
45      * @param rulesSource load the xml rules from this InputSource
46      * @return a new Digester initialized with the rules
47      */

48     public static Digester createDigester(InputSource JavaDoc rulesSource) {
49         RuleSet ruleSet = new FromXmlRuleSet(rulesSource);
50         Digester digester = new Digester();
51         digester.addRuleSet(ruleSet);
52         return digester;
53     }
54
55     /**
56      * Creates a new digester and initializes it from the specified InputSource.
57      * This constructor allows the digester to be used to load the rules to be specified.
58      * This allows properties to be configured on the Digester instance before it is used.
59      *
60      * @param rulesSource load the xml rules from this InputSource
61      * @param rulesDigester digester to load the specified XML file.
62      * @return a new Digester initialized with the rules
63      */

64     public static Digester createDigester(InputSource JavaDoc rulesSource, Digester rulesDigester) {
65         RuleSet ruleSet = new FromXmlRuleSet(rulesSource, rulesDigester);
66         Digester digester = new Digester();
67         digester.addRuleSet(ruleSet);
68         return digester;
69     }
70
71     /**
72      * Creates a new digester and initializes it from the specified XML file
73      * @param rulesXml URL to the XML file defining the digester rules
74      * @return a new Digester initialized with the rules
75      */

76     public static Digester createDigester(URL JavaDoc rulesXml) {
77         RuleSet ruleSet = new FromXmlRuleSet(rulesXml);
78         Digester digester = new Digester();
79         digester.addRuleSet(ruleSet);
80         return digester;
81     }
82
83     /**
84      * Creates a new digester and initializes it from the specified XML file.
85      * This constructor allows specifing a rulesDigester to do the XML file
86      * loading; thus no matter the XML files is packed into a jar, a war, or a
87      * ear, the rulesDigester can always find the XML files with properly set
88      * ClassLoader.
89      *
90      * @param rulesXml URL to the XML file defining the digester rules
91      * @param rulesDigester digester to load the specified XML file.
92      * @return a new Digester initialized with the rules
93      */

94     public static Digester createDigester(URL JavaDoc rulesXml, Digester rulesDigester) {
95         RuleSet ruleSet = new FromXmlRuleSet(rulesXml, rulesDigester);
96         Digester digester = new Digester();
97         digester.addRuleSet(ruleSet);
98         return digester;
99     }
100
101     /**
102      * Given the digester rules XML file, a class loader, and an XML input file,
103      * this method parses the input file into Java objects. The class loader
104      * is used by the digester to create the Java objects.
105      * @param digesterRules URL to the XML document defining the digester rules
106      * @param classLoader the ClassLoader to register with the digester
107      * @param fileURL URL to the XML file to parse into Java objects
108      * @return an Object which is the root of the network of Java objects
109      * created by digesting fileURL
110      */

111     public static Object JavaDoc load(URL JavaDoc digesterRules, ClassLoader JavaDoc classLoader,
112                               URL JavaDoc fileURL) throws IOException JavaDoc, SAXException JavaDoc, DigesterLoadingException {
113         return load(digesterRules, classLoader, fileURL.openStream());
114     }
115
116     /**
117      * Given the digester rules XML file, a class loader, and an input stream,
118      * this method parses the input into Java objects. The class loader
119      * is used by the digester to create the Java objects.
120      * @param digesterRules URL to the XML document defining the digester rules
121      * @param classLoader the ClassLoader to register with the digester
122      * @param input InputStream over the XML file to parse into Java objects
123      * @return an Object which is the root of the network of Java objects
124      * created by digesting fileURL
125      */

126     public static Object JavaDoc load(URL JavaDoc digesterRules, ClassLoader JavaDoc classLoader,
127                               InputStream JavaDoc input) throws IOException JavaDoc, SAXException JavaDoc, DigesterLoadingException {
128         Digester digester = createDigester(digesterRules);
129         digester.setClassLoader(classLoader);
130         try {
131             return digester.parse(input);
132         } catch (XmlLoadException ex) {
133             // This is a runtime exception that can be thrown by
134
// FromXmlRuleSet#addRuleInstances, which is called by the Digester
135
// before it parses the file.
136
throw new DigesterLoadingException(ex.getMessage(), ex);
137         }
138     }
139     
140     /**
141      * Given the digester rules XML file, a class loader, and an input stream,
142      * this method parses the input into Java objects. The class loader
143      * is used by the digester to create the Java objects.
144      * @param digesterRules URL to the XML document defining the digester rules
145      * @param classLoader the ClassLoader to register with the digester
146      * @param reader Reader over the XML file to parse into Java objects
147      * @return an Object which is the root of the network of Java objects
148      * created by digesting fileURL
149      */

150     public static Object JavaDoc load(
151                                 URL JavaDoc digesterRules,
152                                 ClassLoader JavaDoc classLoader,
153                                 Reader JavaDoc reader)
154                                     throws
155                                         IOException JavaDoc,
156                                         SAXException JavaDoc,
157                                         DigesterLoadingException {
158         Digester digester = createDigester(digesterRules);
159         digester.setClassLoader(classLoader);
160         try {
161             return digester.parse(reader);
162         } catch (XmlLoadException ex) {
163             // This is a runtime exception that can be thrown by
164
// FromXmlRuleSet#addRuleInstances, which is called by the Digester
165
// before it parses the file.
166
throw new DigesterLoadingException(ex.getMessage(), ex);
167         }
168     }
169
170
171     /**
172      * Given the digester rules XML file, a class loader, and an XML input file,
173      * this method parses the input file into Java objects. The class loader
174      * is used by the digester to create the Java objects.
175      * @param digesterRules URL to the XML document defining the digester rules
176      * @param classLoader the ClassLoader to register with the digester
177      * @param fileURL URL to the XML file to parse into Java objects
178      * @param rootObject an Object to push onto the digester's stack, prior
179      * to parsing the input
180      * @return an Object which is the root of the network of Java objects.
181      * Usually, this will be the same object as rootObject
182      * created by digesting fileURL
183      */

184     public static Object JavaDoc load(URL JavaDoc digesterRules, ClassLoader JavaDoc classLoader,
185                               URL JavaDoc fileURL, Object JavaDoc rootObject) throws IOException JavaDoc, SAXException JavaDoc,
186             DigesterLoadingException {
187         return load(digesterRules, classLoader, fileURL.openStream(), rootObject);
188     }
189
190     /**
191      * Given the digester rules XML file, a class loader, and an input stream,
192      * this method parses the input into Java objects. The class loader
193      * is used by the digester to create the Java objects.
194      * @param digesterRules URL to the XML document defining the digester rules
195      * @param classLoader the ClassLoader to register with the digester
196      * @param input InputStream over the XML file to parse into Java objects
197      * @param rootObject an Object to push onto the digester's stack, prior
198      * to parsing the input
199      * @return an Object which is the root of the network of Java objects
200      * created by digesting fileURL
201      */

202     public static Object JavaDoc load(URL JavaDoc digesterRules, ClassLoader JavaDoc classLoader,
203                               InputStream JavaDoc input, Object JavaDoc rootObject) throws IOException JavaDoc, SAXException JavaDoc,
204             DigesterLoadingException {
205         Digester digester = createDigester(digesterRules);
206         digester.setClassLoader(classLoader);
207         digester.push(rootObject);
208         try {
209             return digester.parse(input);
210         } catch (XmlLoadException ex) {
211             // This is a runtime exception that can be thrown by
212
// FromXmlRuleSet#addRuleInstances, which is called by the Digester
213
// before it parses the file.
214
throw new DigesterLoadingException(ex.getMessage(), ex);
215         }
216     }
217     
218     /**
219      * Given the digester rules XML file, a class loader, and an input stream,
220      * this method parses the input into Java objects. The class loader
221      * is used by the digester to create the Java objects.
222      * @param digesterRules URL to the XML document defining the digester rules
223      * @param classLoader the ClassLoader to register with the digester
224      * @param input Reader over the XML file to parse into Java objects
225      * @param rootObject an Object to push onto the digester's stack, prior
226      * to parsing the input
227      * @return an Object which is the root of the network of Java objects
228      * created by digesting fileURL
229      */

230     public static Object JavaDoc load(
231                                 URL JavaDoc digesterRules,
232                                 ClassLoader JavaDoc classLoader,
233                                 Reader JavaDoc input,
234                                 Object JavaDoc rootObject)
235                                     throws
236                                         IOException JavaDoc,
237                                         SAXException JavaDoc,
238                                         DigesterLoadingException {
239         Digester digester = createDigester(digesterRules);
240         digester.setClassLoader(classLoader);
241         digester.push(rootObject);
242         try {
243             return digester.parse(input);
244         } catch (XmlLoadException ex) {
245             // This is a runtime exception that can be thrown by
246
// FromXmlRuleSet#addRuleInstances, which is called by the Digester
247
// before it parses the file.
248
throw new DigesterLoadingException(ex.getMessage(), ex);
249         }
250     }
251 }
252
Popular Tags