001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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 java.util.ArrayList;
023    import java.util.List;
024    import java.util.Stack;
025    
026    import org.sonar.squid.api.CodeVisitor;
027    import org.sonar.squid.api.SourceClass;
028    import org.sonar.squid.api.SourceCode;
029    import org.sonar.squid.api.SourcePackage;
030    import org.sonar.squid.text.Source;
031    
032    import com.puppycrawl.tools.checkstyle.api.DetailAST;
033    import com.puppycrawl.tools.checkstyle.api.FileContents;
034    
035    public abstract class AstVisitor implements CodeVisitor {
036    
037      private Stack<SourceCode>          resourcesStack;
038    
039      private FileContents               fileContents;
040    
041      private Source                     source;
042    
043      private static final List<Integer> emptyWantedTokens = new ArrayList<Integer>();
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      final Source getSource() {
058        return source;
059      }
060    
061      public List<Integer> getWantedTokens() {
062        return emptyWantedTokens;
063      }
064    
065      public final void setResourcesStack(Stack<SourceCode> resourcesStack) {
066        this.resourcesStack = resourcesStack;
067      }
068    
069      public final void addResource(SourceCode child) {
070        peekResource().addChild(child);
071        resourcesStack.add(child);
072      }
073    
074      public final void popResource() {
075        resourcesStack.pop();
076      }
077    
078      public final SourceCode peekResource() {
079        return resourcesStack.peek();
080      }
081    
082      public final SourcePackage peekParentPackage() {
083        if (peekResource().getClass().equals(SourcePackage.class)) {
084          return (SourcePackage) peekResource();
085        }
086        return (SourcePackage) peekResource().getParent(SourcePackage.class);
087      }
088    
089      public final SourceClass peekParentClass() {
090        if (peekResource().getClass().equals(SourceClass.class)) {
091          return (SourceClass) peekResource();
092        }
093        return (SourceClass) peekResource().getParent(SourceClass.class);
094      }
095    
096      public void visitFile(DetailAST ast) {
097      }
098    
099      public void visitToken(DetailAST ast) {
100      }
101    
102      public void leaveToken(DetailAST ast) {
103      }
104    
105      public void leaveFile(DetailAST ast) {
106      }
107    
108      @Override
109      public int hashCode() {
110        return this.getClass().hashCode();
111      }
112    
113      @Override
114      public boolean equals(Object obj) {
115        return this.getClass().equals(obj.getClass());
116      }
117    }