[Arakhnę-Dev] [338] * FileSystem: add the following functions on file basename - hasExtension, extension, and extensions.

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


Revision: 338
Author:   galland
Date:     2012-05-15 20:59:34 +0200 (Tue, 15 May 2012)
Log Message:
-----------
* FileSystem: add the following functions on file basename - hasExtension, extension, and extensions.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2012-05-14 17:12:23 UTC (rev 337)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2012-05-15 18:59:34 UTC (rev 338)
@@ -659,6 +659,25 @@
 	 *
 	 * @param filename is the name to parse.
 	 * @return the extension of the specified file
+	 * @see #shortBasename(File)
+	 * @see #largeBasename(File)
+	 * @see #basename(File)
+	 * @see #dirname(File)
+	 * @see #extensions(File)
+	 * @since 7.0
+	 */
+	public static String extension(String filename) {
+		if (filename==null) return null;
+		String largeBasename = largeBasename(filename);
+		int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
+		if (idx<=0) return ""; //$NON-NLS-1$
+		return largeBasename.substring(idx);
+	}
+
+	/** Reply the extension of the specified file.
+	 *
+	 * @param filename is the name to parse.
+	 * @return the extension of the specified file
 	 * @see #shortBasename(URL)
 	 * @see #largeBasename(URL)
 	 * @see #basename(URL)
@@ -692,7 +711,23 @@
 	 *
 	 * @param filename is the name to parse.
 	 * @return the extensions of the specified file
+	 * @since 7.0
 	 */
+	public static String[] extensions(String filename) {
+		if (filename==null) return new String[0];
+		String largeBasename = largeBasename(filename);
+		String[] parts = largeBasename.split(Pattern.quote(Character.toString(getFileExtensionCharacter())));
+		if (parts.length<=1) return new String[0];
+		String[] r = new String[parts.length-1];
+		System.arraycopy(parts, 1, r, 0, r.length);
+		return r;
+	}
+
+	/** Reply all the extensions of the specified file.
+	 *
+	 * @param filename is the name to parse.
+	 * @return the extensions of the specified file
+	 */
 	public static String[] extensions(URL filename) {
 		if (filename==null) return new String[0];
 		String largeBasename = largeBasename(filename);
@@ -911,7 +946,30 @@
 	 * @param extension is the extension to test.
 	 * @return <code>true</code> if the given filename has the given extension,
 	 * otherwise <code>false</code>
+	 * @since 7.0
 	 */
+	public static boolean hasExtension(String filename, String extension) {
+		if (filename==null) return false;
+		assert(extension!=null);
+		String extent = extension;
+		if (!"".equals(extent) && !extent.startsWith(EXTENSION_SEPARATOR)) //$NON-NLS-1$
+			extent = EXTENSION_SEPARATOR+extent;
+		String ext = extension(filename);
+		if (ext==null) return false;
+		if (isCaseSensitiveFilenameSystem())
+			return ext.equals(extent);
+		return ext.equalsIgnoreCase(extent);
+	}
+
+	/** Replies if the specified file has the specified extension.
+	 * <p>
+	 * The test is dependent of the case-sensitive attribute of operating system.
+	 * 
+	 * @param filename is the filename to parse
+	 * @param extension is the extension to test.
+	 * @return <code>true</code> if the given filename has the given extension,
+	 * otherwise <code>false</code>
+	 */
 	public static boolean hasExtension(URL filename, String extension) {
 		if (filename==null) return false;
 		assert(extension!=null);


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