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.List; 025import java.util.SortedSet; 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 */ 035public abstract class AbstractFileSetCheck 036 extends AbstractViolationReporter 037 implements FileSetCheck { 038 039 /** Collects the error messages. */ 040 private final LocalizedMessages messageCollector = new LocalizedMessages(); 041 042 /** The dispatcher errors are fired to. */ 043 private MessageDispatcher messageDispatcher; 044 045 /** The file extensions that are accepted by this filter. */ 046 private String[] fileExtensions = CommonUtils.EMPTY_STRING_ARRAY; 047 048 /** 049 * Called to process a file that matches the specified file extensions. 050 * @param file the file to be processed 051 * @param lines an immutable list of the contents of the file. 052 * @throws CheckstyleException if error condition within Checkstyle occurs. 053 */ 054 protected abstract void processFiltered(File file, List<String> lines) 055 throws CheckstyleException; 056 057 @Override 058 public void init() { 059 // No code by default, should be overridden only by demand at subclasses 060 } 061 062 @Override 063 public void destroy() { 064 // No code by default, should be overridden only by demand at subclasses 065 } 066 067 @Override 068 public void beginProcessing(String charset) { 069 // No code by default, should be overridden only by demand at subclasses 070 } 071 072 @Override 073 public final SortedSet<LocalizedMessage> process(File file, List<String> lines) 074 throws CheckstyleException { 075 messageCollector.reset(); 076 // Process only what interested in 077 if (CommonUtils.matchesFileExtension(file, fileExtensions)) { 078 processFiltered(file, lines); 079 } 080 return messageCollector.getMessages(); 081 } 082 083 @Override 084 public void finishProcessing() { 085 // No code by default, should be overridden only by demand at subclasses 086 } 087 088 @Override 089 public final void setMessageDispatcher(MessageDispatcher messageDispatcher) { 090 this.messageDispatcher = messageDispatcher; 091 } 092 093 /** 094 * A message dispatcher is used to fire violation messages to 095 * interested audit listeners. 096 * 097 * @return the current MessageDispatcher. 098 */ 099 protected final MessageDispatcher getMessageDispatcher() { 100 return messageDispatcher; 101 } 102 103 /** 104 * Makes copy of file extensions and returns them. 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}