[Arakhnę-Dev] [135] Add specific functions for Windows native filenames. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 135
Author: galland
Date: 2010-04-27 21:43:47 +0200 (Tue, 27 Apr 2010)
Log Message:
-----------
Add specific functions for Windows native filenames.
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-02-17 09:53:18 UTC (rev 134)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2010-04-27 19:43:47 UTC (rev 135)
@@ -34,6 +34,8 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/** An utility class that permits to deal with filenames.
@@ -43,6 +45,10 @@
*/
public class FileSystem {
+ /** Regular expression pattern which corresponds to Windows native filename.
+ */
+ private static final String WINDOW_NATIVE_FILENAME_PATTERN = "^([A-Za-z]:)?([^\\\\/:*?\"<>|]*\\\\)*[^\\\\/:*?\"<>|]*$"; //$NON-NLS-1$
+
/** Character used to specify a file extension.
*/
public static final char EXTENSION_SEPARATOR_CHAR = '.';
@@ -1634,5 +1640,91 @@
return new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
}
+ /** Test if the given filename is a local filename and extract
+ * the path component.
+ *
+ * @param filename
+ * @return the path
+ */
+ private static String extractLocalPath(String filename) {
+ if (filename==null ) return null;
+ String fn = filename.toUpperCase();
+ if (fn.startsWith("FILE://")) //$NON-NLS-1$
+ fn = filename.substring(7);
+ else if (fn.startsWith("FILE:")) //$NON-NLS-1$
+ fn = filename.substring(5);
+ else
+ fn = filename;
+ return fn;
+ }
+
+ /** Replies if the given string contains a Windows® native long filename.
+ * <p>
+ * Long filenames (LFN), spelled "long file names" by Microsoft Corporation,
+ * are Microsoft's way of implementing filenames longer than the 8.3,
+ * or short-filename, naming scheme used in Microsoft DOS in their modern
+ * FAT and NTFS filesystems. Because these filenames can be longer than the
+ * 8.3 filename, they can be more descriptive. Another advantage of this
+ * scheme is that it allows for use of *nix files ending in (e.g. .jpeg,
+ * .tiff, .html, and .xhtml) rather than specialized shortened names
+ * (e.g. .jpg, .tif, .htm, .xht).
+ * <p>
+ * The long filename system allows a maximum length of 255 UTF-16 characters,
+ * including spaces and non-alphanumeric characters; excluding the following
+ * characters, which have special meaning within the command interpreter or
+ * the operating system kernel: <code>\</code> <code>/</code> <code>:</code>
+ * <code>*</code> <code>?</code> <code>"</code> <code><</code>
+ * <code>></code> <code>|</code>
+ *
+ * @param filename
+ * @return <code>true</code> if the given filename is a long filename,
+ * otherwise <code>false</code>
+ * @see #normalizeWindowsNativeFilename(String)
+ */
+ public static boolean isWindowsNativeFilename(String filename) {
+ String fn = extractLocalPath(filename);
+ assert(fn!=null);
+ if (fn.length()==0) return false;
+ Pattern pattern = Pattern.compile(WINDOW_NATIVE_FILENAME_PATTERN);
+ Matcher matcher = pattern.matcher(fn);
+ return matcher.find();
+ }
+
+ /** Normalize the given string contains a Windows® native long filename
+ * and replies a Java-standard version.
+ * <p>
+ * Long filenames (LFN), spelled "long file names" by Microsoft Corporation,
+ * are Microsoft's way of implementing filenames longer than the 8.3,
+ * or short-filename, naming scheme used in Microsoft DOS in their modern
+ * FAT and NTFS filesystems. Because these filenames can be longer than the
+ * 8.3 filename, they can be more descriptive. Another advantage of this
+ * scheme is that it allows for use of *nix files ending in (e.g. .jpeg,
+ * .tiff, .html, and .xhtml) rather than specialized shortened names
+ * (e.g. .jpg, .tif, .htm, .xht).
+ * <p>
+ * The long filename system allows a maximum length of 255 UTF-16 characters,
+ * including spaces and non-alphanumeric characters; excluding the following
+ * characters, which have special meaning within the command interpreter or
+ * the operating system kernel: <code>\</code> <code>/</code> <code>:</code>
+ * <code>*</code> <code>?</code> <code>"</code> <code><</code>
+ * <code>></code> <code>|</code>
+ *
+ * @param filename
+ * @return the normalized path or <code>null</code> if not a windows native path.
+ * @see #isWindowsNativeFilename(String)
+ */
+ public static File normalizeWindowsNativeFilename(String filename) {
+ String fn = extractLocalPath(filename);
+ assert(fn!=null);
+ if (fn.length()>0) {
+ Pattern pattern = Pattern.compile(WINDOW_NATIVE_FILENAME_PATTERN);
+ Matcher matcher = pattern.matcher(fn);
+ if (matcher.find()) {
+ return new File(fn.replace('\\', File.separatorChar));
+ }
+ }
+ return null;
+ }
+
}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2010-02-17 09:53:18 UTC (rev 134)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2010-04-27 19:43:47 UTC (rev 135)
@@ -896,5 +896,107 @@
new URL("jar:file:test.jar!/"), //$NON-NLS-1$
FileSystem.getParentURL(new URL("jar:file:test.jar!/"))); //$NON-NLS-1$
}
+
+ /**
+ */
+ public void testIsWindowNativeFilename() {
+ assertTrue(FileSystem.isWindowsNativeFilename("file:C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("a\\b\\c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+
+ assertFalse(FileSystem.isWindowsNativeFilename("file:C:/a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file://C:/a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file:C:a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file://C:a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file:/a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file:///a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file:a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file://a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file://host/a/b/c.txt")); //$NON-NLS-1$
+ assertFalse(FileSystem.isWindowsNativeFilename("file:////host/a/b/c.txt")); //$NON-NLS-1$
+
+ assertTrue(FileSystem.isWindowsNativeFilename("C:c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:C:c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file:c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://C:c.txt")); //$NON-NLS-1$
+ assertTrue(FileSystem.isWindowsNativeFilename("file://c.txt")); //$NON-NLS-1$
+ }
+
+ /**
+ */
+ public void testNormalizeWindowNativeFilename() {
+ assertEquals(new File("C:/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("//host/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("//host/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+
+ assertEquals(new File("C:/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("C:\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://C:a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("\\a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("a\\b\\c.txt")); //$NON-NLS-1$
+ assertEquals(new File("//host/a/b/c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("\\\\host\\a\\b\\c.txt")); //$NON-NLS-1$
+
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:C:/a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file://C:/a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:C:a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file://C:a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:/a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:///a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file://a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file://host/a/b/c.txt")); //$NON-NLS-1$
+ assertNull(FileSystem.normalizeWindowsNativeFilename("file:////host/a/b/c.txt")); //$NON-NLS-1$
+
+ assertEquals(new File("C:c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("C:c.txt")); //$NON-NLS-1$
+ assertEquals(new File("c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:C:c.txt")); //$NON-NLS-1$
+ assertEquals(new File("c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file:c.txt")); //$NON-NLS-1$
+ assertEquals(new File("C:c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://C:c.txt")); //$NON-NLS-1$
+ assertEquals(new File("c.txt"), //$NON-NLS-1$
+ FileSystem.normalizeWindowsNativeFilename("file://c.txt")); //$NON-NLS-1$
+ }
+
}