public class FileMatcher
extends java.lang.Object
Pattern
that is applied when any of the methods
apply()
, matches()
or
preview()
is called.Details of the behavior is controlled through the attributes doBackup, backUpDirectory, baseDirectory and characterSet. See the respective setter methods for details.
Constructor and Description |
---|
FileMatcher(java.util.regex.Pattern regex)
The class must be instantiated with a regular expression
Pattern . |
Modifier and Type | Method and Description |
---|---|
int |
apply(java.io.File file,
ReplacementString replacement)
Applies the regex pattern and the specified replacement string to the file.
|
int |
countMatches(java.io.File file)
Returns the number of times the reges pattern can be found in the
specified file.
|
java.io.File |
getBackUpDirectory()
Returns the backup directory.
|
java.io.File |
getBackupFileName(java.io.File originalFile)
Returns the abstract path name of the backup file for the
specified "original" file, given the current base directory
and current backup directory.
|
java.io.File |
getBaseDirectory()
Returns the base directory for backup.
|
java.nio.charset.Charset |
getCharacterSet()
Returns the character set used to read and write files.
|
boolean |
isDoBackup()
Returns the doBackup flag.
|
boolean |
matches(java.io.File file)
Returns true if the regex pattern applies to some substring in the specified file.
|
java.lang.String |
preview(java.io.File file,
ReplacementString replacement)
Applies the regex pattern and the specified replacement string to the file and returns
the result as string.
|
void |
setBackUpDirectory(java.io.File backUpDirectory)
Sets the backup directory.
|
void |
setBaseDirectory(java.io.File baseDirectory)
Sets the base directory.
|
void |
setCharacterSet(java.nio.charset.Charset charset)
Sets the character set used to read and write files.
|
void |
setDoBackup(boolean doBackup)
Sets the doBackup flag.
|
public FileMatcher(java.util.regex.Pattern regex)
Pattern
.regex
- to be applied to filespublic boolean matches(java.io.File file) throws java.io.IOException
file
- to be testedjava.io.IOException
- if the file can not be found or readpublic int countMatches(java.io.File file) throws java.io.IOException
file
- to be testedjava.io.IOException
- if the file can not be found or readpublic java.lang.String preview(java.io.File file, ReplacementString replacement) throws java.io.IOException
apply()
method on
some particula file. Returns the string the file would contain if apply()
was called.file
- subject of replacement but not alteredreplacement
- to be applied in conjunction with this instances regex patternjava.io.IOException
- if the file can not be found or readpublic int apply(java.io.File file, ReplacementString replacement) throws java.io.IOException
file
- subject of replacementreplacement
- to be applied in conjunction with this instances reges patternjava.io.IOException
- if the file can not be found, read, written or backed up.java.lang.IndexOutOfBoundsException
- if an illegal group indicator was detectedpublic java.io.File getBackupFileName(java.io.File originalFile)
originalFile
- to be backed uppublic java.io.File getBackUpDirectory()
setBackUpDirectory(File)
public void setBackUpDirectory(java.io.File backUpDirectory)
doBackup
flag is set to true, files will
be saved under this directory. The default for this attribute is defined by the java system
property "java.io.tmpdir".backUpDirectory
- backup directory.public boolean isDoBackup()
setDoBackup(boolean)
public void setDoBackup(boolean doBackup)
backup directory
before any replacement is applied.doBackup
- flag should files be backed up?public java.nio.charset.Charset getCharacterSet()
setCharacterSet(Charset)
public void setCharacterSet(java.nio.charset.Charset charset)
charset
- character setpublic java.io.File getBaseDirectory()
setBaseDirectory(File)
public void setBaseDirectory(java.io.File baseDirectory)
baseDirectory
- base directory