001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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
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 * @see AuditListener
039 */
040public final class AuditEvent
041    extends EventObject {
042
043    /** Record a version. */
044    private static final long serialVersionUID = -3774725606973812736L;
045    /** Filename event associated with. **/
046    private final String fileName;
047    /** Violation associated with the event. **/
048    private final Violation violation;
049
050    /**
051     * Creates a new instance.
052     *
053     * @param source the object that created the event
054     */
055    public AuditEvent(Object source) {
056        this(source, null);
057    }
058
059    /**
060     * Creates a new {@code AuditEvent} instance.
061     *
062     * @param src source of the event
063     * @param fileName file associated with the event
064     */
065    public AuditEvent(Object src, String fileName) {
066        this(src, fileName, null);
067    }
068
069    /**
070     * Creates a new {@code AuditEvent} instance.
071     *
072     * @param src source of the event
073     * @param fileName file associated with the event
074     * @param violation the actual violation
075     */
076    public AuditEvent(Object src, String fileName, Violation violation) {
077        super(src);
078        this.fileName = fileName;
079        this.violation = violation;
080    }
081
082    /**
083     * Returns name of file being audited.
084     *
085     * @return the file name currently being audited or null if there is
086     *     no relation to a file.
087     */
088    public String getFileName() {
089        return fileName;
090    }
091
092    /**
093     * Return the line number on the source file where the event occurred.
094     * This may be 0 if there is no relation to a file content.
095     *
096     * @return an integer representing the line number in the file source code.
097     */
098    public int getLine() {
099        return violation.getLineNo();
100    }
101
102    /**
103     * Return the violation associated to the event.
104     *
105     * @return the event violation
106     */
107    public String getMessage() {
108        return violation.getViolation();
109    }
110
111    /**
112     * Gets the column associated with the violation.
113     *
114     * @return the column associated with the violation
115     */
116    public int getColumn() {
117        return violation.getColumnNo();
118    }
119
120    /**
121     * Gets the audit event severity level.
122     *
123     * @return the audit event severity level
124     */
125    public SeverityLevel getSeverityLevel() {
126        SeverityLevel severityLevel = SeverityLevel.INFO;
127        if (violation != null) {
128            severityLevel = violation.getSeverityLevel();
129        }
130        return severityLevel;
131    }
132
133    /**
134     * Returns id of module.
135     *
136     * @return the identifier of the module that generated the event. Can return
137     *         null.
138     */
139    public String getModuleId() {
140        return violation.getModuleId();
141    }
142
143    /**
144     * Gets the name of the source for the violation.
145     *
146     * @return the name of the source for the violation
147     */
148    public String getSourceName() {
149        return violation.getSourceName();
150    }
151
152    /**
153     * Gets the violation.
154     *
155     * @return the violation
156     */
157    public Violation getViolation() {
158        return violation;
159    }
160
161}