Repository: jena
Updated Branches:
  refs/heads/master b6d83d7e6 -> c5fbfdc1b


Converting IRIs to filenames needs to handle /C:/ cases on MS-Windows.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/c5fbfdc1
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/c5fbfdc1
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/c5fbfdc1

Branch: refs/heads/master
Commit: c5fbfdc1bff0b91a8dbf1696df5ab41abcb0c6cb
Parents: b6d83d7
Author: Andy Seaborne <[email protected]>
Authored: Fri Jul 14 18:03:34 2017 +0100
Committer: Andy Seaborne <[email protected]>
Committed: Fri Jul 14 18:03:34 2017 +0100

----------------------------------------------------------------------
 .../java/org/apache/jena/atlas/lib/IRILib.java  | 31 +++++++++++++++-----
 1 file changed, 24 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/c5fbfdc1/jena-base/src/main/java/org/apache/jena/atlas/lib/IRILib.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/lib/IRILib.java 
b/jena-base/src/main/java/org/apache/jena/atlas/lib/IRILib.java
index 6c7e8fc..18f3b94 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/lib/IRILib.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/IRILib.java
@@ -27,6 +27,8 @@ import org.apache.jena.base.Sys ;
 /** Operations related to IRIs */
 public class IRILib
 {
+    // Tests - see also TestFilenameProcessing
+    
     // http://www.w3.org/TR/xpath-functions/#func-encode-for-uri
     // Encodes delimiters.
     
@@ -119,6 +121,12 @@ public class IRILib
             fn = iri.substring("file://".length()) ;
         else
             fn = iri.substring("file:".length()) ;
+        // MS Windows: we can have 
+        //  file:///C:/path or file:/C:/path
+        // At this point, we have a filename of /C:/
+        // so need strip the leading "/"
+        fn = fixupWindows(fn);
+        
         return decode(fn) ;
     }
     
@@ -128,13 +136,10 @@ public class IRILib
         // Make Absolute filename.
 
         boolean trailingSlash = fn.endsWith("/") ;
-        if ( Sys.isWindows ) {
-            // Can be "/C:/" on windows :-(
-            // This happens because of URL.toString.
-            if ( fn.length() >= 3 && fn.charAt(0) == '/' && windowsDrive(fn, 
1))
-                fn = fn.substring(1) ;
-        }
-        
+
+        // To get Path.toAbsolutePath to work, we need to convert /C:/ to C:/
+        // then back again.
+        fn = fixupWindows(fn) ;
         fn = Paths.get(fn).toAbsolutePath().normalize().toString() ;
         
         if ( trailingSlash && ! fn.endsWith("/") )
@@ -156,6 +161,18 @@ public class IRILib
         return "file://"+fn ;
     }
     
+    // Case of Windows /C:/ which can come from URL.toString 
+    // giving file:/C:/ and decoding file:///C:/ 
+    private static String fixupWindows(String fn) {
+        if ( Sys.isWindows && 
+             fn.length() >= 3 && fn.charAt(0) == '/' && windowsDrive(fn, 1))
+             fn = fn.substring(1) ;
+        return fn;
+    }
+    
+    /** Does filename {@code fn} look like a windows-drive rooted file path?
+     * The test is can we find "C:" at location {@code i}. 
+     */
     private static boolean windowsDrive(String fn, int i) {
         return 
             fn.length() >= 2+i && 

Reply via email to