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/08/16 15:04:52 UTC
svn commit: r1373816 [2/3] - in /jena/Experimental/riot-reader: ./
.settings/ src/ src/main/ src/main/java/ src/main/java/dev/
src/main/java/riot_reader/ src/main/java/riot_reader/fm2/
src/main/resources/ src/test/ src/test/java/ src/test/java/riot_rea...
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/FileManager2.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/FileManager2.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/FileManager2.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/FileManager2.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,375 @@
+/*
+ * 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.TypedInputStream2 ;
+
+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 LocatorFile2}, 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>
+ * </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 LocatorFile2} 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 copy of another.
+ * Location mapper and locators chain are copied (the locators are not cloned).
+ * @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)
+ {
+ LocatorFile2 fLoc = new LocatorFile2(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) ;
+
+ TypedInputStream2 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: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/JenaIOEnvironment.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/JenaIOEnvironment.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/JenaIOEnvironment.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/JenaIOEnvironment.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,209 @@
+/*
+ * 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.StringTokenizer ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.TypedInputStream2 ;
+
+import com.hp.hpl.jena.JenaRuntime ;
+import com.hp.hpl.jena.rdf.model.* ;
+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
+{
+ 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 ;
+ TypedInputStream2 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.openNoMapOrNull(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.getInput(), uriConfig, syntax) ;
+ processConfig(model) ;
+ } catch (JenaException ex)
+ {
+ LoggerFactory.getLogger(LocationMapper.class).warn("Error in configuration file: "+ex.getMessage()) ;
+ }
+ return locMap ;
+ }
+
+}
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocationMapper.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocationMapper.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocationMapper.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocationMapper.java Thu Aug 16 13:04:49 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: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/Locator.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/Locator.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/Locator.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/Locator.java Thu Aug 16 13:04:49 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.TypedInputStream2 ;
+
+/**
+ * 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 TypedInputStream2 open(String uri) ;
+ public String getName() ;
+}
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorClassLoader.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorClassLoader.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorClassLoader.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorClassLoader.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,76 @@
+/*
+ * 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.openjena.atlas.web.ContentType ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.Langs ;
+import riot_reader.TypedInputStream2 ;
+
+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 TypedInputStream2 open(String resourceName)
+ {
+ if ( classLoader == null )
+ return null ;
+
+ InputStream in = classLoader.getResourceAsStream(resourceName) ;
+ if ( in == null )
+ {
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Failed to open: "+resourceName) ;
+ return null ;
+ }
+
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Found: "+resourceName) ;
+
+ ContentType ct = Langs.guessContentType(resourceName) ;
+ // No sensible base URI.
+ return new TypedInputStream2(in, ct, null) ;
+ }
+
+ @Override
+ public String getName() { return "ClassLoaderLocator" ; }
+
+}
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorFile2.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorFile2.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorFile2.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorFile2.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,150 @@
+/*
+ * 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.io.InputStream ;
+import java.security.AccessControlException ;
+
+import org.openjena.atlas.io.IO ;
+import org.openjena.atlas.lib.IRILib ;
+import org.openjena.atlas.web.ContentType ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.Langs ;
+import riot_reader.TypedInputStream2 ;
+
+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 LocatorFile2 implements Locator
+{
+ static Logger log = LoggerFactory.getLogger(LocatorFile2.class) ;
+ private String altDir = null ;
+ private String altDirLogStr = "" ;
+
+ /** Create a LocatorFile */
+ public LocatorFile2() { this(null) ; }
+
+ /** Create a LocatorFile that uses the argument as it's working directory
+ * The working directory should be a UNIX style file name,
+ * (relative or absolute), not a URI.
+ */
+ public LocatorFile2(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.
+
+ /** To a File, after processing the filename for file: or relative filename */
+ public String toFileName(String filenameIRI)
+ {
+ // Do not use : it will ignore the directory.
+ //IRILib.filenameToIRI
+
+ 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) ;
+ }
+ // fn is the file name to use.
+ // If it is relative, and we have a different working directory, prepend that.
+ // "/" is a path separator on Windows as well.
+ if ( altDir != null && ! fn.startsWith("/") && ! fn.startsWith(File.separator) )
+ fn = altDir+"/"+fn ;
+ return fn ;
+ }
+
+ public boolean exists(String fileIRI)
+ {
+ String fn = toFileName(fileIRI) ;
+ if ( fn == null )
+ return false ;
+
+ return exists$(fn) ;
+ }
+
+ private boolean exists$(String fn)
+ {
+ return new File(fn).exists() ;
+ }
+
+ /** Open anything that looks a bit like a file name */
+ @Override
+ public TypedInputStream2 open(String filenameIRI)
+ {
+ String fn = toFileName(filenameIRI) ;
+ if ( fn == null )
+ return null ;
+
+ try {
+ if ( ! exists$(fn) )
+ {
+ 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 = IO.openFileEx(fn) ;
+
+ if ( StreamManager.logAllLookups && log.isTraceEnabled() )
+ log.trace("Found: "+filenameIRI+altDirLogStr) ;
+
+ ContentType ct = Langs.guessContentType(filenameIRI) ;
+ return new TypedInputStream2(in, ct, filenameIRI) ;
+ } catch (IOException ioEx)
+ {
+ // Includes FileNotFoundException
+ // We already tested whether the file exists or not.
+ log.warn("File unreadable (but exists): "+fn+" Exception: "+ioEx.getMessage()) ;
+ return null ;
+ }
+ }
+
+ @Override
+ public String getName()
+ {
+ String tmp = "LocatorFile" ;
+ if ( altDir != null )
+ tmp = tmp+"("+altDir+")" ;
+ return tmp ;
+ }
+}
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorURL2.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorURL2.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorURL2.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorURL2.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,69 @@
+/**
+ * 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.TypedInputStream2 ;
+
+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 not include the ":"
+
+ @Override
+ public TypedInputStream2 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) ;
+ if ( uriSchemeName == null )
+ return false ;
+ uriSchemeName = uriSchemeName.toLowerCase() ;
+ for ( int i = 0 ; i < schemeNames.length ; i++ )
+ {
+ if ( uriSchemeName.equals(schemeNames[i]) )
+ return true ;
+ }
+ return false ;
+ }
+}
+
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorZip.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorZip.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorZip.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/LocatorZip.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,91 @@
+/*
+ * 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.openjena.atlas.web.ContentType ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.Langs ;
+import riot_reader.TypedInputStream2 ;
+
+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 TypedInputStream2 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) ;
+
+ ContentType ct = Langs.guessContentType(filenameOrURI) ;
+ return new TypedInputStream2(in, ct, filenameOrURI) ;
+ }
+ catch (IOException ex)
+ {
+ log.warn("IO Exception opening zip entry: " + filenameOrURI);
+ return null;
+ }
+ }
+ @Override
+ public String getName() { return "LocatorZip("+zipFileName+")" ; }
+
+}
Added: jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/StreamManager.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/StreamManager.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/StreamManager.java (added)
+++ jena/Experimental/riot-reader/src/main/java/riot_reader/fm2/StreamManager.java Thu Aug 16 13:04:49 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.util.ArrayList ;
+import java.util.List ;
+
+import org.openjena.riot.RiotNotFoundException ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.TypedInputStream2 ;
+
+/** 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.
+ * Returns null if not found.
+ */
+ public TypedInputStream2 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 openNoMapOrNull(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. Throws RiotNotFoundException if not found.*/
+ public TypedInputStream2 openNoMap(String filenameOrURI)
+ {
+ TypedInputStream2 in = openNoMapOrNull(filenameOrURI) ;
+ if ( in == null )
+ throw new RiotNotFoundException(filenameOrURI) ;
+ return in ;
+ }
+
+ /** Open a file using the locators of this FileManager
+ * without location mapping. Return null if not found
+ */
+
+ public TypedInputStream2 openNoMapOrNull(String filenameOrURI)
+ {
+ for (Locator loc : handlers)
+ {
+ TypedInputStream2 in = loc.open(filenameOrURI) ;
+ if ( in != null )
+ {
+ if ( log.isDebugEnabled() )
+ log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
+ return in ;
+ }
+ }
+ return null;
+ }
+
+}
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TC_ReaderRIOT.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TC_ReaderRIOT.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TC_ReaderRIOT.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TC_ReaderRIOT.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,37 @@
+/**
+ * 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 org.junit.runner.RunWith ;
+import org.junit.runners.Suite ;
+import org.junit.runners.Suite.SuiteClasses ;
+import riot_reader.fm2.TS_IO2 ;
+
+@RunWith(Suite.class)
+@SuiteClasses(
+{
+ TS_ReaderRIOT.class ,
+ TS_IO2.class
+})
+
+public class TC_ReaderRIOT
+{
+
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TS_ReaderRIOT.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TS_ReaderRIOT.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TS_ReaderRIOT.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TS_ReaderRIOT.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,38 @@
+/**
+ * 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 org.junit.runner.RunWith ;
+import org.junit.runners.Suite ;
+import org.junit.runners.Suite.SuiteClasses ;
+
+@RunWith(Suite.class)
+@SuiteClasses(
+{
+ TestLangRIOT.class
+ , TestFileManagerRIOT.class
+ , TestJenaReaderRIOT.class
+ , TestReadDataset.class
+})
+
+public class TS_ReaderRIOT
+{
+
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TestFileManagerRIOT.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TestFileManagerRIOT.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TestFileManagerRIOT.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TestFileManagerRIOT.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,103 @@
+/**
+ * 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.File ;
+
+import org.junit.AfterClass ;
+import org.junit.BeforeClass ;
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+import org.openjena.riot.RiotNotFoundException ;
+import riot_reader.fm2.FileManager2 ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public class TestFileManagerRIOT extends BaseTest
+{
+ private static final String directory = "testing/RIOT/FileManager" ;
+ private static final String absDirectory = new File("testing/RIOT/FileManager").getAbsolutePath() ;
+
+ private static Context context = new Context() ;
+ private static FileManager2 fileMgr = new FileManager2() ;
+ private static FileManager2 fileMgrStd = null ;
+
+ @BeforeClass static public void beforeClass()
+ {
+ fileMgrStd = FileManager2.get() ;
+ fileMgr.addLocatorFile() ;
+ fileMgr.addLocatorFile(directory) ;
+ fileMgr.addLocatorURL() ;
+ context.put(WebReader2.fileManagerSymbol, fileMgr) ;
+ }
+
+ @AfterClass static public void afterClass()
+ {
+ FileManager2.setGlobalFileManager(fileMgrStd) ;
+ context.remove(WebReader2.fileManagerSymbol) ;
+ }
+
+ @Test public void fm_open_01() { open(directory+"/D.ttl", context) ; }
+ @Test public void fm_open_02() { open("D.ttl", context) ; }
+
+ @Test public void fm_open_03() { open("file:"+directory+"/D.ttl", context) ; }
+ @Test public void fm_open_04() { open("file:D.ttl", context) ; }
+
+ @Test public void fm_open_05() { open(absDirectory+"/D.ttl", context) ; }
+ @Test public void fm_open_06() { open("file://"+absDirectory+"/D.ttl", context) ; }
+
+ @Test (expected=RiotNotFoundException.class)
+ public void fm_open_10() { open("nosuchfile", null) ; }
+
+ @Test public void fm_read_01() { read("D.nt") ; }
+ @Test public void fm_read_02() { read("D.ttl") ; }
+ @Test public void fm_read_03() { read("D.rdf") ; }
+ @Test public void fm_read_04() { read("D.json") ; }
+
+ @Test public void fm_read_11() { read("file:D.nt") ; }
+ @Test public void fm_read_12() { read("file:D.ttl") ; }
+ @Test public void fm_read_13() { read("file:D.rdf") ; }
+ @Test public void fm_read_14() { read("file:D.json") ; }
+
+ // TriG
+ // NQuads
+
+ private static void open(String dataName, Context context)
+ {
+ TypedInputStream2 in ;
+ if ( context != null )
+ in = WebReader2.open(dataName, context) ;
+ else
+ in = WebReader2.open(dataName) ;
+ assertNotNull(in) ;
+ in.close() ;
+ }
+
+ private static void read(String dataName)
+ {
+ FileManager2.setGlobalFileManager(fileMgr) ;
+ Model m = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m, dataName) ;
+ assertTrue(m.size() != 0 ) ;
+ FileManager2.setGlobalFileManager(fileMgrStd) ;
+ }
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TestJenaReaderRIOT.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TestJenaReaderRIOT.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TestJenaReaderRIOT.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TestJenaReaderRIOT.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,202 @@
+/**
+ * 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.FileInputStream ;
+import java.io.IOException ;
+import java.io.StringReader ;
+
+import org.junit.AfterClass ;
+import org.junit.BeforeClass ;
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+import org.openjena.atlas.lib.StrUtils ;
+
+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.sparql.util.Context ;
+import com.hp.hpl.jena.util.FileUtils ;
+
+public class TestJenaReaderRIOT extends BaseTest
+{
+ private static final String directory = "testing/RIOT/Reader" ;
+
+ private static Context context = new Context() ;
+
+ @BeforeClass static public void beforeClass()
+ {
+ WebReader2.wireIntoJena() ;
+ }
+
+ @AfterClass static public void afterClass()
+ {
+ // Unwire?
+ }
+
+ @Test public void read_01() { jenaread("D.nt") ; }
+ @Test public void read_02() { jenaread("D.ttl") ; }
+ @Test public void read_03() { jenaread("D.rdf") ; }
+ @Test public void read_04() { jenaread("D.rdf") ; }
+ @Test public void read_05() { jenaread("D.json") ; }
+
+ @Test public void read_11() { jenaread("D.nt", "N-TRIPLES") ; }
+ @Test public void read_12() { jenaread("D.ttl", "TTL") ; }
+ @Test public void read_13() { jenaread("D.rdf", "RDF/XML") ; }
+ @Test public void read_14() { jenaread("D.rdf", "RDF/XML-ABBREV") ; }
+ @Test public void read_15() { jenaread("D.json", "RDF/JSON") ; }
+
+ @Test public void read_21a() { jenaread("D-nt", "N-TRIPLES") ; }
+ @Test public void read_21b() { jenaread("D-nt", "NTRIPLES") ; }
+ @Test public void read_21c() { jenaread("D-nt", "NT") ; }
+
+ @Test public void read_22a() { jenaread("D-ttl", "TURTLE") ; }
+ @Test public void read_22b() { jenaread("D-ttl", "TTL") ; }
+
+ @Test public void read_23a() { jenaread("D-rdf", "RDF/XML") ; }
+ @Test public void read_23b() { jenaread("D-rdf", "RDFXML") ; }
+ @Test public void read_24() { jenaread("D-json", "RDF/JSON") ; }
+
+ @Test public void read_30()
+ {
+ TypedInputStream2 in = WebReader2.open(filename("D-not-TTL.ttl") );
+ Model m0 = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m0, in.getInput(), Langs.langRDFXML) ;
+ Model m1 = ModelFactory.createDefaultModel() ;
+ // Fails until integration with jena-core as hintlang gets lost.
+ m1.read(in.getInput(), null, "RDF/XML") ;
+ }
+
+ // test read from StringReader..
+ @Test public void read_31()
+ {
+ StringReader s = new StringReader("<s> <p> <p> .") ;
+ Model m = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m, s, null, Langs.langNTriples) ;
+ m.read(s, null, "N-TRIPLES") ;
+ }
+
+ @Test public void read_32()
+ {
+ String x = StrUtils.strjoinNL(
+ "<rdf:RDF",
+ " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"",
+ " xmlns:j.0=\"http://example/\">" ,
+ " <rdf:Description rdf:about=\"http://example/s\">" ,
+ " <j.0:p rdf:resource=\"http://example/o\"/>" ,
+ " </rdf:Description>" ,
+ "</rdf:RDF>") ;
+ StringReader s = new StringReader(x) ;
+ Model m = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m, s, null, Langs.langRDFXML) ;
+ m.read(s, null, "RDF/XML") ;
+ }
+
+ // Stream opening is hardwired into jena!
+ @Test public void read_base_1() { jenaread("D-no-base.ttl", "http://baseuri/", "TTL") ; }
+
+ @Test public void read_input_1() throws IOException
+ { jenaread_stream("D.ttl", "TTL") ; }
+
+ @Test public void read_input_2() throws IOException
+ { jenaread_stream("D.rdf", "RDF/XML") ; }
+
+ private static String filename(String filename) { return directory+"/"+filename ; }
+
+ private static void jenaread_stream(String filename, String lang) throws IOException
+ {
+ filename = filename(filename) ;
+
+ // Read with a base
+ FileInputStream in0 = new FileInputStream(filename) ;
+ Model m0 = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m0, in0, "http://example/base2", Langs.nameToLang(lang)) ;
+ in0.close() ;
+
+ // Read again, but without base
+ FileInputStream in1 = new FileInputStream(filename) ;
+ Model m1 = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m1, in1, Langs.nameToLang(lang)) ;
+ in1.close() ;
+
+ // Fail because Jena core does a look up of lang with ModelCom builtin in RDFReaderF, then calls RIOReader().
+ // 1/ Fix Jena - remove RDFReaderF
+ // 2/ Change RDFReaderF to pass in the language name.
+
+ // Read via Jena API.
+ Model m2 = ModelFactory.createDefaultModel() ;
+ FileInputStream in2 = new FileInputStream(filename) ;
+ m2.read(in2, "http://example/base3", lang) ;
+ in2.close() ;
+
+ String x = FileUtils.readWholeFileAsUTF8(filename) ;
+ Model m3 = ModelFactory.createDefaultModel() ;
+ m2.read(new StringReader(x), "http://example/base4", lang) ;
+ }
+
+ // Web.
+ // TriG
+ // NQ
+
+ private static void jenaread(String dataurl)
+ {
+ dataurl = filename(dataurl) ;
+ Model m = ModelFactory.createDefaultModel() ;
+ m.read(dataurl) ;
+ assertTrue(m.size() != 0 ) ;
+ }
+
+ // Wire in.
+ // +1 one with a base.
+ private static void jenaread(String dataurl, String lang)
+ {
+ // read via WebReader to make sure the test setup is right.
+ dataurl = filename(dataurl) ;
+
+ Model m0 = ModelFactory.createDefaultModel() ;
+ WebReader2.read(m0, dataurl, Langs.nameToLang(lang)) ;
+ assertTrue(m0.size() != 0 ) ;
+
+ Model m1 = ModelFactory.createDefaultModel() ;
+ new RDFReaderFactoryRIOT().getReader(lang).read(m1, dataurl) ;
+ assertTrue(m1.size() != 0 ) ;
+
+// // Read via Jena model API.
+// Model m2 = ModelFactory.createDefaultModel() ;
+// m2.read(dataurl, lang) ;
+// assertTrue(m2.size() != 0 ) ;
+ }
+
+ private static void jenaread(String dataurl, String lang, String base)
+ {
+ dataurl = filename(dataurl) ;
+ Model m = ModelFactory.createDefaultModel() ;
+
+ // This
+ WebReader2.read(m, dataurl, base, Langs.nameToLang(lang)) ;
+ // should be implementation of:
+ m.read(dataurl, lang, base) ;
+ assertTrue(m.size() != 0 ) ;
+
+ Resource s = m.listStatements().next().getSubject() ;
+ assertTrue(s.getURI().startsWith("http://")) ;
+ assertTrue(s.getURI().equals("http://baseuri/s")) ;
+ }
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TestLangRIOT.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TestLangRIOT.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TestLangRIOT.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TestLangRIOT.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,76 @@
+/**
+ * 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 org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+
+public class TestLangRIOT extends BaseTest
+{
+ @Test public void lang_01() { test(Langs.langNTriples, "NT") ; }
+ @Test public void lang_02() { test(Langs.langNTriples, "N-Triples") ; }
+ @Test public void lang_03() { test(Langs.langNTriples, "N-TRIPLES") ; }
+ @Test public void lang_04() { test(Langs.langNTriples, "NTRIPLE") ; }
+ @Test public void lang_05() { test(Langs.langNTriples, "NTRIPLES") ; }
+
+ @Test public void lang_11() { test(Langs.langTurtle, "TTL") ; }
+ @Test public void lang_12() { test(Langs.langTurtle, "TUrtLE") ; }
+
+ @Test public void lang_21() { test(Langs.langRDFXML, "RDF/XML") ; }
+ @Test public void lang_22() { test(Langs.langRDFXML, "RDFXML") ; }
+ @Test public void lang_23() { test(Langs.langRDFXML, "RDF/XML-ABBREV") ; }
+
+ @Test public void lang_30() { test(Langs.langRDFJSON, "RDFJSON") ; }
+ @Test public void lang_31() { test(Langs.langRDFJSON, "RDF/json") ; }
+
+ @Test public void lang_40() { test(Langs.langNQuads, "N-QUADS") ; }
+ @Test public void lang_41() { test(Langs.langNQuads, "NQuads") ; }
+
+ @Test public void lang_50() { test(Langs.langTriG, "TriG") ; }
+ @Test public void lang_51() { test(Langs.langTriG, "trig") ; }
+ @Test public void lang_52() { test(Langs.langTriG, "TRIG") ; }
+
+ @Test public void guess_01() { guess("D.nt", Langs.langNTriples) ; }
+ @Test public void guess_02() { guess("D.ttl.nt", Langs.langNTriples) ; }
+
+ @Test public void guess_03() { guess("D.ttl", Langs.langTurtle) ; }
+
+ @Test public void guess_04() { guess("D.rdf", Langs.langRDFXML) ; }
+ @Test public void guess_05() { guess("D.owl", Langs.langRDFXML) ; }
+
+ @Test public void guess_06() { guess("D.rj", Langs.langRDFJSON) ; }
+ @Test public void guess_07() { guess("D.json", Langs.langRDFJSON) ; }
+
+ @Test public void guess_08() { guess("D.nq", Langs.langNQuads) ; }
+ @Test public void guess_09() { guess("D.trig", Langs.langTriG) ; }
+
+ private void test(Lang2 expected, String string)
+ {
+ Lang2 lang = Langs.nameToLang(string) ;
+ assertEquals(expected, lang) ;
+ }
+
+ private void guess(String string, Lang2 expected)
+ {
+ Lang2 lang = Langs.guess(string) ;
+ assertEquals(expected, lang) ;
+ }
+
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/TestReadDataset.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/TestReadDataset.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/TestReadDataset.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/TestReadDataset.java Thu Aug 16 13:04:49 2012
@@ -0,0 +1,113 @@
+/**
+ * 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.FileInputStream ;
+import java.io.IOException ;
+
+import org.junit.AfterClass ;
+import org.junit.BeforeClass ;
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+
+import com.hp.hpl.jena.query.Dataset ;
+import com.hp.hpl.jena.query.DatasetFactory ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public class TestReadDataset extends BaseTest
+{
+ private static final String directory = "testing/RIOT/Reader" ;
+
+ private static Context context = new Context() ;
+
+ @BeforeClass static public void beforeClass()
+ {
+ WebReader2.wireIntoJena() ;
+ }
+
+ @AfterClass static public void afterClass()
+ {
+ // Unwire?
+ }
+
+ @Test public void read_01() { read("D.nq") ; }
+ @Test public void read_02() { read("D.trig") ; }
+ @Test public void read_03() { read("D.nq", Langs.langNQuads) ; }
+ @Test public void read_04() { read("D.trig", Langs.langTriG) ; }
+
+ @Test public void read_11() { read("D.nq", "N-Quads") ; }
+
+ @Test public void read_12() { read("D.nq", "NQuads") ; }
+ @Test public void read_13() { read("D.nq", "NQ") ; }
+ @Test public void read_14() { read("D.trig", "TriG") ; }
+ @Test public void read_15() { read("D.trig", "trig") ; }
+
+ @Test public void read_21() { read("D-nq", Langs.langNQuads) ; }
+ @Test public void read_22() { read("D-trig", Langs.langTriG) ; }
+ @Test public void read_23() { read("D-nq", "NQuads") ; }
+ @Test public void read_24() { read("D-trig", "TRIG") ; }
+
+ @Test public void read_input_1() throws IOException
+ { read_stream("D.nq", "NQ") ; }
+
+ @Test public void read_input_2() throws IOException
+ { read_stream("D.trig", "trig") ; }
+
+ private static String filename(String filename) { return directory+"/"+filename ; }
+
+ // Base.
+
+ private static void read(String dataurl) { read(dataurl, (Lang2)null) ; }
+
+ private static void read(String dataurl, String lang)
+ {
+ read(dataurl, Langs.nameToLang(lang)) ;
+ }
+
+ private static void read(String dataurl, Lang2 lang)
+ {
+ dataurl = filename(dataurl) ;
+ Dataset ds = DatasetFactory.createMem() ;
+ WebReader2.read(ds, dataurl, lang) ;
+ }
+
+ private static void read_stream(String filename, String lang) throws IOException
+ {
+ read_stream(filename, Langs.nameToLang(lang)) ;
+ }
+
+ private static void read_stream(String filename, Lang2 lang) throws IOException
+ {
+ filename = filename(filename) ;
+
+ // Read with a base
+ Dataset ds0 = DatasetFactory.createMem() ;
+ FileInputStream in0 = new FileInputStream(filename) ;
+ WebReader2.read(ds0, in0, "http://example/base2", lang) ;
+ in0.close() ;
+
+ // Read again, but without base
+ Dataset ds1 = DatasetFactory.createMem() ;
+ FileInputStream in1 = new FileInputStream(filename) ;
+ WebReader2.read(ds1, in1, null, lang) ;
+ in1.close() ;
+ }
+
+}
+
Added: jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TS_IO2.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TS_IO2.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TS_IO2.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TS_IO2.java Thu Aug 16 13:04:49 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;
+
+
+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: jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TestFileManager.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TestFileManager.java?rev=1373816&view=auto
==============================================================================
--- jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TestFileManager.java (added)
+++ jena/Experimental/riot-reader/src/test/java/riot_reader/fm2/TestFileManager.java Thu Aug 16 13:04:49 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.io.InputStream ;
+
+import junit.framework.TestCase ;
+import junit.framework.TestSuite ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+import riot_reader.fm2.TestLocationMapper ;
+
+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) {}
+ }
+}