KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > AbstractFileSetCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * Provides common functionality for many FileSetChecks.
28  *
29  * @author lkuehne
30  */

31 public abstract class AbstractFileSetCheck
32     extends AbstractViolationReporter
33     implements FileSetCheck
34 {
35     /** The dispatcher errors are fired to. */
36     private MessageDispatcher mDispatcher;
37
38     /** the file extensions that are accepted by this filter */
39     private String JavaDoc[] mFileExtensions = {};
40
41     /** collects the error messages */
42     private final LocalizedMessages mMessages = new LocalizedMessages();
43
44     /** Name of a charset */
45     private String JavaDoc mCharset = System.getProperty("file.encoding", "UTF-8");
46
47     /** @see com.puppycrawl.tools.checkstyle.api.FileSetCheck */
48     public void destroy()
49     {
50     }
51
52     /** @return the name of the charset */
53     public String JavaDoc getCharset()
54     {
55         return mCharset;
56     }
57
58     /**
59      * Sets a named charset.
60      * @param aCharset the name of a charset
61      * @throws UnsupportedEncodingException if aCharset is unsupported.
62      */

63     public void setCharset(String JavaDoc aCharset)
64         throws UnsupportedEncodingException JavaDoc
65     {
66         // TODO: This is a hack to check that aCharset is supported.
67
// TODO: Find a better way in Java 1.3
68
try {
69             new String JavaDoc(new byte[] {}, aCharset);
70         }
71         catch (final UnsupportedEncodingException JavaDoc es) {
72             final String JavaDoc message = "unsupported charset: " + es.getMessage();
73             throw new UnsupportedEncodingException JavaDoc(message);
74         }
75         mCharset = aCharset;
76     }
77
78     /** {@inheritDoc} */
79     public final void setMessageDispatcher(MessageDispatcher aDispatcher)
80     {
81         mDispatcher = aDispatcher;
82     }
83
84     /**
85      * A message dispatcher is used to fire violation messages to
86      * interested audit listeners.
87      *
88      * @return the current MessageDispatcher.
89      */

90     protected final MessageDispatcher getMessageDispatcher()
91     {
92         return mDispatcher;
93     }
94
95     /**
96      * Determines the set of files this FileSetCheck is interested in.
97      * Returns the files that have one of the currently active file extensions.
98      * If no file extensions are active the argument array is returned.
99      *
100      * <p>
101      * This method can be used in the implementation of <code>process()</code>
102      * to filter it's argument list for interesting files.
103      * </p>
104      *
105      * @param aFiles the candidates for processing
106      * @return the subset of aFiles that this FileSetCheck should process
107      * @see FileSetCheck#process
108      */

109     protected final File JavaDoc[] filter(File JavaDoc[] aFiles)
110     {
111         if ((mFileExtensions == null) || (mFileExtensions.length == 0)) {
112             return aFiles;
113         }
114
115         final ArrayList JavaDoc files = new ArrayList JavaDoc(aFiles.length);
116         for (int i = 0; i < aFiles.length; i++) {
117             final File JavaDoc f = aFiles[i];
118             final String JavaDoc fileName = f.getName();
119             for (int j = 0; j < mFileExtensions.length; j++) {
120                 final String JavaDoc fileExtension = mFileExtensions[j];
121                 if (fileName.endsWith(fileExtension)) {
122                     files.add(f);
123                 }
124             }
125         }
126         return (File JavaDoc[]) files.toArray(new File JavaDoc[files.size()]);
127     }
128
129     /**
130      * Sets the file extensions that identify the files that pass the
131      * filter of this FileSetCheck.
132      * @param aExtensions the set of file extensions. A missing
133      * initial '.' character of an extension is automatically added.
134      */

135     public final void setFileExtensions(String JavaDoc[] aExtensions)
136     {
137         if (aExtensions == null) {
138             mFileExtensions = null;
139             return;
140         }
141
142         mFileExtensions = new String JavaDoc[aExtensions.length];
143         for (int i = 0; i < aExtensions.length; i++) {
144             final String JavaDoc extension = aExtensions[i];
145             if (extension.startsWith(".")) {
146                 mFileExtensions[i] = extension;
147             }
148             else {
149                 mFileExtensions[i] = "." + extension;
150             }
151         }
152     }
153
154     /**
155      * Returns the collector for violation messages.
156      * Subclasses can use the collector to find out the violation
157      * messages to fire via the message dispatcher.
158      *
159      * @return the collector for localized messages.
160      */

161     protected final LocalizedMessages getMessageCollector()
162     {
163         return mMessages;
164     }
165
166     /**
167      * Adds a violation message to the
168      * {@link #getMessageCollector message collector}.
169      * {@inheritDoc}
170      */

171     protected final void log(int aLine, String JavaDoc aKey, Object JavaDoc aArgs[])
172     {
173         log(aLine, 0, aKey, aArgs);
174     }
175
176     /**
177      * Adds a violation message to the
178      * {@link #getMessageCollector message collector}.
179      * {@inheritDoc}
180      */

181     protected final void log(int aLineNo, int aColNo,
182                              String JavaDoc aKey, Object JavaDoc[] aArgs)
183     {
184         getMessageCollector().add(
185             new LocalizedMessage(aLineNo,
186                                  aColNo,
187                                  getMessageBundle(),
188                                  aKey,
189                                  aArgs,
190                                  getSeverityLevel(),
191                                  getId(),
192                                  this.getClass()));
193     }
194
195     /**
196      * Notify all listeners about the errors in a file.
197      * Calls <code>MessageDispatcher.fireErrors()</code> with
198      * all logged errors and than clears errors' list.
199      * @param aFileName the audited file
200      */

201     protected final void fireErrors(String JavaDoc aFileName)
202     {
203         final LocalizedMessage[] errors = getMessageCollector().getMessages();
204         getMessageCollector().reset();
205         getMessageDispatcher().fireErrors(aFileName, errors);
206     }
207 }
208
Popular Tags