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 2020/11/24 18:47:50 UTC

[jena] 01/01: Investigation: Windows+file:

This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch windows-file
in repository https://gitbox.apache.org/repos/asf/jena.git

commit 5f68697e56c38919efd99238ebb7726cec83829f
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Tue Nov 24 18:40:53 2020 +0000

    Investigation: Windows+file:
---
 .../jena/riot/system/stream/LocatorFile.java       | 51 ++++++++++++++--------
 .../apache/jena/riot/stream/TestStreamManager.java | 38 ++++++++--------
 2 files changed, 54 insertions(+), 35 deletions(-)

diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/stream/LocatorFile.java b/jena-arq/src/main/java/org/apache/jena/riot/system/stream/LocatorFile.java
index 984846a..510cb8f 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/stream/LocatorFile.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/stream/LocatorFile.java
@@ -41,7 +41,9 @@ public class LocatorFile implements Locator
 {
     // Implementation note:
     // Java7: Path.resolve may provide an answer from the intricies of MS Windows
-    
+
+    public static boolean DEBUG = false;
+
     static Logger log = LoggerFactory.getLogger(LocatorFile.class) ;
     private final String thisDir ;
     private final String thisDirLogStr ;
@@ -50,7 +52,7 @@ public class LocatorFile implements Locator
      * Relative file names are relative to the working directory of the JVM.
      */
     public LocatorFile() { this(null) ; }
-    
+
     /** Create a LocatorFile that uses the argument as it's working directory.
      * <p>
      * The working directory should be a UNIX style file name,
@@ -58,7 +60,7 @@ public class LocatorFile implements Locator
      * <p>
      * For MS Window, if asked to {@link #open} a file name with a drive letter,
      * the code assumes it is not relative to the working directory
-     * of this {@code LocatorFile}.  
+     * of this {@code LocatorFile}.
      */
     public LocatorFile(String dir)
     {
@@ -71,27 +73,32 @@ public class LocatorFile implements Locator
         else
             thisDirLogStr = "" ;
         thisDir = dir ;
+
+        if ( DEBUG ) {
+            System.out.println("LocatorFile("+dir+") -> thisDir="+dir+"  thisDirLogStr="+thisDir);
+        }
+
     }
 
     /** Processing the filename for file: or relative filename
-     *  and return a filename suitable for file operations. 
+     *  and return a filename suitable for file operations.
      */
     public String toFileName(String filenameIRI)
     {
-        // Do not use directly : it will ignore the directory. 
+        // Do not use directly : 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 ) 
+        if ( scheme != null )
         {
             if ( scheme.length() == 1 )
             {
                 // Not perfect for MS Windows but if thisDir is set then
                 // the main use case is resolving relative (no drive)
                 // filenames against thisDir. Treat the presence of a
-                // drive letter as making this a JVM relative filename. 
+                // drive letter as making this a JVM relative filename.
                 return fn ;
             }
             else if ( scheme.length() > 1 )
@@ -101,14 +108,14 @@ public class LocatorFile implements Locator
                     return null ;
                 fn = IRILib.IRIToFilename(filenameIRI) ;
                 // fall through
-            } 
+            }
         }
         // fn is the file name to use.
         return absolute(fn) ;
     }
 
     /** Make a filename (no URI scheme, no windows drive) absolute if there is
-     * a setting for directory name thisDir  
+     * a setting for directory name thisDir
      */
     private String absolute(String fn)
     {
@@ -116,7 +123,7 @@ public class LocatorFile implements Locator
             fn = thisDir+File.separator+fn ;
         return fn ;
     }
-    
+
     public String getThisDir()
     {
         return thisDir ;
@@ -133,10 +140,10 @@ public class LocatorFile implements Locator
         String fn = toFileName(fileIRI) ;
         if ( fn == null )
             return false ;
-        
+
         return exists$(fn) ;
     }
-    
+
     private boolean exists$(String fn)
     {
         if ( fn.equals("-") )
@@ -144,14 +151,22 @@ public class LocatorFile implements Locator
         return new File(fn).exists() ;
     }
 
-    /** Open anything that looks a bit like a file name */ 
+    /** Open anything that looks a bit like a file name */
     @Override
     public TypedInputStream open(String filenameIRI)
     {
+        if ( LocatorFile.DEBUG ) {
+            System.out.println("> LocatiorFile.open: "+filenameIRI);
+            System.out.println("  thisDir = "+thisDir);
+            System.out.println("  thisDirLogStr = "+thisDirLogStr);
+        }
+
         String fn = toFileName(filenameIRI) ;
+        if ( LocatorFile.DEBUG )
+            System.out.println("  LocatiorFile.open#toFileName: "+fn);
         if ( fn == null )
             return null ;
-        
+
         try {
             if ( ! exists$(fn) )
             {
@@ -163,13 +178,13 @@ public class LocatorFile implements Locator
             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+thisDirLogStr) ;
-            
+
             ContentType ct = RDFLanguages.guessContentType(filenameIRI) ;
             return new TypedInputStream(in, ct, filenameIRI) ;
         } catch (IOException ioEx)
@@ -180,7 +195,7 @@ public class LocatorFile implements Locator
             return null ;
         }
     }
