Class RegexpOnFilenameCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
public class RegexpOnFilenameCheck extends AbstractFileSetCheck
Implementation of a check that looks for a file name and/or path match (or mis-match) against specified patterns. It can also be used to verify files match specific naming patterns not covered by other checks (Ex: properties, xml, etc.).
When customizing the check, the properties are applied in a specific order. The fileExtensions property first picks only files that match any of the specific extensions supplied. Once files are matched against the fileExtensions, the match property is then used in conjunction with the patterns to determine if the check is looking for a match or mis-match on those files. If the fileNamePattern is supplied, the matching is only applied to the fileNamePattern and not the folderPattern. If no fileNamePattern is supplied, then matching is applied to the folderPattern only and will result in all files in a folder to be reported on violations. If no folderPattern is supplied, then all folders that checkstyle finds are examined for violations. The ignoreFileNameExtensions property drops the file extension and applies the fileNamePattern only to the rest of file name. For example, if the file is named 'test.java' and this property is turned on, the pattern is only applied to 'test'.
If this check is configured with no properties, then the default behavior of this check is to report file names with spaces in them. When at least one pattern property is supplied, the entire check is under the user's control to allow them to fully customize the behavior.
It is recommended that if you create your own pattern, to also specify a custom error message. This allows the error message printed to be clear what the violation is, especially if multiple RegexpOnFilename checks are used. Argument 0 for the message populates the check's folderPattern. Argument 1 for the message populates the check's fileNamePattern. The file name is not passed as an argument since it is part of CheckStyle's default error messages.
Check have following options:
- folderPattern - Regular expression to match the folder path against. Default value is null.
- fileNamePattern - Regular expression to match the file name against. Default value is null.
- match - Whether to look for a match or mis-match on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern. Default value is true.
- ignoreFileNameExtensions - Whether to ignore the file extension for the file name match. Default value is false.
- fileExtensions - File type extension of files to process. If this is specified, then only files that match these types are examined with the other patterns. Default value is {}.
To configure the check to report file names that contain a space:
<module name="RegexpOnFilename"/>
To configure the check to force picture files to not be 'gif':
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="\\.gif$"/> </module>
OR:
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="."/> <property name="fileExtensions" value="gif"/> </module>
To configure the check to only allow property and xml files to be located in the resource folder:
<module name="RegexpOnFilename"> <property name="folderPattern" value="[\\/]src[\\/]\\w+[\\/]resources[\\/]"/> <property name="match" value="false"/> <property name="fileExtensions" value="properties, xml"/> </module>
To configure the check to only allow Java and XML files in your folders use the below.
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="\\.(java|xml)$"/> <property name="match" value="false"/> </module>
To configure the check to only allow Java and XML files only in your source folder and ignore any other folders:
Note: 'folderPattern' must be specified if checkstyle is analyzing more than the normal source folder, like the 'bin' folder where class files can be located.
<module name="RegexpOnFilename"> <property name="folderPattern" value="[\\/]src[\\/]"/> <property name="fileNamePattern" value="\\.(java|xml)$"/> <property name="match" value="false"/> </module>
To configure the check to only allow file names to be camel case:
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="^([A-Z][a-z0-9]+\.?)+$"/> <property name="match" value="false"/> <property name="ignoreFileNameExtensions" value="true"/> </module>
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.regex.Pattern
fileNamePattern
Compiled regexp to match a file.private java.util.regex.Pattern
folderPattern
Compiled regexp to match a folder.private boolean
ignoreFileNameExtensions
Whether to ignore the file's extension when looking for matches.private boolean
match
Whether to look for a file name match or mismatch.static java.lang.String
MSG_MATCH
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_MISMATCH
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description RegexpOnFilenameCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
getFileName(java.io.File file)
Retrieves the file name from the givenfile
.private static java.lang.String
getFolderPath(java.io.File file)
Retrieves the folder path from the givenfile
.private static java.lang.String
getStringOrDefault(java.util.regex.Pattern pattern, java.lang.String defaultString)
Retrieves the String form of thepattern
ordefaultString
if null.void
init()
Initialise the instance.private boolean
isMatchFile(java.lang.String fileName)
Checks if the givenfileName
matches the specifiedfileNamePattern
.private boolean
isMatchFolder(java.lang.String folderPath)
Checks if the givenfolderPath
matches the specifiedfolderPattern
.private void
log()
Logs the errors for the check.protected void
processFiltered(java.io.File file, java.util.List<java.lang.String> lines)
Called to process a file that matches the specified file extensions.void
setFileNamePattern(java.util.regex.Pattern fileNamePattern)
Setter for file name format.void
setFolderPattern(java.util.regex.Pattern folderPattern)
Setter for folder format.void
setIgnoreFileNameExtensions(boolean ignoreFileNameExtensions)
Sets whether file name matching should drop the file extension or not.void
setMatch(boolean match)
Sets whether the check should look for a file name match or mismatch.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
beginProcessing, destroy, finishProcessing, fireErrors, getFileExtensions, getMessageCollector, getMessageDispatcher, log, log, process, setFileExtensions, setMessageDispatcher
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Field Detail
-
MSG_MATCH
public static final java.lang.String MSG_MATCH
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_MISMATCH
public static final java.lang.String MSG_MISMATCH
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
folderPattern
private java.util.regex.Pattern folderPattern
Compiled regexp to match a folder.
-
fileNamePattern
private java.util.regex.Pattern fileNamePattern
Compiled regexp to match a file.
-
match
private boolean match
Whether to look for a file name match or mismatch.
-
ignoreFileNameExtensions
private boolean ignoreFileNameExtensions
Whether to ignore the file's extension when looking for matches.
-
-
Method Detail
-
setFolderPattern
public void setFolderPattern(java.util.regex.Pattern folderPattern)
Setter for folder format.- Parameters:
folderPattern
- format of folder.
-
setFileNamePattern
public void setFileNamePattern(java.util.regex.Pattern fileNamePattern)
Setter for file name format.- Parameters:
fileNamePattern
- format of file.
-
setMatch
public void setMatch(boolean match)
Sets whether the check should look for a file name match or mismatch.- Parameters:
match
- check's option for matching file names.
-
setIgnoreFileNameExtensions
public void setIgnoreFileNameExtensions(boolean ignoreFileNameExtensions)
Sets whether file name matching should drop the file extension or not.- Parameters:
ignoreFileNameExtensions
- check's option for ignoring file extension.
-
init
public void init()
Description copied from interface:FileSetCheck
Initialise the instance. This is the time to verify that everything required to perform it job.- Specified by:
init
in interfaceFileSetCheck
- Overrides:
init
in classAbstractFileSetCheck
-
processFiltered
protected void processFiltered(java.io.File file, java.util.List<java.lang.String> lines) throws CheckstyleException
Description copied from class:AbstractFileSetCheck
Called to process a file that matches the specified file extensions.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedlines
- an immutable list of the contents of the file.- Throws:
CheckstyleException
- if error condition within Checkstyle occurs.
-
getFileName
private java.lang.String getFileName(java.io.File file)
Retrieves the file name from the givenfile
.- Parameters:
file
- Input file to examine.- Returns:
- The file name.
-
getFolderPath
private static java.lang.String getFolderPath(java.io.File file) throws CheckstyleException
Retrieves the folder path from the givenfile
.- Parameters:
file
- Input file to examine.- Returns:
- The folder path.
- Throws:
CheckstyleException
- if there is an error getting the canonical path of thefile
.
-
isMatchFolder
private boolean isMatchFolder(java.lang.String folderPath)
Checks if the givenfolderPath
matches the specifiedfolderPattern
.- Parameters:
folderPath
- Input folder path to examine.- Returns:
- true if they do match.
-
isMatchFile
private boolean isMatchFile(java.lang.String fileName)
Checks if the givenfileName
matches the specifiedfileNamePattern
.- Parameters:
fileName
- Input file name to examine.- Returns:
- true if they do match.
-
log
private void log()
Logs the errors for the check.
-
getStringOrDefault
private static java.lang.String getStringOrDefault(java.util.regex.Pattern pattern, java.lang.String defaultString)
Retrieves the String form of thepattern
ordefaultString
if null.- Parameters:
pattern
- The pattern to convert.defaultString
- The result to use ifpattern
is null.- Returns:
- The String form of the
pattern
.
-
-