View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2015 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  package com.puppycrawl.tools.checkstyle.api;
20  
21  import java.io.File;
22  import java.util.List;
23  import java.util.TreeSet;
24  
25  import com.puppycrawl.tools.checkstyle.Utils;
26  
27  /**
28   * Provides common functionality for many FileSetChecks.
29   *
30   * @author lkuehne
31   * @author oliver
32   */
33  public abstract class AbstractFileSetCheck
34      extends AbstractViolationReporter
35      implements FileSetCheck
36  {
37      /** The dispatcher errors are fired to. */
38      private MessageDispatcher dispatcher;
39  
40      /** the file extensions that are accepted by this filter */
41      private String[] fileExtensions = {};
42  
43      /** collects the error messages */
44      private final LocalizedMessages messages = new LocalizedMessages();
45  
46      /**
47       * Called to process a file that matches the specified file extensions.
48       * @param file the file to be processed
49       * @param lines an immutable list of the contents of the file.
50       */
51      protected abstract void processFiltered(File file, List<String> lines);
52  
53      /** {@inheritDoc} */
54      @Override
55      public void init()
56      {
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public void destroy()
62      {
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public void beginProcessing(String charset)
68      {
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final TreeSet<LocalizedMessage> process(File file,
74                                                     List<String> lines)
75      {
76          getMessageCollector().reset();
77          // Process only what interested in
78          if (Utils.fileExtensionMatches(file, fileExtensions)) {
79              processFiltered(file, lines);
80          }
81          return getMessageCollector().getMessages();
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void finishProcessing()
87      {
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public final void setMessageDispatcher(MessageDispatcher dispatcher)
93      {
94          this.dispatcher = dispatcher;
95      }
96  
97      /**
98       * A message dispatcher is used to fire violation messages to
99       * 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 }