[Arakhnę-Dev] [90] Add makeAbsolute functions(). |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 90
Author: galland
Date: 2009-11-28 00:27:41 +0100 (Sat, 28 Nov 2009)
Log Message:
-----------
Add makeAbsolute functions().
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 21:21:48 UTC (rev 89)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2009-11-27 23:27:41 UTC (rev 90)
@@ -595,12 +595,18 @@
}
if ("file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$
String auth = uri.getAuthority();
- if (auth==null || "".equals(auth)) { //$NON-NLS-1$
- // absolute filename in URI
- return new File(uri);
+ String path = uri.getPath();
+ if (path==null) path = uri.getRawPath();
+ if (path==null) path = uri.getSchemeSpecificPart();
+ if (path==null) path = uri.getRawSchemeSpecificPart();
+ if (path!=null) {
+ if (auth==null || "".equals(auth)) { //$NON-NLS-1$
+ // absolute filename in URI
+ return new File(path);
+ }
+ // relative filename in URI, extract it directly
+ return new File(auth+path);
}
- // relative filename in URI, extract it directly
- return new File(auth+uri.getPath());
}
throw new IllegalArgumentException("not a file URL: "+url); //$NON-NLS-1$
}
@@ -642,28 +648,134 @@
if (allowResourceSearch) {
+ String resourceName;
+
if (urlDescription.toLowerCase().startsWith("resource:")) { //$NON-NLS-1$
- String resourceName = urlDescription.substring(9);
- url = ClassLoader.getSystemResource(resourceName);
- if (url!=null) return url;
+ resourceName = urlDescription.substring(9);
+ if (resourceName.startsWith("/")) resourceName = resourceName.substring(1); //$NON-NLS-1$
+ return ClassLoader.getSystemResource(resourceName);
}
- url = ClassLoader.getSystemResource(urlDescription);
+ resourceName = urlDescription;
+ if (resourceName.startsWith("/")) resourceName = resourceName.substring(1); //$NON-NLS-1$
+ url = ClassLoader.getSystemResource(resourceName);
if (url!=null) return url;
}
+ else if (urlDescription.toLowerCase().startsWith("resource:")) { //$NON-NLS-1$
+ return null;
+ }
try {
File file = new File(urlDescription);
URI uri = file.toURI();
- if (uri!=null)
- return uri.toURL();
+ return uri.toURL();
}
- catch (MalformedURLException _) {
+ catch (MalformedURLException e) {
// ignore error
}
return null;
}
+ /**
+ * Make the given filename absolute from the given root if it is not already absolute.
+ *
+ * @param filename is the name to make absolute.
+ * @param current is the current directory which permits to make absolute.
+ * @return an absolute filename.
+ */
+ public static File makeAbsolute(File filename, File current) {
+ if (filename==null) return null;
+ if (current!=null && !filename.isAbsolute()) {
+ try {
+ return new File(current.getCanonicalFile(), filename.getPath());
+ }
+ catch(IOException _) {
+ return new File(current.getAbsoluteFile(), filename.getPath());
+ }
+ }
+ return filename;
+ }
+
+ /** Replies if the given URL is using a protocol which oculd be map to files.
+ *
+ * @param url
+ * @return <code>true</code> if the given url is a "file", "http",
+ * "https", "ftp", "ssh", "jar" or "resource", otherwise <code>false</code>.
+ */
+ public static boolean isFileBasedURL(URL url) {
+ if (url!=null) {
+ String scheme = url.getProtocol();
+ if ("file".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("http".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("https".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("ftp".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("ssh".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("jar".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ if ("resource".equalsIgnoreCase(scheme)) return true; //$NON-NLS-1$
+ }
+ return false;
+ }
+
+ /**
+ * Make the given filename absolute from the given root if it is not already absolute.
+ *
+ * @param filename is the name to make absolute.
+ * @param current is the current directory which permits to make absolute.
+ * @return an absolute filename.
+ */
+ public static URL makeAbsolute(URL filename, File current) {
+ if (filename==null) return null;
+ if (current!=null && isFileBasedURL(filename)) {
+ String scheme = filename.getProtocol();
+ if ("jar".equalsIgnoreCase(scheme)) { //$NON-NLS-1$
+ try {
+ String[] parts = filename.getPath().split("!/"); //$NON-NLS-1$
+ URL u = makeAbsolute(new URL(parts[0]), current);
+ StringBuffer adr = new StringBuffer("jar:"); //$NON-NLS-1$
+ adr.append(u.toExternalForm());
+ for(int i=1; i<parts.length; i++) {
+ adr.append("!/"); //$NON-NLS-1$
+ adr.append(parts[i]);
+ }
+ return new URL(adr.toString());
+ }
+ catch(MalformedURLException _) {
+ // Ignore error
+ }
+ }
+ else {
+ int port = filename.getPort();
+ try {
+ String absPath = filename.getPath();
+ if (!absPath.startsWith("/")) { //$NON-NLS-1$
+ URL rootUrl = current.toURI().toURL();
+ absPath = rootUrl.getPath()+"/"+absPath; //$NON-NLS-1$
+ return new URL(filename.getProtocol(), filename.getHost(), port, absPath);
+ }
+ }
+ catch (MalformedURLException e) {
+ //
+ }
+ }
+ }
+ return filename;
+ }
+
+ /**
+ * Make the given filename absolute from the given root if it is not already absolute.
+ *
+ * @param filename is the name to make absolute.
+ * @param current is the current directory which permits to make absolute.
+ * @return an absolute filename.
+ */
+ public static URL makeAbsolute(URL filename, URL current) {
+ if (current!=null && "file".equalsIgnoreCase(current.getProtocol())) { //$NON-NLS-1$
+ File cur = convertUrlToFile(current);
+ if (cur!=null) return makeAbsolute(filename, cur);
+ }
+ return filename;
+ }
+
}
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 21:21:48 UTC (rev 89)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-11-27 23:27:41 UTC (rev 90)
@@ -227,6 +227,8 @@
FileSystem.convertStringToUrl("file:"+f1.getAbsolutePath(), true)); //$NON-NLS-1$
assertEquals(new URL("file:"+f1.getAbsolutePath()), //$NON-NLS-1$
FileSystem.convertStringToUrl("file:"+f1.getAbsolutePath(), false)); //$NON-NLS-1$
+ assertEquals(new URL("file:./toto"), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("file:./toto", false)); //$NON-NLS-1$
assertEquals(new File("jar:/home/test/j.jar").toURI().toURL(), //$NON-NLS-1$
FileSystem.convertStringToUrl("jar:/home/test/j.jar", true)); //$NON-NLS-1$
@@ -245,7 +247,8 @@
URL testResource = FileSystemTest.class.getResource("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
assertNotNull(testResource);
- URL testResource2 = new URL("file:/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ URL testResourceFileRel = new File("org/arakhne/vmutil/test.txt").toURI().toURL(); //$NON-NLS-1$
+ URL testResourceFileAbs = new File("/org/arakhne/vmutil/test.txt").toURI().toURL(); //$NON-NLS-1$
assertEquals(testResource,
FileSystem.convertStringToUrl("resource:/org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
@@ -253,9 +256,19 @@
FileSystem.convertStringToUrl("resource:/org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
assertEquals(testResource,
+ FileSystem.convertStringToUrl("resource:org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
+ assertEquals(null,
+ FileSystem.convertStringToUrl("resource:org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
+
+ assertEquals(testResource,
FileSystem.convertStringToUrl("/org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
- assertEquals(testResource2,
+ assertEquals(testResourceFileAbs,
FileSystem.convertStringToUrl("/org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
+
+ assertEquals(testResource,
+ FileSystem.convertStringToUrl("org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
+ assertEquals(testResourceFileRel,
+ FileSystem.convertStringToUrl("org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
}
/**
@@ -272,8 +285,147 @@
catch(IllegalArgumentException _) {
//
}
+
+ assertEquals(new File("toto").getCanonicalPath(), //$NON-NLS-1$
+ FileSystem.convertUrlToFile(new URL("file:./toto")).getCanonicalPath()); //$NON-NLS-1$
+
+ assertEquals(new File("toto").getCanonicalPath(), //$NON-NLS-1$
+ FileSystem.convertUrlToFile(new URL("file:toto")).getCanonicalPath()); //$NON-NLS-1$
+
+ assertEquals(new File("toto").getCanonicalPath(), //$NON-NLS-1$
+ FileSystem.convertUrlToFile(new URL("file:./abs/../toto")).getCanonicalPath()); //$NON-NLS-1$
+
+ assertEquals(new File("/toto").getCanonicalPath(), //$NON-NLS-1$
+ FileSystem.convertUrlToFile(new URL("file:/toto")).getCanonicalPath()); //$NON-NLS-1$
+ }
+
+ /**
+ */
+ public void testMakeAbsoluteFileFile() {
+ File root = new File(File.separator+"myroot"); //$NON-NLS-1$
+
+ assertNull(FileSystem.makeAbsolute((File)null, null));
+ assertNull(FileSystem.makeAbsolute((File)null, root));
+
+ assertEquals(new File(File.separator+"toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new File(File.separator+"toto"), null)); //$NON-NLS-1$
assertEquals(new File("toto"), //$NON-NLS-1$
- FileSystem.convertUrlToFile(new URL("file:./toto"))); //$NON-NLS-1$
+ FileSystem.makeAbsolute(new File("toto"), null)); //$NON-NLS-1$
+
+ assertEquals(new File(File.separator+"toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new File(File.separator+"toto"), root)); //$NON-NLS-1$
+ assertEquals(new File(File.separator+"myroot"+File.separator+"toto"), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.makeAbsolute(new File("toto"), root)); //$NON-NLS-1$
}
+ /**
+ * @throws Exception
+ */
+ public void testMakeAbsoluteURLFile() throws Exception {
+ File root = new File(File.separator+"myroot"); //$NON-NLS-1$
+
+ assertNull(FileSystem.makeAbsolute((URL)null, (File)null));
+ assertNull(FileSystem.makeAbsolute((URL)null, root));
+
+ assertEquals(new URL("file:/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:/toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("file:toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("file:/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("file:/myroot/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("http://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/./toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("https://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/./toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("ftp://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/./toto"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("jar:file:/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"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("jar:file: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"), (File)null)); //$NON-NLS-1$
+ assertEquals(new URL("jar:file:/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$
+ 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 testMakeAbsoluteURLURL() throws Exception {
+ URL root = new File(File.separator+"myroot").toURI().toURL(); //$NON-NLS-1$
+
+ assertNull(FileSystem.makeAbsolute((URL)null, (URL)null));
+ assertNull(FileSystem.makeAbsolute((URL)null, root));
+
+ assertEquals(new URL("file:/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:/toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("file:toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("file:/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("file:/myroot/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("file:toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("http://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/./toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("http://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("https://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/./toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("https://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("https://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("ftp://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/./toto"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/toto"), root)); //$NON-NLS-1$
+ assertEquals(new URL("ftp://www.arakhne.org/./toto"), //$NON-NLS-1$
+ FileSystem.makeAbsolute(new URL("ftp://www.arakhne.org/./toto"), root)); //$NON-NLS-1$
+
+ assertEquals(new URL("jar:file:/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"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("jar:file: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"), (URL)null)); //$NON-NLS-1$
+ assertEquals(new URL("jar:file:/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$
+ 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$
+ }
+
}