Source code: com/puppycrawl/tools/checkstyle/api/AbstractViolationReporter.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 * 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[] EMPTY_OBJECT_ARRAY = new Object[0];
33
34 /** the severity level of any violations found */
35 private SeverityLevel mSeverityLevel = SeverityLevel.ERROR;
36
37 /**
38 * Returns the severity level of the messages generated by this module.
39 * @return the severity level
40 * @see SeverityLevel
41 * @see LocalizedMessage#getSeverityLevel
42 */
43 public final SeverityLevel getSeverityLevel()
44 {
45 return mSeverityLevel;
46 }
47
48 /**
49 * Sets the severity level. The string should be one of the names
50 * defined in the <code>SeverityLevel</code> class.
51 *
52 * @param aSeverity The new severity level
53 * @see SeverityLevel
54 */
55 public final void setSeverity(String aSeverity)
56 {
57 mSeverityLevel = SeverityLevel.getInstance(aSeverity);
58 }
59
60 /**
61 * Get the severity level's name.
62 *
63 * @return the check's severity level name.
64 */
65 public final String getSeverity()
66 {
67 return mSeverityLevel.getName();
68 }
69
70 /**
71 * Log a message.
72 *
73 * @param aLine the line number where the error was found
74 * @param aKey the message that describes the error
75 */
76 protected final void log(int aLine, String aKey)
77 {
78 log(aLine, aKey, EMPTY_OBJECT_ARRAY);
79 }
80
81 /**
82 * Helper method to log a LocalizedMessage. Column defaults to 0.
83 *
84 * @param aLineNo line number to associate with the message
85 * @param aKey key to locale message format
86 * @param aArg0 first argument
87 */
88 protected final void log(int aLineNo, String aKey, Object aArg0)
89 {
90 log(aLineNo, aKey, new Object[] {aArg0});
91 }
92
93 /**
94 * Helper method to log a LocalizedMessage. Column defaults to 0.
95 *
96 * @param aLineNo line number to associate with the message
97 * @param aKey key to locale message format
98 * @param aArg0 first argument
99 * @param aArg1 second argument
100 */
101 protected final void log(int aLineNo, String aKey,
102 Object aArg0, Object aArg1)
103 {
104 log(aLineNo, aKey, new Object[] {aArg0, aArg1});
105 }
106
107 /**
108 * Helper method to log a LocalizedMessage.
109 *
110 * @param aLineNo line number to associate with the message
111 * @param aColNo column number to associate with the message
112 * @param aKey key to locale message format
113 */
114 protected final void log(int aLineNo, int aColNo, String aKey)
115 {
116 log(aLineNo, aColNo, aKey, EMPTY_OBJECT_ARRAY);
117 }
118
119 /**
120 * Helper method to log a LocalizedMessage.
121 *
122 * @param aLineNo line number to associate with the message
123 * @param aColNo column number to associate with the message
124 * @param aKey key to locale message format
125 * @param aArg0 an <code>Object</code> value
126 */
127 protected final void log(int aLineNo, int aColNo, String aKey,
128 Object aArg0)
129 {
130 log(aLineNo, aColNo, aKey, new Object[] {aArg0});
131 }
132
133 /**
134 * Helper method to log a LocalizedMessage.
135 *
136 * @param aLineNo line number to associate with the message
137 * @param aColNo column number to associate with the message
138 * @param aKey key to locale message format
139 * @param aArg0 an <code>Object</code> value
140 * @param aArg1 an <code>Object</code> value
141 */
142 protected final void log(int aLineNo, int aColNo, String aKey,
143 Object aArg0, Object aArg1)
144 {
145 log(aLineNo, aColNo, aKey, new Object[] {aArg0, aArg1});
146 }
147
148
149 /**
150 * Returns the message bundle name resourcebundle that contains the messages
151 * used by this module.
152 * <p>
153 * The default implementation expects the resource files to be named
154 * messages.properties, messages_de.properties, etc. The file must
155 * be placed in the same package as the module implementation.
156 * </p>
157 * <p>
158 * Example: If you write com/foo/MyCoolCheck, create resource files
159 * com/foo/messages.properties, com/foo/messages_de.properties, etc.
160 * </p>
161 *
162 * @return name of a resource bundle that contains the messages
163 * used by this module.
164 */
165 protected String getMessageBundle()
166 {
167 final String className = this.getClass().getName();
168 return getMessageBundle(className);
169 }
170
171 /**
172 * for unit tests, especially with a class with no package name.
173 * @param aClassName class name of the module.
174 * @return name of a resource bundle that contains the messages
175 * used by the module.
176 */
177 String getMessageBundle(final String aClassName)
178 {
179 final int endIndex = aClassName.lastIndexOf('.');
180 final String messages = "messages";
181 if (endIndex < 0) {
182 return messages;
183 }
184 final String packageName = aClassName.substring(0, endIndex);
185 return packageName + "." + messages;
186 }
187
188 /**
189 * Log a message that has no column information.
190 *
191 * @param aLine the line number where the error was found
192 * @param aKey the message that describes the error
193 * @param aArgs the details of the message
194 *
195 * @see java.text.MessageFormat
196 */
197 protected abstract void log(int aLine, String aKey, Object aArgs[]);
198
199 /**
200 * Log a message that has column information.
201 *
202 * @param aLine the line number where the error was found
203 * @param aCol the column number where the error was found
204 * @param aKey the message that describes the error
205 * @param aArgs the details of the message
206 *
207 * @see java.text.MessageFormat
208 */
209 protected abstract void log(int aLine,
210 int aCol,
211 String aKey,
212 Object[] aArgs);
213
214 }