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