001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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.List;
025import java.util.regex.Pattern;
026
027import com.google.common.io.Files;
028import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
029import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
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 conjuction 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 */
187public class RegexpOnFilenameCheck extends AbstractFileSetCheck {
188    /**
189     * A key is pointing to the warning message text in "messages.properties"
190     * file.
191     */
192    public static final String MSG_MATCH = "regexp.filename.match";
193    /**
194     * A key is pointing to the warning message text in "messages.properties"
195     * file.
196     */
197    public static final String MSG_MISMATCH = "regexp.filename.mismatch";
198
199    /** Compiled regexp to match a folder. */
200    private Pattern folderPattern;
201    /** Compiled regexp to match a file. */
202    private Pattern fileNamePattern;
203    /** Whether to look for a file name match or mismatch. */
204    private boolean match = true;
205    /** Whether to ignore the file's extension when looking for matches. */
206    private boolean ignoreFileNameExtensions;
207
208    /**
209     * Setter for folder format.
210     *
211     * @param folderPattern format of folder.
212     * @throws org.apache.commons.beanutils.ConversionException if unable to
213     *         create Pattern object.
214     */
215    public void setFolderPattern(String folderPattern) {
216        this.folderPattern = CommonUtils.createPattern(folderPattern);
217    }
218
219    /**
220     * Setter for file name format.
221     *
222     * @param fileNamePattern format of file.
223     * @throws org.apache.commons.beanutils.ConversionException if unable to
224     *         create Pattern object.
225     */
226    public void setFileNamePattern(String fileNamePattern) {
227        this.fileNamePattern = CommonUtils.createPattern(fileNamePattern);
228    }
229
230    /**
231     * Sets whether the check should look for a file name match or mismatch.
232     *
233     * @param match check's option for matching file names.
234     */
235    public void setMatch(boolean match) {
236        this.match = match;
237    }
238
239    /**
240     * Sets whether file name matching should drop the file extension or not.
241     *
242     * @param ignoreFileNameExtensions check's option for ignoring file extension.
243     */
244    public void setIgnoreFileNameExtensions(boolean ignoreFileNameExtensions) {
245        this.ignoreFileNameExtensions = ignoreFileNameExtensions;
246    }
247
248    @Override
249    public void init() {
250        if (fileNamePattern == null && folderPattern == null) {
251            fileNamePattern = CommonUtils.createPattern("\\s");
252        }
253    }
254
255    @Override
256    protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
257        final String fileName = getFileName(file);
258        final String folderPath = getFolderPath(file);
259
260        if (isMatchFolder(folderPath) && isMatchFile(fileName)) {
261            log();
262        }
263    }
264
265    /**
266     * Retrieves the file name from the given {@code file}.
267     *
268     * @param file Input file to examine.
269     * @return The file name.
270     */
271    private String getFileName(File file) {
272        String fileName = file.getName();
273
274        if (ignoreFileNameExtensions) {
275            fileName = Files.getNameWithoutExtension(fileName);
276        }
277
278        return fileName;
279    }
280
281    /**
282     * Retrieves the folder path from the given {@code file}.
283     *
284     * @param file Input file to examine.
285     * @return The folder path.
286     * @throws CheckstyleException if there is an error getting the canonical
287     *         path of the {@code file}.
288     */
289    private static String getFolderPath(File file) throws CheckstyleException {
290        try {
291            return file.getParentFile().getCanonicalPath();
292        }
293        catch (IOException ex) {
294            throw new CheckstyleException("unable to create canonical path names for "
295                    + file.getAbsolutePath(), ex);
296        }
297    }
298
299    /**
300     * Checks if the given {@code folderPath} matches the specified
301     * {@link #folderPattern}.
302     *
303     * @param folderPath Input folder path to examine.
304     * @return true if they do match.
305     */
306    private boolean isMatchFolder(String folderPath) {
307        final boolean result;
308
309        // null pattern always matches, regardless of value of 'match'
310        if (folderPattern == null) {
311            result = true;
312        }
313        else {
314            final boolean useMatch;
315
316            // null pattern means 'match' applies to the folderPattern matching
317            if (fileNamePattern == null) {
318                useMatch = match;
319            }
320            else {
321                useMatch = true;
322            }
323
324            result = folderPattern.matcher(folderPath).find() == useMatch;
325        }
326
327        return result;
328    }
329
330    /**
331     * Checks if the given {@code fileName} matches the specified
332     * {@link #fileNamePattern}.
333     *
334     * @param fileName Input file name to examine.
335     * @return true if they do match.
336     */
337    private boolean isMatchFile(String fileName) {
338        final boolean result;
339
340        // null pattern always matches, regardless of value of 'match'
341        if (fileNamePattern == null) {
342            result = true;
343        }
344        else {
345            result = fileNamePattern.matcher(fileName).find() == match;
346        }
347
348        return result;
349    }
350
351    /** Logs the errors for the check. */
352    private void log() {
353        final String folder = getStringOrDefault(folderPattern, "");
354        final String fileName = getStringOrDefault(fileNamePattern, "");
355
356        if (match) {
357            log(0, MSG_MATCH, folder, fileName);
358        }
359        else {
360            log(0, MSG_MISMATCH, folder, fileName);
361        }
362    }
363
364    /**
365     * Retrieves the String form of the {@code pattern} or {@code defaultString}
366     * if null.
367     *
368     * @param pattern The pattern to convert.
369     * @param defaultString The result to use if {@code pattern} is null.
370     * @return The String form of the {@code pattern}.
371     */
372    private static String getStringOrDefault(Pattern pattern, String defaultString) {
373        final String result;
374
375        if (pattern == null) {
376            result = defaultString;
377        }
378        else {
379            result = pattern.toString();
380        }
381
382        return result;
383    }
384}