1 package org.apache.velocity.tools.config; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.InputStream; 23 import java.io.IOException; 24 import org.xml.sax.SAXException; 25 import org.apache.commons.digester.Digester; 26 import org.apache.commons.digester.RuleSet; 27 28 /** 29 * <p>This reads in configuration info formatted as an XML file 30 * using Commons-{@link Digester}. This uses 31 * {@link XmlFactoryConfigurationRuleSet} as the default set of rules 32 * for processing the XML. However, you may always change this by 33 * passing a new {@link RuleSet} to the {@link #setRuleSet} method. 34 * See the configuration documentation on the main web site for 35 * instructions on the XML format supported by the default rules.</p> 36 * <p>Example usage: 37 * <pre> 38 * FactoryConfiguration cfg = new XmlFactoryConfiguration("Dev Tools"); 39 * cfg.read("devtools.xml"); 40 * ToolboxFactory factory = cfg.createFactory(); 41 * </pre></p> 42 * 43 * @author Nathan Bubna 44 * @version $Id: XmlFactoryConfiguration.java 511959 2007-02-26 19:24:39Z nbubna $ 45 */ 46 public class XmlFactoryConfiguration extends FileFactoryConfiguration 47 { 48 private RuleSet ruleSet; 49 private boolean supportOldXml; 50 51 public XmlFactoryConfiguration() 52 { 53 this(false, ""); 54 } 55 56 public XmlFactoryConfiguration(boolean supportOldConfig) 57 { 58 this(supportOldConfig, String.valueOf(supportOldConfig)); 59 } 60 61 /** 62 * Creates an instance using the specified string 63 * as an identifier to distinguish this instance when debugging. 64 * 65 * @param id the name of the "source" of this instance 66 * @see FactoryConfiguration#setSource(String) 67 */ 68 public XmlFactoryConfiguration(String id) 69 { 70 this(false, id); 71 } 72 73 /** 74 * Creates an instance using the specified string 75 * as an identifier to distinguish this instance when debugging 76 * and using the specified setting for supporting the old toolbox.xml 77 * format from VelocityTools 1.x. 78 * 79 * @param supportOldConfig whether the old toolbox.xml format should be supported 80 * @param id the name of the "source" of this instance 81 * @see FactoryConfiguration#setSource(String) 82 */ 83 public XmlFactoryConfiguration(boolean supportOldConfig, String id) 84 { 85 super(XmlFactoryConfiguration.class, id); 86 setRuleSet(new XmlFactoryConfigurationRuleSet()); 87 this.supportOldXml = supportOldConfig; 88 } 89 90 /** 91 * Sets the {@link RuleSet} this loader will use to 92 * digest the xml toolbox. 93 */ 94 public void setRuleSet(RuleSet rules) 95 { 96 this.ruleSet = rules; 97 } 98 99 /** 100 * <p>Retrieves the rule set Digester should use to parse and load 101 * the toolbox for this manager.</p> 102 */ 103 public RuleSet getRuleSet() 104 { 105 return ruleSet; 106 } 107 108 109 /** 110 * <p>Reads an XML document from an {@link InputStream} 111 * and uses it to configure this {@link FactoryConfiguration}.</p> 112 * 113 * @param input the InputStream to read from 114 */ 115 public void read(InputStream input) throws IOException 116 { 117 Digester digester = new Digester(); 118 digester.setValidating(false); 119 digester.setUseContextClassLoader(true); 120 digester.push(this); 121 digester.addRuleSet(getRuleSet()); 122 if (supportOldXml) 123 { 124 digester.addRuleSet(new OldXmlFactoryConfigurationRuleSet()); 125 } 126 try 127 { 128 digester.parse(input); 129 } 130 catch (SAXException saxe) 131 { 132 throw new RuntimeException("There was an error while parsing the InputStream", saxe); 133 } 134 } 135 136 }