You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/02/04 22:00:26 UTC
svn commit: r1240610 - in
/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader: ./ fm2/
fm2/test/
Author: andy
Date: Sat Feb 4 21:00:25 2012
New Revision: 1240610
URL: http://svn.apache.org/viewvc?rev=1240610&view=rev
Log: (empty)
Added:
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStream2.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/FileManager2.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/JenaIOEnvironment.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocationMapper.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/Locator.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorClassLoader.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorFile.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorURL2.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorZip.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/StreamManager.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TS_IO2.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestFileManager.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocationMapper.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocators.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestStreamManager.java
Modified:
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStreamHttp.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStream2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStream2.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStream2.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStream2.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader;
+
+import java.io.InputStream;
+
+import org.openjena.atlas.io.IO ;
+import org.openjena.riot.ContentType ;
+
+public class TypedStream2
+{
+ private InputStream input ;
+ private ContentType mediaType ;
+ // The URI to use when parsing.
+ // May be different from the URI used to access the resource
+ // e.g. 303 redirection, mapped URI redirection
+ private String baseURI ;
+
+ public TypedStream2(InputStream in)
+ { this(in, null) ; }
+
+ public TypedStream2(InputStream in, String mediaType, String charset, String baseURI)
+ {
+ this(in, ContentType.create(mediaType, charset)) ;
+ }
+
+ public TypedStream2(InputStream in, ContentType ct)
+ {
+ input = in ;
+ mediaType = ct ;
+ }
+
+ public InputStream getInput() { return input ; }
+ public String getContentType() { return mediaType == null ? null : mediaType.getContentType() ; }
+ public String getCharset() { return mediaType == null ? null : mediaType.getCharset() ; }
+ public ContentType getMediaType() { return mediaType ; }
+ public String getBaseURI() { return baseURI ; }
+
+ public void close() { IO.close(input) ; }
+}
Modified: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStreamHttp.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStreamHttp.java?rev=1240610&r1=1240609&r2=1240610&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStreamHttp.java (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TypedStreamHttp.java Sat Feb 4 21:00:25 2012
@@ -22,16 +22,15 @@ import java.io.InputStream ;
import org.apache.http.conn.ClientConnectionManager ;
import org.openjena.atlas.web.MediaType ;
-import org.openjena.atlas.web.TypedStream ;
/** Type streams for HTTP connections - includes Apache HTTP client specific cleanup */
-public class TypedStreamHttp extends TypedStream
+public class TypedStreamHttp extends TypedStream2
{
private ClientConnectionManager connectMgr ;
TypedStreamHttp(InputStream input, MediaType mt, ClientConnectionManager connectMgr)
{
- super(input, mt.getContentType(), mt.getCharset()) ;
+ super(input, mt.getContentType(), mt.getCharset(), null) ;
this.connectMgr = connectMgr ;
}
Modified: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java?rev=1240610&r1=1240609&r2=1240610&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java Sat Feb 4 21:00:25 2012
@@ -23,20 +23,18 @@ import java.io.InputStream ;
import org.openjena.atlas.lib.Sink ;
import org.openjena.atlas.logging.Log ;
import org.openjena.atlas.web.TypedStream ;
-import org.openjena.riot.ContentType ;
-import org.openjena.riot.Lang ;
-import org.openjena.riot.RiotLoader ;
-import org.openjena.riot.WebContent ;
+import org.openjena.riot.* ;
import org.openjena.riot.lang.SinkTriplesToGraph ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
+import riot.reader.fm2.FileManager2 ;
+import riot.reader.fm2.LocatorURL2 ;
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Triple ;
import com.hp.hpl.jena.rdf.model.Model ;
import com.hp.hpl.jena.rdf.model.ModelFactory ;
import com.hp.hpl.jena.sparql.util.Context ;
-import com.hp.hpl.jena.util.FileManager ;
/** General purpose reader framework.
*/
@@ -76,7 +74,7 @@ public class WebReader2
{
Log.enable(WebReader2.class) ;
Model m = ModelFactory.createDefaultModel() ;
- read(m, "http://localhost:3030/books.ttl", WebContent.contentTypeTurtle, null) ;
+ read(m, "http://localhost:3030/ds/get?default", WebContent.contentTypeTurtle, null) ;
//read(m, "file:data.ttl", WebContent.contentTypeTurtle, null) ;
//SSE.write(m) ;
System.out.println("triples = "+m.size()) ;
@@ -145,21 +143,21 @@ public class WebReader2
// return some sort of otehr all status - boolean?
TypedStream in = open(uri) ;
if ( in == null )
- return ;
+ throw new RiotException("Not found: "+uri) ;
process(sink, uri, in, hintLang, context) ;
in.close() ;
}
// --> WebContent, basic formats (not RDFa orreturn null ; other emvbedded data format).
- static String acceptTriples = "text/turtle,application/rdf+xml;q=0.9,application/xml;q=0.8,*/*;q=0.5" ;
+ //static String acceptTriples = "text/turtle,application/rdf+xml;q=0.9,application/xml;q=0.8,*/*;q=0.5" ;
private static TypedStream open(String filenameOrURI)
{
// Testing.
// faked up file manager.
- FileManager fMgr = new FileManager() ;
+ FileManager2 fMgr = new FileManager2() ;
fMgr.addLocatorFile() ;
- fMgr.addLocatorClassLoader(Thread.currentThread().getContextClassLoader()) ;
+ fMgr.addLocator(new LocatorURL2()) ;
InputStream in = fMgr.open(filenameOrURI) ;
@@ -173,9 +171,6 @@ public class WebReader2
}
// Not found - try the web!
String target = fMgr.mapURI(filenameOrURI) ;
-
- if ( target.startsWith("http://") || target.startsWith("https://"))
- return HttpOp2.execHttpGet(filenameOrURI, acceptTriples) ;
return null ;
}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/FileManager2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/FileManager2.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/FileManager2.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/FileManager2.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,378 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.File ;
+import java.io.IOException ;
+import java.util.Iterator ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.TypedStream2 ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.shared.JenaException ;
+import com.hp.hpl.jena.shared.NotFoundException ;
+import com.hp.hpl.jena.util.FileUtils ;
+import com.hp.hpl.jena.util.LocatorURL ;
+
+/** 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
+ */
+
+public class FileManager2 extends StreamManager
+{
+ /** 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 Logger log = LoggerFactory.getLogger(FileManager2.class) ;
+
+ static FileManager2 instance = null ;
+
+ /** Get the global file manager.
+ * @return the global file manager
+ */
+ public static FileManager2 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(FileManager2 globalFileManager)
+ {
+ instance = globalFileManager ;
+ }
+
+ /** Create an uninitialized FileManager */
+ public FileManager2() {}
+
+ /** 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 FileManager2(FileManager2 filemanager)
+ {
+ handlers.addAll(filemanager.handlers) ;
+ mapper = null ;
+ if ( filemanager.getLocationMapper() != null )
+ mapper = new LocationMapper(filemanager.getLocationMapper()) ;
+ }
+
+ /** Create a "standard" FileManager. */
+ public static FileManager2 makeGlobal()
+ {
+ FileManager2 fMgr = new FileManager2(JenaIOEnvironment.get()) ;
+ setStdLocators(fMgr) ;
+ return fMgr ;
+ }
+
+ /** Force a file handler to have the default configuration. */
+ public static void setStdLocators(FileManager2 fMgr)
+ {
+ fMgr.handlers.clear() ;
+ fMgr.addLocatorFile() ;
+ fMgr.addLocatorURL() ;
+ fMgr.addLocatorClassLoader(fMgr.getClass().getClassLoader()) ;
+ }
+ /** Create with the given location mapper */
+ public FileManager2(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<Locator> 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 LocatorURL2() ;
+ 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) ; }
+
+ /** 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)
+ {
+ Model m = ModelFactory.createDefaultModel() ;
+ readModelWorker(m, filenameOrURI, baseURI, rdfSyntax) ;
+ return m ;
+ }
+
+ /**
+ * Read a file of RDF into a model. Guesses the syntax of the file based on filename extension,
+ * defaulting to RDF/XML.
+ * @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)
+ {
+ // 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 && baseURI == null && mappedURI.startsWith( "http:" ) )
+ {
+ // No syntax, no baseURI, HTTP URL ==> use content negotiation
+ model.read(mappedURI) ;
+ return model ;
+ }
+
+ if ( syntax == null )
+ {
+ syntax = FileUtils.guessLang(mappedURI) ;
+ if ( syntax == null || syntax.equals("") )
+ syntax = FileUtils.langXML ;
+ if ( log.isDebugEnabled() )
+ log.debug("Syntax guess: "+syntax);
+ }
+
+ if ( baseURI == null )
+ baseURI = chooseBaseURI(filenameOrURI) ;
+
+ TypedStream2 in = openNoMapOrNull(mappedURI) ;
+ if ( in == null )
+ {
+ if ( log.isDebugEnabled() )
+ log.debug("Failed to locate '"+mappedURI+"'") ;
+ throw new NotFoundException("Not found: "+filenameOrURI) ;
+ }
+ if ( in.getMediaType() != null )
+ {
+ // XXX
+ //syntax
+ }
+ model.read(in.getInput(), baseURI, syntax) ;
+ try { in.getInput().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 ;
+ }
+
+ /** @deprecated Use mapURI */
+ @Deprecated
+ public String remap(String filenameOrURI)
+ { return mapURI(filenameOrURI) ; }
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/JenaIOEnvironment.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/JenaIOEnvironment.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/JenaIOEnvironment.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/JenaIOEnvironment.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.InputStream ;
+import java.util.StringTokenizer ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.JenaRuntime ;
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.rdf.model.RDFNode ;
+import com.hp.hpl.jena.rdf.model.Resource ;
+import com.hp.hpl.jena.rdf.model.Statement ;
+import com.hp.hpl.jena.rdf.model.StmtIterator ;
+import com.hp.hpl.jena.shared.JenaException ;
+import com.hp.hpl.jena.util.FileUtils ;
+import com.hp.hpl.jena.vocabulary.LocationMappingVocab ;
+
+/** Code for using the general facilities of the location mapper/ filemanager subsystem
+ * and set up for Jena usage. e.g. find a location mapper with RDf description.
+ */
+public class JenaIOEnvironment
+{
+ // TODO Singletons
+
+ static LocationMapper theMapper = null ;
+ /** Get the global LocationMapper */
+ public static LocationMapper getLocationMapper()
+ {
+ if ( theMapper == null )
+ {
+ theMapper = new LocationMapper() ;
+ if ( getGlobalConfigPath() != null )
+ JenaIOEnvironment.createLocationMapper(getGlobalConfigPath()) ;
+ }
+ return theMapper ;
+ }
+
+ static Logger log = LoggerFactory.getLogger(JenaIOEnvironment.class) ;
+
+ /** The default path for searching for the location mapper */
+ public static final String DEFAULT_PATH =
+ "file:location-mapping.rdf;file:location-mapping.n3;file:location-mapping.ttl;"+
+ "file:etc/location-mapping.rdf;file:etc/location-mapping.n3;"+
+ "file:etc/location-mapping.ttl" ;
+ public static final String GlobalMapperSystemProperty1 = "http://jena.hpl.hp.com/2004/08/LocationMap" ;
+ public static final String GlobalMapperSystemProperty2 = "LocationMap" ;
+
+ static String s_globalMapperPath = null ;
+
+
+
+ static private String getGlobalConfigPath()
+ {
+ if ( s_globalMapperPath == null )
+ s_globalMapperPath = JenaRuntime.getSystemProperty(GlobalMapperSystemProperty1,null) ;
+ if ( s_globalMapperPath == null )
+ s_globalMapperPath = JenaRuntime.getSystemProperty(GlobalMapperSystemProperty2,null) ;
+ if ( s_globalMapperPath == null )
+ s_globalMapperPath = DEFAULT_PATH ;
+ return s_globalMapperPath ;
+ }
+
+ /** Get the global LocationMapper */
+ public static LocationMapper get()
+ {
+ if ( theMapper == null )
+ {
+ theMapper = new LocationMapper() ;
+ if ( getGlobalConfigPath() != null )
+ JenaIOEnvironment.createLocationMapper(getGlobalConfigPath()) ;
+ }
+ return theMapper ;
+ }
+
+ /** Set the global lcoation mapper. (as returned by get())
+ * If called before any call to get(), then the usual default global location mapper is not created
+ * @param globalLocationMapper
+ */
+ public static void setGlobalLocationMapper(LocationMapper globalLocationMapper)
+ {
+ theMapper = globalLocationMapper ;
+ }
+
+ /** Make a location mapper from the path settings */
+ static public LocationMapper makeGlobal()
+ {
+ LocationMapper lMap = new LocationMapper() ;
+ if ( getGlobalConfigPath() != null )
+ {
+ LocationMapper lMap2 = JenaIOEnvironment.createLocationMapper(getGlobalConfigPath()) ;
+ lMap.copyFrom(lMap2) ;
+ }
+ return lMap ;
+ }
+
+ /** Create a LocationMapper based on Model */
+ public static LocationMapper processConfig(Model m)
+ {
+ LocationMapper locMap = new LocationMapper() ;
+ StmtIterator mappings =
+ m.listStatements(null, LocationMappingVocab.mapping, (RDFNode)null) ;
+
+ for (; mappings.hasNext();)
+ {
+ Statement s = mappings.nextStatement() ;
+ Resource mapping = s.getResource() ;
+
+ if ( mapping.hasProperty(LocationMappingVocab.name) )
+ {
+ try
+ {
+ String name = mapping.getRequiredProperty(LocationMappingVocab.name)
+ .getString() ;
+ String altName = mapping.getRequiredProperty(LocationMappingVocab.altName)
+ .getString() ;
+ locMap.addAltEntry(name, altName) ;
+ log.debug("Mapping: "+name+" => "+altName) ;
+ } catch (JenaException ex)
+ {
+ log.warn("Error processing name mapping: "+ex.getMessage()) ;
+ throw ex ;
+ }
+ }
+
+ if ( mapping.hasProperty(LocationMappingVocab.prefix) )
+ {
+ try
+ {
+ String prefix = mapping.getRequiredProperty(LocationMappingVocab.prefix)
+ .getString() ;
+ String altPrefix = mapping.getRequiredProperty(LocationMappingVocab.altPrefix)
+ .getString() ;
+ locMap.addAltPrefix(prefix, altPrefix) ;
+ log.debug("Prefix mapping: "+prefix+" => "+altPrefix) ;
+ } catch (JenaException ex)
+ {
+ log.warn("Error processing prefix mapping: "+ex.getMessage()) ;
+ throw ex ;
+ }
+ }
+ }
+ return locMap ;
+ }
+
+ /** Search a path (which is delimited by ";" because ":" is used in URIs)
+ * to find a description of a LocationMapper, then create and return a
+ * LocationMapper based on the description.
+ */
+ public static LocationMapper createLocationMapper(String configPath)
+ {
+ LocationMapper locMap = new LocationMapper() ;
+ if ( configPath == null || configPath.length() == 0 )
+ {
+ log.warn("Null configuration") ;
+ return null ;
+ }
+
+ // Make a file manager to look for the location mapping file
+ FileManager2 fm = new FileManager2() ;
+ fm.addLocatorFile() ;
+ fm.addLocatorClassLoader(fm.getClass().getClassLoader()) ;
+
+ try {
+ String uriConfig = null ;
+ InputStream in = null ;
+
+ StringTokenizer pathElems = new StringTokenizer( configPath, FileManager2.PATH_DELIMITER );
+ while (pathElems.hasMoreTokens()) {
+ String uri = pathElems.nextToken();
+ if ( uri == null || uri.length() == 0 )
+ break ;
+
+ in = fm.openNoMap(uri) ;
+ if ( in != null )
+ {
+ uriConfig = uri ;
+ break ;
+ }
+ }
+
+ if ( in == null )
+ {
+ log.debug("Failed to find configuration: "+configPath) ;
+ return null ;
+ }
+ // TODO
+ String syntax = FileUtils.guessLang(uriConfig) ;
+ Model model = ModelFactory.createDefaultModel() ;
+ model.read(in, uriConfig, syntax) ;
+ processConfig(model) ;
+ } catch (JenaException ex)
+ {
+ LoggerFactory.getLogger(LocationMapper.class).warn("Error in configuration file: "+ex.getMessage()) ;
+ }
+ return locMap ;
+ }
+
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocationMapper.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocationMapper.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocationMapper.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocationMapper.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,241 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.util.HashMap ;
+import java.util.Iterator ;
+import java.util.Map ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.rdf.model.Resource ;
+import com.hp.hpl.jena.vocabulary.LocationMappingVocab ;
+
+/**
+ * Alternative locations for URIs. Maintains two maps:
+ * single item alternatives and alternative prefixes.
+ * To suggest an alternative location, first check the single items,
+ * then check the prefixes.
+ *
+ * A LocationMapper can be configured by an RDF file. The default for this
+ * is "etc/location-mapping.n3".
+ *
+ * There is a default LocationMapper which is used by the global @link{FileManager}.
+ */
+
+public class LocationMapper
+{
+ static Logger log = LoggerFactory.getLogger(LocationMapper.class) ;
+ Map<String, String> altLocations = new HashMap<String, String>() ;
+ Map<String, String> altPrefixes = new HashMap<String, String>() ;
+
+ /** Create a LocationMapper with no mapping yet */
+ public LocationMapper() { }
+
+ /** Create a LocationMapper made like another one
+ * This is a deep copy of the location and prefix maps..*/
+ public LocationMapper(LocationMapper locMapper)
+ {
+ altLocations.putAll(locMapper.altLocations) ;
+ altPrefixes.putAll(locMapper.altPrefixes) ;
+ }
+
+ // Moved to Jena IO environment setup.
+// /** Create a LocationMapper from an existing model
+// * @see com.hp.hpl.jena.vocabulary.LocationMappingVocab
+// */
+// public LocationMapper(Model model)
+// {
+// LocationMapper lm = JenaIOEnvironment.processConfig(model) ;
+// if ( lm == null )
+// throw new AtlasException("Model does not provide a location mapping") ;
+// copyFrom(lm) ;
+//
+// }
+//
+// /** Create a LocationMapper from a config file */
+// public LocationMapper(String config)
+// {
+// LocationMapper lm = JenaIOEnvironment.createLocationMapper(config) ;
+// if ( lm == null )
+// throw new AtlasException("Config does not provide a location mapping") ;
+// copyFrom(lm) ;
+// }
+
+ public void copyFrom(LocationMapper lmap2)
+ {
+ this.altLocations.putAll(lmap2.altLocations) ;
+ this.altPrefixes.putAll(lmap2.altPrefixes) ;
+ }
+
+ public String altMapping(String uri)
+ {
+ return altMapping(uri, uri) ;
+ }
+
+ /** Apply mappings: first try for an exact alternative location, then
+ * try to remap by prefix, finally, try the special case of filenames
+ * in a specific base directory.
+ * @param uri
+ * @param otherwise
+ * @return The alternative location choosen
+ */
+ public String altMapping(String uri, String otherwise)
+ {
+ if ( altLocations.containsKey(uri))
+ return altLocations.get(uri) ;
+ String newStart = null ;
+ String oldStart = null ;
+ for ( Iterator<String> iter = altPrefixes.keySet().iterator() ; iter.hasNext() ;)
+ {
+ String prefix = iter.next() ;
+ if ( uri.startsWith(prefix) )
+ {
+ String s = altPrefixes.get(prefix) ;
+ if ( newStart == null || newStart.length() < s.length() )
+ {
+ oldStart = prefix ;
+ newStart = s ;
+ }
+ }
+ }
+
+ if ( newStart != null )
+ return newStart+uri.substring(oldStart.length()) ;
+
+ return otherwise ;
+ }
+
+
+ public void addAltEntry(String uri, String alt)
+ {
+ altLocations.put(uri, alt) ;
+ }
+
+ public void addAltPrefix(String uriPrefix, String altPrefix)
+ {
+ altPrefixes.put(uriPrefix, altPrefix) ;
+ }
+
+ /** Iterate over all the entries registered */
+ public Iterator<String> listAltEntries() { return altLocations.keySet().iterator() ; }
+ /** Iterate over all the prefixes registered */
+ public Iterator<String> listAltPrefixes() { return altPrefixes.keySet().iterator() ; }
+
+ public void removeAltEntry(String uri)
+ {
+ altLocations.remove(uri) ;
+ }
+
+ public void removeAltPrefix(String uriPrefix)
+ {
+ altPrefixes.remove(uriPrefix) ;
+ }
+ public String getAltEntry(String uri)
+ {
+ return altLocations.get(uri) ;
+ }
+
+ public String getAltPrefix(String uriPrefix)
+ {
+ return altPrefixes.get(uriPrefix) ;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int x = 0 ;
+ x = x ^ altLocations.hashCode() ;
+ x = x ^ altPrefixes.hashCode() ;
+ return x ;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if ( ! ( obj instanceof LocationMapper ) )
+ return false ;
+ LocationMapper other = (LocationMapper)obj ;
+
+ if ( ! this.altLocations.equals(other.altLocations) )
+ return false ;
+
+ if ( ! this.altPrefixes.equals(other.altPrefixes) )
+ return false ;
+ return true ;
+ }
+
+ @Override
+ public String toString()
+ {
+ String s = "" ;
+ for ( Iterator<String> iter = altLocations.keySet().iterator() ; iter.hasNext() ; )
+ {
+ String k = iter.next() ;
+ String v = altLocations.get(k) ;
+ s = s+"(Loc:"+k+"=>"+v+") " ;
+ }
+
+ for ( Iterator<String> iter = altPrefixes.keySet().iterator() ; iter.hasNext() ; )
+ {
+ String k = iter.next() ;
+ String v = altPrefixes.get(k) ;
+ s = s+"(Prefix:"+k+"=>"+v+") " ;
+ }
+ return s ;
+ }
+
+ public Model toModel()
+ {
+ Model m = ModelFactory.createDefaultModel() ;
+ m.setNsPrefix("lmap", "http://jena.hpl.hp.com/2004/08/location-mapping#") ;
+ toModel(m) ;
+ return m ;
+ }
+
+ public void toModel(Model model)
+ {
+
+ for ( Iterator<String> iter = altLocations.keySet().iterator() ; iter.hasNext() ; )
+ {
+ Resource r = model.createResource() ;
+ Resource e = model.createResource() ;
+ model.add(r, LocationMappingVocab.mapping, e) ;
+
+ String k = iter.next() ;
+ String v = altLocations.get(k) ;
+ model.add(e, LocationMappingVocab.name, k) ;
+ model.add(e, LocationMappingVocab.altName, v) ;
+ }
+
+ for ( Iterator<String> iter = altPrefixes.keySet().iterator() ; iter.hasNext() ; )
+ {
+ Resource r = model.createResource() ;
+ Resource e = model.createResource() ;
+ model.add(r, LocationMappingVocab.mapping, e) ;
+ String k = iter.next() ;
+ String v = altPrefixes.get(k) ;
+ model.add(e, LocationMappingVocab.prefix, k) ;
+ model.add(e, LocationMappingVocab.altPrefix, v) ;
+ }
+ }
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/Locator.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/Locator.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/Locator.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/Locator.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import riot.reader.TypedStream2 ;
+
+/**
+ * Interface to things that open TypedStreams from a place
+ */
+
+public interface Locator
+{
+ // Open a stream given a name of some kind (not necessarily an IRI).
+ public TypedStream2 open(String uri) ;
+ public String getName() ;
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorClassLoader.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorClassLoader.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorClassLoader.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.InputStream ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.TypedStream2 ;
+
+import com.hp.hpl.jena.util.FileUtils ;
+
+public class LocatorClassLoader implements Locator
+{
+ static Logger log = LoggerFactory.getLogger(LocatorClassLoader.class) ;
+
+ ClassLoader classLoader = null ;
+ public LocatorClassLoader(ClassLoader _classLoader)
+ {
+ classLoader =_classLoader ;
+ }
+
+ @Override
+ public boolean equals( Object other )
+ {
+ return
+ other instanceof LocatorClassLoader
+ && classLoader == ((LocatorClassLoader) other).classLoader;
+ }
+
+ @Override
+ public int hashCode()
+ { return classLoader.hashCode(); }
+
+ @Override
+ public TypedStream2 open(String filenameOrURI)
+ {
+ if ( classLoader == null )
+ return null ;
+
+ String fn = FileUtils.toFilename(filenameOrURI) ;
+ if ( fn == null )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Not found: "+filenameOrURI) ;
+ return null ;
+ }
+ InputStream in = classLoader.getResourceAsStream(fn) ;
+ if ( in == null )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Failed to open: "+filenameOrURI) ;
+ return null ;
+ }
+
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Found: "+filenameOrURI) ;
+
+ return new TypedStream2(in) ;
+ }
+
+ @Override
+ public String getName() { return "ClassLoaderLocator" ; }
+
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorFile.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorFile.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorFile.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorFile.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.File ;
+import java.io.FileInputStream ;
+import java.io.IOException ;
+import java.io.InputStream ;
+import java.security.AccessControlException ;
+
+import org.openjena.atlas.lib.IRILib ;
+import org.openjena.atlas.lib.Lib ;
+import org.openjena.riot.Lang ;
+import org.openjena.riot.WebContent ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.TypedStream2 ;
+
+import com.hp.hpl.jena.util.FileUtils ;
+
+/** Location files in the filing system.
+ * A FileLocator can have a "current directory" - this is separate from any
+ * location mapping (see @link{LocationMapping}) as it applies only to files.
+ */
+
+public class LocatorFile implements Locator
+{
+ static Logger log = LoggerFactory.getLogger(LocatorFile.class) ;
+ private String altDir = null ;
+ private String altDirLogStr = "" ;
+
+ public LocatorFile() { this(null) ; }
+
+ public LocatorFile(String dir)
+ {
+ if ( dir != null )
+ {
+ if ( dir.endsWith("/") || dir.endsWith(java.io.File.separator) )
+ dir = dir.substring(0,dir.length()-1) ;
+ altDirLogStr = " ["+dir+"]" ;
+ }
+ altDir = dir ;
+ }
+
+ // Two LocatorFile are the same if they would look up names to the same files.
+
+ @Override
+ public boolean equals( Object other )
+ {
+ return
+ other instanceof LocatorFile
+ && Lib.equal(altDir, ((LocatorFile) other).altDir );
+ }
+
+ @Override
+ public int hashCode()
+ {
+ if ( altDir == null ) return 57 ;
+ return altDir.hashCode();
+ }
+
+ /** To a File, after processing the filename for file: or relative filename */
+ public File toFile(String filenameIRI)
+ {
+ String scheme = FileUtils.getScheme(filenameIRI) ;
+ String fn = filenameIRI ;
+ // Windows : C:\\ is not a scheme name!
+ if ( scheme != null && scheme.length() > 1 )
+ {
+ if ( ! scheme.equalsIgnoreCase("file") )
+ // Not filename or a file: IRI
+ return null ;
+ fn = IRILib.IRIToFilename(filenameIRI) ;
+ }
+ // AltDir.
+ // "/" is a path separator on Windows.
+ if ( altDir != null && ! fn.startsWith("/") && ! fn.startsWith(File.pathSeparator) )
+ fn = altDir+"/"+fn ;
+ return new File(fn) ;
+ }
+
+ public boolean exists(String fileIRI)
+ {
+ File f = toFile(fileIRI) ;
+
+ if ( f == null )
+ return false ;
+
+ return f.exists() ;
+ }
+
+ /** Opne anything that looks a bit like a file name */
+ @Override
+ public TypedStream2 open(String filenameIRI)
+ {
+ File f = toFile(filenameIRI) ;
+
+ try {
+ if ( f == null || !f.exists() )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled())
+ log.trace("Not found: "+filenameIRI+altDirLogStr) ;
+ return null ;
+ }
+ } catch (AccessControlException e) {
+ log.warn("Security problem testing for file", e);
+ return null;
+ }
+
+ try {
+ InputStream in = new FileInputStream(f) ;
+
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Found: "+filenameIRI+altDirLogStr) ;
+
+ // Guess content from extension.
+ Lang lang = Lang.guess(filenameIRI) ;
+ String contentType = WebContent.mapLangToContentType(lang) ;
+ String charset = WebContent.getCharsetForContentType(contentType) ;
+ return new TypedStream2(in, contentType, charset, null) ;
+ } catch (IOException ioEx)
+ {
+ // Includes FileNotFoundException
+ // We already tested whether the file exists or not.
+ log.warn("File unreadable (but exists): "+f.getPath()+" Exception: "+ioEx.getMessage()) ;
+ return null ;
+ }
+ }
+
+ @Override
+ public String getName()
+ {
+ String tmp = "LocatorFile" ;
+ if ( altDir != null )
+ tmp = tmp+"("+altDir+")" ;
+ return tmp ;
+ }
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorURL2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorURL2.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorURL2.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorURL2.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import com.hp.hpl.jena.util.FileUtils ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.HttpOp2 ;
+import riot.reader.TypedStream2 ;
+
+public class LocatorURL2 implements Locator
+{
+ private static Logger log = LoggerFactory.getLogger(LocatorURL2.class) ;
+ public static final String acceptTriples = "text/turtle,application/rdf+xml;q=0.9,application/xml;q=0.8,*/*;q=0.5" ;
+
+ static final String[] schemeNames = { "http:" , "https:" } ; // Must be lower case and include the ":"
+
+ @Override
+ public TypedStream2 open(String uri)
+ {
+ if ( ! acceptByScheme(uri) )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Not found : "+uri) ;
+ return null;
+ }
+ if ( uri.startsWith("http://") || uri.startsWith("https://"))
+ return HttpOp2.execHttpGet(uri, acceptTriples) ;
+ return null ;
+ }
+
+ @Override
+ public String getName()
+ {
+ return "LocatorURL" ;
+ }
+
+ private static boolean acceptByScheme(String filenameOrURI)
+ {
+ String uriSchemeName = FileUtils.getScheme(filenameOrURI) ;
+ uriSchemeName = uriSchemeName.toLowerCase() ;
+ for ( int i = 0 ; i < schemeNames.length ; i++ )
+ {
+ if ( uriSchemeName.equals(schemeNames[i]) )
+ return true ;
+ }
+ return false ;
+ }
+}
+
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorZip.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorZip.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorZip.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/LocatorZip.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.IOException ;
+import java.io.InputStream ;
+import java.util.zip.ZipEntry ;
+import java.util.zip.ZipFile ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.TypedStream2 ;
+
+import com.hp.hpl.jena.shared.JenaException ;
+
+
+/** Location files in a zip file */
+
+public class LocatorZip implements Locator
+{
+ static Logger log = LoggerFactory.getLogger(LocatorZip.class) ;
+ String zipFileName = null ;
+ ZipFile zipFile = null ;
+
+ public LocatorZip(String zfn)
+ {
+ try {
+ zipFileName = zfn ;
+ zipFile = new ZipFile(zipFileName) ;
+ } catch (IOException ex)
+ {
+ throw new JenaException("Problems accessing "+zipFileName, ex) ;
+ }
+ }
+
+ @Override
+ public TypedStream2 open(String filenameOrURI)
+ {
+ ZipEntry entry = zipFile.getEntry(filenameOrURI) ;
+ if ( entry == null )
+ {
+ if ( StreamManager.logAllLookups && log.isDebugEnabled() )
+ log.debug("Not found: "+zipFileName+" : "+filenameOrURI) ;
+ return null ;
+
+ }
+ try
+ {
+ InputStream in = zipFile.getInputStream(entry) ;
+
+ if ( in == null )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Not found: "+filenameOrURI) ;
+ return null ;
+ }
+
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Found: "+filenameOrURI) ;
+ return new TypedStream2(in) ;
+ }
+ catch (IOException ex)
+ {
+ log.warn("IO Exception opening zip entry: " + filenameOrURI);
+ return null;
+ }
+ }
+ @Override
+ public String getName() { return "LocatorZip("+zipFileName+")" ; }
+
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/StreamManager.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/StreamManager.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/StreamManager.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/StreamManager.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2;
+
+import java.io.InputStream ;
+import java.util.ArrayList ;
+import java.util.List ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot.reader.TypedStream2 ;
+
+/** Operations to open streams, indirecting via a LocationMapper.
+ * Includes filename to IRI, handling ".gz" and "-"
+ *
+ * */
+public class StreamManager
+{
+ // Need to combine with IO to do the .gz and "-" things.
+
+ private static Logger log = LoggerFactory.getLogger(StreamManager.class) ;
+
+ public static boolean logAllLookups = true ;
+
+ protected List<Locator> handlers = new ArrayList<Locator>() ;
+ protected LocationMapper mapper = null ;
+
+ /** 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) ;
+ }
+
+ /** 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 ( StreamManager.logAllLookups && log.isDebugEnabled() )
+ log.debug("Not mapped: "+filenameOrURI) ;
+ uri = filenameOrURI ;
+ }
+ else
+ {
+ if ( log.isDebugEnabled() )
+ log.debug("Mapped: "+filenameOrURI+" => "+uri) ;
+ }
+ return uri ;
+ }
+
+ /** Open a file using the locators of this FileManager
+ * but without location mapping */
+ public InputStream openNoMap(String filenameOrURI)
+ {
+ TypedStream2 in = openNoMapOrNull(filenameOrURI) ;
+ if ( in == null )
+ return null ;
+// if ( in == null )
+// throw new NotFoundException(filenameOrURI) ;
+ return in.getInput() ;
+ }
+
+ /** Open a file using the locators of this FileManager
+ * without location mapping. Return null if not found
+ */
+
+ public TypedStream2 openNoMapOrNull(String filenameOrURI)
+ {
+ for (Locator loc : handlers)
+ {
+ TypedStream2 in = loc.open(filenameOrURI) ;
+ if ( in != null )
+ {
+ if ( log.isDebugEnabled() )
+ log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
+ return in ;
+ }
+ }
+ return null;
+ }
+
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TS_IO2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TS_IO2.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TS_IO2.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TS_IO2.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2.test;
+
+
+import org.junit.runner.RunWith ;
+import org.junit.runners.Suite ;
+
+@RunWith(Suite.class)
+@Suite.SuiteClasses( {
+ TestStreamManager.class
+ , TestLocationMapper.class
+ , TestLocators.class
+})
+public class TS_IO2
+{
+
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestFileManager.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestFileManager.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestFileManager.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestFileManager.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2.test;
+
+import java.io.InputStream ;
+
+import junit.framework.TestCase ;
+import junit.framework.TestSuite ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.shared.NotFoundException ;
+import com.hp.hpl.jena.util.FileManager ;
+import com.hp.hpl.jena.util.LocationMapper ;
+
+public class TestFileManager extends TestCase
+{
+ static Logger log = LoggerFactory.getLogger(TestFileManager.class) ;
+ public static final String testingDir = "testing/FileManager" ;
+ static final String filename = "fmgr-test-file" ;
+ static final String filenameNonExistent = "fmgr-test-file-1421" ;
+ static final String fileModel = "foo.n3" ;
+ static final String zipname = testingDir+"/fmgr-test.zip" ;
+
+ public TestFileManager( String name )
+ {
+ super(name);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite( TestFileManager.class );
+ }
+
+ public void testFileManagerFileLocator()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorFile() ;
+ InputStream in = fileManager.open(testingDir+"/"+filename) ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+
+ public void testFileManagerFileLocatorWithDir()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorFile(testingDir) ;
+ InputStream in = fileManager.open(filename) ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+
+
+ public void testFileManagerNoFile()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorFile() ;
+ try {
+ // Tests either way round - exception or a null return.
+ InputStream in = fileManager.open(filenameNonExistent) ;
+ closeInputStream(in) ;
+ assertNull("Found non-existant file: "+filenameNonExistent, in) ;
+ } catch (NotFoundException ex) {}
+ }
+
+ public void testFileManagerLocatorClassLoader()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ;
+ InputStream in = fileManager.open("java/lang/String.class") ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+
+ public void testFileManagerLocatorClassLoaderNotFound()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ;
+ try {
+ InputStream in = fileManager.open("not/java/lang/String.class") ;
+ closeInputStream(in) ;
+ assertNull("Found non-existant class", in) ;
+ } catch (NotFoundException ex) {}
+ }
+
+ public void testFileManagerLocatorZip()
+ {
+ FileManager fileManager = new FileManager() ;
+ try {
+ fileManager.addLocatorZip(zipname) ;
+ } catch (Exception ex)
+ {
+ fail("Failed to create a filemanager and add a zip locator") ;
+ }
+ InputStream in = fileManager.open(filename) ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+
+ public void testFileManagerLocatorZipNonFound()
+ {
+ FileManager fileManager = new FileManager() ;
+ try {
+ fileManager.addLocatorZip(zipname) ;
+ } catch (Exception ex)
+ { fail("Failed to create a filemanager and add a zip locator") ; }
+ try {
+ InputStream in = fileManager.open(filenameNonExistent) ;
+ closeInputStream(in) ;
+ assertNull("Found non-existant zip file member", in) ;
+ } catch (NotFoundException ex) {}
+ }
+
+ public void testFileManagerClone()
+ {
+ FileManager fileManager1 = new FileManager() ;
+ FileManager fileManager2 = new FileManager(fileManager1) ;
+
+ // Should not affect fileManager2
+ fileManager1.addLocatorFile() ;
+ {
+ InputStream in = fileManager1.open(testingDir+"/"+filename) ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+ // Should not work.
+ try {
+ InputStream in = fileManager2.open(testingDir+"/"+filename) ;
+ closeInputStream(in) ;
+ assertNull("Found file via wrong FileManager", in) ;
+ } catch (NotFoundException ex) {}
+ }
+
+
+ public void testLocationMappingURLtoFileOpen()
+ {
+ LocationMapper locMap = new LocationMapper(TestLocationMapper.mapping) ;
+ FileManager fileManager = new FileManager(locMap) ;
+ fileManager.addLocatorFile() ;
+ InputStream in = fileManager.open("http://example.org/file") ;
+ assertNotNull(in) ;
+ closeInputStream(in) ;
+ }
+
+ public void testLocationMappingURLtoFileOpenNotFound()
+ {
+ LocationMapper locMap = new LocationMapper(TestLocationMapper.mapping) ;
+ FileManager fileManager = new FileManager(locMap) ;
+ fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ;
+ try {
+ InputStream in = fileManager.open("http://example.org/file") ;
+ closeInputStream(in) ;
+ assertNull("Found nont-existant URL", null) ;
+ } catch (NotFoundException ex) {}
+ }
+
+ public void testCache1()
+ {
+ FileManager fileManager = new FileManager() ;
+ fileManager.addLocatorFile(testingDir) ;
+ Model m1 = fileManager.loadModel(fileModel) ;
+ Model m2 = fileManager.loadModel(fileModel) ;
+ assertNotSame(m1, m2) ;
+ }
+
+ public void testCache2()
+ {
+ FileManager fileManager = FileManager.get() ;
+ fileManager.addLocatorFile(testingDir) ;
+ fileManager.setModelCaching(true) ;
+ Model m1 = fileManager.loadModel(fileModel) ;
+ Model m2 = fileManager.loadModel(fileModel) ;
+ assertSame(m1, m2) ;
+ }
+
+ public void testCache3()
+ {
+ FileManager fileManager = FileManager.get() ;
+ fileManager.addLocatorFile(testingDir) ;
+ fileManager.setModelCaching(true) ;
+ Model m1 = fileManager.loadModel(fileModel) ;
+ Model m2 = fileManager.loadModel(fileModel) ;
+ assertSame(m1, m2) ;
+
+ fileManager.removeCacheModel(fileModel) ;
+ Model m3 = fileManager.loadModel(fileModel) ;
+ assertNotSame(m1, m3) ;
+
+ fileManager.resetCache() ;
+ Model m4 = fileManager.loadModel(fileModel) ;
+ Model m5 = fileManager.loadModel(fileModel) ;
+
+ assertSame(m4, m5) ;
+ assertNotSame(m1, m4) ;
+ assertNotSame(m3, m4) ;
+ }
+
+// public void testFileManagerLocatorURL()
+// {
+// FileManager fileManager = new FileManager() ;
+// fileManager.addLocatorURL() ;
+// InputStream in = fileManager.open("http:///www.bbc.co.uk/") ;
+// //assertNotNull(in) ;
+// // Proxies matter.
+// if ( in == null )
+// log.warn("Failed to contact http:///www.bbc.co.uk/: maybe due to proxy issues") ;
+//
+// try { if ( in != null ) in.close() ; }
+// catch (Exception ex) {}
+// }
+
+
+ // -------- Helpers
+
+ private void closeInputStream(InputStream in)
+ {
+ try {
+ if ( in != null )
+ in.close() ;
+ }
+ catch (Exception ex) {}
+ }
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocationMapper.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocationMapper.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocationMapper.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocationMapper.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2.test;
+
+import java.util.Iterator ;
+
+import junit.framework.TestCase ;
+import junit.framework.TestSuite ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.util.FileManager ;
+import com.hp.hpl.jena.util.LocationMapper ;
+
+/** TestLocationMapper
+ *
+ * @author Andy Seaborne
+ * @version $Id: TestLocationMapper.java,v 1.1 2009/06/29 18:42:05 andy_seaborne Exp $
+ */
+
+public class TestLocationMapper extends TestCase
+{
+ static Logger log = LoggerFactory.getLogger(TestLocationMapper.class) ;
+ static final String testingDir = TestFileManager.testingDir ;
+ static final String filename1 = "file:test" ;
+ static final String notFilename = "zzzz" ;
+ static final String filename2 = "file:"+testingDir+"/location-mapping-test-file" ;
+ public static final String mapping = "location-mapping-test.n3;"+
+ testingDir+"/location-mapping-test.n3" ;
+
+
+ public TestLocationMapper( String name )
+ {
+ super(name);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite( TestLocationMapper.class );
+ }
+
+ public void testLocationMapper()
+ {
+ LocationMapper locMap = new LocationMapper(mapping) ;
+ String alt = locMap.altMapping(filename1) ;
+ assertNotNull(alt) ;
+ assertEquals(alt, filename2) ;
+ }
+
+ public void testLocationMapperMiss()
+ {
+ LocationMapper locMap = new LocationMapper(mapping) ;
+ String alt = locMap.altMapping(notFilename) ;
+ assertNotNull(alt) ;
+ assertEquals(alt, notFilename) ;
+ }
+
+ public void testLocationMapperURLtoFile()
+ {
+ LocationMapper locMap = new LocationMapper(mapping) ;
+ String alt = locMap.altMapping("http://example.org/file") ;
+ assertNotNull(alt) ;
+ assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
+ }
+
+ public void testLocationMapperFromModel()
+ {
+ Model model = FileManager.get().loadModel(testingDir+"/location-mapping-test.n3") ;
+ LocationMapper loc = new LocationMapper(model) ;
+
+ // Light test that the two location mappers are "the same"
+ LocationMapper locMap = new LocationMapper(mapping) ;
+ for ( Iterator<String> iter = loc.listAltEntries() ; iter.hasNext() ; )
+ {
+ String e = iter.next() ;
+ String v1 = locMap.getAltEntry(e) ;
+ String v2 = loc.getAltEntry(e) ;
+ assertEquals("Different entries", v1, v2) ;
+ }
+ for ( Iterator<String> iter = loc.listAltPrefixes() ; iter.hasNext() ; )
+ {
+ String e = iter.next() ;
+ String v1 = locMap.getAltPrefix(e) ;
+ String v2 = loc.getAltPrefix(e) ;
+ assertEquals("Different entries", v1, v2) ;
+ }
+ }
+
+ public void testLocationMapperClone1()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ // See testLocationMapperURLtoFile
+// String alt = locMap.altMapping("http://example.org/file") ;
+// assertNotNull(alt) ;
+// assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
+
+ LocationMapper locMap2 = new LocationMapper(locMap1) ;
+ // Remove from original
+ locMap1.removeAltEntry("http://example.org/file") ;
+ String alt = locMap2.altMapping("http://example.org/file") ;
+ assertNotNull(alt) ;
+ assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
+ }
+
+ public void testLocationMapperClone2()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ // See testLocationMapperURLtoFile
+// String alt = locMap.altMapping("http://example.org/file") ;
+// assertNotNull(alt) ;
+// assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
+
+ LocationMapper locMap2 = new LocationMapper(locMap1) ;
+
+ // Change this one
+ locMap2.addAltPrefix("http://example.org/OTHER", "file:OTHER") ;
+ {
+ String alt = locMap2.altMapping("http://example.org/OTHER/f") ;
+ assertNotNull(alt) ;
+ assertEquals(alt, "file:OTHER/f") ;
+ }
+ // Not the other
+ {
+ String alt = locMap1.altMapping("http://example.org/OTHER/f") ;
+ assertNotNull(alt) ;
+ // Did not change
+ assertEquals(alt, "http://example.org/OTHER/f") ;
+ }
+ }
+
+ public void testLocationMapperEquals1()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ LocationMapper locMap2 = new LocationMapper(mapping) ;
+ assertEquals(locMap1, locMap2) ;
+ assertEquals(locMap1.hashCode(), locMap2.hashCode()) ;
+ }
+
+ public void testLocationMapperEquals2()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ LocationMapper locMap2 = new LocationMapper(mapping) ;
+ locMap2.addAltEntry("file:nowhere", "file:somewhere") ;
+ assertFalse(locMap1.equals(locMap2)) ;
+ assertFalse(locMap2.equals(locMap1)) ;
+ }
+
+ public void testLocationMapperToModel1()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ LocationMapper locMap2 = new LocationMapper(locMap1.toModel()) ;
+ assertEquals(locMap1, locMap2) ;
+ assertEquals(locMap1.hashCode(), locMap2.hashCode()) ;
+ }
+
+ public void testLocationMapperToModel2()
+ {
+ LocationMapper locMap1 = new LocationMapper(mapping) ;
+ LocationMapper locMap2 = new LocationMapper(mapping) ;
+ locMap1 = new LocationMapper(locMap1.toModel()) ;
+ locMap2.addAltEntry("file:nowhere", "file:somewhere") ;
+ assertFalse(locMap1.equals(locMap2)) ;
+ assertFalse(locMap2.equals(locMap1)) ;
+ }
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocators.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocators.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocators.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestLocators.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2.test;
+
+import java.io.File ;
+
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+import org.openjena.riot.WebContent ;
+import riot.reader.TypedStream2 ;
+import riot.reader.fm2.LocatorFile ;
+
+public class TestLocators extends BaseTest
+{
+ public static final String testingDir = "testing/RIOT/Files/" ;
+
+ @Test public void locatorFile_01()
+ {
+ LocatorFile loc = new LocatorFile() ;
+ assertTrue(loc.exists("pom.xml")) ;
+ assertTrue(loc.exists(testingDir+"data.ttl")) ;
+ assertFalse(loc.exists("IDoNotExist")) ;
+ }
+
+ @Test public void locatorFile_02()
+ {
+ LocatorFile loc = new LocatorFile(".") ;
+ assertTrue(loc.exists("pom.xml")) ;
+ assertTrue(loc.exists(testingDir+"data.ttl")) ;
+ assertFalse(loc.exists("IDoNotExist")) ;
+ }
+
+ @Test public void locatorFile_03()
+ {
+ String dir = new File(".").getAbsolutePath() ;
+ LocatorFile loc = new LocatorFile(dir) ;
+ assertTrue(loc.exists("pom.xml")) ;
+ assertFalse(loc.exists("IDoNotExist")) ;
+ }
+
+ @Test public void locatorFile_04()
+ {
+ String dir = new File("src").getAbsolutePath() ;
+ LocatorFile loc = new LocatorFile(dir) ;
+
+ assertFalse(loc.exists("pom.xml")) ;
+ assertTrue(loc.exists("org")) ;
+ assertFalse(loc.exists(testingDir+"data.ttl")) ;
+ assertTrue(loc.exists("../pom.xml")) ;
+ assertFalse(loc.exists("/../"+testingDir+"data.ttl")) ;
+ }
+
+ @Test public void locatorFile_05()
+ {
+ LocatorFile loc = new LocatorFile() ;
+ TypedStream2 ts = loc.open(testingDir+"data.ttl") ;
+ assertTrue("Not equal: "+WebContent.contentTypeTurtle+" != "+ts.getMediaType(),
+ WebContent.contentTypeTurtleAlt1.equalsIgnoreCase(ts.getContentType())) ;
+ }
+
+ // TypedStream
+
+ @Test public void locatorURL_01() {}
+
+ @Test public void locatorZip_01() {}
+
+ @Test public void locatorClassloader_01() {}
+}
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestStreamManager.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestStreamManager.java?rev=1240610&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestStreamManager.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/fm2/test/TestStreamManager.java Sat Feb 4 21:00:25 2012
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.reader.fm2.test;
+
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+
+public class TestStreamManager extends BaseTest
+{
+ @Test public void streamManager_01() {}
+}