001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019: package com.puppycrawl.tools.checkstyle.api;
020:
021: /**
022: * Serves as an abstract base class for all modules that report inspection
023: * findings. Such modules have a Severity level which is used for the
024: * {@link LocalizedMessage localized messages} that are created by the module.
025: *
026: * @author lkuehne
027: */
028: public abstract class AbstractViolationReporter extends AutomaticBean {
029: /** resuable constant for message formating */
030: private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
031:
032: /** the severity level of any violations found */
033: private SeverityLevel mSeverityLevel = SeverityLevel.ERROR;
034:
035: /** the identifier of the reporter */
036: private String mId;
037:
038: /**
039: * Returns the severity level of the messages generated by this module.
040: * @return the severity level
041: * @see SeverityLevel
042: * @see LocalizedMessage#getSeverityLevel
043: */
044: public final SeverityLevel getSeverityLevel() {
045: return mSeverityLevel;
046: }
047:
048: /**
049: * Sets the severity level. The string should be one of the names
050: * defined in the <code>SeverityLevel</code> class.
051: *
052: * @param aSeverity The new severity level
053: * @see SeverityLevel
054: */
055: public final void setSeverity(String aSeverity) {
056: mSeverityLevel = SeverityLevel.getInstance(aSeverity);
057: }
058:
059: /**
060: * Get the severity level's name.
061: *
062: * @return the check's severity level name.
063: */
064: public final String getSeverity() {
065: return mSeverityLevel.getName();
066: }
067:
068: /**
069: * Returns the identifier of the reporter. Can be null.
070: * @return the id
071: */
072: public final String getId() {
073: return mId;
074: }
075:
076: /**
077: * Sets the identifer of the reporter. Can be null.
078: * @param aId the id
079: */
080: public final void setId(final String aId) {
081: mId = aId;
082: }
083:
084: /**
085: * Log a message.
086: *
087: * @param aLine the line number where the error was found
088: * @param aKey the message that describes the error
089: */
090: protected final void log(int aLine, String aKey) {
091: log(aLine, aKey, EMPTY_OBJECT_ARRAY);
092: }
093:
094: /**
095: * Helper method to log a LocalizedMessage. Column defaults to 0.
096: *
097: * @param aLineNo line number to associate with the message
098: * @param aKey key to locale message format
099: * @param aArg0 first argument
100: */
101: protected final void log(int aLineNo, String aKey, Object aArg0) {
102: log(aLineNo, aKey, new Object[] { aArg0 });
103: }
104:
105: /**
106: * Helper method to log a LocalizedMessage. Column defaults to 0.
107: *
108: * @param aLineNo line number to associate with the message
109: * @param aKey key to locale message format
110: * @param aArg0 first argument
111: * @param aArg1 second argument
112: */
113: protected final void log(int aLineNo, String aKey, Object aArg0,
114: Object aArg1) {
115: log(aLineNo, aKey, new Object[] { aArg0, aArg1 });
116: }
117:
118: /**
119: * Helper method to log a LocalizedMessage.
120: *
121: * @param aLineNo line number to associate with the message
122: * @param aColNo column number to associate with the message
123: * @param aKey key to locale message format
124: */
125: protected final void log(int aLineNo, int aColNo, String aKey) {
126: log(aLineNo, aColNo, aKey, EMPTY_OBJECT_ARRAY);
127: }
128:
129: /**
130: * Helper method to log a LocalizedMessage.
131: *
132: * @param aAST a node to get line and column numbers associated
133: * with the message
134: * @param aKey key to locale message format
135: */
136: protected final void log(DetailAST aAST, String aKey) {
137: log(aAST.getLineNo(), aAST.getColumnNo(), aKey);
138: }
139:
140: /**
141: * Helper method to log a LocalizedMessage.
142: *
143: * @param aLineNo line number to associate with the message
144: * @param aColNo column number to associate with the message
145: * @param aKey key to locale message format
146: * @param aArg0 an <code>Object</code> value
147: */
148: protected final void log(int aLineNo, int aColNo, String aKey,
149: Object aArg0) {
150: log(aLineNo, aColNo, aKey, new Object[] { aArg0 });
151: }
152:
153: /**
154: * Helper method to log a LocalizedMessage.
155: *
156: * @param aAST a node to get line and column numbers associated
157: * with the message
158: * @param aKey key to locale message format
159: * @param aArg0 an <code>Object</code> value
160: */
161: protected final void log(DetailAST aAST, String aKey, Object aArg0) {
162: log(aAST.getLineNo(), aAST.getColumnNo(), aKey, aArg0);
163: }
164:
165: /**
166: * Helper method to log a LocalizedMessage.
167: *
168: * @param aLineNo line number to associate with the message
169: * @param aColNo column number to associate with the message
170: * @param aKey key to locale message format
171: * @param aArg0 an <code>Object</code> value
172: * @param aArg1 an <code>Object</code> value
173: */
174: protected final void log(int aLineNo, int aColNo, String aKey,
175: Object aArg0, Object aArg1) {
176: log(aLineNo, aColNo, aKey, new Object[] { aArg0, aArg1 });
177: }
178:
179: /**
180: * Helper method to log a LocalizedMessage.
181: *
182: * @param aAST a node to get line and column numbers associated
183: * with the message
184: * @param aKey key to locale message format
185: * @param aArg0 an <code>Object</code> value
186: * @param aArg1 an <code>Object</code> value
187: */
188: protected final void log(DetailAST aAST, String aKey, Object aArg0,
189: Object aArg1) {
190: log(aAST.getLineNo(), aAST.getColumnNo(), aKey, aArg0, aArg1);
191: }
192:
193: /**
194: * Returns the message bundle name resourcebundle that contains the messages
195: * used by this module.
196: * <p>
197: * The default implementation expects the resource files to be named
198: * messages.properties, messages_de.properties, etc. The file must
199: * be placed in the same package as the module implementation.
200: * </p>
201: * <p>
202: * Example: If you write com/foo/MyCoolCheck, create resource files
203: * com/foo/messages.properties, com/foo/messages_de.properties, etc.
204: * </p>
205: *
206: * @return name of a resource bundle that contains the messages
207: * used by this module.
208: */
209: protected String getMessageBundle() {
210: final String className = this .getClass().getName();
211: return getMessageBundle(className);
212: }
213:
214: /**
215: * for unit tests, especially with a class with no package name.
216: * @param aClassName class name of the module.
217: * @return name of a resource bundle that contains the messages
218: * used by the module.
219: */
220: String getMessageBundle(final String aClassName) {
221: final int endIndex = aClassName.lastIndexOf('.');
222: final String messages = "messages";
223: if (endIndex < 0) {
224: return messages;
225: }
226: final String packageName = aClassName.substring(0, endIndex);
227: return packageName + "." + messages;
228: }
229:
230: /**
231: * Log a message that has no column information.
232: *
233: * @param aLine the line number where the error was found
234: * @param aKey the message that describes the error
235: * @param aArgs the details of the message
236: *
237: * @see java.text.MessageFormat
238: */
239: protected abstract void log(int aLine, String aKey, Object aArgs[]);
240:
241: /**
242: * Log a message that has column information.
243: *
244: * @param aLine the line number where the error was found
245: * @param aCol the column number where the error was found
246: * @param aKey the message that describes the error
247: * @param aArgs the details of the message
248: *
249: * @see java.text.MessageFormat
250: */
251: protected abstract void log(int aLine, int aCol, String aKey,
252: Object[] aArgs);
253:
254: }
|