org.moyoman.client.reference.util
Class FileSupport

java.lang.Object
  |
  +--org.moyoman.client.reference.util.FileSupport

public class FileSupport
extends Object

Provides file system methods for reading icons, input streams, properties, and directory listings. These methods search for files in the following order:
1. MOYOMAN_HOME directory
2. classpath (includes the application jar)
3. user.dir System property

Also provides methods to ensure the existence of directories, and to copy Jar contents into a local directory.

Since:
v0.01
Version:
v0.01
Author:
Jeffrey M. Thompson

Field Summary
private static String _baseDirectory
          The base directory.
private static String _jarFilename
          The jar filename.
private static String _moyomanDirectory
          The Moyoman home directory.
private static String _userDirectory
          The user directory.
 
Constructor Summary
FileSupport()
           
 
Method Summary
static void copyDirectoryContentFromJar(String relativePath0)
          Copy the content of the given relative path in the Jar to the base directory plus relative path.
static void ensureDirectoryExists(String relativePath)
          Ensure the given directory exists.
private static String formatJarPath(String path)
          Format the given filepath to start with a forward slash.
private static String formatPath(String path)
          Format the given filepath to start with a file separator.
static AudioClip getAudioClip(String relativeFilePath)
          Return the audio clip from the given filename.
private static AudioClip getAudioClipFromURL(URL url)
          Return the audio clip from the given URL.
static String getBaseDirectory()
          Return the base application directory.
static Icon getIcon(String relativeFilePath)
          Return the icon from the given filename.
private static Icon getIconFromClasspath(String relativeFilePath)
          Return the icon from the classpath from the given file.
private static Icon getIconFromDirectory(String directory, String relativeFilePath)
          Return the icon from the given directory and file.
static InputStream getInputStream(String relativeFilePath)
          Return an input stream for the given file.
private static InputStream getInputStreamFromClasspath(String relativeFilePath)
          Return the input stream from the classpath.
private static InputStream getInputStreamFromDirectory(String directory, String relativeFilePath)
          Return an input stream for the given filename.
static Properties getProperties(String relativeFilePath)
          Return the properties from the given filename.
static boolean isWindowsApplication()
          Return true if this application is running on Windows.
static String[] listFiles(String relativePath, String filePrefix, String fileSuffix)
          List the files in the given directory which have the given file prefix and the given file suffix.
private static String[] listFilesFromDirectory(String baseDirectory, String relativePath, String filePrefix, String fileSuffix)
          List the files in the given directory which have the given file prefix and the given file suffix.
private static String[] listFilesFromJar(String jarFilename, String relativePath, String filePrefix, String fileSuffix)
          List the files from the Jar in the given directory which have the given file prefix and the given file suffix.
static void main(String[] args)
          Test method.
private static boolean matchesPrefix(String name, String prefix)
          Return true if the given name matches the file prefix.
private static boolean matchesSuffix(String name, String suffix)
          Return true if the given name matches the file suffix.
static void setJarFilename(String jarFilename)
          Set the jar filename.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_moyomanDirectory

private static final String _moyomanDirectory
The Moyoman home directory.


_userDirectory

private static final String _userDirectory
The user directory.


_jarFilename

private static String _jarFilename
The jar filename.


_baseDirectory

private static final String _baseDirectory
The base directory.

Constructor Detail

FileSupport

public FileSupport()
Method Detail

getAudioClip

public static AudioClip getAudioClip(String relativeFilePath)
Return the audio clip from the given filename. Search for the file in the Moyoman directory; if that fails, search for the file in the classpath; if that fails, search for the file in the user directory.

Parameters:
relativeFilePath - The file path relative to the base directory.
Returns:
The audio clip read from the file, or null if the file is not found.
Since:
v0.03

getBaseDirectory

public static String getBaseDirectory()
Return the base application directory. This is the MOYOMAN_HOME System property, or if that's null, the user.dir System property.

Since:
v0.01

getIcon

public static Icon getIcon(String relativeFilePath)
Return the icon from the given filename. Search for the file in the Moyoman directory; if that fails, search for the file in the classpath; if that fails, search for the file in the user directory.

Parameters:
relativeFilePath - The file path relative to the base directory.
Returns:
The icon read from the file, or null if the file is not found.
Since:
v0.01

getInputStream

public static InputStream getInputStream(String relativeFilePath)
Return an input stream for the given file. Search for the file in the Moyoman directory; if that fails, search for the file in the classpath; if that fails, search for the file in the user directory.

