001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.regexp;
021
022import java.io.File;
023import java.io.IOException;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.StatelessCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
028import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
029import com.puppycrawl.tools.checkstyle.api.FileText;
030import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
031
032/**
033 * <p>
034 * Checks that a specified pattern matches based on file and/or folder path.
035 * It can also be used to verify files
036 * match specific naming patterns not covered by other checks (Ex: properties,
037 * xml, etc.).
038 * </p>
039 *
040 * <p>
041 * When customizing the check, the properties are applied in a specific order.
042 * The fileExtensions property first picks only files that match any of the
043 * specific extensions supplied. Once files are matched against the
044 * fileExtensions, the match property is then used in conjunction with the
045 * patterns to determine if the check is looking for a match or mis-match on
046 * those files. If the fileNamePattern is supplied, the matching is only applied
047 * to the fileNamePattern and not the folderPattern. If no fileNamePattern is
048 * supplied, then matching is applied to the folderPattern only and will result
049 * in all files in a folder to be reported on violations. If no folderPattern is
050 * supplied, then all folders that checkstyle finds are examined for violations.
051 * The ignoreFileNameExtensions property drops the file extension and applies
052 * the fileNamePattern only to the rest of file name. For example, if the file
053 * is named 'test.java' and this property is turned on, the pattern is only
054 * applied to 'test'.
055 * </p>
056 *
057 * <p>
058 * If this check is configured with no properties, then the default behavior of
059 * this check is to report file names with spaces in them. When at least one
060 * pattern property is supplied, the entire check is under the user's control to
061 * allow them to fully customize the behavior.
062 * </p>
063 *
064 * <p>
065 * It is recommended that if you create your own pattern, to also specify a
066 * custom violation message. This allows the violation message printed to be clear what
067 * the violation is, especially if multiple RegexpOnFilename checks are used.
068 * Argument 0 for the message populates the check's folderPattern. Argument 1
069 * for the message populates the check's fileNamePattern. The file name is not
070 * passed as an argument since it is part of CheckStyle's default violation
071 * messages.
072 * </p>
073 * <ul>
074 * <li>
075 * Property {@code folderPattern} - Specify the regular expression to match the folder path against.
076 * Default value is {@code null}.</li>
077 *
078 * <li>
079 * Property {@code fileNamePattern} - Specify the regular expression to match the file name against.
080 * Default value is {@code null}.</li>
081 *
082 * <li>
083 * Property {@code match} - Control whether to look for a match or mis-match on the file name, if
084 * the fileNamePattern is supplied, otherwise it is applied on the folderPattern.
085 * Default value is {@code true}.</li>
086 *
087 * <li>
088 * Property {@code ignoreFileNameExtensions} - Control whether to ignore the file extension for
089 * the file name match. Default value is {@code false}.</li>
090 *
091 * <li>
092 * Property {@code fileExtensions} - Specify the file type extension of files to process. If this is
093 * specified, then only files that match these types are examined with the other
094 * patterns. Default value is {@code all files}.</li>
095 * </ul>
096 *
097 * <p>
098 * To configure the check to report file names that contain a space:
099 * </p>
100 *
101 * <pre>
102 * &lt;module name=&quot;RegexpOnFilename&quot;/&gt;
103 * </pre>
104 *
105 * <p>Example:</p>
106 * <pre>
107 * src/xdocs/config_regexp.xml  //OK, contains no whitespace
108 * src/xdocs/&quot;config regexp&quot;.xml  //violation, contains whitespace
109 * </pre>
110 *
111 * <p>
112 * To configure the check to forbid 'gif' files in folders:
113 * </p>
114 *
115 * <pre>
116 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
117 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\.gif$&quot;/&gt;
118 * &lt;/module&gt;
119 * </pre>
120 *
121 * <p>Example:</p>
122 * <pre>
123 * src/site/resources/images/favicon.png  //OK
124 * src/site/resources/images/logo.jpg  //OK
125 * src/site/resources/images/groups.gif  //violation, .gif images not allowed
126 * </pre>
127 *
128 * <p>
129 * To configure the check to forbid 'md' files except 'README.md file' in folders,
130 * with custom message:
131 * </p>
132 *
133 * <pre>
134 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
135 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;README&quot;/&gt;
136 *   &lt;property name=&quot;fileExtensions&quot; value=&quot;md&quot;/&gt;
137 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
138 *   &lt;message key=&quot;regexp.filename.mismatch&quot;
139 *     value=&quot;No '*.md' files other then 'README.md'&quot;/&gt;
140 * &lt;/module&gt;
141 * </pre>
142 *
143 * <p>Example:</p>
144 * <pre>
145 * src/site/resources/README.md  //OK
146 * src/site/resources/Logo.png  //OK
147 * src/site/resources/Text.md  //violation, .md files other than 'README.md' are not allowed
148 * </pre>
149 *
150 * <p>
151 * To configure the check to only allow property and xml files to be located in
152 * the resource folder:
153 * </p>
154 *
155 * <pre>
156 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
157 *   &lt;property name=&quot;folderPattern&quot;
158 *     value=&quot;[\\/]src[\\/]\w+[\\/]resources[\\/]&quot;/&gt;
159 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
160 *   &lt;property name=&quot;fileExtensions&quot; value=&quot;properties, xml&quot;/&gt;
161 * &lt;/module&gt;
162 * </pre>
163 *
164 * <p>Example:</p>
165 * <pre>
166 * src/main/resources/sun_checks.xml  //OK
167 * src/main/resources/check_properties.properties  //OK
168 * src/main/resources/JavaClass.java  //violation, xml|property files are allowed in resource folder
169 * </pre>
170 *
171 * <p>
172 * To configure the check to only allow Java and XML files in your folders use
173 * the below.
174 * </p>
175 *
176 * <pre>
177 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
178 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\.(java|xml)$&quot;/&gt;
179 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
180 * &lt;/module&gt;
181 * </pre>
182 *
183 * <p>Example:</p>
184 * <pre>
185 * src/main/java/JavaClass.java  //OK
186 * src/main/MainClass.java  //OK
187 * src/main/java/java_xml.xml  //OK
188 * src/main/main_xml.xml  //OK
189 * src/main/java/image.png  //violation, folders should only contain java or xml files
190 * src/main/check_properties.properties  //violation, folders should only contain java or xml files
191 * </pre>
192 *
193 * <p>
194 * To configure the check to only allow Java and XML files only in your source
195 * folder and ignore any other folders:
196 * </p>
197 *
198 * <pre>
199 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
200 *   &lt;property name=&quot;folderPattern&quot; value=&quot;[\\/]src[\\/]&quot;/&gt;
201 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\.(java|xml)$&quot;/&gt;
202 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
203 * &lt;/module&gt;
204 * </pre>
205 *
206 * <p>Example:</p>
207 * <pre>
208 * src/SourceClass.java  //OK
209 * src/source_xml.xml  //OK
210 * src/image.png  //violation, only java and xml files are allowed in src folder
211 * src/main/main_properties.properties  //OK, this check only applies to src folder
212 * </pre>
213 *
214 * <p>
215 * <b>Note:</b> 'folderPattern' must be specified if checkstyle is analyzing
216 * more than the normal source folder, like the 'bin' folder where class files
217 * can be located.
218 * </p>
219 *
220 * <p>
221 * To configure the check to only allow file names to be camel case:
222 * </p>
223 *
224 * <pre>
225 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
226 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;^([A-Z][a-z0-9]+\.?)+$&quot;/&gt;
227 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
228 *   &lt;property name=&quot;ignoreFileNameExtensions&quot; value=&quot;true&quot;/&gt;
229 * &lt;/module&gt;
230 * </pre>
231 *
232 * <p>Example:</p>
233 * <pre>
234 * src/main/java/JavaClass.java  //OK
235 * src/main/MainClass.java  //OK
236 * src/main/java/java_class.java  //violation, file names should be in Camel Case
237 * src/main/main_class.java  //violation, file names should be in Camel Case
238 * </pre>
239 *
240 * @since 6.15
241 */
242@StatelessCheck
243public class RegexpOnFilenameCheck extends AbstractFileSetCheck {
244
245    /**
246     * A key is pointing to the warning message text in "messages.properties"
247     * file.
248     */
249    public static final String MSG_MATCH = "regexp.filename.match";
250    /**
251     * A key is pointing to the warning message text in "messages.properties"
252     * file.
253     */
254    public static final String MSG_MISMATCH = "regexp.filename.mismatch";
255
256    /** Specify the regular expression to match the folder path against. */
257    private Pattern folderPattern;
258    /** Specify the regular expression to match the file name against. */
259    private Pattern fileNamePattern;
260    /**
261     * Control whether to look for a match or mis-match on the file name,
262     * if the fileNamePattern is supplied, otherwise it is applied on the folderPattern.
263     */
264    private boolean match = true;
265    /** Control whether to ignore the file extension for the file name match. */
266    private boolean ignoreFileNameExtensions;
267
268    /**
269     * Setter to specify the regular expression to match the folder path against.
270     *
271     * @param folderPattern format of folder.
272     */
273    public void setFolderPattern(Pattern folderPattern) {
274        this.folderPattern = folderPattern;
275    }
276
277    /**
278     * Setter to specify the regular expression to match the file name against.
279     *
280     * @param fileNamePattern format of file.
281     */
282    public void setFileNamePattern(Pattern fileNamePattern) {
283        this.fileNamePattern = fileNamePattern;
284    }
285
286    /**
287     * Setter to control whether to look for a match or mis-match on the file name,
288     * if the fileNamePattern is supplied, otherwise it is applied on the folderPattern.
289     *
290     * @param match check's option for matching file names.
291     */
292    public void setMatch(boolean match) {
293        this.match = match;
294    }
295
296    /**
297     * Setter to control whether to ignore the file extension for the file name match.
298     *
299     * @param ignoreFileNameExtensions check's option for ignoring file extension.
300     */
301    public void setIgnoreFileNameExtensions(boolean ignoreFileNameExtensions) {
302        this.ignoreFileNameExtensions = ignoreFileNameExtensions;
303    }
304
305    @Override
306    public void init() {
307        if (fileNamePattern == null && folderPattern == null) {
308            fileNamePattern = CommonUtil.createPattern("\\s");
309        }
310    }
311
312    @Override
313    protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
314        final String fileName = getFileName(file);
315        final String folderPath = getFolderPath(file);
316
317        if (isMatchFolder(folderPath) && isMatchFile(fileName)) {
318            log();
319        }
320    }
321
322    /**
323     * Retrieves the file name from the given {@code file}.
324     *
325     * @param file Input file to examine.
326     * @return The file name.
327     */
328    private String getFileName(File file) {
329        String fileName = file.getName();
330
331        if (ignoreFileNameExtensions) {
332            fileName = CommonUtil.getFileNameWithoutExtension(fileName);
333        }
334
335        return fileName;
336    }
337
338    /**
339     * Retrieves the folder path from the given {@code file}.
340     *
341     * @param file Input file to examine.
342     * @return The folder path.
343     * @throws CheckstyleException if there is an error getting the canonical
344     *         path of the {@code file}.
345     */
346    private static String getFolderPath(File file) throws CheckstyleException {
347        try {
348            return file.getCanonicalFile().getParent();
349        }
350        catch (IOException ex) {
351            throw new CheckstyleException("unable to create canonical path names for "
352                    + file.getAbsolutePath(), ex);
353        }
354    }
355
356    /**
357     * Checks if the given {@code folderPath} matches the specified
358     * {@link #folderPattern}.
359     *
360     * @param folderPath Input folder path to examine.
361     * @return true if they do match.
362     */
363    private boolean isMatchFolder(String folderPath) {
364        final boolean result;
365
366        // null pattern always matches, regardless of value of 'match'
367        if (folderPattern == null) {
368            result = true;
369        }
370        else {
371            // null pattern means 'match' applies to the folderPattern matching
372            final boolean useMatch = fileNamePattern != null || match;
373            result = folderPattern.matcher(folderPath).find() == useMatch;
374        }
375
376        return result;
377    }
378
379    /**
380     * Checks if the given {@code fileName} matches the specified
381     * {@link #fileNamePattern}.
382     *
383     * @param fileName Input file name to examine.
384     * @return true if they do match.
385     */
386    private boolean isMatchFile(String fileName) {
387        // null pattern always matches, regardless of value of 'match'
388        return fileNamePattern == null || fileNamePattern.matcher(fileName).find() == match;
389    }
390
391    /** Logs the violations for the check. */
392    private void log() {
393        final String folder = getStringOrDefault(folderPattern, "");
394        final String fileName = getStringOrDefault(fileNamePattern, "");
395
396        if (match) {
397            log(1, MSG_MATCH, folder, fileName);
398        }
399        else {
400            log(1, MSG_MISMATCH, folder, fileName);
401        }
402    }
403
404    /**
405     * Retrieves the String form of the {@code pattern} or {@code defaultString}
406     * if null.
407     *
408     * @param pattern The pattern to convert.
409     * @param defaultString The result to use if {@code pattern} is null.
410     * @return The String form of the {@code pattern}.
411     */
412    private static String getStringOrDefault(Pattern pattern, String defaultString) {
413        final String result;
414
415        if (pattern == null) {
416            result = defaultString;
417        }
418        else {
419            result = pattern.toString();
420        }
421
422        return result;
423    }
424
425}