1 package org.sapia.soto.util; 2 3 import java.io.File ; 4 import java.io.FileNotFoundException ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 8 9 33 public class FileResourceHandler implements ResourceHandler, Schemes { 34 35 private boolean _fallBackToClasspath = true; 36 private String _basedir; 37 38 41 public Resource getResourceObject(String uri) throws IOException { 42 String fileUri = mangleUri(uri); 43 File f = getFile(fileUri); 44 45 if (f.exists()) { 46 return new FileResource(f); 47 } else if (_fallBackToClasspath) { 48 return new ClasspathResourceHandler().getResourceObject(uri); 49 } else { 50 throw new FileNotFoundException (uri); 51 } 52 } 53 54 58 public File getFile(String uri) { 59 uri = mangleUri(Utils.chopScheme(uri)); 60 61 return new File (uri); 62 } 63 64 67 public InputStream getResource(String uri) throws IOException { 68 String fileUri = mangleUri(uri); 69 File f = getFile(fileUri); 70 71 if (f.exists()) { 72 return f.toURL().openStream(); 73 } else if (_fallBackToClasspath) { 74 return new ClasspathResourceHandler().getResourceObject(uri) 75 .getInputStream(); 76 } else { 77 throw new FileNotFoundException (uri); 78 } 79 } 80 81 85 public void setFallBackToClasspath(boolean fallback) { 86 _fallBackToClasspath = fallback; 87 } 88 89 93 public void setBasedir(String basedir) { 94 _basedir = basedir; 95 } 96 97 100 public boolean accepts(String uri) { 101 String scheme = Utils.getScheme(uri); 102 if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_FILE)){ 103 return true; 104 } 105 return false; 106 } 107 108 109 private String mangleUri(String uri) { 110 if ((_basedir != null) && Utils.isRelativePath(uri)) { 111 return Utils.getRelativePath(_basedir, uri, false); 112 } else { 113 return uri; 114 } 115 } 116 } 117 | Popular Tags |