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