1 5 6 package com.hp.hpl.jena.util; 7 8 import java.io.* ; 9 import java.util.* ; 10 11 import org.apache.commons.logging.*; 12 14 import com.hp.hpl.jena.rdf.model.* ; 15 import com.hp.hpl.jena.shared.*; 16 17 45 46 public class FileManager 47 { 48 49 public static final String PATH_DELIMITER = ";"; 50 public static final String filePathSeparator = java.io.File.separator ; 51 static Log log = LogFactory.getLog(FileManager.class) ; 52 53 static FileManager instance = null ; 54 55 static boolean logAllLookups = true ; 56 List handlers = new ArrayList() ; 57 LocationMapper mapper = null ; 58 boolean cacheModelLoads = false ; 59 Map modelCache = null ; 60 61 62 65 public static FileManager get() 66 { 67 if ( instance == null ) 69 instance = makeGlobal() ; 70 return instance ; 71 } 72 73 74 public FileManager() {} 75 76 77 private static FileManager makeGlobal() 78 { 79 FileManager fMgr = new FileManager(LocationMapper.get()) ; 80 fMgr.addLocatorFile() ; 81 fMgr.addLocatorURL() ; 82 fMgr.addLocatorClassLoader(fMgr.getClass().getClassLoader()) ; 83 return fMgr ; 84 } 85 86 87 public FileManager(LocationMapper _mapper) 88 { 89 setMapper(_mapper) ; 90 } 91 92 93 public void setMapper(LocationMapper _mapper) 94 { 95 mapper = _mapper ; 96 } 97 98 99 100 public Iterator locators() { return handlers.listIterator() ; } 101 102 103 public void addLocator(Locator loc) 104 { 105 log.debug("Add location: "+loc.getName()) ; 106 handlers.add(loc) ; } 107 108 109 public void addLocatorFile() { addLocatorFile(null) ; } 110 111 112 public void addLocatorFile(String dir) 113 { 114 LocatorFile fLoc = new LocatorFile(dir) ; 115 addLocator(fLoc) ; 116 } 117 118 119 public void addLocatorClassLoader(ClassLoader cLoad) 120 { 121 LocatorClassLoader cLoc = new LocatorClassLoader(cLoad) ; 122 addLocator(cLoc) ; 123 } 124 125 126 public void addLocatorURL() 127 { 128 Locator loc = new LocatorURL() ; 129 addLocator(loc) ; 130 } 131 132 133 public void addLocatorZip(String zfn) 134 { 135 Locator loc = new LocatorZip(zfn) ; 136 addLocator(loc) ; 137 } 138 139 140 141 public void remove(Locator loc) { handlers.remove(loc) ; } 142 143 144 public void resetCache() 145 { 146 if ( modelCache != null ) 147 { 148 for ( Iterator iter = modelCache.keySet().iterator() ; iter.hasNext() ; ) 149 { 150 String name = (String )iter.next() ; 151 Model m = (Model)modelCache.remove(name) ; 152 if ( m != null ) 153 m.close() ; 154 } 155 modelCache = new HashMap() ; 156 } 157 } 158 159 160 161 public void setModelCaching(boolean state) 162 { 163 cacheModelLoads = state ; 164 if ( cacheModelLoads && modelCache == null ) 165 modelCache = new HashMap() ; 166 } 167 168 175 176 public Model loadModel(String filenameOrURI) 177 { return loadModel(filenameOrURI, null) ; } 178 179 187 188 public Model loadModel(String filenameOrURI, String rdfSyntax) 189 { 190 return loadModel(filenameOrURI, null, rdfSyntax) ; 191 } 192 193 201 202 public Model loadModel(String filenameOrURI, String baseURI, String rdfSyntax) 203 { 204 if ( modelCache != null && modelCache.containsKey(filenameOrURI) ) 205 { 206 log.debug("Model cache hit: "+filenameOrURI) ; 207 return (Model)modelCache.get(filenameOrURI) ; 208 } 209 210 Model m = ModelFactory.createDefaultModel() ; 211 readModel(m, filenameOrURI, baseURI, rdfSyntax) ; 212 213 if ( this.cacheModelLoads ) 214 { 215 if ( modelCache == null ) 216 modelCache = new HashMap() ; 217 modelCache.put(filenameOrURI, m) ; 218 } 219 return m ; 220 } 221 222 229 230 public Model readModel(Model model, String filenameOrURI) 231 { return readModel(model, filenameOrURI, null); } 232 233 241 242 public Model readModel(Model model, String filenameOrURI, String rdfSyntax) 243 { 244 return readModel(model, filenameOrURI, null, rdfSyntax); 245 } 246 247 256 257 public Model readModel(Model model, String filenameOrURI, String baseURI, String syntax) 258 { 259 if ( baseURI == null ) 260 baseURI = chooseBaseURI(filenameOrURI) ; 261 262 String mappedURI = remap(filenameOrURI) ; 263 264 if ( syntax == null ) 265 { 266 syntax = FileUtils.guessLang(mappedURI) ; 267 if ( syntax == null || syntax.equals("") ) 268 syntax = FileUtils.langXML ; 269 if ( log.isDebugEnabled() ) 270 log.debug("Syntax guess: "+syntax); 271 } 272 273 InputStream in = openNoMap(mappedURI) ; 274 if ( in == null ) 275 { 276 if ( log.isTraceEnabled() ) 277 log.trace("Failed to locate '"+mappedURI+"'") ; 278 throw new NotFoundException("Not found: "+filenameOrURI) ; 279 } 280 model.read(in, baseURI, syntax) ; 281 return model ; 282 } 283 284 private static String chooseBaseURI(String baseURI) 286 { 287 String scheme = FileUtils.getScheme(baseURI) ; 288 289 if ( scheme != null ) 290 { 291 if ( scheme.equals("file") ) 292 { 293 if ( ! baseURI.startsWith("file:///") ) 294 { 295 try { 296 String tmp = baseURI.substring("file:".length()) ; 298 File f = new File(tmp) ; 299 baseURI = "file:///"+f.getCanonicalPath() ; 300 baseURI = baseURI.replace('\\','/') ; 301 } catch (Exception ex) {} 306 } 307 } 308 return baseURI ; 309 } 310 311 if ( baseURI.startsWith("/") ) 312 return "file://"+baseURI ; 313 return "file:"+baseURI ; 314 } 315 316 317 public InputStream open(String filenameOrURI) 318 { 319 if ( log.isDebugEnabled()) 320 log.debug("open("+filenameOrURI+")") ; 321 322 String uri = remap(filenameOrURI) ; 323 324 return openNoMap(uri) ; 325 } 326 327 328 329 public String remap(String filenameOrURI) 330 { 331 if ( mapper == null ) 332 return filenameOrURI ; 333 334 String uri = mapper.altMapping(filenameOrURI, null) ; 335 336 if ( uri == null ) 337 { 338 if ( FileManager.logAllLookups && log.isDebugEnabled() ) 339 log.debug("Not mapped: "+filenameOrURI) ; 340 uri = filenameOrURI ; 341 } 342 else 343 { 344 if ( log.isDebugEnabled() ) 345 log.debug("Mapped: "+filenameOrURI+" => "+uri) ; 346 } 347 return uri ; 348 } 349 350 351 public String readWholeFileAsUTF8(InputStream in) 352 { 353 try { 354 Reader r = FileUtils.asBufferedUTF8(in) ; 355 StringWriter sw = new StringWriter(1024); 356 char buff[] = new char[1024]; 357 while (r.ready()) { 358 int l = r.read(buff); 359 if (l <= 0) 360 break; 361 sw.write(buff, 0, l); 362 } 363 r.close(); 364 sw.close(); 365 return sw.toString(); 366 } catch (IOException ex) 367 { 368 throw new WrappedIOException(ex) ; 369 } 370 } 371 372 373 public String readWholeFileAsUTF8(String filename) 374 { 375 InputStream in = open(filename) ; 376 if ( in == null ) 377 throw new NotFoundException("File not found: "+filename) ; 378 return readWholeFileAsUTF8(in) ; 379 } 380 381 383 public InputStream openNoMap(String filenameOrURI) 384 { 385 for ( Iterator iter = handlers.iterator() ; iter.hasNext() ; ) 386 { 387 Locator loc = (Locator)iter.next() ; 388 InputStream in = loc.open(filenameOrURI) ; 389 if ( in != null ) 390 { 391 if ( log.isDebugEnabled() ) 392 log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ; 393 return in ; 394 } 395 } 396 return null; 397 } 398 } 399 400 401 402 428 429 | Popular Tags |