-    
+
     @Override
     public String getName()
     {
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/stream/TestStreamManager.java b/jena-arq/src/test/java/org/apache/jena/riot/stream/TestStreamManager.java
index e8d84a3..2d39e8d 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/stream/TestStreamManager.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/stream/TestStreamManager.java
@@ -42,43 +42,43 @@ public class TestStreamManager
 {
     private static final String directory = "testing/RIOT/StreamManager" ;
     private static final String absDirectory = new File(directory).getAbsolutePath() ;
-    
+
     private static StreamManager streamMgrDir ;
     private static StreamManager streamMgrHere ;
     private static StreamManager streamMgrNull ;
     private static StreamManager streamMgrStd ;
-    
+
     @BeforeClass static public void beforeClass()
-    { 
+    {
         streamMgrStd = StreamManager.get() ;
         streamMgrDir = new StreamManager() ;
         // Not current directory.
         streamMgrDir.addLocator(new LocatorFile(directory)) ;
         streamMgrDir.addLocator(new LocatorHTTP()) ;
-        
+
         streamMgrHere = new StreamManager() ;
         // Not current directory.
         streamMgrHere.addLocator(new LocatorFile()) ;
         streamMgrHere.addLocator(new LocatorHTTP()) ;
-        
+
         streamMgrNull = new StreamManager() ;
     }
-    
+
     @AfterClass static public void afterClass()
-    { 
+    {
         StreamManager.setGlobal(streamMgrStd) ;
     }
-    
+
     private static Context context(StreamManager streamMgr)
     {
         Context context = new Context() ;
         context.put(SysRIOT.sysStreamManager, streamMgr) ;
         return context ;
     }
-    
+
     @Test public void fm_open_01()  { open(streamMgrNull, directory+"/D.ttl", context(streamMgrHere)) ; }
     @Test public void fm_open_02()  { open(streamMgrHere, directory+"/D.ttl", null) ; }
-    
+
     @Test public void fm_open_03()  { open(streamMgrNull,  "D.ttl", context(streamMgrDir)) ; }
     @Test public void fm_open_04()  { open(streamMgrDir, "D.ttl", null) ; }
 
@@ -87,17 +87,17 @@ public class TestStreamManager
 
     @Test public void fm_open_07()  { open(streamMgrHere, "file:D.ttl", context(streamMgrDir)) ; }
     @Test public void fm_open_08()  { open(streamMgrDir, "file:D.ttl", null) ; }
-    
+
     @Test public void fm_open_09()  { open(streamMgrHere, absDirectory+"/D.ttl", null) ; }
     @Test public void fm_open_10()  { open(streamMgrDir,  absDirectory+"/D.ttl", null) ; }
     @Test public void fm_open_11()  { open(streamMgrDir,  "file://"+absDirectory+"/D.ttl", null) ; }
     @Test public void fm_open_12()  { open(streamMgrHere, "file://"+absDirectory+"/D.ttl", null) ; }
-    
+
     @Test (expected=RiotNotFoundException.class)
     public void fm_open_20()        { open(null, "nosuchfile", context(streamMgrDir)) ; }
     @Test (expected=RiotNotFoundException.class)
     public void fm_open_21()        { open(streamMgrHere, "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") ; }
@@ -109,24 +109,28 @@ public class TestStreamManager
     @Test public void fm_read_13()  { read("file:D.rdf") ; }
     @Test public void fm_read_14()  { read("file:D.rj") ; }
     @Test public void fm_read_15()  { read("file:D.jsonld") ; }
-    
+
     // TriG
     // NQuads
-    
+
     private static void open(StreamManager streamMgr, String dataName, Context context)
     {
+        boolean bool = LocatorFile.DEBUG;
+        LocatorFile.DEBUG = true;
+
         StreamManager.setGlobal(streamMgr) ;
         try {
-            TypedInputStream in = ( context != null ) 
+            TypedInputStream in = ( context != null )
                 ? RDFDataMgr.open(dataName, context)
                 : RDFDataMgr.open(dataName) ;
             assertNotNull(in) ;
             IO.close(in) ;
         } finally {
             StreamManager.setGlobal(streamMgrStd) ;
+            LocatorFile.DEBUG = bool;
         }
     }
-    
+
     private static void read(String dataName)
     {
         try {