001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.api; 021 022import java.io.File; 023import java.util.Arrays; 024import java.util.SortedSet; 025import java.util.TreeSet; 026 027import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 028 029/** 030 * Provides common functionality for many FileSetChecks. 031 * 032 * @author lkuehne 033 * @author oliver 034 * @noinspection NoopMethodInAbstractClass 035 */ 036public abstract class AbstractFileSetCheck 037 extends AbstractViolationReporter 038 implements FileSetCheck { 039 040 /** 041 * Collects the error messages. 042 */ 043 private static final ThreadLocal<SortedSet<LocalizedMessage>> MESSAGE_COLLECTOR = 044 ThreadLocal.withInitial(TreeSet::new); 045 046 /** The dispatcher errors are fired to. */ 047 private MessageDispatcher messageDispatcher; 048 049 /** The file extensions that are accepted by this filter. */ 050 private String[] fileExtensions = CommonUtils.EMPTY_STRING_ARRAY; 051 052 /** 053 * Called to process a file that matches the specified file extensions. 054 * @param file the file to be processed 055 * @param fileText the contents of the file. 056 * @throws CheckstyleException if error condition within Checkstyle occurs. 057 */ 058 protected abstract void processFiltered(File file, FileText fileText) 059 throws CheckstyleException; 060 061 @Override 062 public void init() { 063 // No code by default, should be overridden only by demand at subclasses 064 } 065 066 @Override 067 public void destroy() { 068 // No code by default, should be overridden only by demand at subclasses 069 } 070 071 @Override 072 public void beginProcessing(String charset) { 073 // No code by default, should be overridden only by demand at subclasses 074 } 075 076 @Override 077 public final SortedSet<LocalizedMessage> process(File file, FileText fileText) 078 throws CheckstyleException { 079 MESSAGE_COLLECTOR.get().clear(); 080 // Process only what interested in 081 if (CommonUtils.matchesFileExtension(file, fileExtensions)) { 082 processFiltered(file, fileText); 083 } 084 return new TreeSet<>(MESSAGE_COLLECTOR.get()); 085 } 086 087 @Override 088 public void finishProcessing() { 089 // No code by default, should be overridden only by demand at subclasses 090 } 091 092 @Override 093 public final void setMessageDispatcher(MessageDispatcher messageDispatcher) { 094 this.messageDispatcher = messageDispatcher; 095 } 096 097 /** 098 * A message dispatcher is used to fire violation messages to 099 * interested audit listeners. 100 * 101 * @return the current MessageDispatcher. 102 */ 103 protected final MessageDispatcher getMessageDispatcher() { 104 return messageDispatcher; 105 } 106 107 /** 108 * Makes copy of file extensions and returns them. 109 * @return file extensions that identify the files that pass the 110 * filter of this FileSetCheck. 111 */ 112 public String[] getFileExtensions() { 113 return Arrays.copyOf(fileExtensions, fileExtensions.length); 114 } 115 116 /** 117 * Sets the file extensions that identify the files that pass the 118 * filter of this FileSetCheck. 119 * @param extensions the set of file extensions. A missing 120 * initial '.' character of an extension is automatically added. 121 * @throws IllegalArgumentException is argument is null 122 */ 123 public final void setFileExtensions(String... extensions) { 124 if (extensions == null) { 125 throw new IllegalArgumentException("Extensions array can not be null"); 126 } 127 128 fileExtensions = new String[extensions.length]; 129 for (int i = 0; i < extensions.length; i++) { 130 final String extension = extensions[i]; 131 if (CommonUtils.startsWithChar(extension, '.')) { 132 fileExtensions[i] = extension; 133 } 134 else { 135 fileExtensions[i] = "." + extension; 136 } 137 } 138 } 139 140 /** 141 * Adds the sorted set of {@link LocalizedMessage} to the message collector. 142 * @param messages the sorted set of {@link LocalizedMessage}. 143 */ 144 protected static void addMessages(SortedSet<LocalizedMessage> messages) { 145 MESSAGE_COLLECTOR.get().addAll(messages); 146 } 147 148 @Override 149 public final void log(int line, String key, Object... args) { 150 log(line, 0, key, args); 151 } 152 153 @Override 154 public final void log(int lineNo, int colNo, String key, 155 Object... args) { 156 MESSAGE_COLLECTOR.get().add( 157 new LocalizedMessage(lineNo, 158 colNo, 159 getMessageBundle(), 160 key, 161 args, 162 getSeverityLevel(), 163 getId(), 164 getClass(), 165 getCustomMessages().get(key))); 166 } 167 168 /** 169 * Notify all listeners about the errors in a file. 170 * Calls {@code MessageDispatcher.fireErrors()} with 171 * all logged errors and than clears errors' list. 172 * @param fileName the audited file 173 */ 174 protected final void fireErrors(String fileName) { 175 final SortedSet<LocalizedMessage> errors = new TreeSet<>(MESSAGE_COLLECTOR.get()); 176 MESSAGE_COLLECTOR.get().clear(); 177 messageDispatcher.fireErrors(fileName, errors); 178 } 179}