1 17 18 package org.apache.commons.digester.plugins.strategies; 19 20 import java.util.Properties ; 21 import java.io.InputStream ; 22 23 import org.apache.commons.digester.Digester; 24 import org.apache.commons.digester.plugins.RuleFinder; 25 import org.apache.commons.digester.plugins.RuleLoader; 26 import org.apache.commons.digester.plugins.PluginException; 27 28 35 36 public class FinderFromResource extends RuleFinder { 37 41 public static String DFLT_RESOURCE_ATTR = "resource"; 42 43 44 private String resourceAttr; 45 46 47 public FinderFromResource() { 48 this(DFLT_RESOURCE_ATTR); 49 } 50 51 52 public FinderFromResource(String resourceAttr) { 53 this.resourceAttr = resourceAttr; 54 } 55 56 66 public RuleLoader findLoader(Digester d, Class pluginClass, Properties p) 67 throws PluginException { 68 69 String resourceName = p.getProperty(resourceAttr); 70 if (resourceName == null) { 71 return null; 74 } 75 76 InputStream is = 77 pluginClass.getClassLoader().getResourceAsStream( 78 resourceName); 79 80 if (is == null) { 81 throw new PluginException( 82 "Resource " + resourceName + " not found."); 83 } 84 85 return loadRules(d, pluginClass, is, resourceName); 86 } 87 88 99 public static RuleLoader loadRules(Digester d, Class pluginClass, 100 InputStream is, String resourceName) 101 throws PluginException { 102 103 try { 104 RuleLoader loader = new LoaderFromStream(is); 105 return loader; 106 } catch(Exception e) { 107 throw new PluginException( 108 "Unable to load xmlrules from resource [" + 109 resourceName + "]", e); 110 } finally { 111 try { 112 is.close(); 113 } catch(java.io.IOException ioe) { 114 throw new PluginException( 115 "Unable to close stream for resource [" + 116 resourceName + "]", ioe); 117 } 118 } 119 } 120 } 121 122 | Popular Tags |