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