[Arakhnę-Dev] [109] Add FileSystem.convertStringToURL(String,boolean,boolean).

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


Revision: 109
Author:   galland
Date:     2010-01-02 12:20:12 +0100 (Sat, 02 Jan 2010)
Log Message:
-----------
Add FileSystem.convertStringToURL(String,boolean,boolean).

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	2010-01-02 10:12:32 UTC (rev 108)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-01-02 11:20:12 UTC (rev 109)
@@ -1291,6 +1291,36 @@
 	 * @see Resources#getResource(String)
 	 */
 	public static URL convertStringToUrl(String urlDescription, boolean allowResourceSearch) {
+		return convertStringToUrl(urlDescription, allowResourceSearch, true);
+	}
+
+	/** Convert a string to an URL according to several rules.
+	 * <p>
+	 * The rules are (the first succeeded is replied):
+	 * <ul>
+	 * <li>if <var>urlDescription</var> is <code>null</code> or empty, return <code>null</code>;</li>
+	 * <li>try to build an {@link URL} with <var>urlDescription</var> as parameter;</li>
+	 * <li>if <var>allowResourceSearch</var> is <code>true</code> and 
+	 * <var>urlDescription</var> starts with {@code "resource:"}, call
+	 * {@link Resources#getResource(String)} with the rest of the string as parameter;</li>
+	 * <li>if <var>allowResourceSearch</var> is <code>true</code>, call
+	 * {@link Resources#getResource(String)} with the <var>urlDescription</var> as
+	 * parameter;</li>
+	 * <li>if <var>repliesFileURL</var> is <code>true</code> and 
+	 * assuming that the <var>urlDescription</var> is
+	 * a filename, call {@link File#toURI()} to retreive an URI and then
+	 * {@link URI#toURL()};</li>
+	 * <li>If everything else failed, return <code>null</code>.</li>
+	 * </ul>
+	 * 
+	 * @param urlDescription is a string which is describing an URL.
+	 * @param allowResourceSearch indicates if the convertion must take into account the Java resources.
+	 * @param repliesFileURL indicates if urlDescription is allowed to be a filename.
+	 * @return the URL.
+	 * @throws IllegalArgumentException is the string could not be formatted to URL.
+	 * @see Resources#getResource(String)
+	 */
+	public static URL convertStringToUrl(String urlDescription, boolean allowResourceSearch, boolean repliesFileURL) {
 		if (urlDescription==null || urlDescription.length()==0) return null;
 		
 		try {
@@ -1319,14 +1349,16 @@
 			return null;
 		}
 
-		try {
-			File file = new File(urlDescription);
-			URI uri = file.toURI();
-			return uri.toURL();
+		if (repliesFileURL) {
+			try {
+				File file = new File(urlDescription);
+				URI uri = file.toURI();
+				return uri.toURL();
+			}
+			catch (MalformedURLException e) {
+				// ignore error
+			}
 		}
-		catch (MalformedURLException e) {
-			// ignore error
-		}
 		
 		return null;
 	}


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