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