1 package com.tirsen.nanning.xml; 2 3 import com.tirsen.nanning.AspectException; 4 import com.tirsen.nanning.config.AspectSystem; 5 import org.apache.commons.digester.xmlrules.DigesterLoader; 6 import org.apache.commons.digester.xmlrules.DigesterLoadingException; 7 import org.xml.sax.SAXException ; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.ByteArrayInputStream ; 12 import java.net.URL ; 13 14 public class AspectSystemParser { 15 private static final String RULES_RESOURCE = "com/tirsen/nanning/xml/rules.xml"; 16 17 public AspectSystem parse(InputStream input) throws IOException { 18 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 19 ClassLoader classLoader = getClass().getClassLoader(); 20 URL rules = null; 21 if (contextClassLoader != null) { 22 rules = contextClassLoader.getResource(RULES_RESOURCE); 23 } 24 if (rules == null) { 25 rules = classLoader.getResource(RULES_RESOURCE); 26 } 27 assert rules != null : "could not find rules in class-loader " + classLoader; 28 try { 29 return (AspectSystem) DigesterLoader.load(rules, 30 contextClassLoader != null ? contextClassLoader : classLoader, input); 31 } catch (SAXException e) { 32 throw new AspectException(e); 33 } catch (DigesterLoadingException e) { 34 throw new AspectException(e); 35 } 36 } 37 38 public AspectSystem parse(String xmlAsString) throws IOException { 39 return parse(new ByteArrayInputStream (xmlAsString.getBytes())); 40 } 41 42 public AspectSystem parse(URL resource) throws IOException { 43 InputStream input = resource.openStream(); 44 try { 45 return parse(input); 46 } finally { 47 input.close(); 48 } 49 } 50 } 51 | Popular Tags |