1 package org.sapia.resource; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.URL ; 6 7 import com.sun.org.apache.xerces.internal.util.URI; 8 9 22 public class ClasspathResourceHandler implements ResourceHandler, Schemes { 23 24 public ClasspathResourceHandler() { 25 super(); 26 } 27 28 public Resource getResourceObject(String uri) throws IOException { 29 URI uriObj = null; 30 if(Utils.hasScheme(uri)){ 31 uriObj = new URI(uri); 32 } 33 else{ 34 uriObj = new URI(Schemes.SCHEME_RESOURCE+":"+uri); 35 } 36 37 String path = uriObj.getPath(); 38 39 if(path.charAt(0) == '/') { 40 path = path.substring(1); 41 } 42 43 URL url = null; 44 45 url = getClass().getClassLoader().getResource(path); 46 47 if(url == null) { 48 url = Thread.currentThread().getContextClassLoader().getResource(path); 49 } 50 51 if(url == null) { 52 url = ClassLoader.getSystemResource(path); 53 } 54 55 if(url == null) { 56 throw new ResourceNotFoundException(path); 57 } 58 59 return new UrlResource(url); 60 } 61 62 public InputStream getResource(String uri) throws IOException { 63 return getResourceObject(uri).getInputStream(); 64 } 65 66 public boolean accepts(String uri) { 67 return doAccepts(Utils.getScheme(uri)); 68 } 69 70 public boolean accepts(java.net.URI uri) { 71 return doAccepts(uri.getScheme()); 72 } 73 74 private boolean doAccepts(String scheme){ 75 if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_RESOURCE)) { 76 return true; 77 } 78 return false; 79 } 80 81 } 82 | Popular Tags |