[Arakhnę-Dev] [182] * Add toShortestURL() function in FileSystem class.

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


Revision: 182
Author:   galland
Date:     2010-09-09 20:39:30 +0200 (Thu, 09 Sep 2010)
Log Message:
-----------
* Add toShortestURL() function in FileSystem class.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.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-09-07 19:14:53 UTC (rev 181)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-09-09 18:39:30 UTC (rev 182)
@@ -1898,13 +1898,23 @@
 	 */
 	public static URL convertFileToURL(File file) {
 		if (file==null) return null;
-		URL url;
 		try {
-			url = file.toURI().toURL();
+			return toShortestURL(file.toURI().toURL());
 		}
 		catch (MalformedURLException e) {
 			return null;
 		}
+	}
+	
+	/** Replies an URL for the given url and translate it into a
+	 * resource URL if the given file is inside the classpath.
+	 * 
+	 * @param url is the URL to make shortest.
+	 * @return the URL which is corresponding to file, or <code>null</code> if 
+	 * the url cannot be computed.
+	 * @since 4.0
+	 */
+	public static URL toShortestURL(URL url) {
 		String s = url.toExternalForm().replaceAll("/$", "");  //$NON-NLS-1$//$NON-NLS-2$
 		String sp;
 		URL[] classpath = ClasspathUtil.getCurrentClasspath();

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2010-09-07 19:14:53 UTC (rev 181)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2010-09-09 18:39:30 UTC (rev 182)
@@ -39,7 +39,11 @@
  */
 public class Resources {
 
-    /**
+	static {
+		URLHandlerUtil.installArakhneHandlers();
+	}
+
+	/**
      * Replies the URL of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java	2010-09-07 19:14:53 UTC (rev 181)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/URISchemeType.java	2010-09-09 18:39:30 UTC (rev 182)
@@ -473,5 +473,9 @@
 	 */
 	public abstract boolean isFileBasedScheme();
 
+	static {
+		URLHandlerUtil.installArakhneHandlers();
+	}
+
 }
 

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-09-07 19:14:53 UTC (rev 181)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-09-09 18:39:30 UTC (rev 182)
@@ -1109,10 +1109,34 @@
 			assertEqualUrls(u2e, actual);
 		}
 		finally {
-			URLHandlerUtil.installArakhneHandlers();
+			URLHandlerUtil.uninstallArakhneHandlers();
 		}
 	}
 	
+	/**
+	 * @throws Exception
+	 */
+	public void testToShortestURLURL() throws Exception {
+		URLHandlerUtil.installArakhneHandlers();
+		try {
+			File f1 = new File("/toto"); //$NON-NLS-1$
+			URL u1 = f1.toURI().toURL();
+			URL u2 = Resources.getResource("org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+			URL u2e = new URL("resource:org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+			
+			URL actual;
+			
+			actual = FileSystem.toShortestURL(u1);
+			assertEqualUrls(u1, actual);
+
+			actual = FileSystem.toShortestURL(u2);
+			assertEqualUrls(u2e, actual);
+		}
+		finally {
+			URLHandlerUtil.uninstallArakhneHandlers();
+		}
+	}
+
 	private void assertEqualUrls(URL expected, URL actual) {
 		String u1 = expected==null ? null : expected.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
 		String u2 = actual==null ? null : actual.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$


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