Class RegexpOnFilenameCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable, FileSetCheck

    public class RegexpOnFilenameCheck
    extends AbstractFileSetCheck

    Checks that a specified pattern matches based on file and/or folder path. 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 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.

    • Property folderPattern - Specify the regular expression to match the folder path against. Type is java.util.regex.Pattern. Default value is null.
    • Property fileNamePattern - Specify the regular expression to match the file name against. Type is java.util.regex.Pattern. Default value is null.
    • Property match - Control 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. Type is boolean. Default value is true.
    • Property ignoreFileNameExtensions - Control whether to ignore the file extension for the file name match. Type is boolean. Default value is false.
    • Property fileExtensions - Specify the file type extension of files to process. If this is specified, then only files that match these types are examined with the other patterns. Type is java.lang.String[]. Default value is "".

    To configure the check to report file names that contain a space:

     <module name="RegexpOnFilename"/>
     

    Example:

     src/xdocs/config_regexp.xml  //OK, contains no whitespace
     src/xdocs/"config regexp".xml  //violation, contains whitespace
     

    To configure the check to forbid 'gif' files in folders:

     <module name="RegexpOnFilename">
       <property name="fileNamePattern" value="\.gif$"/>
     </module>
     

    Example:

     src/site/resources/images/favicon.png  //OK
     src/site/resources/images/logo.jpg  //OK
     src/site/resources/images/groups.gif  //violation, .gif images not allowed
     

    To configure the check to forbid 'md' files except 'README.md file' in folders, with custom message:

     <module name="RegexpOnFilename">
       <property name="fileNamePattern" value="README"/>
       <property name="fileExtensions" value="md"/>
       <property name="match" value="false"/>
       <message key="regexp.filename.mismatch"
         value="No '*.md' files other then 'README.md'"/>
     </module>
     

    Example:

     src/site/resources/README.md  //OK
     src/site/resources/Logo.png  //OK
     src/site/resources/Text.md  //violation, .md files other than 'README.md' are not allowed
     

    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>
     

    Example:

     src/main/resources/sun_checks.xml  //OK
     src/main/resources/check_properties.properties  //OK
     src/main/resources/JavaClass.java  //violation, xml|property files are allowed in resource folder
     

    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>
     

    Example:

     src/main/java/JavaClass.java  //OK
     src/main/MainClass.java  //OK
     src/main/java/java_xml.xml  //OK
     src/main/main_xml.xml  //OK
     src/main/java/image.png  //violation, folders should only contain java or xml files
     src/main/check_properties.properties  //violation, folders should only contain java or xml files
     

    To configure the check to only allow Java and XML files only in your source folder and ignore any other folders:

     <module name="RegexpOnFilename">
       <property name="folderPattern" value="[\\/]src[\\/]"/>
       <property name="fileNamePattern" value="\.(java|xml)$"/>
       <property name="match" value="false"/>
     </module>
     

    Example:

     src/SourceClass.java  //OK
     src/source_xml.xml  //OK
     src/image.png  //violation, only java and xml files are allowed in src folder
     src/main/main_properties.properties  //OK, this check only applies to src folder
     

    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.

    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>
     

    Example:

     src/main/java/JavaClass.java  //OK
     src/main/MainClass.java  //OK
     src/main/java/java_class.java  //violation, file names should be in Camel Case
     src/main/main_class.java  //violation, file names should be in Camel Case
     

    Parent is com.puppycrawl.tools.checkstyle.Checker

    Violation Message Keys:

    • regexp.filename.match
    • regexp.filename.mismatch
    Since:
    6.15
    • Field Detail

      • folderPattern

        private Pattern folderPattern
        Specify the regular expression to match the folder path against.
      • fileNamePattern

        private Pattern fileNamePattern
        Specify the regular expression to match the file name against.
      • match

        private boolean match
        Control 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.
      • ignoreFileNameExtensions

        private boolean ignoreFileNameExtensions
        Control whether to ignore the file extension for the file name match.
    • Method Detail

      • setFolderPattern

        public void setFolderPattern​(Pattern folderPattern)
        Setter to specify the regular expression to match the folder path against.
        Parameters:
        folderPattern - format of folder.
      • setFileNamePattern

        public void setFileNamePattern​(Pattern fileNamePattern)
        Setter to specify the regular expression to match the file name against.
        Parameters:
        fileNamePattern - format of file.
      • setMatch

        public void setMatch​(boolean match)
        Setter to control 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.
        Parameters:
        match - check's option for matching file names.
      • setIgnoreFileNameExtensions

        public void setIgnoreFileNameExtensions​(boolean ignoreFileNameExtensions)
        Setter to control whether to ignore the file extension for the file name match.
        Parameters:
        ignoreFileNameExtensions - check's option for ignoring file extension.
      • getFileName

        private String getFileName​(File file)
        Retrieves the file name from the given file.
        Parameters:
        file - Input file to examine.
        Returns:
        The file name.
      • isMatchFolder

        private boolean isMatchFolder​(String folderPath)
        Checks if the given folderPath matches the specified folderPattern.
        Parameters:
        folderPath - Input folder path to examine.
        Returns:
        true if they do match.
      • isMatchFile

        private boolean isMatchFile​(String fileName)
        Checks if the given fileName matches the specified fileNamePattern.
        Parameters:
        fileName - Input file name to examine.
        Returns:
        true if they do match.
      • log

        private void log()
        Logs the violations for the check.
      • getStringOrDefault

        private static String getStringOrDefault​(Pattern pattern,
                                                 String defaultString)
        Retrieves the String form of the pattern or defaultString if null.
        Parameters:
        pattern - The pattern to convert.
        defaultString - The result to use if pattern is null.
        Returns:
        The String form of the pattern.