1 17 18 19 package org.apache.commons.digester.xmlrules; 20 21 22 import java.net.URL ; 23 24 import org.apache.commons.digester.Digester; 25 import org.apache.commons.digester.RuleSetBase; 26 27 import org.xml.sax.InputSource ; 28 29 34 public class FromXmlRuleSet extends RuleSetBase { 35 36 public static final String DIGESTER_DTD_PATH = "org/apache/commons/digester/xmlrules/digester-rules.dtd"; 37 38 41 private XMLRulesLoader rulesLoader; 42 43 46 private DigesterRuleParser parser; 47 48 51 private Digester rulesDigester; 52 53 58 public FromXmlRuleSet(URL rulesXml) { 59 this(rulesXml, new DigesterRuleParser(), new Digester()); 60 } 61 62 68 public FromXmlRuleSet(URL rulesXml, Digester rulesDigester) { 69 this(rulesXml, new DigesterRuleParser(), rulesDigester); 70 } 71 72 76 public FromXmlRuleSet(URL rulesXml, DigesterRuleParser parser) { 77 this(rulesXml, parser, new Digester()); 78 } 79 80 85 public FromXmlRuleSet(URL rulesXml, DigesterRuleParser parser, Digester rulesDigester) { 86 init(new URLXMLRulesLoader(rulesXml), parser, rulesDigester); 87 } 88 89 94 public FromXmlRuleSet(InputSource inputSource) { 95 this(inputSource, new DigesterRuleParser(), new Digester()); 96 } 97 98 104 public FromXmlRuleSet(InputSource inputSource, Digester rulesDigester) { 105 this(inputSource, new DigesterRuleParser(), rulesDigester); 106 } 107 108 112 public FromXmlRuleSet(InputSource inputSource, DigesterRuleParser parser) { 113 this(inputSource, parser, new Digester()); 114 } 115 116 121 public FromXmlRuleSet(InputSource inputSource, DigesterRuleParser parser, Digester rulesDigester) { 122 init(new InputSourceXMLRulesLoader(inputSource), parser, rulesDigester); 123 } 124 125 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 139 public void addRuleInstances(org.apache.commons.digester.Digester digester) throws XmlLoadException { 140 addRuleInstances(digester, null); 141 } 142 143 158 public void addRuleInstances( 159 org.apache.commons.digester.Digester digester, 160 String basePath) 161 throws XmlLoadException { 162 163 URL 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 182 private abstract static class XMLRulesLoader { 183 184 public abstract void loadRules() throws XmlLoadException; 185 } 186 187 188 private class URLXMLRulesLoader extends XMLRulesLoader { 189 private URL url; 190 public URLXMLRulesLoader(URL url) { 191 this.url = url; 192 } 193 194 public void loadRules() throws XmlLoadException { 195 try { 196 rulesDigester.parse(url.openStream()); 197 } catch (Exception ex) { 198 throw new XmlLoadException(ex); 199 } 200 } 201 } 202 203 204 private class InputSourceXMLRulesLoader extends XMLRulesLoader { 205 private InputSource inputSource; 206 public InputSourceXMLRulesLoader(InputSource inputSource) { 207 this.inputSource = inputSource; 208 } 209 210 public void loadRules() throws XmlLoadException { 211 try { 212 rulesDigester.parse(inputSource); 213 } catch (Exception ex) { 214 throw new XmlLoadException(ex); 215 } 216 } 217 } 218 } 219 220 | Popular Tags |