org.springframework.shell.support.util
Class FileUtils

java.lang.Object
  extended by org.springframework.shell.support.util.FileUtils

public final class FileUtils
extends Object

Utilities for handling File instances.

Since:
1.0

Field Summary
static String CURRENT_DIRECTORY
          The relative file path to the current directory.
 
Method Summary
static String backOneDirectory(String fileIdentifier)
          Returns the given file system path minus its last element
static boolean copyRecursively(File source, File destination, boolean deleteDestinationOnExit)
          Copies the specified source directory to the destination.
static boolean deleteRecursively(File file)
          Deletes the specified File.
static boolean denotesAbsolutePath(String fileName)
          Checks if the provided fileName denotes an absolute path on the file system.
static String ensureTrailingSeparator(String path)
          Ensures that the given path has exactly one trailing File.separator
static String getCanonicalPath(File file)
          Returns the canonical path of the given File.
static File getFile(Class<?> loadingClass, String filename)
          Loads the given file from the classpath.
static String getFileSeparatorAsRegex()
          Returns the platform-specific file separator as a regular expression.
static String getFirstDirectory(String fileIdentifier)
          Returns the part of the given path that represents a directory, in other words the given path if it's already a directory, or the parent directory if it's a file.
static InputStream getInputStream(Class<?> loadingClass, String filename)
          Loads the given file from the classpath.
static String getPath(Class<?> loadingClass, String relativeFilename)
          Determines the path to the requested file, relative to the given class.
static String getSystemDependentPath(Collection<String> pathElements)
          Returns an operating-system-dependent path consisting of the given elements, separated by File.separator.
static String getSystemDependentPath(String... pathElements)
          Returns an operating-system-dependent path consisting of the given elements, separated by File.separator.
static boolean matchesAntPath(String antPattern, String canonicalPath)
          Indicates whether the given canonical path matches the given Ant-style pattern
static String read(File file)
          Returns the contents of the given File as a String.
static String readBanner(Class<?> loadingClass, String resourceName)
          Reads a banner from the given resource.
static String readBanner(Reader reader)
          Reads a banner from the given resource.
static String removeLeadingAndTrailingSeparators(String path)
          Removes any leading or trailing File.separators from the given path.
static String removeTrailingSeparator(String path)
          Removes any trailing File.separators from the given path
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CURRENT_DIRECTORY

public static final String CURRENT_DIRECTORY
The relative file path to the current directory. Should be valid on all platforms that Roo supports.

See Also:
Constant Field Values
Method Detail

deleteRecursively

public static boolean deleteRecursively(File file)
Deletes the specified File.

If the File refers to a directory, any contents of that directory (including other directories) are also deleted.

If the File does not already exist, this method immediately returns true.

Parameters:
file - to delete (required; the file may or may not exist)
Returns:
true if the file is fully deleted, or false if there was a failure when deleting

copyRecursively

public static boolean copyRecursively(File source,
                                      File destination,
                                      boolean deleteDestinationOnExit)
Copies the specified source directory to the destination.

Both the source must exist. If the destination does not already exist, it will be created. If the destination does exist, it must be a directory (not a file).

Parameters:
source - the already-existing source directory (required)
destination - the destination directory (required)
deleteDestinationOnExit - indicates whether to mark any created destinations for deletion on exit
Returns:
true if the copy was successful

denotesAbsolutePath

public static boolean denotesAbsolutePath(String fileName)
Checks if the provided fileName denotes an absolute path on the file system. On Windows, this includes both paths with and without drive letters, where the latter have to start with '\'. No check is performed to see if the file actually exists!

Parameters:
fileName - name of a file, which could be an absolute path
Returns:
true if the fileName looks like an absolute path for the current OS

getFirstDirectory

public static String getFirstDirectory(String fileIdentifier)
Returns the part of the given path that represents a directory, in other words the given path if it's already a directory, or the parent directory if it's a file.

