001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.FileContents;
024import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
025
026/**
027 * Raw {@code TreeWalker} event for audit.
028 *
029 * @author Timur Tibeyev
030 */
031public class TreeWalkerAuditEvent {
032
033    /** Filename event associated with. **/
034    private final String fileName;
035    /** The file contents. */
036    private final FileContents fileContents;
037    /** Message associated with the event. **/
038    private final LocalizedMessage localizedMessage;
039    /** Root ast element. **/
040    private final DetailAST rootAst;
041
042    /**
043     * Creates a new {@code TreeWalkerAuditEvent} instance.
044     *
045     * @param fileContents contents of the file associated with the event
046     * @param fileName file associated with the event
047     * @param localizedMessage the actual message
048     * @param rootAst root AST element {@link DetailAST} of the file
049     */
050    public TreeWalkerAuditEvent(FileContents fileContents, String fileName,
051                                LocalizedMessage localizedMessage, DetailAST rootAst) {
052        this.fileContents = fileContents;
053        this.fileName = fileName;
054        this.localizedMessage = localizedMessage;
055        this.rootAst = rootAst;
056    }
057
058    /**
059     * Returns name of file being audited.
060     * @return the file name currently being audited or null if there is
061     *     no relation to a file.
062     */
063    public String getFileName() {
064        return fileName;
065    }
066
067    /**
068     * Returns contents of the file.
069     * @return contents of the file.
070     */
071    public FileContents getFileContents() {
072        return fileContents;
073    }
074
075    /**
076     * Gets the localized message.
077     * @return the localized message
078     */
079    public LocalizedMessage getLocalizedMessage() {
080        return localizedMessage;
081    }
082
083    /**
084     * Return the line number on the source file where the event occurred.
085     * This may be 0 if there is no relation to a file content.
086     * @return an integer representing the line number in the file source code.
087     */
088    public int getLine() {
089        return localizedMessage.getLineNo();
090    }
091
092    /**
093     * Return the message associated to the event.
094     * @return the event message
095     */
096    public String getMessage() {
097        return localizedMessage.getMessage();
098    }
099
100    /**
101     * Gets the column associated with the message.
102     * @return the column associated with the message
103     */
104    public int getColumn() {
105        return localizedMessage.getColumnNo();
106    }
107
108    /**
109     * Gets the column char index associated with the message.
110     * @return the column char index associated with the message
111     */
112    public int getColumnCharIndex() {
113        return localizedMessage.getColumnCharIndex();
114    }
115
116    /**
117     * Returns id of module.
118     * @return the identifier of the module that generated the event. Can return
119     *         null.
120     */
121    public String getModuleId() {
122        return localizedMessage.getModuleId();
123    }
124
125    /**
126     * Gets the name of the source for the message.
127     * @return the name of the source for the message
128     */
129    public String getSourceName() {
130        return localizedMessage.getSourceName();
131    }
132
133    /**
134     * Gets the token type of the message.
135     * @return the token type of the message
136     */
137    public int getTokenType() {
138        return localizedMessage.getTokenType();
139    }
140
141    /**
142     * Gets the root element of the AST tree.
143     * @return the root element of the AST tree
144     */
145    public DetailAST getRootAst() {
146        return rootAst;
147    }
148
149}