001: /* $Id: DigesterLoader.java 471661 2006-11-06 08:09:25Z skitching $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.commons.digester.xmlrules;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.Reader;
024: import java.net.URL;
025:
026: import org.apache.commons.digester.Digester;
027: import org.apache.commons.digester.RuleSet;
028:
029: import org.xml.sax.SAXException;
030: import org.xml.sax.InputSource;
031:
032: /**
033: * This class manages the creation of Digester instances from XML digester
034: * rules files.
035: *
036: * @since 1.2
037: */
038:
039: public class DigesterLoader {
040:
041: /**
042: * Creates a new digester and initializes it from the specified InputSource
043: * @param rulesSource load the xml rules from this InputSource
044: * @return a new Digester initialized with the rules
045: */
046: public static Digester createDigester(InputSource rulesSource) {
047: RuleSet ruleSet = new FromXmlRuleSet(rulesSource);
048: Digester digester = new Digester();
049: digester.addRuleSet(ruleSet);
050: return digester;
051: }
052:
053: /**
054: * Creates a new digester and initializes it from the specified InputSource.
055: * This constructor allows the digester to be used to load the rules to be specified.
056: * This allows properties to be configured on the Digester instance before it is used.
057: *
058: * @param rulesSource load the xml rules from this InputSource
059: * @param rulesDigester digester to load the specified XML file.
060: * @return a new Digester initialized with the rules
061: */
062: public static Digester createDigester(InputSource rulesSource,
063: Digester rulesDigester) {
064: RuleSet ruleSet = new FromXmlRuleSet(rulesSource, rulesDigester);
065: Digester digester = new Digester();
066: digester.addRuleSet(ruleSet);
067: return digester;
068: }
069:
070: /**
071: * Creates a new digester and initializes it from the specified XML file
072: * @param rulesXml URL to the XML file defining the digester rules
073: * @return a new Digester initialized with the rules
074: */
075: public static Digester createDigester(URL rulesXml) {
076: RuleSet ruleSet = new FromXmlRuleSet(rulesXml);
077: Digester digester = new Digester();
078: digester.addRuleSet(ruleSet);
079: return digester;
080: }
081:
082: /**
083: * Creates a new digester and initializes it from the specified XML file.
084: * This constructor allows specifing a rulesDigester to do the XML file
085: * loading; thus no matter the XML files is packed into a jar, a war, or a
086: * ear, the rulesDigester can always find the XML files with properly set
087: * ClassLoader.
088: *
089: * @param rulesXml URL to the XML file defining the digester rules
090: * @param rulesDigester digester to load the specified XML file.
091: * @return a new Digester initialized with the rules
092: */
093: public static Digester createDigester(URL rulesXml,
094: Digester rulesDigester) {
095: RuleSet ruleSet = new FromXmlRuleSet(rulesXml, rulesDigester);
096: Digester digester = new Digester();
097: digester.addRuleSet(ruleSet);
098: return digester;
099: }
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 load(URL digesterRules,
112: ClassLoader classLoader, URL fileURL) throws IOException,
113: SAXException, DigesterLoadingException {
114: return load(digesterRules, classLoader, fileURL.openStream());
115: }
116:
117: /**
118: * Given the digester rules XML file, a class loader, and an input stream,
119: * this method parses the input into Java objects. The class loader
120: * is used by the digester to create the Java objects.
121: * @param digesterRules URL to the XML document defining the digester rules
122: * @param classLoader the ClassLoader to register with the digester
123: * @param input InputStream over the XML file to parse into Java objects
124: * @return an Object which is the root of the network of Java objects
125: * created by digesting fileURL
126: */
127: public static Object load(URL digesterRules,
128: ClassLoader classLoader, InputStream input)
129: throws IOException, SAXException, DigesterLoadingException {
130: Digester digester = createDigester(digesterRules);
131: digester.setClassLoader(classLoader);
132: try {
133: return digester.parse(input);
134: } catch (XmlLoadException ex) {
135: // This is a runtime exception that can be thrown by
136: // FromXmlRuleSet#addRuleInstances, which is called by the Digester
137: // before it parses the file.
138: throw new DigesterLoadingException(ex.getMessage(), ex);
139: }
140: }
141:
142: /**
143: * Given the digester rules XML file, a class loader, and an input stream,
144: * this method parses the input into Java objects. The class loader
145: * is used by the digester to create the Java objects.
146: * @param digesterRules URL to the XML document defining the digester rules
147: * @param classLoader the ClassLoader to register with the digester
148: * @param reader Reader over the XML file to parse into Java objects
149: * @return an Object which is the root of the network of Java objects
150: * created by digesting fileURL
151: */
152: public static Object load(URL digesterRules,
153: ClassLoader classLoader, Reader reader) throws IOException,
154: SAXException, DigesterLoadingException {
155: Digester digester = createDigester(digesterRules);
156: digester.setClassLoader(classLoader);
157: try {
158: return digester.parse(reader);
159: } catch (XmlLoadException ex) {
160: // This is a runtime exception that can be thrown by
161: // FromXmlRuleSet#addRuleInstances, which is called by the Digester
162: // before it parses the file.
163: throw new DigesterLoadingException(ex.getMessage(), ex);
164: }
165: }
166:
167: /**
168: * Given the digester rules XML file, a class loader, and an XML input file,
169: * this method parses the input file into Java objects. The class loader
170: * is used by the digester to create the Java objects.
171: * @param digesterRules URL to the XML document defining the digester rules
172: * @param classLoader the ClassLoader to register with the digester
173: * @param fileURL URL to the XML file to parse into Java objects
174: * @param rootObject an Object to push onto the digester's stack, prior
175: * to parsing the input
176: * @return an Object which is the root of the network of Java objects.
177: * Usually, this will be the same object as rootObject
178: * created by digesting fileURL
179: */
180: public static Object load(URL digesterRules,
181: ClassLoader classLoader, URL fileURL, Object rootObject)
182: throws IOException, SAXException, DigesterLoadingException {
183: return load(digesterRules, classLoader, fileURL.openStream(),
184: rootObject);
185: }
186:
187: /**
188: * Given the digester rules XML file, a class loader, and an input stream,
189: * this method parses the input into Java objects. The class loader
190: * is used by the digester to create the Java objects.
191: * @param digesterRules URL to the XML document defining the digester rules
192: * @param classLoader the ClassLoader to register with the digester
193: * @param input InputStream over the XML file to parse into Java objects
194: * @param rootObject an Object to push onto the digester's stack, prior
195: * to parsing the input
196: * @return an Object which is the root of the network of Java objects
197: * created by digesting fileURL
198: */
199: public static Object load(URL digesterRules,
200: ClassLoader classLoader, InputStream input,
201: Object rootObject) throws IOException, SAXException,
202: DigesterLoadingException {
203: Digester digester = createDigester(digesterRules);
204: digester.setClassLoader(classLoader);
205: digester.push(rootObject);
206: try {
207: return digester.parse(input);
208: } catch (XmlLoadException ex) {
209: // This is a runtime exception that can be thrown by
210: // FromXmlRuleSet#addRuleInstances, which is called by the Digester
211: // before it parses the file.
212: throw new DigesterLoadingException(ex.getMessage(), ex);
213: }
214: }
215:
216: /**
217: * Given the digester rules XML file, a class loader, and an input stream,
218: * this method parses the input into Java objects. The class loader
219: * is used by the digester to create the Java objects.
220: * @param digesterRules URL to the XML document defining the digester rules
221: * @param classLoader the ClassLoader to register with the digester
222: * @param input Reader over the XML file to parse into Java objects
223: * @param rootObject an Object to push onto the digester's stack, prior
224: * to parsing the input
225: * @return an Object which is the root of the network of Java objects
226: * created by digesting fileURL
227: */
228: public static Object load(URL digesterRules,
229: ClassLoader classLoader, Reader input, Object rootObject)
230: throws IOException, SAXException, DigesterLoadingException {
231: Digester digester = createDigester(digesterRules);
232: digester.setClassLoader(classLoader);
233: digester.push(rootObject);
234: try {
235: return digester.parse(input);
236: } catch (XmlLoadException ex) {
237: // This is a runtime exception that can be thrown by
238: // FromXmlRuleSet#addRuleInstances, which is called by the Digester
239: // before it parses the file.
240: throw new DigesterLoadingException(ex.getMessage(), ex);
241: }
242: }
243: }
|