Package org.codehaus.cargo.util
Class DefaultFileHandler
- java.lang.Object
-
- org.codehaus.cargo.util.log.LoggedObject
-
- org.codehaus.cargo.util.DefaultFileHandler
-
- All Implemented Interfaces:
FileHandler
,Loggable
public class DefaultFileHandler extends LoggedObject implements FileHandler
File operations that are performed in Cargo. All file operations must use this class.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.codehaus.cargo.util.FileHandler
FileHandler.XmlReplacementDetails
-
-
Field Summary
Fields Modifier and Type Field Description static int
FILE_BUFFER_SIZE
Default file buffer size.-
Fields inherited from interface org.codehaus.cargo.util.FileHandler
NEW_LINE
-
-
Constructor Summary
Constructors Constructor Description DefaultFileHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
append(String path, String suffixToAppend)
Append a suffix path to an existing path.void
copy(InputStream in, OutputStream out)
Copies data from an InputStream to an OutputStream using a default buffer size.void
copy(InputStream in, OutputStream out, int bufSize)
Copies data from an InputStream to an OutputStream.void
copyDirectory(String source, String target)
Copy a directory from a source to a destination.void
copyDirectory(String source, String target, List<String> excludes)
Copy a directory from a source to a destination specifying files to be excluded.void
copyDirectory(String source, String target, Map<String,String> replacements, Charset encoding)
Copy a directory from a source to a destination using a token replacement.void
copyFile(String source, String target)
Copy a file from a source to a destination.void
copyFile(String source, String target, boolean overwrite)
Copy a file from a source to a destination specifying if source files may overwrite newer destination files.void
copyFile(String source, String target, Map<String,String> replacements, Charset encoding)
Copy a file from a source to a destination using token replacement.String
createDirectory(String parentDir, String name)
Convenience method for creating a new directory inside another one.void
createFile(String file)
Create a file.String
createUniqueTmpDirectory()
Creates a unique temporary directory.void
delete(String path)
Deletes a file or directory, removing any children as appropriate.boolean
exists(String path)
void
explode(String war, String exploded)
Extracts a war file into a directory.String
getAbsolutePath(String path)
gets the absolute path from a file that may be relative to the current directory.String[]
getChildren(String directory)
String[]
getChildren(String directory, List<String> filters)
InputStream
getInputStream(String file)
String
getName(String file)
OutputStream
getOutputStream(String file)
String
getParent(String path)
long
getSize(String file)
String
getTmpPath(String name)
Compute the location of a temporary directory.String
getURL(String path)
boolean
isDirectory(String path)
boolean
isDirectoryEmpty(String dir)
void
mkdirs(String path)
Create directories for the passed path.String
readTextFile(String file, Charset encoding)
void
replaceInFile(String file, Map<String,String> replacements, Charset encoding)
Replaces using a map of replacements in a given file.void
replaceInFile(String file, Map<String,String> replacements, Charset encoding, boolean ignoreNonExistingProperties)
Replaces using a map of replacements in a given file.void
replaceInXmlFile(XmlReplacement... xmlReplacements)
Replaces using a map of XML replacements in a given file.static String
sanitizeFilename(String filename, Logger logger)
Sanitize a given name to turn it into a safe file name, removing for example leading or trailing slashes, as well as intermediate parent path jumps.void
writeTextFile(String file, String content, Charset encoding)
-
Methods inherited from class org.codehaus.cargo.util.log.LoggedObject
getLogger, setLogger
-
-
-
-
Field Detail
-
FILE_BUFFER_SIZE
public static final int FILE_BUFFER_SIZE
Default file buffer size.- See Also:
- Constant Field Values
-
-
Method Detail
-
sanitizeFilename
public static String sanitizeFilename(String filename, Logger logger)
Sanitize a given name to turn it into a safe file name, removing for example leading or trailing slashes, as well as intermediate parent path jumps.- Parameters:
filename
- name to sanitizelogger
- Logger to log when sanitization happens (optional)- Returns:
- sanitized name
-
copyFile
public void copyFile(String source, String target)
Copy a file from a source to a destination. If destination file already exists, it is not overwritten.- Specified by:
copyFile
in interfaceFileHandler
- Parameters:
source
- the file to copy from. Must not benull
.target
- the file to copy to. Must not benull
.
-
copyFile
public void copyFile(String source, String target, boolean overwrite)
Copy a file from a source to a destination specifying if source files may overwrite newer destination files.- Specified by:
copyFile
in interfaceFileHandler
- Parameters:
source
- the file to copy from. Must not benull
.target
- the file to copy to. Must not benull
.overwrite
- to overwrite the file if it already exists.
-
copyFile
public void copyFile(String source, String target, Map<String,String> replacements, Charset encoding)
Copy a file from a source to a destination using token replacement.- Specified by:
copyFile
in interfaceFileHandler
- Parameters:
source
- the file to copy from. Must not benull
target
- the file to copy to. Must not benull
replacements
- the token replacements to use. Must not benull
encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.
-
copyDirectory
public void copyDirectory(String source, String target)
Copy a directory from a source to a destination.- Specified by:
copyDirectory
in interfaceFileHandler
- Parameters:
source
- the directory to copy from. Must not benull
.target
- the directory to copy to. Must not benull
.
-
copyDirectory
public void copyDirectory(String source, String target, List<String> excludes)
Copy a directory from a source to a destination specifying files to be excluded.- Specified by:
copyDirectory
in interfaceFileHandler
- Parameters:
source
- the directory to copy from. Must not benull
.target
- the directory to copy to. Must not benull
.excludes
- the list of files to be excluded, can benull
. Each entry in the list is an Ant file pattern.
-
copyDirectory
public void copyDirectory(String source, String target, Map<String,String> replacements, Charset encoding)
Copy a directory from a source to a destination using a token replacement.- Specified by:
copyDirectory
in interfaceFileHandler
- Parameters:
source
- the directory to copy from. Must not benull
target
- the directory to copy to. Must not benull
replacements
- the token replacements to use. Must not benull
encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.
-
explode
public void explode(String war, String exploded)
Extracts a war file into a directory.- Specified by:
explode
in interfaceFileHandler
- Parameters:
war
- the War archive to be extracted.exploded
- the directory that receives files.
-
createDirectory
public String createDirectory(String parentDir, String name)
Convenience method for creating a new directory inside another one. If the directory already exists, it will return the already created directory.- Specified by:
createDirectory
in interfaceFileHandler
- Parameters:
parentDir
- The directory in which the new directory should be createdname
- The name of the directory to create- Returns:
- The new directory
-
copy
public void copy(InputStream in, OutputStream out, int bufSize)
Copies data from an InputStream to an OutputStream.- Specified by:
copy
in interfaceFileHandler
- Parameters:
in
- InputStream to copy data fromout
- OutputStream to copy data tobufSize
- size of the copy buffer
-
copy
public void copy(InputStream in, OutputStream out)
Copies data from an InputStream to an OutputStream using a default buffer size.. The default buffer size isFILE_BUFFER_SIZE
.- Specified by:
copy
in interfaceFileHandler
- Parameters:
in
- InputStream to copy data fromout
- OutputStream to copy data to
-
replaceInFile
public void replaceInFile(String file, Map<String,String> replacements, Charset encoding) throws CargoException
Replaces using a map of replacements in a given file.- Specified by:
replaceInFile
in interfaceFileHandler
- Parameters:
file
- File to replace in.replacements
- Map containing replacements.encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.- Throws:
CargoException
- If anything fails, most notably if one of the replacements does not exist in the file.
-
replaceInFile
public void replaceInFile(String file, Map<String,String> replacements, Charset encoding, boolean ignoreNonExistingProperties) throws CargoException
Replaces using a map of replacements in a given file.- Specified by:
replaceInFile
in interfaceFileHandler
- Parameters:
file
- File to replace in.replacements
- Map containing replacements.encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.ignoreNonExistingProperties
- Whether to ignore if one of the keys cannot be found.- Throws:
CargoException
- If anything fails, most notably if one of the replacements does not exist in the file.
-
replaceInXmlFile
public void replaceInXmlFile(XmlReplacement... xmlReplacements) throws CargoException
Replaces using a map of XML replacements in a given file.- Specified by:
replaceInXmlFile
in interfaceFileHandler
- Parameters:
xmlReplacements
- Array of XML replacements.- Throws:
CargoException
- If anything fails, most notably if one of the replacements does not exist in the file.
-
getTmpPath
public String getTmpPath(String name)
Compute the location of a temporary directory.- Specified by:
getTmpPath
in interfaceFileHandler
- Parameters:
name
- The name of the directory to compute inside the tmp directory- Returns:
- the location of the computed temporary directory
-
createUniqueTmpDirectory
public String createUniqueTmpDirectory()
Creates a unique temporary directory.- Specified by:
createUniqueTmpDirectory
in interfaceFileHandler
- Returns:
- the newly created temporary directory
-
delete
public void delete(String path)
Deletes a file or directory, removing any children as appropriate.- Specified by:
delete
in interfaceFileHandler
- Parameters:
path
- the path to the file or directory to remove
-
getSize
public long getSize(String file)
- Specified by:
getSize
in interfaceFileHandler
- Parameters:
file
- the file for which to get the size- Returns:
- long Size of the file, in bytes
-
getInputStream
public InputStream getInputStream(String file)
- Specified by:
getInputStream
in interfaceFileHandler
- Parameters:
file
- the file for which to get an InputStream for- Returns:
- an InputStream pointing to the file
-
getOutputStream
public OutputStream getOutputStream(String file)
- Specified by:
getOutputStream
in interfaceFileHandler
- Parameters:
file
- the file for which to get an OutputStream for- Returns:
- an OutputStream pointing to the file
-
append
public String append(String path, String suffixToAppend)
Append a suffix path to an existing path.- Specified by:
append
in interfaceFileHandler
- Parameters:
path
- the path to append tosuffixToAppend
- the suffix to append to the path- Returns:
- the new full path
-
mkdirs
public void mkdirs(String path)
Create directories for the passed path.- Specified by:
mkdirs
in interfaceFileHandler
- Parameters:
path
- the path for which to create all directories leading to it
-
getParent
public String getParent(String path)
- Specified by:
getParent
in interfaceFileHandler
- Parameters:
path
- the path for which to return its parent- Returns:
- the parent path of the passed path
-
exists
public boolean exists(String path)
- Specified by:
exists
in interfaceFileHandler
- Parameters:
path
- the path to check the existence for- Returns:
- true if the path exists or false otherwise
-
createFile
public void createFile(String file)
Create a file.- Specified by:
createFile
in interfaceFileHandler
- Parameters:
file
- the file to create
-
isDirectoryEmpty
public boolean isDirectoryEmpty(String dir)
- Specified by:
isDirectoryEmpty
in interfaceFileHandler
- Parameters:
dir
- the directory to check- Returns:
- true if the passed directory is empty
-
getName
public String getName(String file)
- Specified by:
getName
in interfaceFileHandler
- Parameters:
file
- the file name for which to return the file name- Returns:
- the file name (without path)
-
getURL
public String getURL(String path)
- Specified by:
getURL
in interfaceFileHandler
- Parameters:
path
- the path for which to return the URL- Returns:
- the URL for the passed path
-
isDirectory
public boolean isDirectory(String path)
- Specified by:
isDirectory
in interfaceFileHandler
- Parameters:
path
- the path to verify- Returns:
- true if the path passed is a directory or false otherwise
-
getChildren
public String[] getChildren(String directory)
- Specified by:
getChildren
in interfaceFileHandler
- Parameters:
directory
- the directory for which to return all children- Returns:
- the children of the passed directory
-
getChildren
public String[] getChildren(String directory, List<String> filters)
- Specified by:
getChildren
in interfaceFileHandler
- Parameters:
directory
- the directory for which to return all childrenfilters
- any filters to apply when listing, can benull
Each entry in the list is an Ant file pattern.- Returns:
- the children of the passed directory
-
getAbsolutePath
public String getAbsolutePath(String path)
gets the absolute path from a file that may be relative to the current directory.- Specified by:
getAbsolutePath
in interfaceFileHandler
- Parameters:
path
- - what to extract the file path from- Returns:
- - absolute path to the file
-
readTextFile
public String readTextFile(String file, Charset encoding)
- Specified by:
readTextFile
in interfaceFileHandler
- Parameters:
file
- the file for which to load into a String object.encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.- Returns:
- a String with the file's contents.
-
writeTextFile
public void writeTextFile(String file, String content, Charset encoding)
- Specified by:
writeTextFile
in interfaceFileHandler
- Parameters:
file
- the file in which to write the String object.content
- String to write into the file.encoding
- The character encoding to use, may benull
or empty to use the platform's default encoding.
-
-