Parameters:
fileIdentifier - the path to parse (required)
Returns:
see above
Since:
1.2.0

backOneDirectory

public static String backOneDirectory(String fileIdentifier)
Returns the given file system path minus its last element

Parameters:
fileIdentifier -
Returns:
Since:
1.2.0

removeTrailingSeparator

public static String removeTrailingSeparator(String path)
Removes any trailing File.separators from the given path

Parameters:
path - the path to modify (can be null)
Returns:
the modified path
Since:
1.2.0

matchesAntPath

public static boolean matchesAntPath(String antPattern,
                                     String canonicalPath)
Indicates whether the given canonical path matches the given Ant-style pattern

Parameters:
antPattern - the pattern to check against (can't be blank)
canonicalPath - the path to check (can't be blank)
Returns:
see above
Since:
1.2.0

removeLeadingAndTrailingSeparators

public static String removeLeadingAndTrailingSeparators(String path)
Removes any leading or trailing File.separators from the given path.

Parameters:
path - the path to modify (can be null)
Returns:
the path, modified as above, or null if null was given
Since:
1.2.0

ensureTrailingSeparator

public static String ensureTrailingSeparator(String path)
Ensures that the given path has exactly one trailing File.separator

Parameters:
path - the path to modify (can't be null)
Returns:
the normalised path
Since:
1.2.0

getSystemDependentPath

public static String getSystemDependentPath(String... pathElements)
Returns an operating-system-dependent path consisting of the given elements, separated by File.separator.

Parameters:
pathElements - the path elements from uppermost downwards (can't be empty)
Returns:
a non-blank string
Since:
1.2.0

getSystemDependentPath

public static String getSystemDependentPath(Collection<String> pathElements)
Returns an operating-system-dependent path consisting of the given elements, separated by File.separator.

Parameters:
pathElements - the path elements from uppermost downwards (can't be empty)
Returns:
a non-blank string
Since:
1.2.0

getCanonicalPath

public static String getCanonicalPath(File file)
Returns the canonical path of the given File.

Parameters:
file - the file for which to find the canonical path (can be null)
Returns:
the canonical path, or null if a null file is given
Since:
1.2.0

getFileSeparatorAsRegex

public static String getFileSeparatorAsRegex()
Returns the platform-specific file separator as a regular expression.

Returns:
a non-blank regex
Since:
1.2.0

getPath

public static String getPath(Class<?> loadingClass,
                             String relativeFilename)
Determines the path to the requested file, relative to the given class.

Parameters:
loadingClass - the class to whose package the given file is relative (required)
relativeFilename - the name of the file relative to that package (required)
Returns:
the full classloader-specific path to the file (never null)
Since:
1.2.0

getFile

public static File getFile(Class<?> loadingClass,
                           String filename)
Loads the given file from the classpath.

Parameters:
loadingClass - the class from whose package to load the file (required)
filename - the name of the file to load, relative to that package (required)
Returns:
the file's input stream (never null)
Throws:
IllegalArgumentException - if the given file cannot be found

getInputStream

public static InputStream getInputStream(Class<?> loadingClass,
                                         String filename)
Loads the given file from the classpath.

Parameters:
loadingClass - the class from whose package to load the file (required)
filename - the name of the file to load, relative to that package (required)
Returns:
the file's input stream (never null)
Throws:
IllegalArgumentException - if the given file cannot be found

readBanner

public static String readBanner(Reader reader)
Reads a banner from the given resource. Performs conversion of any line separator contained by the source to that of the running platform.

Returns:
platform-compatible banner as a String

readBanner

public static String readBanner(Class<?> loadingClass,
                                String resourceName)
Reads a banner from the given resource. Performs conversion of any line separator contained by the source to that of the running platform.

Returns:
platform-compatible banner as a String

read

public static String read(File file)
Returns the contents of the given File as a String.

Parameters:
file - the file to read from (must be an existing file)
Returns:
the contents
Throws:
IllegalStateException - in case of I/O errors
Since:
1.2.0