001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.api;
020
021import java.io.File;
022import java.util.List;
023import java.util.TreeSet;
024
025import com.puppycrawl.tools.checkstyle.Utils;
026
027/**
028 * Provides common functionality for many FileSetChecks.
029 *
030 * @author lkuehne
031 * @author oliver
032 */
033public abstract class AbstractFileSetCheck
034    extends AbstractViolationReporter
035    implements FileSetCheck
036{
037    /** The dispatcher errors are fired to. */
038    private MessageDispatcher dispatcher;
039
040    /** the file extensions that are accepted by this filter */
041    private String[] fileExtensions = {};
042
043    /** collects the error messages */
044    private final LocalizedMessages messages = new LocalizedMessages();
045
046    /**
047     * Called to process a file that matches the specified file extensions.
048     * @param file the file to be processed
049     * @param lines an immutable list of the contents of the file.
050     */
051    protected abstract void processFiltered(File file, List<String> lines);
052
053    /** {@inheritDoc} */
054    @Override
055    public void init()
056    {
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public void destroy()
062    {
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public void beginProcessing(String charset)
068    {
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public final TreeSet<LocalizedMessage> process(File file,
074                                                   List<String> lines)
075    {
076        getMessageCollector().reset();
077        // Process only what interested in
078        if (Utils.fileExtensionMatches(file, fileExtensions)) {
079            processFiltered(file, lines);
080        }
081        return getMessageCollector().getMessages();
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public void finishProcessing()
087    {
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public final void setMessageDispatcher(MessageDispatcher dispatcher)
093    {
094        this.dispatcher = dispatcher;
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    {
105        return dispatcher;
106    }
107
108    /**
109     * Sets the file extensions that identify the files that pass the
110     * filter of this FileSetCheck.
111     * @param extensions the set of file extensions. A missing
112     * initial '.' character of an extension is automatically added.
113     */
114    public final void setFileExtensions(String[] extensions)
115    {
116        if (extensions == null) {
117            fileExtensions = null;
118            return;
119        }
120
121        fileExtensions = new String[extensions.length];
122        for (int i = 0; i < extensions.length; i++) {
123            final String extension = extensions[i];
124            if (extension.startsWith(".")) {
125                fileExtensions[i] = extension;
126            }
127            else {
128                fileExtensions[i] = "." + extension;
129            }
130        }
131    }
132
133    /**
134     * Returns the collector for violation messages.
135     * Subclasses can use the collector to find out the violation
136     * messages to fire via the message dispatcher.
137     *
138     * @return the collector for localized messages.
139     */
140    protected final LocalizedMessages getMessageCollector()
141    {
142        return messages;
143    }
144
145    @Override
146    public final void log(int line, String key, Object... args)
147    {
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    {
155        getMessageCollector().add(
156            new LocalizedMessage(lineNo,
157                                 colNo,
158                                 getMessageBundle(),
159                                 key,
160                                 args,
161                                 getSeverityLevel(),
162                                 getId(),
163                                 this.getClass(),
164                                 this.getCustomMessages().get(key)));
165    }
166
167    /**
168     * Notify all listeners about the errors in a file.
169     * Calls <code>MessageDispatcher.fireErrors()</code> 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    {
175        final TreeSet<LocalizedMessage> errors = getMessageCollector()
176                .getMessages();
177        getMessageCollector().reset();
178        getMessageDispatcher().fireErrors(fileName, errors);
179    }
180}