|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.mmm.util.component.base.AbstractComponent
net.sf.mmm.util.component.base.AbstractLoggableComponent
net.sf.mmm.util.file.base.FileUtilImpl
@Singleton @Named public class FileUtilImpl
This class is a collection of utility functions for File
handling and
manipulation.
getInstance()
Nested Class Summary | |
---|---|
protected static class |
FileUtilImpl.PathSegment
This inner class represents a segment of a glob-matching path. |
Field Summary | |
---|---|
private static String |
HOME_ROOT
The typical home directory of the user "root" under Unix/Linux. |
private static FileUtil |
instance
|
private StringUtil |
stringUtil
|
private File |
temporaryDirectory
|
private String |
temporaryDirectoryPath
|
private static String |
UNC_PATH_PREFIX
The prefix of an UNC (Uniform Naming Convention) path (e.g. |
private static Pattern |
URL_SCHEMA_PATTERN
The Pattern for an URL schema such as http:// or
ftp:// . |
private File |
userHomeDirectory
|
private String |
userHomeDirectoryPath
|
private String |
userLogin
|
Fields inherited from interface net.sf.mmm.util.file.api.FileUtil |
---|
NO_FILES, PATH_SEGMENT_CURRENT, PATH_SEGMENT_PARENT, PROPERTY_TMP_DIR, PROPERTY_USER_HOME |
Constructor Summary | |
---|---|
FileUtilImpl()
The constructor. |
Method Summary | |
---|---|
private void |
collectMatchingFiles(File cwd,
FileUtilImpl.PathSegment[] segments,
int segmentIndex,
FileType fileType,
List<File> list)
This method adds all files matching to the given path and
fileType to the list . |
boolean |
collectMatchingFiles(File cwd,
String path,
FileType fileType,
List<File> list)
This method adds all files matching the given path and
fileType to the list . |
void |
copyFile(File source,
File destination)
This method copies the file given by source to the file given
by destination . |
void |
copyFile(File source,
File destination,
boolean keepFlags)
This method copies the file given by source to the file given
by destination . |
void |
copyRecursive(File source,
File destination,
boolean allowOverwrite)
This method copies the file or directory given by source into
the given destination . |
void |
copyRecursive(File source,
File destination,
boolean allowOverwrite,
FileFilter filter)
This method copies the file or directory given by source into
the given destination . |
private void |
copyRecursive(File source,
File destination,
FileFilter filter)
This method copies the file or directory given by source into
the given destination . |
int |
deleteChildren(File directory)
This method deletes all children of the given directory recursively. |
int |
deleteRecursive(File path)
This method deletes the given path . |
protected void |
doInitialize()
This method performs the actual initialization . |
String |
getBasename(String filename)
This method gets the basename of the given filename
(path). |
String |
getDirname(String filename)
This method gets the directory-name of the given filename
(path). |
String |
getExtension(String filename)
This method extracts the extension from the given filename . |
static FileUtil |
getInstance()
This method gets the singleton instance of this FileUtilImpl . |
File[] |
getMatchingFiles(File cwd,
String path,
FileType fileType)
This method gets all files matching to the given
path and fileType . |
FileAccessPermissions |
getPermissions(File file,
FileAccessClass accessClass)
This method gets the permissions of the given
file . |
protected StringUtil |
getStringUtil()
This method gets the StringUtilImpl that is used by this
FileUtilImpl . |
File |
getTemporaryDirectory()
This method gets the File representing the
temporary directory . |
File |
getUserHomeDirectory()
This method gets the File representing the
home directory of the user . |
String |
getUserLogin()
This method gets the String . |
String |
normalizePath(String path)
This method is a shortcut for . |
String |
normalizePath(String path,
char separator)
This method normalizes a given path . |
private String |
normalizePathInternal(String path,
char slash)
This method handles normalizePath(String) internally. |
void |
setPermissions(File file,
FileAccessPermissions permissions)
This method sets the
of the given file . |
void |
setStringUtil(StringUtil stringUtil)
This method sets the StringUtil . |
void |
setTemporaryDirectoryPath(String tmpDir)
This method sets the tmp directory . |
void |
setUserHomeDirectoryPath(String userHome)
This method set the users home directory . |
void |
setUserLogin(String userLogin)
|
private boolean |
tokenizePath(String path,
List<FileUtilImpl.PathSegment> list,
PatternCompiler patternCompiler)
This method tokenized the given path by adding
FileUtilImpl.PathSegment s to the given list . |
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent |
---|
getLogger, setLogger |
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent |
---|
doInitialized, getInitializationState, initialize |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String HOME_ROOT
private static final String UNC_PATH_PREFIX
\\10.0.0.1\share
).
private static final Pattern URL_SCHEMA_PATTERN
Pattern
for an URL schema such as http://
or
ftp://
.
private static FileUtil instance
getInstance()
private StringUtil stringUtil
getStringUtil()
private String userHomeDirectoryPath
setUserHomeDirectoryPath(String)
private File userHomeDirectory
getUserHomeDirectory()
private String userLogin
getUserLogin()
private String temporaryDirectoryPath
setTemporaryDirectoryPath(String)
private File temporaryDirectory
getTemporaryDirectory()
Constructor Detail |
---|
public FileUtilImpl()
Method Detail |
---|
public static FileUtil getInstance()
FileUtilImpl
.getInstance()
methods and
construct new instances via the container-framework of your choice (like
plexus, pico, springframework, etc.). To wire up the dependent components
everything is properly annotated using common-annotations (JSR-250). If
your container does NOT support this, you should consider using a better
one.
protected void doInitialize()
initialization
. It is
called when AbstractComponent.initialize()
is invoked for the first time.super.AbstractComponent.doInitialize()
.
doInitialize
in class AbstractLoggableComponent
protected StringUtil getStringUtil()
StringUtilImpl
that is used by this
FileUtilImpl
.
StringUtilImpl
.@Inject public void setStringUtil(StringUtil stringUtil) throws AlreadyInitializedException
StringUtil
. It can only be
set once during initialization.
stringUtil
- the stringUtil to set.
AlreadyInitializedException
- if the value has already been set.public File getUserHomeDirectory()
File
representing the
home directory of the user
.
getUserHomeDirectory
in interface FileUtil
public void setUserHomeDirectoryPath(String userHome) throws AlreadyInitializedException
users home directory
.
It can only be set once during initialization.
userHome
- is the home directory of the user.
AlreadyInitializedException
- if the value has already been set.public File getTemporaryDirectory()
File
representing the
temporary directory
.
getTemporaryDirectory
in interface FileUtil
public void setTemporaryDirectoryPath(String tmpDir) throws AlreadyInitializedException
tmp directory
.
tmpDir
- the tmpDir to set
AlreadyInitializedException
- if the value has already been set.public String getUserLogin()
String
.
String
.public void setUserLogin(String userLogin)
userLogin
- is the userLogin to setpublic String normalizePath(String path)
normalizePath
(path,
File.separatorChar
)
.
normalizePath
in interface FileUtil
path
- is the path to resolve.
FileUtil.normalizePath(String, char)
public String normalizePath(String path, char separator)
path
. It will resolve ".." and
"." segments, normalize backslashes and remove duplicated slashes. Further
it can resolve "~" at the beginning of the path (like in bash-scripts,
etc.). Therefore this method resolves the path in such situations (e.g. to
"/home/login/foo") and returns a physical path.separator
is '/'
(backslashes are NOT escaped):
path |
normalizePath(path) |
---|---|
"folder/subfolder//../.\some.file" |
"folder/some.file" |
"../.\some.file" |
"../some.file" |
"http://www.host.com/foo/bar/./test/.././.." |
"http://www.host.com/foo" |
"\\unc.host\printers\pr3761" |
"\\unc.host\printers\pr3761" |
"~/documents/index.html" |
|
"~root/subfolder/../folder/.//index.html" |
"/root/folder/index.html" |
normalizePath
in interface FileUtil
path
- is the path to resolve.separator
- is the character to use as file
separator
.
private String normalizePathInternal(String path, char slash)
normalizePath(String)
internally.
path
- is the path.slash
- is the character used to to separate folders.
public String getExtension(String filename)
filename
.getExtension
("test.java")
would
return "java"
.filename
is just a dot followed by the extension (e.g.
".java"
), the empty string is returned.
getExtension
in interface FileUtil
filename
- is the filename and may include an absolute or relative
path.
filename
excluding the dot
in lowercase
or the empty string if
NOT present.public String getBasename(String filename)
filename
(path). The basename is the raw name of the file without the
path
.filename |
|
---|---|
/ | / |
\/\ | \ |
/. | . |
/foo.bar | foo.bar |
/foo/bar/ | bar |
c:\\ | |
c:\\foo | foo |
http://foo.org/bar | bar |
getBasename
in interface FileUtil
filename
- is the path to a file or directory.
filename
.public String getDirname(String filename)
filename
(path).filename | FileUtil.getDirname(String) |
---|---|
foo | . |
/foo | / |
/foo/bar | /foo |
/foo/bar/ | /foo |
./foo/bar/ | ./foo |
./foo/bar/../ | ./foo/bar |
getDirname
in interface FileUtil
filename
- is the path to a file or directory.
filename
.FileUtil.normalizePath(String)
public void copyFile(File source, File destination)
source
to the file given
by destination
.
copyFile
in interface FileUtil
source
- is the existing file to copy from.destination
- is the file to copy to. It will be created if it does
NOT exist and overridden otherwise.public void copyFile(File source, File destination, boolean keepFlags)
source
to the file given
by destination
.
copyFile
in interface FileUtil
source
- is the existing file to copy from.destination
- is the file to copy to. It will be created if it does
NOT exist and overridden otherwise.keepFlags
- - true
if the flags of the file should be
copied as well, false
otherwise (a new file is created
with default flags and only the content is copied).public void copyRecursive(File source, File destination, boolean allowOverwrite)
source
into
the given destination
.source
a new
name
, the destination
has to point to
the final place where the copy should appear rather than the directory
where the copy will be located in.File
source = newFile
("/usr/local/foo");File
destination = newFile
("/tmp", source.getName()); // file: "/tmp/foo"fileUtil
.copyRecursive
(source, destination, true);
copyRecursive
in interface FileUtil
source
- is the file or directory to copy.destination
- is the final place where the copy should appear.allowOverwrite
- - if false
and the
destination
already exists, a
RuntimeIoException
is thrown, else if true
the
destination
will be overwritten.public void copyRecursive(File source, File destination, boolean allowOverwrite, FileFilter filter)
source
into
the given destination
.source
a new
name
, the destination
has to point to
the final place where the copy should appear rather than the directory
where the copy will be located in.
copyRecursive
in interface FileUtil
source
- is the file or directory to copy.destination
- is the final place where the copy should appear.allowOverwrite
- - if false
and the
destination
already exists, a
RuntimeIoException
is thrown, else if true
the
destination
will be overwritten.filter
- is a FileFilter
that decides
which files should be copied. Only
accepted
files and directories are
copied, others will be ignored.FileUtil.copyRecursive(File, File, boolean)
private void copyRecursive(File source, File destination, FileFilter filter)
source
into
the given destination
.source
a new
name
, the destination
has to point to
the final place where the copy should appear rather than the directory
where the copy will be located in.File
source = newFile
("/usr/local/foo");File
destination = newFile
("/tmp", source.getName()); // file: "/tmp/foo"FileUtilImpl
.copyRecursive(source, destination, true);
source
- is the file or directory to copy.destination
- is the final place where the copy should appear.filter
- is a FileFilter
that decides
which files should be copied. Only
accepted
files and directories are
copied, others will be ignored.public FileAccessPermissions getPermissions(File file, FileAccessClass accessClass)
permissions
of the given
file
.
getPermissions
in interface FileUtil
file
- is the file for which the permissions are requested.accessClass
- is the distinct class
the
permission should be applied to in the returned permissions. It may
be null
to apply the permissions to all distinct
classes.
file
.public void setPermissions(File file, FileAccessPermissions permissions)
permissions
of the given file
.FileAccessClass.OTHERS
and FileAccessClass.USER
so FileAccessClass.GROUP
flags are ignored as well as the global
s-bits (sticky
,
setgid
and
setuid
).
setPermissions
in interface FileUtil
file
- is the file to modify.permissions
- are the permissions to set.public int deleteRecursive(File path)
deletes
the given path
. If
the path
denotes a directory
then
it will be deleted recursively.
deleteRecursive
in interface FileUtil
path
- is the path to delete.
FileUtil.deleteChildren(File)
public int deleteChildren(File directory)
deletes
all children
of the given directory
recursively. If the given
directory
denotes an existing
directory
then it will be empty after the call
of this method, else this method will have no effect.
deleteChildren
in interface FileUtil
directory
- is the directory to delete.
public File[] getMatchingFiles(File cwd, String path, FileType fileType)
files
matching to the given
path
and fileType
. The path
may
contain wildcards
.getMatchingFiles
(cwd,
"*", FileType.DIRECTORY
)
will return all
directories
in cwd
getMatchingFiles
(cwd,
"*/*.xml", FileType.FILE
)
will return all
files
from all direct sub-folders
of cwd
that end with ".xml"getMatchingFiles
(cwd,
"**/*.xml", FileType.FILE
)
will return all
files
in cwd
or any of its transitive
sub-folders
that end with ".xml"
getMatchingFiles
in interface FileUtil
cwd
- is the current working directory and should therefore point to
an existing directory
. If the given
path
is NOT absolute
it is
interpreted relative to this directory.path
- is the path the requested files must match. If this path is NOT
absolute
it is interpreted relative to the
directory
given by cwd
.fileType
- is the type of the requested files or null
if
files of any type are acceptable.
files
that match the given
path
and apply to ignore
FileUtil.collectMatchingFiles(File, String, FileType, List)
public boolean collectMatchingFiles(File cwd, String path, FileType fileType, List<File> list)
path
and
fileType
to the list
. The path
may
contain wildcards
.
collectMatchingFiles
in interface FileUtil
cwd
- is the current working directory and should therefore point to
an existing directory
. If the given
path
is NOT absolute
it is
interpreted relative to this directory.path
- is the path the files to collect must match. If this path is
NOT absolute
it is interpreted relative to
the directory
given by cwd
.fileType
- is the type of the files to collect or null
if
files of any type are acceptable.list
- is the list where to add
the collected
files.
false
if the path is a regular string and
true
if the given path contains at least one
wildcard
(
'*'
or '?'
).private void collectMatchingFiles(File cwd, FileUtilImpl.PathSegment[] segments, int segmentIndex, FileType fileType, List<File> list)
path
and
fileType
to the list
. The path
may
contain wildcards
.
cwd
- is the current working directory and should therefore point to
an existing directory
. If the given
path
is NOT absolute
it is
interpreted relative to this directory.segments
- is the path the files to collect must match. If this path
is NOT absolute
it is interpreted relative
to the directory
given by
cwd
.segmentIndex
- is the current index in pathChars
for the
collection process.fileType
- is the type of the files to collect or null
if
files of any type are acceptable.list
- is the list where to add
the collected
files.private boolean tokenizePath(String path, List<FileUtilImpl.PathSegment> list, PatternCompiler patternCompiler)
path
by adding
FileUtilImpl.PathSegment
s to the given list
.
path
- is the path to tokenizedlist
- is the list where to add the segment tokens.patternCompiler
- is the PatternCompiler
used to compile the
individual segments
of the given
path
.
true
if the path is a glob-pattern (contains '*' or
'?'), false
otherwise.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |