📄 filemanager.java
字号:
/*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
*/
package com.hp.hpl.jena.util;
import java.io.* ;
import java.util.* ;
import org.apache.commons.logging.*;
//import javax.servlet.* ;
import com.hp.hpl.jena.rdf.model.* ;
import com.hp.hpl.jena.shared.*;
/** FileManager
*
* A FileManager provides access to named file-like resources by opening
* InputStreams to things in the filing system, by URL (http: and file:) and
* found by the classloader. It can also load RDF data from such a system
* resource into an existing model or create a new (Memory-based) model.
* There is a global FileManager which provide uniform access to system
* resources: applications may also create specialised FileManagers.
*
* A FileManager contains a list of location functions to try: the global
* FileManger has one {@link LocatorFile}, one {@link LocatorClassLoader} and
* one {@link LocatorURL}
*
* Main operations:
* * <ul>
* <li>loadModel, readModel : URI to model</li>
* <li>open, openNoMap : URI to input stream</li>
* <li>mapURI : map URI to another by {@link LocationMapper}</li>
* </ul>
*
* Utilities:
* <ul>
* <li>readWholeFileAsUTF8</li>
* <li>optional caching of models<li>
* </ul>
*
* A FileManager works in conjunction with a LocationMapper.
* A {@link LocationMapper} is a set of alternative locations for system
* resources and a set of alternative prefix locations. For example, a local
* copy of a common RDF dataset may be used whenever the usual URL is used by
* the application.
*
* The {@link LocatorFile} also supports the idea of "current directory".
*
* @see LocationMapper
* @see FileUtils
*
*
* @author Andy Seaborne
* @version $Id: FileManager.java,v 1.39 2007/06/07 12:56:29 andy_seaborne Exp $
*/
public class FileManager
{
/** Delimiter between path entries : because URI scheme names use : we only allow ; */
public static final String PATH_DELIMITER = ";";
public static final String filePathSeparator = java.io.File.separator ;
private static Log log = LogFactory.getLog(FileManager.class) ;
static FileManager instance = null ;
static boolean logAllLookups = true ;
List handlers = new ArrayList() ;
LocationMapper mapper = null ;
boolean cacheModelLoads = false ;
ModelCache modelCache = null ;
/** Get the global file manager.
* @return the global file manager
*/
public static FileManager get()
{
// Singleton pattern adopted in case we later have several file managers.
if ( instance == null )
instance = makeGlobal() ;
return instance ;
}
/** Set the global file manager (as returned by get())
* If called before any call to get(), then the usual default filemanager is not created
* @param globalFileManager
*/
public static void setGlobalFileManager(FileManager globalFileManager)
{
instance = globalFileManager ;
}
/** Create an uninitialized FileManager */
public FileManager() {}
/** Create a new file manager that is a deep copy another.
* Location mapper and locators chain are copied (the locators are not cloned).
* The model cache is not copied and is initially set to not cache.
* @param filemanager
*/
public FileManager(FileManager filemanager)
{
handlers.addAll(filemanager.handlers) ;
mapper = null ;
if ( filemanager.getLocationMapper() != null )
mapper = new LocationMapper(filemanager.getLocationMapper()) ;
cacheModelLoads = false ;
modelCache = null ;
}
/** Create a "standard" FileManager. */
public static FileManager makeGlobal()
{
FileManager fMgr = new FileManager(LocationMapper.get()) ;
setStdLocators(fMgr) ;
return fMgr ;
}
/** Force a file handler to have the default configuration. */
public static void setStdLocators(FileManager fMgr)
{
fMgr.handlers.clear() ;
fMgr.addLocatorFile() ;
fMgr.addLocatorURL() ;
fMgr.addLocatorClassLoader(fMgr.getClass().getClassLoader()) ;
}
/** Create with the given location mapper */
public FileManager(LocationMapper _mapper) { setLocationMapper(_mapper) ; }
/** @deprecated Use setLocationMapper */
public void setMapper(LocationMapper _mapper) { setLocationMapper(_mapper) ; }
/** Set the location mapping */
public void setLocationMapper(LocationMapper _mapper) { mapper = _mapper ; }
/** Get the location mapping */
public LocationMapper getLocationMapper() { return mapper ; }
/** Return an iterator over all the handlers */
public Iterator locators() { return handlers.listIterator() ; }
/** Add a locator to the end of the locators list */
public void addLocator(Locator loc)
{
log.debug("Add location: "+loc.getName()) ;
handlers.add(loc) ; }
/** Add a file locator */
public void addLocatorFile() { addLocatorFile(null) ; }
/** Add a file locator which uses dir as its working directory */
public void addLocatorFile(String dir)
{
LocatorFile fLoc = new LocatorFile(dir) ;
addLocator(fLoc) ;
}
/** Add a class loader locator */
public void addLocatorClassLoader(ClassLoader cLoad)
{
LocatorClassLoader cLoc = new LocatorClassLoader(cLoad) ;
addLocator(cLoc) ;
}
/** Add a URL locator */
public void addLocatorURL()
{
Locator loc = new LocatorURL() ;
addLocator(loc) ;
}
/** Add a zip file locator */
public void addLocatorZip(String zfn)
{
Locator loc = new LocatorZip(zfn) ;
addLocator(loc) ;
}
/** Remove a locator */
public void remove(Locator loc) { handlers.remove(loc) ; }
// -------- Cache operations
/** Reset the model cache */
public void resetCache()
{
if ( modelCache != null )
modelCache.reset() ;
}
/** Change the state of model cache : does not clear the cache */
public void setModelCaching(boolean state)
{
cacheModelLoads = state ;
if ( cacheModelLoads && modelCache == null )
modelCache = new ModelCache() ;
}
/** return whether caching is on of off */
public boolean getCachingModels() { return cacheModelLoads ; }
/** Read out of the cache - return null if not in the cache */
public Model getFromCache(String filenameOrURI)
{
if ( ! getCachingModels() )
return null;
return modelCache.get(filenameOrURI) ;
}
public boolean hasCachedModel(String filenameOrURI)
{
if ( ! getCachingModels() )
return false ;
return modelCache.contains(filenameOrURI) ;
}
public void addCacheModel(String uri, Model m)
{
if ( getCachingModels() )
modelCache.put(uri, m) ;
}
public void removeCacheModel(String uri)
{
if ( getCachingModels() )
modelCache.remove(uri) ;
}
// -------- Cache operations (end)
/** Load a model from a file (local or remote).
* Guesses the syntax of the file based on filename extension,
* defaulting to RDF/XML.
* @param filenameOrURI The filename or a URI (file:, http:)
* @return a new model
* @exception JenaException if there is syntax error in file.
*/
public Model loadModel(String filenameOrURI)
{
if ( log.isDebugEnabled() )
log.debug("loadModel("+filenameOrURI+")") ;
return loadModelWorker(filenameOrURI, null, null) ;
}
/** Load a model from a file (local or remote).
* URI is the base for reading the model.
*
* @param filenameOrURI The filename or a URI (file:, http:)
* @param rdfSyntax RDF Serialization syntax.
* @return a new model
* @exception JenaException if there is syntax error in file.
*/
public Model loadModel(String filenameOrURI, String rdfSyntax)
{
if ( log.isDebugEnabled() )
log.debug("loadModel("+filenameOrURI+", "+rdfSyntax+")") ;
return loadModelWorker(filenameOrURI, null, rdfSyntax) ;
}
/** Load a model from a file (local or remote).
*
* @param filenameOrURI The filename or a URI (file:, http:)
* @param baseURI Base URI for loading the RDF model.
* @param rdfSyntax RDF Serialization syntax.
* @return a new model
* @exception JenaException if there is syntax error in file.
*/
public Model loadModel(String filenameOrURI, String baseURI, String rdfSyntax)
{
if ( log.isDebugEnabled() )
log.debug("loadModel("+filenameOrURI+", "+baseURI+", "+rdfSyntax+")") ;
return loadModelWorker(filenameOrURI, baseURI, rdfSyntax) ;
}
private Model loadModelWorker(String filenameOrURI, String baseURI, String rdfSyntax)
{
// Better: if ( hasCachedModel(filenameOrURI) ) return getFromCache(filenameOrURI) ;
if ( modelCache != null && modelCache.contains(filenameOrURI) )
{
if ( log.isDebugEnabled() )
log.debug("Model cache hit: "+filenameOrURI) ;
return modelCache.get(filenameOrURI) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -