[Arakhnę-Dev] [281] * Bug fix: File class on Windows contains the "\" character.

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


Revision: 281
Author:   galland
Date:     2011-09-03 23:18:23 +0200 (Sat, 03 Sep 2011)
Log Message:
-----------
* Bug fix: File class on Windows contains the "\" character. File#getPath() replies a path with "\". They are incompatible with the URL standard. So it caused invalid jar URL building.

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

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-09-01 17:16:53 UTC (rev 280)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-09-03 21:18:23 UTC (rev 281)
@@ -118,6 +118,8 @@
 	private static final Random RANDOM = new Random();
 	
 	private static final DeleteOnExitHook deleteOnExitHook = new DeleteOnExitHook();
+	
+	private static Boolean isFileCompatibleWithURL = null;
 
 	/** Decode the given string.
 	 * 
@@ -129,7 +131,41 @@
 		if (s==null) return null;
 		return URLDecoder.decode(s, Charset.defaultCharset().displayName());
 	}
+	
+	/** Decode the given file to obtain a string representation
+	 * which is compatible with the URL standard.
+	 * This function was introduced to have a work around
+	 * on the '\' character on Windows operating system.
+	 * 
+	 * @since 6.2
+	 */
+	private static String getFilePath(File f) {
+		if (f==null) return null;
+		return getFilePath(f.getPath());
+	}
 
+	/** Decode the given file to obtain a string representation
+	 * which is compatible with the URL standard.
+	 * This function was introduced to have a work around
+	 * on the '\' character on Windows operating system.
+	 * 
+	 * @since 6.2
+	 */
+	private static String getFilePath(String f) {
+		if (f==null) return null;
+		if (isFileCompatibleWithURL==null) {
+			isFileCompatibleWithURL = Boolean.valueOf(
+					URL_PATH_SEPARATOR.equals(File.separator));
+		}
+		String filePath = f;
+		if (!isFileCompatibleWithURL) {
+			filePath = filePath.replaceAll(
+					Pattern.quote(File.separator),
+					Matcher.quoteReplacement(URL_PATH_SEPARATOR));
+		}
+		return filePath;
+	}
+
 	/** Replies if the given URL has a jar scheme.
 	 * 
 	 * @param url
@@ -182,7 +218,7 @@
 	 */
 	public static URL toJarURL(File jarFile, File insideFile) throws MalformedURLException {
 		if (jarFile==null || insideFile==null) return null;
-		return toJarURL(jarFile, insideFile.getPath());
+		return toJarURL(jarFile, getFilePath(insideFile));
 	}
 
 	/** Replies the jar-schemed URL composed of the two given components.
@@ -193,18 +229,7 @@
 	 * @throws MalformedURLException when the URL is malformed.
 	 */
 	public static URL toJarURL(File jarFile, String insideFile) throws MalformedURLException {
-		if (jarFile==null || insideFile==null) return null;
-		StringBuffer buf = new StringBuffer(URISchemeType.JAR.toString());
-		buf.append(jarFile.toURI().toURL().toExternalForm());
-		buf.append(JAR_URL_FILE_ROOT);
-		String path = insideFile.replace(File.separatorChar, URL_PATH_SEPARATOR_CHAR);
-		if (path.startsWith(URL_PATH_SEPARATOR)) {
-			buf.append(path.substring(URL_PATH_SEPARATOR.length()));
-		}
-		else {
-			buf.append(path);
-		}
-		return new URL(buf.toString());
+		return toJarURL(jarFile.toURI().toURL(), insideFile);
 	}
 
 	/** Replies the jar-schemed URL composed of the two given components.
@@ -216,7 +241,7 @@
 	 */
 	public static URL toJarURL(URL jarFile, File insideFile) throws MalformedURLException {
 		if (jarFile==null || insideFile==null) return null;
-		return toJarURL(jarFile, insideFile.getPath());
+		return toJarURL(jarFile, getFilePath(insideFile));
 	}
 
 	/** Replies the jar-schemed URL composed of the two given components.
@@ -231,7 +256,7 @@
 		StringBuffer buf = new StringBuffer();
 		buf.append(jarFile.toExternalForm());
 		buf.append(JAR_URL_FILE_ROOT);
-		String path = insideFile.replace(File.separatorChar, URL_PATH_SEPARATOR_CHAR);
+		String path = getFilePath(insideFile);
 		if (path.startsWith(URL_PATH_SEPARATOR)) {
 			buf.append(path.substring(URL_PATH_SEPARATOR.length()));
 		}
@@ -284,7 +309,7 @@
 	 */
 	public static URL dirname(File filename) {
 		if (filename==null) return null;
-		String parent = filename.getParent();
+		String parent = getFilePath(filename.getParent());
 		try {
 			if (parent==null || "".equals(parent)) { //$NON-NLS-1$
 				if (filename.isAbsolute()) return null;
@@ -313,7 +338,7 @@
 		String path;
 		if (isJarURL(filename)) {
 			prefix = getJarURL(filename);
-			path = getJarFile(filename).getPath();
+			path = getFilePath(getJarFile(filename));
 		}
 		else
 			path = filename.getPath();
@@ -517,23 +542,26 @@
 	 */
 	public static String shortBasename(String filename) {
 		if (filename==null) return null;
-		assert(!isWindowsNativeFilename(filename));
+		if (isWindowsNativeFilename(filename)) {
+			return shortBasename(normalizeWindowsNativeFilename(filename));
+		}
+		String normalizedFilename = getFilePath(filename);
 		int idx;
-		int end = filename.length();
+		int end = normalizedFilename.length();
 		do {
 			end--;
-			idx = filename.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end);
+			idx = normalizedFilename.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end);
 		}
 		while (idx>=0 && end>=0 && idx>=end);
 		String basename;
 		if (idx<0) {
-			if (end<filename.length()-1)
-				basename = filename.substring(0, end+1);
+			if (end<normalizedFilename.length()-1)
+				basename = normalizedFilename.substring(0, end+1);
 			else
-				basename = filename;
+				basename = normalizedFilename;
 		}
 		else
-			basename = filename.substring(idx+1, end+1);
+			basename = normalizedFilename.substring(idx+1, end+1);
 
 		idx = basename.indexOf(getFileExtensionCharacter());
 		if (idx<0) return basename;
@@ -669,9 +697,8 @@
 		if (filename==null) return new String[0];
 		String path;
 		if (isJarURL(filename))
-			path = getJarFile(filename).getPath();
-		else
-			path = filename.getPath();
+			return split(getJarFile(filename));
+		path = filename.getPath();
 		return path.split(Pattern.quote(URL_PATH_SEPARATOR));
 	}
 
@@ -789,7 +816,7 @@
 					buf.append(URL_PATH_SEPARATOR_CHAR);
 				}
 			}
