1 4 package net.sourceforge.pmd.util; 5 6 import net.sourceforge.pmd.RuleSetNotFoundException; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.FileNotFoundException ; 11 import java.io.InputStream ; 12 import java.net.URL ; 13 14 public class ResourceLoader { 15 16 private ResourceLoader() { 18 } 19 20 25 public static InputStream loadResourceAsStream(String name) throws RuleSetNotFoundException { 26 InputStream stream = ResourceLoader.loadResourceAsStream(name, ResourceLoader.class.getClassLoader()); 27 if (stream == null) { 28 throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH"); 29 } 30 return stream; 31 } 32 33 37 public static InputStream loadResourceAsStream(String name, ClassLoader loader) throws RuleSetNotFoundException { 38 File file = new File (name); 39 if (file.exists()) { 40 try { 41 return new FileInputStream (file); 42 } catch (FileNotFoundException e) { 43 } 45 } else { 46 try { 47 return new URL (name).openConnection().getInputStream(); 48 } catch (Exception e) { 49 return loader.getResourceAsStream(name); 50 } 51 } 52 throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH"); 53 } 54 } 55 | Popular Tags |