📄 filemanager.java
字号:
}
Model m = ModelFactory.createDefaultModel() ;
readModelWorker(m, filenameOrURI, baseURI, rdfSyntax) ;
if ( this.cacheModelLoads )
modelCache.put(filenameOrURI, m) ;
return m ;
}
/**
* Read a file of RDF into a model.
* @param model
* @param filenameOrURI
* @return The model or null, if there was an error.
* @exception JenaException if there is syntax error in file.
*/
public Model readModel(Model model, String filenameOrURI)
{
if ( log.isDebugEnabled() )
log.debug("readModel(model,"+filenameOrURI+")") ;
return readModel(model, filenameOrURI, null);
}
/**
* Read a file of RDF into a model.
* @param model
* @param filenameOrURI
* @param rdfSyntax RDF Serialization syntax.
* @return The model or null, if there was an error.
* @exception JenaException if there is syntax error in file.
*/
public Model readModel(Model model, String filenameOrURI, String rdfSyntax)
{
if ( log.isDebugEnabled() )
log.debug("readModel(model,"+filenameOrURI+", "+rdfSyntax+")") ;
return readModelWorker(model, filenameOrURI, null, rdfSyntax);
}
/**
* Read a file of RDF into a model.
* @param model
* @param filenameOrURI
* @param baseURI
* @param syntax
* @return The model
* @exception JenaException if there is syntax error in file.
*/
public Model readModel(Model model, String filenameOrURI, String baseURI, String syntax)
{
if ( log.isDebugEnabled() )
log.debug("readModel(model,"+filenameOrURI+", "+baseURI+", "+syntax+")") ;
return readModelWorker(model, filenameOrURI, baseURI, syntax) ;
}
private Model readModelWorker(Model model, String filenameOrURI, String baseURI, String syntax)
{
if ( baseURI == null )
baseURI = chooseBaseURI(filenameOrURI) ;
// Doesn't call open() - we want to make the synatx guess based on the mapped URI.
String mappedURI = mapURI(filenameOrURI) ;
if ( log.isDebugEnabled() && ! mappedURI.equals(filenameOrURI) )
log.debug("Map: "+filenameOrURI+" => "+mappedURI) ;
if ( syntax == null )
{
syntax = FileUtils.guessLang(mappedURI) ;
if ( syntax == null || syntax.equals("") )
syntax = FileUtils.langXML ;
if ( log.isDebugEnabled() )
log.debug("Syntax guess: "+syntax);
}
InputStream in = openNoMapOrNull(mappedURI) ;
if ( in == null )
{
if ( log.isDebugEnabled() )
log.debug("Failed to locate '"+mappedURI+"'") ;
throw new NotFoundException("Not found: "+filenameOrURI) ;
}
model.read(in, baseURI, syntax) ;
try { in.close(); } catch (IOException ex) {}
return model ;
}
private static String chooseBaseURI(String baseURI)
{
String scheme = FileUtils.getScheme(baseURI) ;
if ( scheme != null )
{
if ( scheme.equals("file") )
{
if ( ! baseURI.startsWith("file:///") )
{
try {
// Fix up file URIs. Yuk.
String tmp = baseURI.substring("file:".length()) ;
File f = new File(tmp) ;
baseURI = "file:///"+f.getCanonicalPath() ;
baseURI = baseURI.replace('\\','/') ;
// baseURI = baseURI.replace(" ","%20");
// baseURI = baseURI.replace("~","%7E");
// Convert to URI. Except that it removes ///
// Could do that and fix up (again)
//java.net.URL u = new java.net.URL(baseURI) ;
//baseURI = u.toExternalForm() ;
} catch (Exception ex) {}
}
}
return baseURI ;
}
if ( baseURI.startsWith("/") )
return "file://"+baseURI ;
return "file:"+baseURI ;
}
/** Open a file using the locators of this FileManager */
public InputStream open(String filenameOrURI)
{
if ( log.isDebugEnabled())
log.debug("open("+filenameOrURI+")") ;
String uri = mapURI(filenameOrURI) ;
if ( log.isDebugEnabled() && ! uri.equals(filenameOrURI) )
log.debug("open: mapped to "+uri) ;
return openNoMap(uri) ;
}
/** @deprecated Use mapURI */
public String remap(String filenameOrURI)
{ return mapURI(filenameOrURI) ; }
/** Apply the mapping of a filename or URI */
public String mapURI(String filenameOrURI)
{
if ( mapper == null )
return filenameOrURI ;
String uri = mapper.altMapping(filenameOrURI, null) ;
if ( uri == null )
{
if ( FileManager.logAllLookups && log.isDebugEnabled() )
log.debug("Not mapped: "+filenameOrURI) ;
uri = filenameOrURI ;
}
else
{
if ( log.isDebugEnabled() )
log.debug("Mapped: "+filenameOrURI+" => "+uri) ;
}
return uri ;
}
/** Slurp up a whole file */
public String readWholeFileAsUTF8(InputStream in)
{
try {
Reader r = FileUtils.asBufferedUTF8(in) ;
StringWriter sw = new StringWriter(1024);
char buff[] = new char[1024];
while (r.ready()) {
int l = r.read(buff);
if (l <= 0)
break;
sw.write(buff, 0, l);
}
r.close();
sw.close();
return sw.toString();
} catch (IOException ex)
{
throw new WrappedIOException(ex) ;
}
}
/** Slurp up a whole file: map filename as necessary */
public String readWholeFileAsUTF8(String filename)
{
InputStream in = open(filename) ;
if ( in == null )
throw new NotFoundException("File not found: "+filename) ;
return readWholeFileAsUTF8(in) ;
}
/** Open a file using the locators of this FileManager
* but without location mapping */
public InputStream openNoMap(String filenameOrURI)
{
InputStream in = openNoMapOrNull(filenameOrURI) ;
// if ( in == null )
// throw new NotFoundException(filenameOrURI) ;
return in ;
}
/** Open a file using the locators of this FileManager
* but without location mapping.
* Return null if not found
*/
public InputStream openNoMapOrNull(String filenameOrURI)
{
for ( Iterator iter = handlers.iterator() ; iter.hasNext() ; )
{
Locator loc = (Locator)iter.next() ;
InputStream in = loc.open(filenameOrURI) ;
if ( in != null )
{
if ( log.isDebugEnabled() )
log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
return in ;
}
}
return null;
}
}
class ModelCache
{
Map modelCache = new HashMap() ;
ModelCache() {}
/** Reset the model cache */
public void reset()
{
if ( modelCache != null )
{
// for ( Iterator iter = modelCache.keySet().iterator() ; iter.hasNext() ; )
// {
// String name = (String)iter.next() ;
// Model m = (Model)modelCache.get(name) ;
// if ( m != null )
// m.close() ;
// }
modelCache.clear() ;
}
}
public Model get(String filenameOrURI)
{ return (Model)modelCache.get(filenameOrURI) ; }
public boolean contains(String filenameOrURI)
{ return modelCache.containsKey(filenameOrURI) ; }
public void put(String uri, Model m)
{ modelCache.put(uri, m) ; }
public void remove(String uri)
{ modelCache.remove(uri) ; }
}
/*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -