001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.java.ast.visitor;
021    
022    import com.puppycrawl.tools.checkstyle.api.DetailAST;
023    import com.puppycrawl.tools.checkstyle.api.FileContents;
024    import org.sonar.api.resources.InputFile;
025    import org.sonar.squid.api.CodeVisitor;
026    import org.sonar.squid.api.SourceClass;
027    import org.sonar.squid.api.SourceCode;
028    import org.sonar.squid.api.SourcePackage;
029    import org.sonar.squid.text.Source;
030    
031    import java.util.Collections;
032    import java.util.List;
033    import java.util.Stack;
034    
035    public abstract class JavaAstVisitor implements CodeVisitor {
036    
037      private Stack<SourceCode> sourceCodeStack;
038    
039      private FileContents fileContents;
040    
041      private InputFile inputFile;
042    
043      private Source source;
044    
045      public final void setFileContents(FileContents fileContents) {
046        this.fileContents = fileContents;
047      }
048    
049      public final FileContents getFileContents() {
050        return fileContents;
051      }
052    
053      public final void setSource(Source source) {
054        this.source = source;
055      }
056    
057      public final InputFile getInputFile() {
058        return inputFile;
059      }
060    
061      public final void setInputFile(InputFile inputFile) {
062        this.inputFile = inputFile;
063      }
064    
065      final Source getSource() {
066        return source;
067      }
068    
069      public List<Integer> getWantedTokens() {
070        return Collections.emptyList();
071      }
072    
073      public final void setSourceCodeStack(Stack<SourceCode> sourceCodeStack) {
074        this.sourceCodeStack = sourceCodeStack;
075      }
076    
077      public final void addSourceCode(SourceCode child) {
078        peekSourceCode().addChild(child);
079        sourceCodeStack.add(child);
080      }
081    
082      public final void popSourceCode() {
083        sourceCodeStack.pop();
084      }
085    
086      public final SourceCode peekSourceCode() {
087        return sourceCodeStack.peek();
088      }
089    
090      public final SourcePackage peekParentPackage() {
091        if (peekSourceCode().getClass().equals(SourcePackage.class)) {
092          return (SourcePackage) peekSourceCode();
093        }
094        return peekSourceCode().getParent(SourcePackage.class);
095      }
096    
097      public final SourceClass peekParentClass() {
098        if (peekSourceCode().getClass().equals(SourceClass.class)) {
099          return (SourceClass) peekSourceCode();
100        }
101        return peekSourceCode().getParent(SourceClass.class);
102      }
103    
104      public void visitFile(DetailAST ast) {
105      }
106    
107      public void visitToken(DetailAST ast) {
108      }
109    
110      public void leaveToken(DetailAST ast) {
111      }
112    
113      public void leaveFile(DetailAST ast) {
114      }
115    }