001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.api;
021
022import java.util.EventObject;
023
024/**
025 * Raw event for audit.
026 * <p>
027 * <i>
028 * I'm not very satisfied about the design of this event since there are
029 * optional methods that will return null in most of the case. This will
030 * need some work to clean it up especially if we want to introduce
031 * a more sequential reporting action rather than a packet error
032 * reporting. This will allow for example to follow the process quickly
033 * in an interface or a servlet (yep, that's cool to run a check via
034 * a web interface in a source repository ;-)
035 * </i>
036 * </p>
037 *
038 * @author <a href="mailto:stephane.bailliez@wanadoo.fr">Stephane Bailliez</a>
039 * @see AuditListener
040 */
041public final class AuditEvent
042    extends EventObject {
043    /** Record a version. */
044    private static final long serialVersionUID = -3774725606973812736L;
045    /** Filename event associated with. **/
046    private final String fileName;
047    /** Message associated with the event. **/
048    private final LocalizedMessage localizedMessage;
049
050    /**
051     * Creates a new instance.
052     * @param source the object that created the event
053     */
054    public AuditEvent(Object source) {
055        this(source, null);
056    }
057
058    /**
059     * Creates a new {@code AuditEvent} instance.
060     * @param src source of the event
061     * @param fileName file associated with the event
062     */
063    public AuditEvent(Object src, String fileName) {
064        this(src, fileName, null);
065    }
066
067    /**
068     * Creates a new {@code AuditEvent} instance.
069     *
070     * @param src source of the event
071     * @param fileName file associated with the event
072     * @param localizedMessage the actual message
073     */
074    public AuditEvent(Object src, String fileName, LocalizedMessage localizedMessage) {
075        super(src);
076        this.fileName = fileName;
077        this.localizedMessage = localizedMessage;
078    }
079
080    /**
081     * Returns name of file being audited.
082     * @return the file name currently being audited or null if there is
083     *     no relation to a file.
084     */
085    public String getFileName() {
086        return fileName;
087    }
088
089    /**
090     * Return the line number on the source file where the event occurred.
091     * This may be 0 if there is no relation to a file content.
092     * @return an integer representing the line number in the file source code.
093     */
094    public int getLine() {
095        return localizedMessage.getLineNo();
096    }
097
098    /**
099     * Return the message associated to the event.
100     * @return the event message
101     */
102    public String getMessage() {
103        return localizedMessage.getMessage();
104    }
105
106    /**
107     * Gets the column associated with the message.
108     * @return the column associated with the message
109     */
110    public int getColumn() {
111        return localizedMessage.getColumnNo();
112    }
113
114    /**
115     * Gets the audit event severity level.
116     * @return the audit event severity level
117     */
118    public SeverityLevel getSeverityLevel() {
119        SeverityLevel severityLevel = SeverityLevel.INFO;
120        if (localizedMessage != null) {
121            severityLevel = localizedMessage.getSeverityLevel();
122        }
123        return severityLevel;
124    }
125
126    /**
127     * Returns id of module.
128     * @return the identifier of the module that generated the event. Can return
129     *         null.
130     */
131    public String getModuleId() {
132        return localizedMessage.getModuleId();
133    }
134
135    /**
136     * Gets the name of the source for the message.
137     * @return the name of the source for the message
138     */
139    public String getSourceName() {
140        return localizedMessage.getSourceName();
141    }
142
143    /**
144     * Gets the localized message.
145     * @return the localized message
146     */
147    public LocalizedMessage getLocalizedMessage() {
148        return localizedMessage;
149    }
150}