1 6 7 package com.hp.hpl.jena.util; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 14 import org.apache.commons.logging.*; 15 16 23 24 public class LocatorFile implements Locator 25 { 26 static Log log = LogFactory.getLog(LocatorFile.class) ; 27 private String altDir = null ; 28 private String altDirLogStr = "" ; 29 30 LocatorFile(String dir) 31 { 32 if ( false ) 33 { 34 if ( dir == null ) 35 { 36 try { 37 String wd = new File (".").getCanonicalPath() ; 39 log.debug("Base file directory: "+wd) ; 40 } catch (IOException ex) 41 { 42 log.error("Failed to discover the working directory", ex) ; 43 } 44 return ; 45 } 46 else 47 { 48 log.debug("Base file directory: "+dir) ; 49 } 50 } 51 if ( dir != null ) 52 { 53 if ( dir.endsWith("/") || dir.endsWith(java.io.File.separator) ) 54 dir = dir.substring(0,dir.length()-1) ; 55 altDirLogStr = " ["+dir+"]" ; 56 } 57 altDir = dir ; 58 } 59 60 LocatorFile() 61 { 62 this(null) ; 63 } 64 65 private File toFile(String filenameOrURI) 66 { 67 String fn = FileUtils.toFilename(filenameOrURI) ; 68 if ( fn == null ) 69 return null ; 70 71 if ( altDir != null && ! fn.startsWith("/") && ! fn.startsWith(FileManager.filePathSeparator) ) 72 fn = altDir+java.io.File.separator+fn ; 73 74 return new File (fn) ; 75 } 76 77 78 public boolean exists(String filenameOrURI) 79 { 80 File f = toFile(filenameOrURI) ; 81 82 if ( f == null ) 83 return false ; 84 85 return f.exists() ; 86 } 87 88 public InputStream open(String filenameOrURI) 89 { 90 File f = toFile(filenameOrURI) ; 91 92 if ( f == null || !f.exists() ) 93 { 94 if ( FileManager.logAllLookups && log.isTraceEnabled()) 95 log.trace("Not found: "+filenameOrURI+altDirLogStr) ; 96 return null ; 97 } 98 99 try { 100 InputStream in = new FileInputStream (f) ; 101 if ( in == null ) 102 { 103 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 105 log.trace("LocatorFile: Failed to open: "+filenameOrURI+altDirLogStr) ; 106 return null ; 107 } 108 109 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 110 log.trace("Found: "+filenameOrURI+altDirLogStr) ; 111 112 113 return in ; 117 } catch (IOException ioEx) 118 { 119 log.warn("File unreadable (but exists): "+f.getPath()+" Exception: "+ioEx.getMessage()) ; 122 return null ; 123 } 124 } 125 public String getName() 126 { 127 String tmp = "LocatorFile" ; 128 if ( altDir != null ) 129 tmp = tmp+"("+altDir+")" ; 130 return tmp ; 131 } 132 } 133 | Popular Tags |