[Arakhnę-Dev] [159] Bug fix: FileSystem dirname does not reply current directory.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 159
Author:   galland
Date:     2010-06-15 11:17:53 +0200 (Tue, 15 Jun 2010)
Log Message:
-----------
Bug fix: FileSystem dirname does not reply current directory.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-06-15 08:11:52 UTC (rev 158)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-06-15 09:17:53 UTC (rev 159)
@@ -263,7 +263,15 @@
 		if (idx==path.length()-1)
 			idx = path.lastIndexOf(URL_PATH_SEPARATOR_CHAR, path.length()-2);
 
-		path = (idx<0) ? URL_PATH_SEPARATOR : path.substring(0, idx+1);
+		if (idx<0) {
+			if (URISchemeType.getSchemeType(filename).isFileBasedScheme())
+				path = CURRENT_DIRECTORY;
+			else
+				path = URL_PATH_SEPARATOR;
+		}
+		else {
+			path = path.substring(0, idx+1);
+		}
 				
 		try {
 			if (prefix!=null) {
@@ -280,8 +288,19 @@
 			return uri.toURL();
 		}
 		catch (Throwable _) {
-			return null;
+			//
 		}
+
+		try {
+			return new URL(
+					filename.getProtocol(), 
+					filename.getHost(), 
+					path);
+		}
+		catch (Throwable _) {
+			//
+		}
+		return null;
 	}
 	
 	/** Replies the basename of the specified file with the extension.
@@ -1368,7 +1387,9 @@
 	 * @param url
 	 * @return <code>true</code> if the given url is a "file", "http", 
 	 * "https", "ftp", "ssh", "jar" or "resource", otherwise <code>false</code>.
+	 * @deprecated see {@link URISchemeType#isFileBasedScheme()} 
 	 */
