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.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     * @return file extensions that identify the files that pass the
105     *     filter of this FileSetCheck.
106     */
107    public String[] getFileExtensions() {
108        return Arrays.copyOf(fileExtensions, fileExtensions.length);
109    }
110
111    /**
112     * Sets the file extensions that identify the files that pass the
113     * filter of this FileSetCheck.
114     * @param extensions the set of file extensions. A missing
115     *         initial '.' character of an extension is automatically added.
116     * @throws IllegalArgumentException is argument is null
117     */
118    public final void setFileExtensions(String... extensions) {
119        if (extensions == null) {
120            throw new IllegalArgumentException("Extensions array can not be null");
121        }
122
123        fileExtensions = new String[extensions.length];
124        for (int i = 0; i < extensions.length; i++) {
125            final String extension = extensions[i];
126            if (CommonUtils.startsWithChar(extension, '.')) {
127                fileExtensions[i] = extension;
128            }
129            else {
130                fileExtensions[i] = "." + extension;
131            }
132        }
133    }
134
135    /**
136     * Returns the collector for violation messages.
137     * Subclasses can use the collector to find out the violation
138     * messages to fire via the message dispatcher.
139     *
140     * @return the collector for localized messages.
141     */
142    protected final LocalizedMessages getMessageCollector() {
143        return messageCollector;
144    }
145
146    @Override
147    public final void log(int line, String key, Object... args) {
148        log(line, 0, key, args);
149    }
150
151    @Override
152    public final void log(int lineNo, int colNo, String key,
153            Object... args) {
154        messageCollector.add(
155                new LocalizedMessage(lineNo,
156                        colNo,
157                        getMessageBundle(),
158                        key,
159                        args,
160                        getSeverityLevel(),
161                        getId(),
162                        getClass(),
163                        getCustomMessages().get(key)));
164    }
165
166    /**
167     * Notify all listeners about the errors in a file.
168     * Calls {@code MessageDispatcher.fireErrors()} with
169     * all logged errors and than clears errors' list.
170     * @param fileName the audited file
171     */
172    protected final void fireErrors(String fileName) {
173        final SortedSet<LocalizedMessage> errors = messageCollector
174                .getMessages();
175        messageCollector.reset();
176        getMessageDispatcher().fireErrors(fileName, errors);
177    }
178}