Class RegexpOnFilenameCheck
- All Implemented Interfaces:
Configurable,Contextualizable,FileSetCheck
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 mismatch 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 violation message. This allows the violation 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 violation messages.
- Since:
- 6.15
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PatternSpecify the regular expression to match the file name against.private PatternSpecify the regular expression to match the folder path against.private booleanControl whether to ignore the file extension for the file name match.private booleanControl whether to look for a match or mismatch on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringgetFileName(File file) Retrieves the file name from the givenfile.private static StringgetFolderPath(File file) Retrieves the folder path from the givenfile.private static StringgetStringOrDefault(Pattern pattern, String defaultString) Retrieves the String form of thepatternordefaultStringif null.voidinit()Initialise the instance.private booleanisMatchFile(String fileName) Checks if the givenfileNamematches the specifiedfileNamePattern.private booleanisMatchFolder(String folderPath) Checks if the givenfolderPathmatches the specifiedfolderPattern.private voidlog()Logs the violations for the check.protected voidprocessFiltered(File file, FileText fileText) Called to process a file that matches the specified file extensions.voidsetFileNamePattern(Pattern fileNamePattern) Setter to specify the regular expression to match the file name against.voidsetFolderPattern(Pattern folderPattern) Setter to specify the regular expression to match the folder path against.voidsetIgnoreFileNameExtensions(boolean ignoreFileNameExtensions) Setter to control whether to ignore the file extension for the file name match.voidsetMatch(boolean match) Setter to control whether to look for a match or mismatch on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidthMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChildMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configureMethods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
Field Details
-
MSG_MATCH
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_MISMATCH
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
folderPattern
Specify the regular expression to match the folder path against. -
fileNamePattern
Specify the regular expression to match the file name against. -
match
Control whether to look for a match or mismatch on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern. -
ignoreFileNameExtensions
Control whether to ignore the file extension for the file name match.
-
-
Constructor Details
-
RegexpOnFilenameCheck
public RegexpOnFilenameCheck()
-
-
Method Details
-
setFolderPattern
Setter to specify the regular expression to match the folder path against.- Parameters:
folderPattern- format of folder.- Since:
- 6.15
-
setFileNamePattern
Setter to specify the regular expression to match the file name against.- Parameters:
fileNamePattern- format of file.- Since:
- 6.15
-
setMatch
Setter to control whether to look for a match or mismatch on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern.- Parameters:
match- check's option for matching file names.- Since:
- 6.15
-
setIgnoreFileNameExtensions
Setter to control whether to ignore the file extension for the file name match.- Parameters:
ignoreFileNameExtensions- check's option for ignoring file extension.- Since:
- 6.15
-
init
Description copied from interface:FileSetCheckInitialise the instance. This is the time to verify that everything required to perform its job.- Specified by:
initin interfaceFileSetCheck- Overrides:
initin classAbstractFileSetCheck
-
processFiltered
Description copied from class:AbstractFileSetCheckCalled to process a file that matches the specified file extensions.- Specified by:
processFilteredin classAbstractFileSetCheck- Parameters:
file- the file to be processedfileText- the contents of the file.- Throws:
CheckstyleException- if error condition within Checkstyle occurs.
-
getFileName
Retrieves the file name from the givenfile.- Parameters:
file- Input file to examine.- Returns:
- The file name.
-
getFolderPath
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
Checks if the givenfolderPathmatches the specifiedfolderPattern.- Parameters:
folderPath- Input folder path to examine.- Returns:
- true if they do match.
-
isMatchFile
Checks if the givenfileNamematches the specifiedfileNamePattern.- Parameters:
fileName- Input file name to examine.- Returns:
- true if they do match.
-
log
Logs the violations for the check. -
getStringOrDefault
Retrieves the String form of thepatternordefaultStringif null.- Parameters:
pattern- The pattern to convert.defaultString- The result to use ifpatternis null.- Returns:
- The String form of the
pattern.
-