-			buf.append(elt.getPath());
+			buf.append(getFilePath(elt));
 		}
 		try {
 			if (isJarURL(urlBase)) {
@@ -1687,7 +1714,8 @@
 			else if (URISchemeType.FILE.isScheme(urlDescription)) {
 				File file = new File(URISchemeType.FILE.removeScheme(urlDescription));
 				try {
-					url = new URL(URISchemeType.FILE.name(), "", file.getPath()); //$NON-NLS-1$
+					url = new URL(URISchemeType.FILE.name(), "", //$NON-NLS-1$
+							getFilePath(file));
 				}
 				catch (MalformedURLException e) {
 					//
@@ -1734,7 +1762,8 @@
 					if (url==null) {
 						try {
 							File file = new File(urlPart);
-							url = new URL(URISchemeType.FILE.name(), "", file.getPath()); //$NON-NLS-1$
+							url = new URL(URISchemeType.FILE.name(), "", //$NON-NLS-1$ 
+									getFilePath(file));
 						}
 						catch (MalformedURLException e) {
 							// ignore error
@@ -2120,7 +2149,8 @@
 				return join(current, filename);
 			}
 			try {
-				return new URL(URISchemeType.FILE.toString()+filename.getPath());
+				return new URL(URISchemeType.FILE.toString()+
+						getFilePath(filename));
 			}
 			catch (MalformedURLException _) {
 				// ignore error

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java	2011-09-01 17:16:53 UTC (rev 280)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java	2011-09-03 21:18:23 UTC (rev 281)
@@ -213,7 +213,10 @@
 	 * @param parameters is the parameters to pass to the <code>main</code>.
 	 */
 	public static void saveVMParameters(Class<?> classToLaunch, String... parameters) {
-		saveVMParameters(classToLaunch.getCanonicalName(), parameters);
+ 		saveVMParameters(
+ 				(classToLaunch!=null)
+ 				? classToLaunch.getCanonicalName()
+ 				: null, parameters);
 	}
 
 	/** Save parameters that permit to relaunch a VM with

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2011-09-01 17:16:53 UTC (rev 280)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2011-09-03 21:18:23 UTC (rev 281)
@@ -424,21 +424,8 @@
 		assertEquals("home", FileSystem.shortBasename(f2.getAbsolutePath())); //$NON-NLS-1$
 		assertEquals("a", FileSystem.shortBasename("/a.b.c/")); //$NON-NLS-1$ //$NON-NLS-2$
 		assertEquals("", FileSystem.shortBasename("")); //$NON-NLS-1$ //$NON-NLS-2$
-
-		try {
-			assertEquals("terrain_physx", FileSystem.shortBasename("D:\\vivus_test\\export dae\\yup\\terrain_physx.dae")); //$NON-NLS-1$ //$NON-NLS-2$
-			fail("expecting assertion failure"); //$NON-NLS-1$
-		}
-		catch(AssertionError _) {
-			//
-		}
-		try {
-			assertEquals("terrain_physx", FileSystem.shortBasename("file:D:\\vivus_test\\export dae\\yup\\terrain_physx.dae")); //$NON-NLS-1$ //$NON-NLS-2$
-			fail("expecting assertion failure"); //$NON-NLS-1$
-		}
-		catch(AssertionError _) {
-			//
-		}
+		assertEquals("terrain_physx", FileSystem.shortBasename("D:\\vivus_test\\export dae\\yup\\terrain_physx.dae")); //$NON-NLS-1$ //$NON-NLS-2$
+		assertEquals("terrain_physx", FileSystem.shortBasename("file:D:\\vivus_test\\export dae\\yup\\terrain_physx.dae")); //$NON-NLS-1$ //$NON-NLS-2$
 	}
 
 	/**

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java	2011-09-01 17:16:53 UTC (rev 280)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java	2011-09-03 21:18:23 UTC (rev 281)
@@ -46,7 +46,7 @@
 	@Override
 	protected void setUp() throws Exception {
 		super.setUp();
-		VMCommandLine.saveVMParameters(null, new String[0]);
+		VMCommandLine.saveVMParameters((Class<?>)null, new String[0]);
 	}
 	
 	/**


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