[Arakhnę-Dev] [201] * Add function FileSystem.makeCanonicalURL().

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


Revision: 201
Author:   galland
Date:     2011-01-12 19:51:47 +0100 (Wed, 12 Jan 2011)
Log Message:
-----------
* Add function FileSystem.makeCanonicalURL().

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.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	2011-01-04 18:45:04 UTC (rev 200)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-01-12 18:51:47 UTC (rev 201)
@@ -38,6 +38,8 @@
 import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.Charset;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -2270,6 +2272,80 @@
 		
 		return result.toString();
 	}
+	
+	/**
+     * <p>
+     * A canonical pathname is both absolute and unique.  This method maps 
+     * the pathname to its unique form.  This typically involves removing redundant names
+     * such as <tt>"."</tt> and <tt>".."</tt> from the pathname.
+	 * 
+	 * @param url is the URL to make canonical
+	 * @return the canonical form of the given URL.
+	 * @since 6.0
+	 */
+	public static URL makeCanonicalURL(URL url) {
+		if (url!=null) {
+			String[] pathComponents = url.getPath().split(Pattern.quote(URL_PATH_SEPARATOR));
+			
+			List<String> canonicalPath = new LinkedList<String>();
+			for(String component : pathComponents) {
+				if (!CURRENT_DIRECTORY.equals(component)) {
+					if (PARENT_DIRECTORY.equals(component)) {
+						if (!canonicalPath.isEmpty()) {
+							canonicalPath.remove(canonicalPath.size()-1);
+						}
+						else {
+							canonicalPath.add(component);
+						}
+					}
+					else {
+						canonicalPath.add(component);
+					}
+				}
+			}
+			
+			StringBuffer newPathBuffer = new StringBuffer();
+			boolean isFirst = true;
+			for(String component : canonicalPath) {
+				if (!isFirst) {
+					newPathBuffer.append(URL_PATH_SEPARATOR_CHAR);
+				}
+				else {
+					isFirst = false;
+				}
+				newPathBuffer.append(component);
+			}
+			
+			try {
+				URI uri = new URI(
+						url.getProtocol(), 
+						url.getUserInfo(),
+						url.getHost(),
+						url.getPort(), 
+						newPathBuffer.toString(),
+						url.getQuery(),
+						url.getRef());
+				return uri.toURL();
+			}
+			catch (MalformedURLException _) {
+				//
+			}
+			catch (URISyntaxException _) {
+				//
+			}
 
+			try {
+				return new URL(
+						url.getProtocol(), 
+						url.getHost(), 
+						newPathBuffer.toString());
+			}
+			catch (Throwable _) {
+				//
+			}
+		}
+		return url;
+	}
+
 }
 

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2011-01-04 18:45:04 UTC (rev 200)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2011-01-12 18:51:47 UTC (rev 201)
@@ -292,7 +292,7 @@
 		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$
+				FileSystem.dirname(new URL("http", "www.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));
@@ -1369,5 +1369,42 @@
 				+"a"+File.separator+"zz"+File.separator+"bc"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
 		assertEquals(rel, FileSystem.makeRelative(abs, root));
 	}
+	
+	/**
+	 * @throws MalformedURLException
+	 */
+	public void testMakeCanonicalURL() throws MalformedURLException {
+		assertEquals(
+				new URL("http://toto:titi@xxxxxxxxxxxxxxx/path/to/file.x.z.z?toto#frag";), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL(TEST_URL1)));
 
+		assertEquals(
+				new URL("jar:file:/home/test/j.jar!/org/arakhne/vmutil/file.x.z.z"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL(TEST_URL2)));
+
+		assertEquals(
+				new URL("jar:jar:http://www.arakhne.org/j.jar!/inner/myjar.jar!/org/arakhne/vmutil/file.x.z.z";), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL(TEST_URL3)));
+		
+		assertEquals(
+				new URL("file:/a/b/c/d/e"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL("file:/a/b/./c/./d/e"))); //$NON-NLS-1$
+		
+		assertEquals(
+				new URL("file:/a/d/e"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL("file:/a/b/../c/../d/e"))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("file:/a/b/d/e"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL("file:/a/b/./c/../d/e"))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("file:../a/b/c/d/e"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL("file:../a/b/./c/./d/e"))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("file:../a/c/d/e"), //$NON-NLS-1$
+				FileSystem.makeCanonicalURL(new URL("file:../a/b/../c/./d/e"))); //$NON-NLS-1$
+	}
+
 }


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