001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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
022/**
023 * Raw event for audit.
024 * <p>
025 * <i>
026 * I'm not very satisfied about the design of this event since there are
027 * optional methods that will return null in most of the case. This will
028 * need some work to clean it up especially if we want to introduce
029 * a more sequential reporting action rather than a packet
030 * reporting. This will allow for example to follow the process quickly
031 * in an interface or a servlet (yep, that's cool to run a check via
032 * a web interface in a source repository ;-)
033 * </i>
034 * </p>
035 *
036 * @see AuditListener
037 */
038public final class AuditEvent {
039
040    /** The object on which the Event initially occurred. */
041    private final Object source;
042    /** Filename event associated with. **/
043    private final String fileName;
044    /** Violation associated with the event. **/
045    private final Violation violation;
046
047    /**
048     * Creates a new instance.
049     *
050     * @param source the object that created the event
051     */
052    public AuditEvent(Object source) {
053        this(source, null);
054    }
055
056    /**
057     * Creates a new {@code AuditEvent} instance.
058     *
059     * @param src source of the event
060     * @param fileName file associated with the event
061     */
062    public AuditEvent(Object src, String fileName) {
063        this(src, fileName, null);
064    }
065
066    /**
067     * Creates a new {@code AuditEvent} instance.
068     *
069     * @param src source of the event
070     * @param fileName file associated with the event
071     * @param violation the actual violation
072     * @throws IllegalArgumentException if {@code src} is {@code null}.
073     */
074    public AuditEvent(Object src, String fileName, Violation violation) {
075        if (src == null) {
076            throw new IllegalArgumentException("null source");
077        }
078
079        source = src;
080        this.fileName = fileName;
081        this.violation = violation;
082    }
083
084    /**
085     * The object on which the Event initially occurred.
086     *
087     * @return the object on which the Event initially occurred
088     */
089    public Object getSource() {
090        return source;
091    }
092
093    /**
094     * Returns name of file being audited.
095     *
096     * @return the file name currently being audited or null if there is
097     *     no relation to a file.
098     */
099    public String getFileName() {
100        return fileName;
101    }
102
103    /**
104     * Return the line number on the source file where the event occurred.
105     * This may be 0 if there is no relation to a file content.
106     *
107     * @return an integer representing the line number in the file source code.
108     */
109    public int getLine() {
110        return violation.getLineNo();
111    }
112
113    /**
114     * Return the violation associated to the event.
115     *
116     * @return the event violation
117     */
118    public String getMessage() {
119        return violation.getViolation();
120    }
121
122    /**
123     * Gets the column associated with the violation.
124     *
125     * @return the column associated with the violation
126     */
127    public int getColumn() {
128        return violation.getColumnNo();
129    }
130
131    /**
132     * Gets the audit event severity level.
133     *
134     * @return the audit event severity level
135     */
136    public SeverityLevel getSeverityLevel() {
137        SeverityLevel severityLevel = SeverityLevel.INFO;
138        if (violation != null) {
139            severityLevel = violation.getSeverityLevel();
140        }
141        return severityLevel;
142    }
143
144    /**
145     * Returns id of module.
146     *
147     * @return the identifier of the module that generated the event. Can return
148     *         null.
149     */
150    public String getModuleId() {
151        return violation.getModuleId();
152    }
153
154    /**
155     * Gets the name of the source for the violation.
156     *
157     * @return the name of the source for the violation
158     */
159    public String getSourceName() {
160        return violation.getSourceName();
161    }
162
163    /**
164     * Gets the violation.
165     *
166     * @return the violation
167     */
168    public Violation getViolation() {
169        return violation;
170    }
171
172}