org.aitools.programd.util
Class FileManager

java.lang.Object
  extended by org.aitools.programd.util.FileManager

public class FileManager
extends java.lang.Object

FileManager provides a standard interface for getting File objects and paths.


Field Summary
static java.lang.String FILE
          The string ""file"".
private static org.apache.log4j.Logger logger
          The logger.
private static java.net.URL root
          The root path for running Program D.
private static java.lang.String SLASH
           
private static java.util.Stack<java.net.URL> workingDirectory
          The current working directory.
 
Constructor Summary
FileManager()
           
 
Method Summary
static java.lang.String getAbsolutePath(java.lang.String path)
          Returns the absolute path.
static java.io.File getBestFile(java.lang.String path)
          Gets a file from a given path.
static java.io.File getExistingDirectory(java.lang.String path)
          Sames as getExistingFile(java.lang.String) except that it also checks that the given path is a directory.
static java.io.File getExistingFile(java.lang.String path)
          Gets a file from a given path.
static java.lang.String getFileContents(java.lang.String path)
          Returns the entire contents of a file as a String.
static java.io.FileInputStream getFileInputStream(java.lang.String path)
          Opens and returns a FileInputStream for a given path.
static java.io.FileOutputStream getFileOutputStream(java.lang.String path)
          Opens and returns a FileOutputStream for a given path.
static java.io.FileWriter getFileWriter(java.lang.String path, boolean append)
          Gets a FileWriter from a given path.
static java.net.URL getRootPath()
          Returns the root path.
static java.net.URL getWorkingDirectory()
          Returns the working directory.
static java.util.List<java.io.File> glob(java.lang.String path)
          Expands a localized file name that may contain wildcards to an array of file names without wildcards.
static java.util.List<java.io.File> glob(java.lang.String path, java.lang.String workingDirectoryToUse)
           Expands a localized file name that may contain wildcards to an array of file names without wildcards.
static java.io.File checkOrCreate(java.lang.String path, java.lang.String description)
          Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.
static java.io.File checkOrCreateDirectory(java.lang.String path, java.lang.String description)
          Checks whether a directory given by a path exists, and if not, creates it, along with any necessary subdirectories.
static java.lang.String loadFileAsString(java.io.File file)
          Loads a file into a String.
static java.lang.String loadFileAsString(java.lang.String path)
          Loads a file into a String.
static void popWorkingDirectory()
          Pops a working directory off the stack.
static void pushWorkingDirectory(java.net.URL path)
          Pushes a new working directory onto the stack.
static void setRootPath(java.net.URL url)
          Sets the root path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

private static java.net.URL root
The root path for running Program D.


logger

private static org.apache.log4j.Logger logger
The logger.


workingDirectory

private static java.util.Stack<java.net.URL> workingDirectory
The current working directory.


SLASH

private static final java.lang.String SLASH
See Also:
Constant Field Values

FILE

public static final java.lang.String FILE
The string ""file"".

See Also:
Constant Field Values
Constructor Detail

FileManager

public FileManager()
Method Detail

setRootPath

public static void setRootPath(java.net.URL url)
Sets the root path.

Parameters:
url - the root path

getRootPath

public static java.net.URL getRootPath()
Returns the root path.

Returns:
the root path

getBestFile

public static java.io.File getBestFile(java.lang.String path)
Gets a file from a given path. Tries to return it as a canonical file; failing that, returns the absolute file, (which may not be valid, but we do not check that here).

Parameters:
path - the path for the file
Returns:
the file (may not exist!)

getExistingFile

public static java.io.File getExistingFile(java.lang.String path)
                                    throws java.io.FileNotFoundException
Gets a file from a given path. First tries to use the path as-is if it's absolute, then looks in the current working directory. The file must already exist, or an exception will be thrown.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
Returns:
the file
Throws:
java.io.FileNotFoundException

getExistingDirectory

public static java.io.File getExistingDirectory(java.lang.String path)
Sames as getExistingFile(java.lang.String) except that it also checks that the given path is a directory.

Parameters:
path - the path for the directory (may be absolute or relative to root directory)
Returns:
the directory

getFileInputStream

public static java.io.FileInputStream getFileInputStream(java.lang.String path)
                                                  throws java.io.FileNotFoundException
