Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2003  Oliver Burn
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  
22  import java.io.File;
23  import java.util.ArrayList;
24  
25  /**
26   * Provides common functionality for many FileSetChecks.
27   *
28   * @author lkuehne
29   */
30  public abstract class AbstractFileSetCheck
31      extends AbstractViolationReporter
32      implements FileSetCheck
33  {
34      /** The dispatcher errors are fired to. */
35      private MessageDispatcher mDispatcher;
36  
37      /** the file extensions that are accepted by this filter */
38      private String[] mFileExtensions = {};
39  
40      /** collects the error messages */
41      private final LocalizedMessages mMessages = new LocalizedMessages();
42  
43      /** @see com.puppycrawl.tools.checkstyle.api.FileSetCheck */
44      public void destroy()
45      {
46      }
47  
48      /** @see com.puppycrawl.tools.checkstyle.api.FileSetCheck */
49      public final void setMessageDispatcher(MessageDispatcher aDispatcher)
50      {
51          mDispatcher = aDispatcher;
52      }
53  
54      /**
55       * A message dispatcher is used to fire violation messages to
56       * interested audit listeners.
57       *
58       * @return the current MessageDispatcher.
59       */
60      protected final MessageDispatcher getMessageDispatcher()
61      {
62          return mDispatcher;
63      }
64  
65      /**
66       * Determines the set of files this FileSetCheck is interested in.
67       * Returns the files that have one of the currently active file extensions.
68       * If no file extensions are active the argument array is returned.
69       *
70       * <p>
71       * This method can be used in the implementation of <code>process()</code>
72       * to filter it's argument list for interesting files.
73       * </p>
74       *
75       * @param aFiles the candidates for processing
76       * @return the subset of aFiles that this FileSetCheck should process
77       * @see FileSetCheck#process
78       */
79      protected final File[] filter(File[] aFiles)
80      {
81          if ((mFileExtensions == null) || (mFileExtensions.length == 0)) {
82              return aFiles;
83          }
84  
85          final ArrayList files = new ArrayList(aFiles.length);
86          for (int i = 0; i < aFiles.length; i++) {
87              final File f = aFiles[i];
88              final String fileName = f.getName();
89              for (int j = 0; j < mFileExtensions.length; j++) {
90                  final String fileExtension = mFileExtensions[j];
91                  if (fileName.endsWith(fileExtension)) {
92                      files.add(f);
93                  }
94              }
95          }
96          return (File[]) files.toArray(new File[files.size()]);
97      }
98  
99      /**
100      * Sets the file extensions that identify the files that pass the
101      * filter of this FileSetCheck.
102      * @param aExtensions the set of file extensions. A missing
103      * initial '.' character of an extension is automatically added.
104      */
105     public final void setFileExtensions(String[] aExtensions)
106     {
107         if (aExtensions == null) {
108             mFileExtensions = null;
109             return;
110         }
111 
112         mFileExtensions = new String[aExtensions.length];
113         for (int i = 0; i < aExtensions.length; i++) {
114             final String extension = aExtensions[i];
115             if (extension.startsWith(".")) {
116                 mFileExtensions[i] = extension;
117             }
118             else {
119                 mFileExtensions[i] = "." + extension;
120             }
121         }
122     }
123 
124     /**
125      * Returns the collector for violation messages.
126      * Subclasses can use the collector to find out the violation
127      * messages to fire via the message dispatcher.
128      *
129      * @return the collector for localized messages.
130      */
131     protected final LocalizedMessages getMessageCollector()
132     {
133         return mMessages;
134     }
135 
136     /**
137      * Adds a violation message to the
138      * {@link #getMessageCollector message collector}.
139      * @see AbstractViolationReporter#log(int, String, Object[])
140      */
141     protected final void log(int aLine, String aKey, Object aArgs[])
142     {
143         log(aLine, 0, aKey, aArgs);
144     }
145 
146     /**
147      * Adds a violation message to the
148      * {@link #getMessageCollector message collector}.
149      * @see AbstractViolationReporter#log(int, int, String, Object[])
150      */
151     protected final void log(int aLineNo, int aColNo,
152                              String aKey, Object[] aArgs)
153     {
154         getMessageCollector().add(
155             new LocalizedMessage(aLineNo,
156                                  aColNo,
157                                  getMessageBundle(),
158                                  aKey,
159                                  aArgs,
160                                  getSeverityLevel(),
161                                  this.getClass()));
162     }
163 
164     /**
165      * Notify all listeners about the errors in a file.
166      * Calls <code>MessageDispatcher.fireErrors()</code> with
167      * all logged errors and than clears errors' list.
168      * @param aFileName the audited file
169      */
170     protected final void fireErrors(String aFileName)
171     {
172         final LocalizedMessage[] errors = getMessageCollector().getMessages();
173         getMessageCollector().reset();
174         getMessageDispatcher().fireErrors(aFileName, errors);
175     }
176 }