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 intFILE_BUFFER_SIZEDefault 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 Stringappend(String path, String suffixToAppend)Append a suffix path to an existing path.voidcopy(InputStream in, OutputStream out)Copies data from an InputStream to an OutputStream using a default buffer size.voidcopy(InputStream in, OutputStream out, int bufSize)Copies data from an InputStream to an OutputStream.voidcopyDirectory(String source, String target)Copy a directory from a source to a destination.voidcopyDirectory(String source, String target, List<String> excludes)Copy a directory from a source to a destination specifying files to be excluded.voidcopyDirectory(String source, String target, Map<String,String> replacements, Charset encoding)Copy a directory from a source to a destination using a token replacement.voidcopyFile(String source, String target)Copy a file from a source to a destination.voidcopyFile(String source, String target, boolean overwrite)Copy a file from a source to a destination specifying if source files may overwrite newer destination files.voidcopyFile(String source, String target, Map<String,String> replacements, Charset encoding)Copy a file from a source to a destination using token replacement.StringcreateDirectory(String parentDir, String name)Convenience method for creating a new directory inside another one.voidcreateFile(String file)Create a file.StringcreateUniqueTmpDirectory()Creates a unique temporary directory.voiddelete(String path)Deletes a file or directory, removing any children as appropriate.booleanexists(String path)voidexplode(String war, String exploded)Extracts a war file into a directory.StringgetAbsolutePath(String path)gets the absolute path from a file that may be relative to the current directory.String[]getChildren(String directory, String... filters)InputStreamgetInputStream(String file)StringgetName(String file)OutputStreamgetOutputStream(String file)StringgetParent(String path)longgetSize(String file)StringgetTmpPath(String name)Compute the location of a temporary directory.StringgetURL(String path)booleanisDirectory(String path)booleanisDirectoryEmpty(String dir)voidmkdirs(String path)Create directories for the passed path.StringreadTextFile(String file, Charset encoding)voidreplaceInFile(String file, Map<String,String> replacements, Charset encoding)Replaces using a map of replacements in a given file.voidreplaceInFile(String file, Map<String,String> replacements, Charset encoding, boolean ignoreNonExistingProperties)Replaces using a map of replacements in a given file.voidreplaceInXmlFile(XmlReplacement... xmlReplacements)Replaces using a map of XML replacements in a given file.static StringsanitizeFilename(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.voidwriteTextFile(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:
copyFilein 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:
copyFilein 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:
copyFilein interfaceFileHandler- Parameters:
source- the file to copy from. Must not benulltarget- the file to copy to. Must not benullreplacements- the token replacements to use. Must not benullencoding- The character encoding to use, may benullor 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:
copyDirectoryin 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:
copyDirectoryin 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:
copyDirectoryin interfaceFileHandler- Parameters:
source- the directory to copy from. Must not benulltarget- the directory to copy to. Must not benullreplacements- the token replacements to use. Must not benullencoding- The character encoding to use, may benullor 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:
explodein 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:
createDirectoryin 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:
copyin 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:
copyin 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:
replaceInFilein interfaceFileHandler- Parameters:
file- File to replace in.replacements- Map containing replacements.encoding- The character encoding to use, may benullor 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:
replaceInFilein interfaceFileHandler- Parameters:
file- File to replace in.replacements- Map containing replacements.encoding- The character encoding to use, may benullor 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:
replaceInXmlFilein 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:
getTmpPathin 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:
createUniqueTmpDirectoryin 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:
deletein interfaceFileHandler- Parameters:
path- the path to the file or directory to remove
-
getSize
public long getSize(String file)
- Specified by:
getSizein 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:
getInputStreamin 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:
getOutputStreamin 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:
appendin 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:
mkdirsin interfaceFileHandler- Parameters:
path- the path for which to create all directories leading to it
-
getParent
public String getParent(String path)
- Specified by:
getParentin 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:
existsin 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:
createFilein interfaceFileHandler- Parameters:
file- the file to create
-
isDirectoryEmpty
public boolean isDirectoryEmpty(String dir)
- Specified by:
isDirectoryEmptyin interfaceFileHandler- Parameters:
dir- the directory to check- Returns:
- true if the passed directory is empty
-
getName
public String getName(String file)
- Specified by:
getNamein 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:
getURLin 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:
isDirectoryin 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, String... filters)
- Specified by:
getChildrenin interfaceFileHandler- Parameters:
directory- the directory for which to return all childrenfilters- any filters to apply when listing. 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:
getAbsolutePathin 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:
readTextFilein interfaceFileHandler- Parameters:
file- the file for which to load into a String object.encoding- The character encoding to use, may benullor 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:
writeTextFilein 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 benullor empty to use the platform's default encoding.
-
-