KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Serves as an abstract base class for all modules that report inspection
23  * findings. Such modules have a Severity level which is used for the
24  * {@link LocalizedMessage localized messages} that are created by the module.
25  *
26  * @author lkuehne
27  */

28 public abstract class AbstractViolationReporter
29     extends AutomaticBean
30 {
31     /** resuable constant for message formating */
32     private static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
33
34     /** the severity level of any violations found */
35     private SeverityLevel mSeverityLevel = SeverityLevel.ERROR;
36
37     /** the identifier of the reporter */
38     private String JavaDoc mId;
39
40     /**
41      * Returns the severity level of the messages generated by this module.
42      * @return the severity level
43      * @see SeverityLevel
44      * @see LocalizedMessage#getSeverityLevel
45      */

46     public final SeverityLevel getSeverityLevel()
47     {
48         return mSeverityLevel;
49     }
50
51     /**
52      * Sets the severity level. The string should be one of the names
53      * defined in the <code>SeverityLevel</code> class.
54      *
55      * @param aSeverity The new severity level
56      * @see SeverityLevel
57      */

58     public final void setSeverity(String JavaDoc aSeverity)
59     {
60         mSeverityLevel = SeverityLevel.getInstance(aSeverity);
61     }
62
63     /**
64      * Get the severity level's name.
65      *
66      * @return the check's severity level name.
67      */

68     public final String JavaDoc getSeverity()
69     {
70         return mSeverityLevel.getName();
71     }
72
73     /**
74      * Returns the identifier of the reporter. Can be null.
75      * @return the id
76      */

77     public final String JavaDoc getId()
78     {
79         return mId;
80     }
81
82     /**
83      * Sets the identifer of the reporter. Can be null.
84      * @param aId the id
85      */

86     public final void setId(final String JavaDoc aId)
87     {
88         mId = aId;
89     }
90
91     /**
92      * Log a message.
93      *
94      * @param aLine the line number where the error was found
95      * @param aKey the message that describes the error
96      */

97     protected final void log(int aLine, String JavaDoc aKey)
98     {
99         log(aLine, aKey, EMPTY_OBJECT_ARRAY);
100     }
101
102     /**
103      * Helper method to log a LocalizedMessage. Column defaults to 0.
104      *
105      * @param aLineNo line number to associate with the message
106      * @param aKey key to locale message format
107      * @param aArg0 first argument
108      */

109     protected final void log(int aLineNo, String JavaDoc aKey, Object JavaDoc aArg0)
110     {
111         log(aLineNo, aKey, new Object JavaDoc[] {aArg0});
112     }
113
114     /**
115      * Helper method to log a LocalizedMessage. Column defaults to 0.
116      *
117      * @param aLineNo line number to associate with the message
118      * @param aKey key to locale message format
119      * @param aArg0 first argument
120      * @param aArg1 second argument
121      */

122     protected final void log(int aLineNo, String JavaDoc aKey,
123                              Object JavaDoc aArg0, Object JavaDoc aArg1)
124     {
125         log(aLineNo, aKey, new Object JavaDoc[] {aArg0, aArg1});
126     }
127
128     /**
129      * Helper method to log a LocalizedMessage.
130      *
131      * @param aLineNo line number to associate with the message
132      * @param aColNo column number to associate with the message
133      * @param aKey key to locale message format
134      */

135     protected final void log(int aLineNo, int aColNo, String JavaDoc aKey)
136     {
137         log(aLineNo, aColNo, aKey, EMPTY_OBJECT_ARRAY);
138     }
139
140     /**
141      * Helper method to log a LocalizedMessage.
142      *
143      * @param aAST a node to get line and column numbers associated
144      * with the message
145      * @param aKey key to locale message format
146      */

147     protected final void log(DetailAST aAST, String JavaDoc aKey)
148     {
149         log(aAST.getLineNo(), aAST.getColumnNo(), aKey);
150     }
151
152     /**
153      * Helper method to log a LocalizedMessage.
154      *
155      * @param aLineNo line number to associate with the message
156      * @param aColNo column number to associate with the message
157      * @param aKey key to locale message format
158      * @param aArg0 an <code>Object</code> value
159      */

160     protected final void log(int aLineNo, int aColNo, String JavaDoc aKey,
161                     Object JavaDoc aArg0)
162     {
163         log(aLineNo, aColNo, aKey, new Object JavaDoc[] {aArg0});
164     }
165
166     /**
167      * Helper method to log a LocalizedMessage.
168      *
169      * @param aAST a node to get line and column numbers associated
170      * with the message
171      * @param aKey key to locale message format
172      * @param aArg0 an <code>Object</code> value
173      */

174     protected final void log(DetailAST aAST, String JavaDoc aKey, Object JavaDoc aArg0)
175     {
176         log(aAST.getLineNo(), aAST.getColumnNo(), aKey, aArg0);
177     }
178     /**
179      * Helper method to log a LocalizedMessage.
180      *
181      * @param aLineNo line number to associate with the message
182      * @param aColNo column number to associate with the message
183      * @param aKey key to locale message format
184      * @param aArg0 an <code>Object</code> value
185      * @param aArg1 an <code>Object</code> value
186      */

187     protected final void log(int aLineNo, int aColNo, String JavaDoc aKey,
188                     Object JavaDoc aArg0, Object JavaDoc aArg1)
189     {
190         log(aLineNo, aColNo, aKey, new Object JavaDoc[] {aArg0, aArg1});
191     }
192
193     /**
194      * Helper method to log a LocalizedMessage.
195      *
196      * @param aAST a node to get line and column numbers associated
197      * with the message
198      * @param aKey key to locale message format
199      * @param aArg0 an <code>Object</code> value
200      * @param aArg1 an <code>Object</code> value
201      */

202     protected final void log(DetailAST aAST, String JavaDoc aKey,
203                              Object JavaDoc aArg0, Object JavaDoc aArg1)
204     {
205         log(aAST.getLineNo(), aAST.getColumnNo(), aKey, aArg0, aArg1);
206     }
207
208     /**
209      * Returns the message bundle name resourcebundle that contains the messages
210      * used by this module.
211      * <p>
212      * The default implementation expects the resource files to be named
213      * messages.properties, messages_de.properties, etc. The file must
214      * be placed in the same package as the module implementation.
215      * </p>
216      * <p>
217      * Example: If you write com/foo/MyCoolCheck, create resource files
218      * com/foo/messages.properties, com/foo/messages_de.properties, etc.
219      * </p>
220      *
221      * @return name of a resource bundle that contains the messages
222      * used by this module.
223      */

224     protected String JavaDoc getMessageBundle()
225     {
226         final String JavaDoc className = this.getClass().getName();
227         return getMessageBundle(className);
228     }
229
230     /**
231      * for unit tests, especially with a class with no package name.
232      * @param aClassName class name of the module.
233      * @return name of a resource bundle that contains the messages
234      * used by the module.
235      */

236     String JavaDoc getMessageBundle(final String JavaDoc aClassName)
237     {
238         final int endIndex = aClassName.lastIndexOf('.');
239         final String JavaDoc messages = "messages";
240         if (endIndex < 0) {
241             return messages;
242         }
243         final String JavaDoc packageName = aClassName.substring(0, endIndex);
244         return packageName + "." + messages;
245     }
246
247     /**
248      * Log a message that has no column information.
249      *
250      * @param aLine the line number where the error was found
251      * @param aKey the message that describes the error
252      * @param aArgs the details of the message
253      *
254      * @see java.text.MessageFormat
255      */

256     protected abstract void log(int aLine, String JavaDoc aKey, Object JavaDoc aArgs[]);
257
258     /**
259      * Log a message that has column information.
260      *
261      * @param aLine the line number where the error was found
262      * @param aCol the column number where the error was found
263      * @param aKey the message that describes the error
264      * @param aArgs the details of the message
265      *
266      * @see java.text.MessageFormat
267      */

268     protected abstract void log(int aLine,
269                                 int aCol,
270                                 String JavaDoc aKey,
271                                 Object JavaDoc[] aArgs);
272
273 }
274
Popular Tags