1. /* $Id: DigesterLoader.java,v 1.14 2004/05/10 06:30:08 skitching Exp $
  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. package org.apache.commons.digester.xmlrules;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.Reader;
  21. import java.net.URL;
  22. import org.apache.commons.digester.Digester;
  23. import org.apache.commons.digester.RuleSet;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.InputSource;
  26. /**
  27. * This class manages the creation of Digester instances from XML digester
  28. * rules files.
  29. *
  30. * @since 1.2
  31. */
  32. public class DigesterLoader {
  33. /**
  34. * Creates a new digester and initializes it from the specified InputSource
  35. * @param rulesSource load the xml rules from this InputSource
  36. * @return a new Digester initialized with the rules
  37. */
  38. public static Digester createDigester(InputSource rulesSource) {
  39. RuleSet ruleSet = new FromXmlRuleSet(rulesSource);
  40. Digester digester = new Digester();
  41. digester.addRuleSet(ruleSet);
  42. return digester;
  43. }
  44. /**
  45. * Creates a new digester and initializes it from the specified InputSource.
  46. * This constructor allows the digester to be used to load the rules to be specified.
  47. * This allows properties to be configured on the Digester instance before it is used.
  48. *
  49. * @param rulesSource load the xml rules from this InputSource
  50. * @param rulesDigester digester to load the specified XML file.
  51. * @return a new Digester initialized with the rules
  52. */
  53. public static Digester createDigester(InputSource rulesSource, Digester rulesDigester) {
  54. RuleSet ruleSet = new FromXmlRuleSet(rulesSource, rulesDigester);
  55. Digester digester = new Digester();
  56. digester.addRuleSet(ruleSet);
  57. return digester;
  58. }
  59. /**
  60. * Creates a new digester and initializes it from the specified XML file
  61. * @param rulesXml URL to the XML file defining the digester rules
  62. * @return a new Digester initialized with the rules
  63. */
  64. public static Digester createDigester(URL rulesXml) {
  65. RuleSet ruleSet = new FromXmlRuleSet(rulesXml);
  66. Digester digester = new Digester();
  67. digester.addRuleSet(ruleSet);
  68. return digester;
  69. }
  70. /**
  71. * Creates a new digester and initializes it from the specified XML file.
  72. * This constructor allows specifing a rulesDigester to do the XML file
  73. * loading; thus no matter the XML files is packed into a jar, a war, or a
  74. * ear, the rulesDigester can always find the XML files with properly set
  75. * ClassLoader.
  76. *
  77. * @param rulesXml URL to the XML file defining the digester rules
  78. * @param rulesDigester digester to load the specified XML file.
  79. * @return a new Digester initialized with the rules
  80. */
  81. public static Digester createDigester(URL rulesXml, Digester rulesDigester) {
  82. RuleSet ruleSet = new FromXmlRuleSet(rulesXml, rulesDigester);
  83. Digester digester = new Digester();
  84. digester.addRuleSet(ruleSet);
  85. return digester;
  86. }
  87. /**
  88. * Given the digester rules XML file, a class loader, and an XML input file,
  89. * this method parses the input file into Java objects. The class loader
  90. * is used by the digester to create the Java objects.
  91. * @param digesterRules URL to the XML document defining the digester rules
  92. * @param classLoader the ClassLoader to register with the digester
  93. * @param fileURL URL to the XML file to parse into Java objects
  94. * @return an Object which is the root of the network of Java objects
  95. * created by digesting fileURL
  96. */
  97. public static Object load(URL digesterRules, ClassLoader classLoader,
  98. URL fileURL) throws IOException, SAXException, DigesterLoadingException {
  99. return load(digesterRules, classLoader, fileURL.openStream());
  100. }
  101. /**
  102. * Given the digester rules XML file, a class loader, and an input stream,
  103. * this method parses the input 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 input InputStream over 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, ClassLoader classLoader,
  112. InputStream input) throws IOException, SAXException, DigesterLoadingException {
  113. Digester digester = createDigester(digesterRules);
  114. digester.setClassLoader(classLoader);
  115. try {
  116. return digester.parse(input);
  117. } catch (XmlLoadException ex) {
  118. // This is a runtime exception that can be thrown by
  119. // FromXmlRuleSet#addRuleInstances, which is called by the Digester
  120. // before it parses the file.
  121. throw new DigesterLoadingException(ex.getMessage(), ex);
  122. }
  123. }
  124. /**
  125. * Given the digester rules XML file, a class loader, and an input stream,
  126. * this method parses the input into Java objects. The class loader
  127. * is used by the digester to create the Java objects.
  128. * @param digesterRules URL to the XML document defining the digester rules
  129. * @param classLoader the ClassLoader to register with the digester
  130. * @param reader Reader over the XML file to parse into Java objects
  131. * @return an Object which is the root of the network of Java objects
  132. * created by digesting fileURL
  133. */
  134. public static Object load(
  135. URL digesterRules,
  136. ClassLoader classLoader,
  137. Reader reader)
  138. throws
  139. IOException,
  140. SAXException,
  141. DigesterLoadingException {
  142. Digester digester = createDigester(digesterRules);
  143. digester.setClassLoader(classLoader);
  144. try {
  145. return digester.parse(reader);
  146. } catch (XmlLoadException ex) {
  147. // This is a runtime exception that can be thrown by
  148. // FromXmlRuleSet#addRuleInstances, which is called by the Digester
  149. // before it parses the file.
  150. throw new DigesterLoadingException(ex.getMessage(), ex);
  151. }
  152. }
  153. /**
  154. * Given the digester rules XML file, a class loader, and an XML input file,
  155. * this method parses the input file into Java objects. The class loader
  156. * is used by the digester to create the Java objects.
  157. * @param digesterRules URL to the XML document defining the digester rules
  158. * @param classLoader the ClassLoader to register with the digester
  159. * @param fileURL URL to the XML file to parse into Java objects
  160. * @param rootObject an Object to push onto the digester's stack, prior
  161. * to parsing the input
  162. * @return an Object which is the root of the network of Java objects.
  163. * Usually, this will be the same object as rootObject
  164. * created by digesting fileURL
  165. */
  166. public static Object load(URL digesterRules, ClassLoader classLoader,
  167. URL fileURL, Object rootObject) throws IOException, SAXException,
  168. DigesterLoadingException {
  169. return load(digesterRules, classLoader, fileURL.openStream(), rootObject);
  170. }
  171. /**
  172. * Given the digester rules XML file, a class loader, and an input stream,
  173. * this method parses the input 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 input InputStream over 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. * created by digesting fileURL
  182. */
  183. public static Object load(URL digesterRules, ClassLoader classLoader,
  184. InputStream input, Object rootObject) throws IOException, SAXException,
  185. DigesterLoadingException {
  186. Digester digester = createDigester(digesterRules);
  187. digester.setClassLoader(classLoader);
  188. digester.push(rootObject);
  189. try {
  190. return digester.parse(input);
  191. } catch (XmlLoadException ex) {
  192. // This is a runtime exception that can be thrown by
  193. // FromXmlRuleSet#addRuleInstances, which is called by the Digester
  194. // before it parses the file.
  195. throw new DigesterLoadingException(ex.getMessage(), ex);
  196. }
  197. }
  198. /**
  199. * Given the digester rules XML file, a class loader, and an input stream,
  200. * this method parses the input into Java objects. The class loader
  201. * is used by the digester to create the Java objects.
  202. * @param digesterRules URL to the XML document defining the digester rules
  203. * @param classLoader the ClassLoader to register with the digester
  204. * @param input Reader over the XML file to parse into Java objects
  205. * @param rootObject an Object to push onto the digester's stack, prior
  206. * to parsing the input
  207. * @return an Object which is the root of the network of Java objects
  208. * created by digesting fileURL
  209. */
  210. public static Object load(
  211. URL digesterRules,
  212. ClassLoader classLoader,
  213. Reader input,
  214. Object rootObject)
  215. throws
  216. IOException,
  217. SAXException,
  218. DigesterLoadingException {
  219. Digester digester = createDigester(digesterRules);
  220. digester.setClassLoader(classLoader);
  221. digester.push(rootObject);
  222. try {
  223. return digester.parse(input);
  224. } catch (XmlLoadException ex) {
  225. // This is a runtime exception that can be thrown by
  226. // FromXmlRuleSet#addRuleInstances, which is called by the Digester
  227. // before it parses the file.
  228. throw new DigesterLoadingException(ex.getMessage(), ex);
  229. }
  230. }
  231. }