1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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
20 package com.puppycrawl.tools.checkstyle.api;
21
22 /**
23 * Raw event for audit.
24 * <p>
25 * <i>
26 * I'm not very satisfied about the design of this event since there are
27 * optional methods that will return null in most of the case. This will
28 * need some work to clean it up especially if we want to introduce
29 * a more sequential reporting action rather than a packet
30 * reporting. This will allow for example to follow the process quickly
31 * in an interface or a servlet (yep, that's cool to run a check via
32 * a web interface in a source repository ;-)
33 * </i>
34 * </p>
35 *
36 * @see AuditListener
37 */
38 public final class AuditEvent {
39
40 /** The object on which the Event initially occurred. */
41 private final Object source;
42 /** Filename event associated with. **/
43 private final String fileName;
44 /** Violation associated with the event. **/
45 private final Violation violation;
46
47 /**
48 * Creates a new instance.
49 *
50 * @param source the object that created the event
51 */
52 public AuditEvent(Object source) {
53 this(source, null);
54 }
55
56 /**
57 * Creates a new {@code AuditEvent} instance.
58 *
59 * @param src source of the event
60 * @param fileName file associated with the event
61 */
62 public AuditEvent(Object src, String fileName) {
63 this(src, fileName, null);
64 }
65
66 /**
67 * Creates a new {@code AuditEvent} instance.
68 *
69 * @param src source of the event
70 * @param fileName file associated with the event
71 * @param violation the actual violation
72 * @throws IllegalArgumentException if {@code src} is {@code null}.
73 */
74 public AuditEvent(Object src, String fileName, Violation violation) {
75 if (src == null) {
76 throw new IllegalArgumentException("null source");
77 }
78
79 source = src;
80 this.fileName = fileName;
81 this.violation = violation;
82 }
83
84 /**
85 * The object on which the Event initially occurred.
86 *
87 * @return the object on which the Event initially occurred
88 */
89 public Object getSource() {
90 return source;
91 }
92
93 /**
94 * Returns name of file being audited.
95 *
96 * @return the file name currently being audited or null if there is
97 * no relation to a file.
98 */
99 public String getFileName() {
100 return fileName;
101 }
102
103 /**
104 * Return the line number on the source file where the event occurred.
105 * This may be 0 if there is no relation to a file content.
106 *
107 * @return an integer representing the line number in the file source code.
108 */
109 public int getLine() {
110 return violation.getLineNo();
111 }
112
113 /**
114 * Return the violation associated to the event.
115 *
116 * @return the event violation
117 */
118 public String getMessage() {
119 return violation.getViolation();
120 }
121
122 /**
123 * Gets the column associated with the violation.
124 *
125 * @return the column associated with the violation
126 */
127 public int getColumn() {
128 return violation.getColumnNo();
129 }
130
131 /**
132 * Gets the audit event severity level.
133 *
134 * @return the audit event severity level
135 */
136 public SeverityLevel getSeverityLevel() {
137 SeverityLevel severityLevel = SeverityLevel.INFO;
138 if (violation != null) {
139 severityLevel = violation.getSeverityLevel();
140 }
141 return severityLevel;
142 }
143
144 /**
145 * Returns id of module.
146 *
147 * @return the identifier of the module that generated the event. Can return
148 * null.
149 */
150 public String getModuleId() {
151 return violation.getModuleId();
152 }
153
154 /**
155 * Gets the name of the source for the violation.
156 *
157 * @return the name of the source for the violation
158 */
159 public String getSourceName() {
160 return violation.getSourceName();
161 }
162
163 /**
164 * Gets the violation.
165 *
166 * @return the violation
167 */
168 public Violation getViolation() {
169 return violation;
170 }
171
172 }