[Arakhnę-Dev] [91] Add FileSystem.getParentURL(URL).

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


Revision: 91
Author:   galland
Date:     2009-11-28 22:20:03 +0100 (Sat, 28 Nov 2009)
Log Message:
-----------
Add FileSystem.getParentURL(URL).

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	2009-11-27 23:27:41 UTC (rev 90)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2009-11-28 21:20:03 UTC (rev 91)
@@ -777,5 +777,46 @@
 		return filename;
 	}
 
+	/** Replies the parent URL for the given URL.
+	 * 
+	 * @param url
+	 * @return the parent URL
+	 * @throws MalformedURLException 
+	 */
+	public static URL getParentURL(URL url) throws MalformedURLException {
+		String path = url.getPath();
+		String prefix, parentStr;
+
+		if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
+			int index = path.indexOf("!/"); //$NON-NLS-1$
+			assert(index>0);
+			prefix = path.substring(0,index+1);
+			path = path.substring(index+1);
+			parentStr = "/"; //$NON-NLS-1$
+		}
+		else if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
+			prefix = null;
+			parentStr = "../"; //$NON-NLS-1$
+		}
+		else {
+			prefix = null;
+			parentStr = "/"; //$NON-NLS-1$
+		}
+		
+		if (path==null || "".equals(path)) path = parentStr; //$NON-NLS-1$
+		int index = path.lastIndexOf('/');
+		if (index==-1) path = parentStr;
+		else if (index==path.length()-1) {
+			index = path.lastIndexOf('/', index-1);
+			if (index==-1) path = parentStr;
+			else path = path.substring(0, index+1);
+		}
+		else path = path.substring(0, index+1);
+		
+		if (prefix!=null)  path = prefix + path;
+		
+		return new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
+	}
+	
 }
 

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2009-11-27 23:27:41 UTC (rev 90)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2009-11-28 21:20:03 UTC (rev 91)
@@ -427,5 +427,120 @@
 		assertEquals(new URL("jar:file:/myroot/home/test/j.jar!/org/arakhne/vmutil/ff.properties"), //$NON-NLS-1$
 				FileSystem.makeAbsolute(new URL("jar:file:home/test/j.jar!/org/arakhne/vmutil/ff.properties"), root)); //$NON-NLS-1$
 	}
+	
+	/**
+	 * @throws Exception
+	 */
+	public void testGetParentURLURL() throws Exception {
+		assertEquals(
+				new URL("http://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("http://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("http://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org/toto";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("http://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org/toto/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("http://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org/toto/titi";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("http://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("http://www.arakhne.org/toto/titi/";))); //$NON-NLS-1$
 
+		assertEquals(
+				new URL("https://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("https://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("https://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org/toto";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("https://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org/toto/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("https://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org/toto/titi";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("https://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("https://www.arakhne.org/toto/titi/";))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("ftp://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("ftp://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("ftp://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org/toto";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("ftp://www.arakhne.org/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org/toto/";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("ftp://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org/toto/titi";))); //$NON-NLS-1$
+		assertEquals(
+				new URL("ftp://www.arakhne.org/toto/";), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("ftp://www.arakhne.org/toto/titi/";))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("file:/toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:/toto/titi"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:/toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:/toto/titi/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:/toto"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:/toto/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:./toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:./toto/titi"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:./toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:./toto/titi/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:./"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:./toto"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:./"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:./toto/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:../"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:."))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:../"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:./"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:../"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:toto"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("file:../"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("file:toto/"))); //$NON-NLS-1$
+
+		assertEquals(
+				new URL("jar:file:test.jar!/toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("jar:file:test.jar!/toto/titi"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("jar:file:test.jar!/toto/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("jar:file:test.jar!/toto/titi/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("jar:file:test.jar!/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("jar:file:test.jar!/toto"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("jar:file:test.jar!/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("jar:file:test.jar!/toto/"))); //$NON-NLS-1$
+		assertEquals(
+				new URL("jar:file:test.jar!/"), //$NON-NLS-1$
+				FileSystem.getParentURL(new URL("jar:file:test.jar!/"))); //$NON-NLS-1$
+	}
+
 }


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