1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2015 the original author or authors.
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 import java.util.Map;
22
23 /**
24 * Serves as an abstract base class for all modules that report inspection
25 * findings. Such modules have a Severity level which is used for the
26 * {@link LocalizedMessage localized messages} that are created by the module.
27 *
28 * @author lkuehne
29 */
30 public abstract class AbstractViolationReporter
31 extends AutomaticBean
32 {
33 /** the severity level of any violations found */
34 private SeverityLevel severityLevel = SeverityLevel.ERROR;
35
36 /** the identifier of the reporter */
37 private String id;
38
39 /**
40 * Returns the severity level of the messages generated by this module.
41 * @return the severity level
42 * @see SeverityLevel
43 * @see LocalizedMessage#getSeverityLevel
44 */
45 public final SeverityLevel getSeverityLevel()
46 {
47 return severityLevel;
48 }
49
50 /**
51 * Sets the severity level. The string should be one of the names
52 * defined in the <code>SeverityLevel</code> class.
53 *
54 * @param severity The new severity level
55 * @see SeverityLevel
56 */
57 public final void setSeverity(String severity)
58 {
59 severityLevel = SeverityLevel.getInstance(severity);
60 }
61
62 /**
63 * Get the severity level's name.
64 *
65 * @return the check's severity level name.
66 */
67 public final String getSeverity()
68 {
69 return severityLevel.getName();
70 }
71
72 /**
73 * Returns the identifier of the reporter. Can be null.
74 * @return the id
75 */
76 public final String getId()
77 {
78 return id;
79 }
80
81 /**
82 * Sets the identifier of the reporter. Can be null.
83 * @param id the id
84 */
85 public final void setId(final String id)
86 {
87 this.id = id;
88 }
89
90 /**
91 * Helper method to log a LocalizedMessage.
92 *
93 * @param ast a node to get line id column numbers associated
94 * with the message
95 * @param key key to locale message format
96 * @param args arguments to format
97 */
98 protected final void log(DetailAST ast, String key, Object... args)
99 {
100 log(ast.getLineNo(), ast.getColumnNo(), key, args);
101 }
102
103 /**
104 * Returns the message bundle name resourcebundle that contains the messages
105 * used by this module.
106 * <p>
107 * The default implementation expects the resource files to be named
108 * messages.properties, messages_de.properties, etc. The file must
109 * be placed in the same package as the module implementation.
110 * </p>
111 * <p>
112 * Example: If you write com/foo/MyCoolCheck, create resource files
113 * com/foo/messages.properties, com/foo/messages_de.properties, etc.
114 * </p>
115 *
116 * @return name of a resource bundle that contains the messages
117 * used by this module.
118 */
119 protected String getMessageBundle()
120 {
121 final String className = this.getClass().getName();
122 return getMessageBundle(className);
123 }
124
125 /**
126 * Returns an unmodifiable map instance containing the custom messages
127 * for this configuration.
128 * @return unmodifiable map containing custom messages
129 */
130 protected Map<String, String> getCustomMessages()
131 {
132 return getConfiguration().getMessages();
133 }
134
135 /**
136 * for unit tests, especially with a class with no package name.
137 * @param className class name of the module.
138 * @return name of a resource bundle that contains the messages
139 * used by the module.
140 */
141 String getMessageBundle(final String className)
142 {
143 final int endIndex = className.lastIndexOf('.');
144 final String messages = "messages";
145 if (endIndex < 0) {
146 return messages;
147 }
148 final String packageName = className.substring(0, endIndex);
149 return packageName + "." + messages;
150 }
151
152 /**
153 * Log a message that has no column information.
154 *
155 * @param line the line number where the error was found
156 * @param key the message that describes the error
157 * @param args the details of the message
158 *
159 * @see java.text.MessageFormat
160 */
161 public abstract void log(int line, String key, Object... args);
162
163 /**
164 * Log a message that has column information.
165 *
166 * @param line the line number where the error was found
167 * @param col the column number where the error was found
168 * @param key the message that describes the error
169 * @param args the details of the message
170 *
171 * @see java.text.MessageFormat
172 */
173 public abstract void log(int line, int col, String key,
174 Object... args);
175 }