[Arakhnę-Dev] [190] * Add makeRelative() in FileSystem with URL as parameters. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 190
Author: galland
Date: 2010-10-01 19:29:53 +0200 (Fri, 01 Oct 2010)
Log Message:
-----------
* Add makeRelative() in FileSystem with URL as parameters.
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-29 17:52:35 UTC (rev 189)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2010-10-01 17:29:53 UTC (rev 190)
@@ -2155,6 +2155,55 @@
return new File(CURRENT_DIRECTORY, relPath);
}
+ /**
+ * 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.
+ * @since 6.0
+ */
+ public static File makeRelative(File filenameToMakeRelative, URL rootPath) throws IOException {
+ assert(filenameToMakeRelative!=null);
+ assert(rootPath!=null);
+
+ if (!filenameToMakeRelative.isAbsolute()) return filenameToMakeRelative;
+
+ File dir = filenameToMakeRelative.getParentFile().getCanonicalFile();
+
+ String[] parts1 = split(dir);
+ String[] parts2 = split(rootPath);
+
+ String relPath = makeRelative(parts1, parts2, filenameToMakeRelative.getName());
+
+ return new File(CURRENT_DIRECTORY, relPath);
+ }
+
+ /**
+ * 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.
+ * @since 6.0
+ */
+ public static File makeRelative(URL filenameToMakeRelative, URL rootPath) throws IOException {
+ assert(filenameToMakeRelative!=null);
+ assert(rootPath!=null);
+
+ String basename = largeBasename(filenameToMakeRelative);
+ URL dir = dirname(filenameToMakeRelative);
+
+ String[] parts1 = split(dir);
+ String[] parts2 = split(rootPath);
+
+ String relPath = makeRelative(parts1, parts2, basename);
+
+ return new File(CURRENT_DIRECTORY, relPath);
+ }
+
private static String makeRelative(String[] parts1, String[] parts2, String basename) {
int firstDiff = -1;
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-29 17:52:35 UTC (rev 189)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2010-10-01 17:29:53 UTC (rev 190)
@@ -1283,4 +1283,88 @@
assertEquals(rel, FileSystem.makeRelative(abs, root));
}
+ /**
+ * @throws Exception
+ */
+ public void testMakeRelativeFileURL() throws Exception {
+ File abs, rel;
+ URL root;
+
+ root = FileSystem.getUserHomeDirectory().toURI().toURL();
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a"); //$NON-NLS-1$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator+"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(FileSystem.CURRENT_DIRECTORY+File.separator+"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 = FileSystem.join(FileSystem.getUserHomeDirectory().toURI().toURL(), "zz", "abc"); //$NON-NLS-1$ //$NON-NLS-2$
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a"); //$NON-NLS-1$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator
+ +FileSystem.PARENT_DIRECTORY+File.separator
+ +FileSystem.PARENT_DIRECTORY+File.separator
+ +"a"); //$NON-NLS-1$
+ assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+
+
+ root = FileSystem.join(FileSystem.getUserHomeDirectory().toURI().toURL(), "zz", "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.CURRENT_DIRECTORY+File.separator
+ +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));
+ }
+
+ /**
+ * @throws Exception
+ */
+ public void testMakeRelativeURLURL() throws Exception {
+ File rel;
+ URL root, abs;
+
+ root = FileSystem.getUserHomeDirectory().toURI().toURL();
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a").toURI().toURL(); //$NON-NLS-1$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator+"a"); //$NON-NLS-1$
+ assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a"+File.separator+"b").toURI().toURL(); //$NON-NLS-1$ //$NON-NLS-2$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator+"a","b"); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+
+
+ root = FileSystem.join(FileSystem.getUserHomeDirectory().toURI().toURL(), "zz", "abc"); //$NON-NLS-1$ //$NON-NLS-2$
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a").toURI().toURL(); //$NON-NLS-1$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator
+ +FileSystem.PARENT_DIRECTORY+File.separator
+ +FileSystem.PARENT_DIRECTORY+File.separator
+ +"a"); //$NON-NLS-1$
+ assertEquals(rel, FileSystem.makeRelative(abs, root));
+
+
+
+ root = FileSystem.join(FileSystem.getUserHomeDirectory().toURI().toURL(), "zz", "abc"); //$NON-NLS-1$//$NON-NLS-2$
+
+ abs = new File(FileSystem.getUserHomeDirectory(), "a"+File.separator+"zz"+File.separator+"bc").toURI().toURL(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ rel = new File(FileSystem.CURRENT_DIRECTORY+File.separator
+ +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));
+ }
+
}