1 package org.sapia.soto.util; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 7 import java.net.URL ; 8 9 25 public class ClasspathResourceHandler implements ResourceHandler, Schemes { 26 27 30 public ClasspathResourceHandler() { 31 super(); 32 } 33 34 37 public Resource getResourceObject(String uri) throws IOException { 38 String path = Utils.chopScheme(uri.replace('\\', '/')); 39 40 if (path.charAt(0) == '/') { 41 path = path.substring(1); 42 } 43 44 URL url = null; 45 46 url = getClass().getClassLoader().getResource(path); 47 48 if (url == null) { 49 url = Thread.currentThread().getContextClassLoader().getResource(path); 50 } 51 52 if (url == null) { 53 url = ClassLoader.getSystemResource(path); 54 } 55 56 if (url == null) { 57 throw new FileNotFoundException (path); 58 } 59 60 return new UrlResource(url); 61 } 62 63 66 public InputStream getResource(String uri) throws IOException { 67 return getResourceObject(uri).getInputStream(); 68 } 69 70 73 public boolean accepts(String uri) { 74 String scheme = Utils.getScheme(uri); 75 if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_RESOURCE)){ 76 return true; 77 } 78 return false; 79 } 80 81 } 82 | Popular Tags |