[Arakhnę-Dev] [77] - Update comments

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


Revision: 77
Author:   galland
Date:     2009-08-04 14:28:20 +0200 (Tue, 04 Aug 2009)

Log Message:
-----------
- Update comments
- Add getSystemConfigurationDirectoryFor() and getSystemConfigurationDirectoryNameFor()
- Add convertUrlToFile() and convertStringToUrl()
- Deprecate UrlToFile()

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	2009-08-04 12:26:54 UTC (rev 76)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2009-08-04 12:28:20 UTC (rev 77)
@@ -1,7 +1,7 @@
 /* 
  * $Id$
  * 
- * Copyright (C) 2004-2008 Stéphane GALLAND
+ * Copyright (C) 2004-2009 Stéphane GALLAND
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -26,6 +26,7 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -35,7 +36,7 @@
 /** An utility class that permits to deal with filenames.
  * 
  * @author Stéphane GALLAND <galland@xxxxxxxxxxx>
- * @version $Name:  $ $Revision: 1.4 $ $Date: 2007-05-04 07:21:05 $
+ * @version $Name$ $Revision$ $Date$
  */
 public class FileSystem {
 	
@@ -213,8 +214,10 @@
 	public static String join(String... elements) {
 		StringBuffer buf = new StringBuffer();
 		for (String elt : elements) {
-			if (buf.length()>0) buf.append(File.separatorChar);
-			buf.append(elt);
+			if (elt!=null && elt.length()>0) { 
+				if (buf.length()>0) buf.append(File.separatorChar);
+				buf.append(elt);
+			}
 		}
 		return buf.toString();
 	}
@@ -350,9 +353,17 @@
 	}
 
 	/** Replies the user configuration directory for the specified software.
+	 * <p>
+	 * On Unix operating systems, the user directory for a
+	 * software is by default {@code $HOME/.software} where {@code software}
+	 * is the given parameter (case-sensitive). On Windows&reg; operating systems, the user
+	 * directory for a software is by default
+	 * {@code C:<span>\</span>Documents and Settings<span>\</span>userName<span>\</span>Local Settings<span>\</span>Application Data<span>\</span>software}
+	 * where {@code userName} is the login of the current user and {@code software}
+	 * is the given parameter (case-insensitive). 
 	 *
 	 * @param software is the name of the concerned software.
-	 * @return the configuration directory of the current user.
+	 * @return the configuration directory of the software for the current user.
 	 */
 	public static File getUserConfigurationDirectoryFor(String software) {
 		try {
@@ -380,9 +391,17 @@
 	}
 	
 	/** Replies the user configuration directory for the specified software.
+	 * <p>
+	 * On Unix operating systems, the user directory for a
+	 * software is by default {@code $HOME/.software} where {@code software}
+	 * is the given parameter (case-sensitive). On Windows&reg; operating systems, the user
+	 * directory for a software is by default
+	 * {@code C:<span>\</span>Documents and Settings<span>\</span>userName<span>\</span>Local Settings<span>\</span>Application Data<span>\</span>software}
+	 * where {@code userName} is the login of the current user and {@code software}
+	 * is the given parameter (case-insensitive). 
 	 * 
 	 * @param software is the name of the concerned software.
-	 * @return the configuration directory of the current user.
+	 * @return the configuration directory of the software for the current user.
 	 */
 	public static String getUserConfigurationDirectoryNameFor(String software) {
 		File directory = getUserConfigurationDirectoryFor(software);
@@ -390,13 +409,74 @@
 		return null;
 	}
 
+	/** Replies the system configuration directory for the specified software.
+	 * <p>
+	 * On Unix operating systems, the system directory for a
+	 * software is by default {@code /etc/software} where {@code software}
+	 * is the given parameter (case-sensitive). On Windows&reg; operating systems, the user
+	 * directory for a software is by default
+	 * {@code C:<span>\</span>Program Files<span>\</span>software}
+	 * where {@code software} is the given parameter (case-insensitive). 
+	 *
+	 * @param software is the name of the concerned software.
+	 * @return the configuration directory of the software for the current user.
+	 */
+	public static File getSystemConfigurationDirectoryFor(String software) {
+		OperatingSystem os = OperatingSystem.getCurrentOS();
+		if (os.isUnixCompliant()) {
+			File[] roots = File.listRoots();
+			return new File(new File(join(roots[0].getAbsolutePath(),"etc")), software); //$NON-NLS-1$
+		}
+		else if (os==OperatingSystem.WIN) {
+			File pfDirectory;
+			for(File root : File.listRoots()) {
+				pfDirectory = new File(root, "Program Files"); //$NON-NLS-1$
+				if (pfDirectory.isDirectory()) {
+					return new File(root, software);
+				}
+			}
+		}
+		return null;
+	}
+	
+	/** Replies the user configuration directory for the specified software.
+	 * <p>
+	 * On Unix operating systems, the system directory for a
+	 * software is by default {@code /etc/software} where {@code software}
+	 * is the given parameter (case-sensitive). On Windows&reg; operating systems, the user
+	 * directory for a software is by default
+	 * {@code C:<span>\</span>Program Files<span>\</span>software}
+	 * where {@code software} is the given parameter (case-insensitive). 
+	 * 
+	 * @param software is the name of the concerned software.
+	 * @return the configuration directory of the software for the current user.
+	 */
+	public static String getSystemConfigurationDirectoryNameFor(String software) {
+		File directory = getSystemConfigurationDirectoryFor(software);
+		if (directory!=null) return directory.getAbsolutePath();
+		return null;
+	}
+
 	/** Convert an URL which represents a local file into a File.
 	 * 
 	 * @param url is the URL to convert.
 	 * @return the file.
 	 * @throws IllegalArgumentException is the URL was malformed.
+	 * @deprecated see {@link #convertUrlToFile(URL)}
+	 * @see #convertUrlToFile(URL)
 	 */
+	@Deprecated
 	public static File UrlToFile(URL url) {
+		return convertUrlToFile(url);
+	}
+
+	/** Convert an URL which represents a local file into a File.
+	 * 
+	 * @param url is the URL to convert.
+	 * @return the file.
+	 * @throws IllegalArgumentException is the URL was malformed.
+	 */
+	public static File convertUrlToFile(URL url) {
 		URI uri;
 		try {
 			// this is the step that can fail, and so
@@ -436,5 +516,64 @@
 		throw new IllegalArgumentException("not a file URL: "+url); //$NON-NLS-1$
 	}
 	
+	/** 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 Class#getResource(String)} with the rest of the string as parameter;</li>
+	 * <li>if <var>allowResourceSearch</var> is <code>true</code>, call
+	 * {@link Class#getResource(String)} with the <var>urlDescription</var> as
+	 * parameter;</li>
+	 * <li>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 every thing 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 resources.
+	 * @return the URL.
+	 * @throws IllegalArgumentException is the string could not be formatted to URL.
+	 */
+	public static URL convertStringToUrl(String urlDescription, boolean allowResourceSearch) {
+		if (urlDescription==null || urlDescription.length()==0) return null;
+		
+		try {
+			return new URL(urlDescription);
+		}
+		catch (MalformedURLException _) {
+			// ignore error
+		}
+		
+		URL url;
+		
+		if (allowResourceSearch) {
+			
+			if (urlDescription.toLowerCase().startsWith("resource:")) { //$NON-NLS-1$
+				url = ClassLoader.getSystemResource(urlDescription.substring(9));
+				if (url!=null) return url;
+			}
+			
+			url = ClassLoader.getSystemResource(urlDescription);
+			if (url!=null) return url;
+		}
+
+		try {
+			File file = new File(urlDescription);
+			URI uri = file.toURI();
+			if (uri!=null)
+				return uri.toURL();
+		}
+		catch (MalformedURLException _) {
+			// ignore error
+		}
+		
+		return null;
+	}
+
 }
 


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