View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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   * @author <a href="mailto:stephane.bailliez@wanadoo.fr">Stephane Bailliez</a>
39   * @see AuditListener
40   * @noinspection SerializableHasSerializationMethods
41   */
42  public final class AuditEvent
43      extends EventObject {
44  
45      /** Record a version. */
46      private static final long serialVersionUID = -3774725606973812736L;
47      /** Filename event associated with. **/
48      private final String fileName;
49      /** Message associated with the event. **/
50      private final LocalizedMessage localizedMessage;
51  
52      /**
53       * Creates a new instance.
54       * @param source the object that created the event
55       */
56      public AuditEvent(Object source) {
57          this(source, null);
58      }
59  
60      /**
61       * Creates a new {@code AuditEvent} instance.
62       * @param src source of the event
63       * @param fileName file associated with the event
64       */
65      public AuditEvent(Object src, String fileName) {
66          this(src, fileName, null);
67      }
68  
69      /**
70       * Creates a new {@code AuditEvent} instance.
71       *
72       * @param src source of the event
73       * @param fileName file associated with the event
74       * @param localizedMessage the actual message
75       */
76      public AuditEvent(Object src, String fileName, LocalizedMessage localizedMessage) {
77          super(src);
78          this.fileName = fileName;
79          this.localizedMessage = localizedMessage;
80      }
81  
82      /**
83       * Returns name of file being audited.
84       * @return the file name currently being audited or null if there is
85       *     no relation to a file.
86       */
87      public String getFileName() {
88          return fileName;
89      }
90  
91      /**
92       * Return the line number on the source file where the event occurred.
93       * This may be 0 if there is no relation to a file content.
94       * @return an integer representing the line number in the file source code.
95       */
96      public int getLine() {
97          return localizedMessage.getLineNo();
98      }
99  
100     /**
101      * Return the message associated to the event.
102      * @return the event message
103      */
104     public String getMessage() {
105         return localizedMessage.getMessage();
106     }
107 
108     /**
109      * Gets the column associated with the message.
110      * @return the column associated with the message
111      */
112     public int getColumn() {
113         return localizedMessage.getColumnNo();
114     }
115 
116     /**
117      * Gets the audit event severity level.
118      * @return the audit event severity level
119      */
120     public SeverityLevel getSeverityLevel() {
121         SeverityLevel severityLevel = SeverityLevel.INFO;
122         if (localizedMessage != null) {
123             severityLevel = localizedMessage.getSeverityLevel();
124         }
125         return severityLevel;
126     }
127 
128     /**
129      * Returns id of module.
130      * @return the identifier of the module that generated the event. Can return
131      *         null.
132      */
133     public String getModuleId() {
134         return localizedMessage.getModuleId();
135     }
136 
137     /**
138      * Gets the name of the source for the message.
139      * @return the name of the source for the message
140      */
141     public String getSourceName() {
142         return localizedMessage.getSourceName();
143     }
144 
145     /**
146      * Gets the localized message.
147      * @return the localized message
148      */
149     public LocalizedMessage getLocalizedMessage() {
150         return localizedMessage;
151     }
152 
153 }