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/24 16:51:15 UTC

svn commit: r1293304 - in /incubator/jena: Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java

Author: andy
Date: Fri Feb 24 15:51:14 2012
New Revision: 1293304

URL: http://svn.apache.org/viewvc?rev=1293304&view=rev
Log: (empty)

Added:
    incubator/jena/Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java
Modified:
    incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java
    incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java

Added: incubator/jena/Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java?rev=1293304&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/org/openjena/riot/RiotNotFoundException.java Fri Feb 24 15:51:14 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 org.openjena.riot;
+
+public class RiotNotFoundException extends RiotException
+{
+    public RiotNotFoundException()                          { super() ; }
+    public RiotNotFoundException(String msg)                { super(msg) ; }
+    public RiotNotFoundException(Throwable th)              { super(th) ; }
+    public RiotNotFoundException(String msg, Throwable th)  { super(msg, th) ; }
+}

Modified: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java?rev=1293304&r1=1293303&r2=1293304&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/TestWebReader.java Fri Feb 24 15:51:14 2012
@@ -18,10 +18,82 @@
 
 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 TestWebReader extends BaseTest
 {
+    private static final String directory = "testing/FileManager" ;
+    private static final String absDirectory = new File("testing/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) ;
+    }
+    
+    @Test public void open_01() { open(directory+"/D.ttl", context) ; }
+    @Test public void open_02() { open("D.ttl", context) ; }
+
+    @Test public void open_03() { open("file:"+directory+"/D.ttl", context) ; }
+    @Test public void open_04() { open("file:D.ttl", context) ; }
+    
+    @Test public void open_05() { open(absDirectory+"/D.ttl", context) ; }
+    @Test public void open_06() { open("file://"+absDirectory+"/D.ttl", context) ; }
+    
+    @Test (expected=RiotNotFoundException.class)
+    public void open_10() { open("nosuchfile", null) ; }
+    
+    @Test public void read_01() { read("D.nt") ; }
+    @Test public void read_02() { read("D.ttl") ; }
+    @Test public void read_03() { read("D.rdf") ; }
+    @Test public void read_04() { read("D.json") ; }
 
+    @Test public void read_11() { read("file:D.nt") ; }
+    @Test public void read_12() { read("file:D.ttl") ; }
+    @Test public void read_13() { read("file:D.rdf") ; }
+    @Test public void read_14() { read("file:D.json") ; }
+    
+    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) ;
+    }
 }
 

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=1293304&r1=1293303&r2=1293304&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 Fri Feb 24 15:51:14 2012
@@ -26,13 +26,13 @@ import org.openjena.riot.ContentType ;
 import org.openjena.riot.Lang ;
 import org.openjena.riot.RiotException ;
 import org.openjena.riot.RiotLoader ;
+import org.openjena.riot.RiotNotFoundException ;
 import org.openjena.riot.SysRIOT ;
 import org.openjena.riot.WebContent ;
 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 ;
@@ -40,6 +40,8 @@ import com.hp.hpl.jena.rdf.model.Model ;
 import com.hp.hpl.jena.rdf.model.ModelFactory ;
 import com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl ;
 import com.hp.hpl.jena.sparql.util.Context ;
+import com.hp.hpl.jena.sparql.util.Symbol ;
+import com.hp.hpl.jena.sparql.util.Utils ;
 
 /** General purpose reader framework.
  */
@@ -162,9 +164,13 @@ public class WebReader2
         read(sink, uri, hintLang, context) ;
     }
         
+    private static String base = "http://jena.apache.org/riot/" ; 
+    private static String fileManagerSymbolStr = base+"filemanager" ; 
+    public static Symbol fileManagerSymbol = Symbol.create(fileManagerSymbolStr) ; 
+    
     private static void read(Sink<Triple> sink, String uri, String hintLang, Context context)
     {
-        TypedInputStream2 in = open(uri) ;
+        TypedInputStream2 in = open(uri, context) ;
         if ( in == null )
             throw new RiotException("Not found: "+uri) ;
         process(sink, uri, in, hintLang, context) ;
@@ -172,13 +178,24 @@ public class WebReader2
     }
 
     // --> WebContent, basic formats (not RDFa) or return null
-
-    private static TypedInputStream2 open(String filenameOrURI)
+    public static TypedInputStream2 open(String filenameOrURI)
+    { return open(filenameOrURI, (Context)null) ; }
+    
+    static TypedInputStream2 open(String filenameOrURI, Context context)
     {
-        FileManager2 fMgr = new FileManager2() ;
-        fMgr.addLocatorFile() ;
-        fMgr.addLocator(new LocatorURL2()) ;
+        FileManager2 fMgr = FileManager2.get() ;
+        if ( context != null )
+        {
+            try { fMgr = (FileManager2)context.get(fileManagerSymbol, context) ; }
+            catch (ClassCastException ex) 
+            { log.warn("Context symbol '"+fileManagerSymbol+"' is not a "+Utils.classShortName(FileManager2.class)) ; }
+        }
         
+        return open(filenameOrURI, fMgr) ;
+    }
+    
+    static TypedInputStream2 open(String filenameOrURI, FileManager2 fMgr)
+    {
         TypedInputStream2 in = fMgr.open(filenameOrURI) ;
             
         if ( in == null )
@@ -186,7 +203,8 @@ public class WebReader2
             if ( log.isDebugEnabled() )
                 //log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
                 log.debug("Not Found: "+filenameOrURI) ;
-            return null ;
+            throw new RiotNotFoundException("Not found: "+filenameOrURI) ;
+            //return null ;
         }
         if ( log.isDebugEnabled() )
             //log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
@@ -211,7 +229,7 @@ public class WebReader2
         String _ct = null ;
         if ( in.getMediaType() != null )
             _ct = WebContent.contentTypeCanonical(in.getContentType()) ;
-        if ( _ct == null || isTextPlain )
+        if ( ( _ct == null || isTextPlain ) && hintLang != null )
             _ct = hintLang ;
         // Error? ....
         ContentType ct = ContentType.parse(_ct) ;