1 5 6 package com.hp.hpl.jena.util; 7 8 import java.io.*; 9 10 import java.net.URL ; 11 import java.nio.charset.Charset ; 12 13 import com.hp.hpl.jena.shared.JenaException; 14 import com.hp.hpl.jena.shared.WrappedIOException; 15 import com.hp.hpl.jena.JenaRuntime ; 16 17 public class FileUtils 18 { 19 public static final String langXML = "RDF/XML" ; 20 public static final String langXMLAbbrev = "RDF/XML-ABBREV" ; 21 public static final String langNTriple = "N-TRIPLE" ; 22 public static final String langN3 = "N3" ; 23 public static final String langTurtle = "TURTLE" ; 24 public static final String langBDB = "RDF/BDB" ; 26 public static final String langSQL = "RDF/SQL" ; 27 28 static Charset utf8 = null ; 29 static { 30 try { 31 utf8 = Charset.forName("utf-8") ; 32 } catch (Throwable ex) {} 33 } 34 35 36 37 38 static public Reader asUTF8(InputStream in) { 39 if ( JenaRuntime.runUnder(JenaRuntime.featureNoCharset) ) 40 return new InputStreamReader(in) ; 41 return new InputStreamReader(in, utf8); 42 } 43 44 45 46 static public BufferedReader asBufferedUTF8(InputStream in) { 47 return new BufferedReader(asUTF8(in)) ; 48 } 49 50 51 52 static public Writer asUTF8(OutputStream out) { 53 if ( JenaRuntime.runUnder(JenaRuntime.featureNoCharset) ) 54 return new OutputStreamWriter(out) ; 55 return new OutputStreamWriter(out, utf8); 56 } 57 58 59 60 static public PrintWriter asPrintWriterUTF8(OutputStream out) { 61 return new PrintWriter(asUTF8(out)); 62 } 63 64 77 78 public static String guessLang( String name, String otherwise ) 79 { 80 if ( name.startsWith("jdbc:") || name.startsWith("JDBC:") ) 81 return langSQL ; 82 83 String suffix = getFilenameExt( name ); 84 if (suffix.equals( "n3" )) return langN3; 85 if (suffix.equals( "nt" )) return langNTriple; 86 if (suffix.equals( "ttl" )) return langTurtle ; 87 if (suffix.equals( "rdf" )) return langXML; 88 if (suffix.equals( "owl" )) return langXML; 89 return otherwise; 90 } 91 92 93 105 106 public static String guessLang(String urlStr) 107 { 108 return guessLang(urlStr, langXML) ; 109 } 110 111 112 113 public static String toFilename(String filenameOrURI) 114 { 115 if ( !isFile(filenameOrURI) ) 116 return null ; 117 118 String fn = filenameOrURI ; 119 if ( fn.startsWith("file://localhost/") ) 120 return fn.substring("file://localhost".length()) ; 122 123 if ( fn.startsWith("file:") ) 124 return fn.substring("file:".length()) ; 125 126 return fn ; 127 } 128 129 134 public static boolean isFile(String name) 135 { 136 String scheme = getScheme(name) ; 137 138 if ( scheme == null ) 139 return true ; 141 142 if ( scheme.equals("file") ) 143 return true ; 145 146 if ( scheme.length() == 1 ) 148 return true ; 150 151 return false ; 152 153 } 155 156 161 public static boolean isURI(String name) 162 { 163 167 return (getScheme(name) != null) ; 168 } 169 170 public static String getScheme(String uri) 171 { 172 for ( int i = 0 ; i < uri.length() ; i++ ) 174 { 175 char ch = uri.charAt(i) ; 176 if ( ch == ':' ) 177 return uri.substring(0,i) ; 178 if ( ! isASCIILetter(ch) ) 179 break ; 181 } 182 return null ; 183 } 184 185 private static boolean isASCIILetter(char ch) 186 { 187 return ( ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z' ) ; 188 } 189 190 195 public static String getDirname(String filename) 196 { 197 File f = new File(filename) ; 198 return f.getParent() ; 199 } 200 201 206 public static String getBasename(String filename) 207 { 208 File f = new File(filename) ; 209 return f.getName() ; 210 } 211 212 215 public static String getFilenameExt( String filename) 216 { 217 int iSlash = filename.lastIndexOf( '/' ); 218 int iBack = filename.lastIndexOf( '\\' ); 219 int iExt = filename.lastIndexOf( '.' ); 220 if (iBack > iSlash) iSlash = iBack; 221 return iExt > iSlash ? filename.substring( iExt+1 ).toLowerCase() : ""; 222 } 223 224 233 public static File tempFileName( String prefix, String suffix ) 234 { 235 File result = new File( getTempDirectory(), prefix + randomNumber() + suffix ); 236 if (result.exists()) return tempFileName( prefix, suffix ); 237 result.deleteOnExit(); 238 return result; 239 } 240 241 249 public static File getScratchDirectory( String prefix ) 250 { 251 File result = new File( getTempDirectory(), prefix + randomNumber() ); 252 if (result.exists()) return getScratchDirectory( prefix ); 253 if (result.mkdir() == false) throw new JenaException( "mkdir failed on " + result ); 254 result.deleteOnExit(); 255 return result; 256 } 257 258 public static String getTempDirectory() 259 { return System.getProperty( "java.io.tmpdir" ); } 260 261 private static int counter = 0; 262 263 private static int randomNumber() 264 { 265 return ++counter; 266 } 267 268 273 public static BufferedReader openResourceFile( String filename ) 274 { 275 try 276 { 277 InputStream is = FileUtils.openResourceFileAsStream( filename ); 278 return new BufferedReader(new InputStreamReader(is, "UTF-8")); 279 } 280 catch (IOException e) 281 { throw new WrappedIOException( e ); } 282 } 283 284 287 public static InputStream openResourceFileAsStream(String filename) 288 throws FileNotFoundException { 289 InputStream is = ClassLoader.getSystemResourceAsStream(filename); 290 if (is == null) { 291 is = FileUtils.class.getResourceAsStream("/" + filename); 293 if (is == null) { 294 is = new FileInputStream(filename); 296 } 297 } 298 return is; 299 } 300 301 public static BufferedReader readerFromURL( String urlStr ) 303 { 304 try { return asBufferedUTF8( new URL (urlStr).openStream() ); } 305 catch (java.net.MalformedURLException e) 306 { try { return asBufferedUTF8( new FileInputStream( urlStr ) ); } 308 catch (FileNotFoundException f) { throw new WrappedIOException( f ); } 309 } 310 catch (IOException e) 311 { throw new WrappedIOException( e ); } 312 } 313 314 319 320 public static String readWholeFileAsUTF8(String filename) throws IOException { 321 InputStream in = new FileInputStream(filename) ; 322 return readWholeFileAsUTF8(in) ; 323 } 324 325 331 public static String readWholeFileAsUTF8(InputStream in) throws IOException 332 { 333 Reader r = new BufferedReader(asUTF8(in),1024) ; 334 return readWholeFileAsUTF8(r) ; 335 } 336 337 343 344 private static String readWholeFileAsUTF8(Reader r) throws IOException 346 { 347 StringWriter sw = new StringWriter(1024); 348 char buff[] = new char[1024]; 349 while (r.ready()) { 350 int l = r.read(buff); 351 if (l <= 0) 352 break; 353 sw.write(buff, 0, l); 354 } 355 r.close(); 356 sw.close(); 357 return sw.toString(); 358 } 359 360 } 361 362 388 | Popular Tags |