[Arakhnę-Dev] [180] * Add makeRelative() in FileSystem.

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


Revision: 180
Author:   galland
Date:     2010-09-07 19:47:54 +0200 (Tue, 07 Sep 2010)
Log Message:
-----------
* Add makeRelative() in FileSystem.

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	2010-09-07 15:37:38 UTC (rev 179)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-09-07 17:47:54 UTC (rev 180)
@@ -1926,5 +1926,59 @@
 		return url;
 	}
 
+	/**
+	 * Make the given filename relative to the given root path.
+	 *
+	 * @param filenameToMakeRelative is the name to make relative.
+	 * @param rootPath is the root path from which the relative path will be set.
+	 * @return a relative filename.
+	 * @throws IOException when is is impossible to retreive canonical paths.
+	 */
+	public static File makeRelative(File filenameToMakeRelative, File rootPath) throws IOException {
+		assert(filenameToMakeRelative!=null);
+		assert(rootPath!=null);
+		
+		if (!filenameToMakeRelative.isAbsolute()) return filenameToMakeRelative;
+		if (!rootPath.isAbsolute()) return filenameToMakeRelative;
+
+		File root = rootPath.getCanonicalFile();
+		File dir = filenameToMakeRelative.getParentFile().getCanonicalFile();
+		
+		String[] parts1 = split(dir);
+		String[] parts2 = split(root);
+		
+		return new File(makeRelative(parts1, parts2, filenameToMakeRelative.getName()));
+	}
+	
+	private static String makeRelative(String[] parts1, String[] parts2, String basename) {
+		int firstDiff = -1;
+		
+		for(int i=0; firstDiff<0 && i<parts1.length && i<parts2.length; i++) {
+			if (!parts1[i].equals(parts2[i])) {
+				firstDiff = i;
+			}
+		}
+		
+		StringBuffer result = new StringBuffer();
+		if (firstDiff<0) {
+			firstDiff = Math.min(parts1.length, parts2.length);
+		}
+
+		for(int i=firstDiff; i<parts2.length; i++) {
+			if (result.length()>0) result.append(File.separator);
+			result.append(PARENT_DIRECTORY);
+		}
+
+		for(int i=firstDiff; i<parts1.length; i++) {
+			if (result.length()>0) result.append(File.separator);
+			result.append(parts1[i]);
+		}
+
+		if (result.length()>0) result.append(File.separator);
+		result.append(basename);
+		
+		return result.toString();
+	}
+
 }
 

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 15:37:38 UTC (rev 179)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-09-07 17:47:54 UTC (rev 180)
@@ -1118,5 +1118,46 @@
 		String u2 = actual==null ? null : actual.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
 		assertEquals(u1, u2);
 	}
+	
+	/**
+	 * @throws Exception 
+	 */
+	public void testMakeRelativeFileFile() throws Exception {
+		File root, abs, rel;
+		
+		root = FileSystem.getUserHomeDirectory();
+		
+		abs = new File(FileSystem.getUserHomeDirectory(), "a"); //$NON-NLS-1$
+		rel = new File("a"); //$NON-NLS-1$
+		assertEquals(rel, FileSystem.makeRelative(abs, root));
 
+		abs = new File(FileSystem.getUserHomeDirectory(), "a"+File.separator+"b"); //$NON-NLS-1$ //$NON-NLS-2$
+		rel = new File("a","b"); //$NON-NLS-1$ //$NON-NLS-2$
+		assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+		abs = new File("a","b"); //$NON-NLS-1$//$NON-NLS-2$
+		rel = new File("a","b"); //$NON-NLS-1$//$NON-NLS-2$
+		assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+		
+		
+		root = new File(FileSystem.getUserHomeDirectory(), "zz"+File.separator+"abc"); //$NON-NLS-1$ //$NON-NLS-2$
+		
+		abs = new File(FileSystem.getUserHomeDirectory(), "a"); //$NON-NLS-1$
+		rel = new File(FileSystem.PARENT_DIRECTORY+File.separator
+				+FileSystem.PARENT_DIRECTORY+File.separator
+				+"a"); //$NON-NLS-1$
+		assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+	
+	
+		root = new File(FileSystem.getUserHomeDirectory(), "zz"+File.separator+"abc"); //$NON-NLS-1$//$NON-NLS-2$
+		
+		abs = new File(FileSystem.getUserHomeDirectory(), "a"+File.separator+"zz"+File.separator+"bc"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+		rel = new File(FileSystem.PARENT_DIRECTORY+File.separator
+				+FileSystem.PARENT_DIRECTORY+File.separator
+				+"a"+File.separator+"zz"+File.separator+"bc"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+		assertEquals(rel, FileSystem.makeRelative(abs, root));
+	}
+
 }


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