Parameters:
relativeFilePath - The file path relative to the base directory.
Since:
v0.01

setJarFilename

public static void setJarFilename(String jarFilename)
Set the jar filename.

Parameters:
jarFilename - The new jar filename.
Since:
v0.01

getProperties

public static Properties getProperties(String relativeFilePath)
Return the properties from the given filename. Search for the file in the Moyoman directory; if that fails, search for the file in the classpath; if that fails, search for the file in the user directory.

Parameters:
relativeFilePath - The file path relative to the base directory.
Returns:
The properties read from the file, or null if the file is not found.
Since:
v0.01

isWindowsApplication

public static boolean isWindowsApplication()
Return true if this application is running on Windows.

Since:
v0.01

copyDirectoryContentFromJar

public static void copyDirectoryContentFromJar(String relativePath0)
Copy the content of the given relative path in the Jar to the base directory plus relative path. If a particular file already exists in the local directory, it will not be overwritten.

Parameters:
relativePath0 - The path relative to the base directory.
Since:
v0.01

ensureDirectoryExists

public static void ensureDirectoryExists(String relativePath)
Ensure the given directory exists.

Parameters:
relativePath - The relative directory path.
Throws:
SecurityException - if the application has no rights to create the directory.
Since:
v0.01

listFiles

public static String[] listFiles(String relativePath,
                                 String filePrefix,
                                 String fileSuffix)
List the files in the given directory which have the given file prefix and the given file suffix. Search for the directory in the Moyoman directory; if that fails, search for the directory in the classpath; if that fails, search for the directory in the user directory.

Parameters:
relativePath - The relative directory path.
filePrefix - The file prefix (ignored if null).
fileSuffix - The file suffix (ignored if null).
Since:
v0.01

main

public static void main(String[] args)
Test method.


getAudioClipFromURL

private static AudioClip getAudioClipFromURL(URL url)
Return the audio clip from the given URL.

Parameters:
url - URL.
Since:
v0.03

getIconFromClasspath

private static Icon getIconFromClasspath(String relativeFilePath)
Return the icon from the classpath from the given file.

Parameters:
relativeFilePath - The file path relative to the base directory.
Since:
v0.01

getIconFromDirectory

private static Icon getIconFromDirectory(String directory,
                                         String relativeFilePath)
Return the icon from the given directory and file.

Parameters:
directory - The directory.
relativeFilePath - The file path relative to the base directory.
Since:
v0.01

getInputStreamFromClasspath

private static InputStream getInputStreamFromClasspath(String relativeFilePath)
Return the input stream from the classpath.

Parameters:
relativeFilePath - The file path relative to the base directory.
Since:
v0.01

getInputStreamFromDirectory

private static InputStream getInputStreamFromDirectory(String directory,
                                                       String relativeFilePath)
Return an input stream for the given filename.

Parameters:
directory - The directory.
relativeFilePath - The file path relative to the base directory.
Since:
v0.01

formatJarPath

private static String formatJarPath(String path)
Format the given filepath to start with a forward slash.

Since:
v0.01

formatPath

private static String formatPath(String path)
Format the given filepath to start with a file separator.

Since:
v0.01

listFilesFromDirectory

private static String[] listFilesFromDirectory(String baseDirectory,
                                               String relativePath,
                                               String filePrefix,
                                               String fileSuffix)
List the files in the given directory which have the given file prefix and the given file suffix.

Parameters:
baseDirectory - The base directory.
relativePath - The relative directory path.
filePrefix - The file prefix (ignored if null).
fileSuffix - The file suffix (ignored if null).
Since:
v0.01

listFilesFromJar

private static String[] listFilesFromJar(String jarFilename,
                                         String relativePath,
                                         String filePrefix,
                                         String fileSuffix)
List the files from the Jar in the given directory which have the given file prefix and the given file suffix.

Parameters:
jarFilename - The jar filename.
relativePath - The relative directory path.
filePrefix - The file prefix (ignored if null).
fileSuffix - The file suffix (ignored if null).
Since:
v0.01

matchesPrefix

private static boolean matchesPrefix(String name,
                                     String prefix)
Return true if the given name matches the file prefix.

Since:
v0.01

matchesSuffix

private static boolean matchesSuffix(String name,
                                     String suffix)
Return true if the given name matches the file suffix.

Since:
v0.01