001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.CommonUtils;
031
032/**
033 * <p>
034 * Implementation of a check that looks for a file name and/or path match (or
035 * mis-match) against specified patterns. 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 error message. This allows the error 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 error
071 * messages.
072 * </p>
073 *
074 * <p>
075 * Check have following options:
076 * </p>
077 * <ul>
078 * <li>
079 * folderPattern - Regular expression to match the folder path against. Default
080 * value is null.</li>
081 *
082 * <li>
083 * fileNamePattern - Regular expression to match the file name against. Default
084 * value is null.</li>
085 *
086 * <li>
087 * match - Whether to look for a match or mis-match on the file name, if the
088 * fileNamePattern is supplied, otherwise it is applied on the folderPattern.
089 * Default value is true.</li>
090 *
091 * <li>
092 * ignoreFileNameExtensions - Whether to ignore the file extension for the file
093 * name match. Default value is false.</li>
094 *
095 * <li>
096 * fileExtensions - File type extension of files to process. If this is
097 * specified, then only files that match these types are examined with the other
098 * patterns. Default value is {}.</li>
099 * </ul>
100 * <br>
101 *
102 * <p>
103 * To configure the check to report file names that contain a space:
104 * </p>
105 *
106 * <pre>
107 * &lt;module name=&quot;RegexpOnFilename&quot;/&gt;
108 * </pre>
109 * <p>
110 * To configure the check to force picture files to not be 'gif':
111 * </p>
112 *
113 * <pre>
114 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
115 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\\.gif$&quot;/&gt;
116 * &lt;/module&gt;
117 * </pre>
118 * <p>
119 * OR:
120 * </p>
121 *
122 * <pre>
123 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
124 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;.&quot;/&gt;
125 *   &lt;property name=&quot;fileExtensions&quot; value=&quot;gif&quot;/&gt;
126 * &lt;/module&gt;
127 * </pre>
128 *
129 * <p>
130 * To configure the check to only allow property and xml files to be located in
131 * the resource folder:
132 * </p>
133 *
134 * <pre>
135 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
136 *   &lt;property name=&quot;folderPattern&quot;
137 *     value=&quot;[\\/]src[\\/]\\w+[\\/]resources[\\/]&quot;/&gt;
138 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
139 *   &lt;property name=&quot;fileExtensions&quot; value=&quot;properties, xml&quot;/&gt;
140 * &lt;/module&gt;
141 * </pre>
142 *
143 * <p>
144 * To configure the check to only allow Java and XML files in your folders use
145 * the below.
146 * </p>
147 *
148 * <pre>
149 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
150 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\\.(java|xml)$&quot;/&gt;
151 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
152 * &lt;/module&gt;
153 * </pre>
154 * <p>
155 * To configure the check to only allow Java and XML files only in your source
156 * folder and ignore any other folders:
157 * </p>
158 *
159 * <p>
160 * <b>Note:</b> 'folderPattern' must be specified if checkstyle is analyzing
161 * more than the normal source folder, like the 'bin' folder where class files
162 * can be located.
163 * </p>
164 *
165 * <pre>
166 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
167 *   &lt;property name=&quot;folderPattern&quot; value=&quot;[\\/]src[\\/]&quot;/&gt;
168 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;\\.(java|xml)$&quot;/&gt;
169 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
170 * &lt;/module&gt;
171 * </pre>
172 * <p>
173 * To configure the check to only allow file names to be camel case:
174 * </p>
175 *
176 * <pre>
177 * &lt;module name=&quot;RegexpOnFilename&quot;&gt;
178 *   &lt;property name=&quot;fileNamePattern&quot;
179 *     value=&quot;^([A-Z][a-z0-9]+\.?)+$&quot;/&gt;
180 *   &lt;property name=&quot;match&quot; value=&quot;false&quot;/&gt;
181 *   &lt;property name=&quot;ignoreFileNameExtensions&quot; value=&quot;true&quot;/&gt;
182 * &lt;/module&gt;
183 * </pre>
184 *
185 * @author Richard Veach
186 */
187@StatelessCheck
188public class RegexpOnFilenameCheck extends AbstractFileSetCheck {
189
190    /**
191     * A key is pointing to the warning message text in "messages.properties"
192     * file.
193     */
194    public static final String MSG_MATCH = "regexp.filename.match";
195    /**
196     * A key is pointing to the warning message text in "messages.properties"
197     * file.
198     */
199    public static final String MSG_MISMATCH = "regexp.filename.mismatch";
200
201    /** Compiled regexp to match a folder. */
202    private Pattern folderPattern;
203    /** Compiled regexp to match a file. */
204    private Pattern fileNamePattern;
205    /** Whether to look for a file name match or mismatch. */
206    private boolean match = true;
207    /** Whether to ignore the file's extension when looking for matches. */
208    private boolean ignoreFileNameExtensions;
209
210    /**
211     * Setter for folder format.
212     *
213     * @param folderPattern format of folder.
214     */
215    public void setFolderPattern(Pattern folderPattern) {
216        this.folderPattern = folderPattern;
217    }
218
219    /**
220     * Setter for file name format.
221     *
222     * @param fileNamePattern format of file.
223     */
224    public void setFileNamePattern(Pattern fileNamePattern) {
225        this.fileNamePattern = fileNamePattern;
226    }
227
228    /**
229     * Sets whether the check should look for a file name match or mismatch.
230     *
231     * @param match check's option for matching file names.
232     */
233    public void setMatch(boolean match) {
234        this.match = match;
235    }
236
237    /**
238     * Sets whether file name matching should drop the file extension or not.
239     *
240     * @param ignoreFileNameExtensions check's option for ignoring file extension.
241     */
242    public void setIgnoreFileNameExtensions(boolean ignoreFileNameExtensions) {
243        this.ignoreFileNameExtensions = ignoreFileNameExtensions;
244    }
245
246    @Override
247    public void init() {
248        if (fileNamePattern == null && folderPattern == null) {
249            fileNamePattern = CommonUtils.createPattern("\\s");
250        }
251    }
252
253    @Override
254    protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
255        final String fileName = getFileName(file);
256        final String folderPath = getFolderPath(file);
257
258        if (isMatchFolder(folderPath) && isMatchFile(fileName)) {
259            log();
260        }
261    }
262
263    /**
264     * Retrieves the file name from the given {@code file}.
265     *
266     * @param file Input file to examine.
267     * @return The file name.
268     */
269    private String getFileName(File file) {
270        String fileName = file.getName();
271
272        if (ignoreFileNameExtensions) {
273            fileName = CommonUtils.getFileNameWithoutExtension(fileName);
274        }
275
276        return fileName;
277    }
278
279    /**
280     * Retrieves the folder path from the given {@code file}.
281     *
282     * @param file Input file to examine.
283     * @return The folder path.
284     * @throws CheckstyleException if there is an error getting the canonical
285     *         path of the {@code file}.
286     */
287    private static String getFolderPath(File file) throws CheckstyleException {
288        try {
289            return file.getCanonicalFile().getParent();
290        }
291        catch (IOException ex) {
292            throw new CheckstyleException("unable to create canonical path names for "
293                    + file.getAbsolutePath(), ex);
294        }
295    }
296
297    /**
298     * Checks if the given {@code folderPath} matches the specified
299     * {@link #folderPattern}.
300     *
301     * @param folderPath Input folder path to examine.
302     * @return true if they do match.
303     */
304    private boolean isMatchFolder(String folderPath) {
305        final boolean result;
306
307        // null pattern always matches, regardless of value of 'match'
308        if (folderPattern == null) {
309            result = true;
310        }
311        else {
312            // null pattern means 'match' applies to the folderPattern matching
313            final boolean useMatch = fileNamePattern != null || match;
314            result = folderPattern.matcher(folderPath).find() == useMatch;
315        }
316
317        return result;
318    }
319
320    /**
321     * Checks if the given {@code fileName} matches the specified
322     * {@link #fileNamePattern}.
323     *
324     * @param fileName Input file name to examine.
325     * @return true if they do match.
326     */
327    private boolean isMatchFile(String fileName) {
328        // null pattern always matches, regardless of value of 'match'
329        return fileNamePattern == null || fileNamePattern.matcher(fileName).find() == match;
330    }
331
332    /** Logs the errors for the check. */
333    private void log() {
334        final String folder = getStringOrDefault(folderPattern, "");
335        final String fileName = getStringOrDefault(fileNamePattern, "");
336
337        if (match) {
338            log(0, MSG_MATCH, folder, fileName);
339        }
340        else {
341            log(0, MSG_MISMATCH, folder, fileName);
342        }
343    }
344
345    /**
346     * Retrieves the String form of the {@code pattern} or {@code defaultString}
347     * if null.
348     *
349     * @param pattern The pattern to convert.
350     * @param defaultString The result to use if {@code pattern} is null.
351     * @return The String form of the {@code pattern}.
352     */
353    private static String getStringOrDefault(Pattern pattern, String defaultString) {
354        final String result;
355
356        if (pattern == null) {
357            result = defaultString;
358        }
359        else {
360            result = pattern.toString();
361        }
362
363        return result;
364    }
365
366}