+	@Deprecated
 	public static boolean isFileBasedURL(URL url) {
 		if (url!=null) {
 			return isFileBasedScheme(URISchemeType.getSchemeType(url));
@@ -1382,22 +1403,12 @@
 	 * @return <code>true</code> if the given scheme is a "file", "http", 
 	 * "https", "ftp", "ssh", "jar" or "resource", otherwise <code>false</code>.
 	 * @since 5.0
+	 * @deprecated see {@link URISchemeType#isFileBasedScheme()}
 	 */
+	@Deprecated
 	public static boolean isFileBasedScheme(URISchemeType scheme) {
 		if (scheme!=null) {
-			return (scheme==URISchemeType.FILE
-					||
-					scheme==URISchemeType.HTTP
-					||
-					scheme==URISchemeType.HTTPS
-					||
-					scheme==URISchemeType.FTP
-					||
-					scheme==URISchemeType.SSH
-					||
-					scheme==URISchemeType.JAR
-					||
-					scheme==URISchemeType.RESOURCE);
+			return scheme.isFileBasedScheme();
 		}
 		return false;
 	}

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java	2010-06-15 08:11:52 UTC (rev 158)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java	2010-06-15 09:17:53 UTC (rev 159)
@@ -45,7 +45,12 @@
 	 * the Internet. It is used to transfer almost all of 
 	 * the documents you download using your Web browser. 
 	 */
-	HTTP,
+	HTTP {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 	
 	/**
 	 * One of the most widely used URL scheme is the https scheme. 
@@ -63,7 +68,12 @@
 	 * specified in
 	 * <a href="http://tools.ietf.org/html/rfc2660";>RFC 2660</a>.
 	 */
-	HTTPS,
+	HTTPS {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 	
 	/**
 	 * The ftp scheme is very similar to the http scheme, and is 
@@ -96,7 +106,12 @@
 	 * if you're going to be paranoid, at least do it for 
 	 * the right reasons. 
 	 */
-	FTP,
+	FTP {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 	
 	/**
 	 * The file scheme is used to point to files on your computer. 
@@ -123,7 +138,12 @@
 	 * Unix uses slashes, Windows uses backslashes, Macintosh and 
 	 * other operating systems use other conventions. 
 	 */
-	FILE,
+	FILE {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 	
 	/**
 	 * The mailto scheme is an example of an opaque URI 
@@ -138,7 +158,12 @@
 	 * mailto:someone@xxxxxxxxxxx?subject=Feedback
 	 * </code></pre>
 	 */
-	MAILTO,
+	MAILTO {
+		@Override
+		public boolean isFileBasedScheme() {
+			return false;
+		}
+	},
 	
 	/**
 	 * The news scheme is another opaque URL scheme. 
@@ -159,7 +184,12 @@
 	 * newsgroups and can be used to refer to Usenet 
 	 * in general. 
 	 */
-	NEWS,
+	NEWS {
+		@Override
+		public boolean isFileBasedScheme() {
+			return false;
+		}
+	},
 	
 	/**
 	 * The telnet scheme has identical syntax to the ftp scheme, 
@@ -174,7 +204,12 @@
 	 * with password "password" on port 35 of 
 	 * somehost.internet.com.
 	 */
-	TELNET,
+	TELNET {
+		@Override
+		public boolean isFileBasedScheme() {
+			return false;
+		}
+	},
 	
 	/**
 	 * The ssh scheme (also known as sftp - for Secure FTP) 
@@ -205,7 +240,12 @@
 	 * confidentiality and integrity of data over an insecure 
 	 * network, such as the Internet.
 	 */
-	SSH,
+	SSH {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 	
 	/**
 	 * The jar scheme describes a Java ARchive (JAR) file or 
@@ -241,7 +281,12 @@
 	 * of the Jar file.</li>
 	 * </ul>
 	 */
-	JAR,
+	JAR {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 
 	/**
 	 * In the Java programming language a resource is a piece of data 
@@ -265,12 +310,22 @@
 	 * Because the pathname here represents a path name in the 
 	 * class paths, the slashes are mandatory. 
 	 */
-	RESOURCE,
+	RESOURCE {
+		@Override
+		public boolean isFileBasedScheme() {
+			return true;
+		}
+	},
 
 	/**
 	 * This value indicates that the scheme is not recognized. 
 	 */
-	UNSUPPORTED;
+	UNSUPPORTED {
+		@Override
+		public boolean isFileBasedScheme() {
+			return false;
+		}
+	};
 	
 	/** Replies the scheme string ended with a column character.
 	 * 
@@ -410,6 +465,13 @@
 			return getSchemeType(uri.getScheme());
 		return URISchemeType.UNSUPPORTED;		
 	}
+	
+	/** Replies if this URI scheme represents a local or remote file.
+	 * 
+	 * @return <code>true</code> if this scheme is file-based,
+	 * otherwise <code>false</code>
+	 */
+	public abstract boolean isFileBasedScheme();
 
 }
 

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-06-15 08:11:52 UTC (rev 158)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-06-15 09:17:53 UTC (rev 159)
@@ -181,12 +181,17 @@
 	 * @throws MalformedURLException 
 	 */
 	public void testDirnameURL() throws MalformedURLException {
+		assertEquals(new URL("file", "", "."), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+				FileSystem.dirname(new URL("file", "", "marbre.jpg"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+		assertEquals(new URL("http", "www.arakhne.org", "."), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+				FileSystem.dirname(new URL("http", "arakhne.org", "marbre.jpg"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 		assertEquals(pu1, FileSystem.dirname(u1));
 		assertEquals(pu2, FileSystem.dirname(u2));
 		assertEquals(pu3, FileSystem.dirname(u3));
 		assertEquals(pu7, FileSystem.dirname(u7));
 		assertEquals(pu13, FileSystem.dirname(u13));
-		assertEquals(new URL("file:///"), FileSystem.dirname(new URL("file:///a/"))); //$NON-NLS-1$ //$NON-NLS-2$
+		assertEquals(new URL("file", "", "/"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+				FileSystem.dirname(new URL("file:///a/"))); //$NON-NLS-1$
 		assertNull(FileSystem.dirname(new URL("file://"))); //$NON-NLS-1$
 	}
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/