Opens and returns a FileInputStream for a given path. If the specified file doesn't exist, an exception will be thrown.

Parameters:
path -
Returns:
a stream pointing to the given path
Throws:
java.io.FileNotFoundException - if the file does not exist

getFileOutputStream

public static java.io.FileOutputStream getFileOutputStream(java.lang.String path)
                                                    throws java.io.FileNotFoundException
Opens and returns a FileOutputStream for a given path. If the specified file doesn't exist, an exception will be thrown.

Parameters:
path - the path to which to return a stream
Returns:
a stream pointing to the given path
Throws:
java.io.FileNotFoundException - if the file does not exist

getFileWriter

public static java.io.FileWriter getFileWriter(java.lang.String path,
                                               boolean append)
                                        throws java.io.IOException
Gets a FileWriter from a given path. First tries to use the path as-is if it's absolute, then (otherwise) looks in the defined root directory.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
append - if true, then bytes will be written to the end of the file rather than the beginning
Returns:
the FileWriter
Throws:
java.io.IOException - if the specified file is not found or if some other I/O error occurs

getAbsolutePath

public static java.lang.String getAbsolutePath(java.lang.String path)
                                        throws java.io.FileNotFoundException
Returns the absolute path. First checks whether the path as-is is absolute, then (otherwise) looks in the defined root directory.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
Returns:
the absolute path
Throws:
java.io.FileNotFoundException - if a file with the given path cannot be located

checkOrCreate

public static java.io.File checkOrCreate(java.lang.String path,
                                         java.lang.String description)
Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.

Parameters:
path - denoting the file to create
description - describes what the file is for, for trace messages. Should fit into a sentence like, "created new description ". May be null (which will result in less informative messages).
Returns:
the file that is created (or retrieved)

checkOrCreateDirectory

public static java.io.File checkOrCreateDirectory(java.lang.String path,
                                                  java.lang.String description)
Checks whether a directory given by a path exists, and if not, creates it, along with any necessary subdirectories.

Parameters:
path - denoting the directory to create
description - describes what the directory is for, for trace messages. Should fit into a sentence like, "created new description ". May be null (which will result in less informative messages).
Returns:
the directory that is created (or retrieved)

getFileContents

public static java.lang.String getFileContents(java.lang.String path)
Returns the entire contents of a file as a String.

Parameters:
path - the path to the file (local file or URL)
Returns:
the entire contents of a file as a String

glob

public static java.util.List<java.io.File> glob(java.lang.String path)
                                         throws java.io.FileNotFoundException
Expands a localized file name that may contain wildcards to an array of file names without wildcards. All file separators in the file name must preceed any wildcard. The current directory is assumed to be the working directory.

Parameters:
path -
Returns:
array of file names without wildcards
Throws:
java.io.FileNotFoundException - if wild card is misused

glob

public static java.util.List<java.io.File> glob(java.lang.String path,
                                                java.lang.String workingDirectoryToUse)
                                         throws java.io.FileNotFoundException

Expands a localized file name that may contain wildcards to an array of file names without wildcards. All file separators in the file name must preceed any wildcard.

Adapted, with gratitude, from the JMK project. (Under the GNU LGPL)

Parameters:
path - the path string to glob
workingDirectoryToUse - the path to which relative paths should be considered relative
Returns:
array of file names without wildcards
Throws:
java.io.FileNotFoundException - if wild card is misused
See Also:
JMK

pushWorkingDirectory

public static void pushWorkingDirectory(java.net.URL path)
Pushes a new working directory onto the stack.

Parameters:
path - the directory path

popWorkingDirectory

public static void popWorkingDirectory()
Pops a working directory off the stack.


getWorkingDirectory

public static java.net.URL getWorkingDirectory()
Returns the working directory.

Returns:
the working directory

loadFileAsString

public static java.lang.String loadFileAsString(java.lang.String path)
                                         throws java.io.FileNotFoundException
Loads a file into a String.

Parameters:
path - the path to the file
Returns:
the loaded template
Throws:
java.io.FileNotFoundException

loadFileAsString

public static java.lang.String loadFileAsString(java.io.File file)
Loads a file into a String.

Parameters:
file - the file
Returns:
the loaded template