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.Arrays;
023    import java.util.List;
024    
025    import org.sonar.squid.api.SourceFile;
026    import org.sonar.squid.measures.Metric;
027    
028    import com.puppycrawl.tools.checkstyle.api.DetailAST;
029    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030    
031    public class CommentVisitor extends AstVisitor {
032    
033      private static final List<Integer> wantedTokens = Arrays.asList(TokenTypes.RCURLY);
034    
035      @Override
036      public List<Integer> getWantedTokens() {
037        return wantedTokens;
038      }
039    
040      @Override
041      public void visitToken(DetailAST ast) {
042        int startAtLine = peekResource().getStartAtLine();
043        int endAtLine = peekResource().getEndAtLine();
044        peekResource().setMeasure(Metric.COMMENTED_OUT_CODE_LINES,
045            getSource().getMeasure(Metric.COMMENTED_OUT_CODE_LINES, startAtLine, endAtLine));
046        peekResource().setMeasure(Metric.COMMENT_LINES, getSource().getMeasure(Metric.COMMENT_LINES, startAtLine, endAtLine));
047        peekResource().setMeasure(Metric.COMMENT_BLANK_LINES, getSource().getMeasure(Metric.COMMENT_BLANK_LINES, startAtLine, endAtLine));
048      }
049    
050      @Override
051      public void visitFile(DetailAST ast) {
052    
053        SourceFile file = (SourceFile) peekResource();
054        file.addNoSonarTagLines(getSource().getNoSonarTagLines());
055        file.setMeasure(Metric.HEADER_COMMENT_LINES, getSource().getMeasure(Metric.HEADER_COMMENT_LINES));
056        file.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, getSource().getMeasure(Metric.COMMENTED_OUT_CODE_LINES));
057        file.setMeasure(Metric.COMMENT_LINES, getSource().getMeasure(Metric.COMMENT_LINES));
058        file.setMeasure(Metric.COMMENT_BLANK_LINES, getSource().getMeasure(Metric.COMMENT_BLANK_LINES));
059      }
060    
061    }