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.plugins.squid.bridges;
021    
022    import org.sonar.api.checks.NoSonarFilter;
023    import org.sonar.api.resources.Resource;
024    import org.sonar.java.squid.SquidVisitorNotifier;
025    import org.sonar.java.squid.visitor.SquidVisitor;
026    import org.sonar.squid.api.SourceClass;
027    import org.sonar.squid.api.SourceCode;
028    import org.sonar.squid.api.SourceFile;
029    import org.sonar.squid.api.SourceMethod;
030    
031    import java.util.HashSet;
032    import java.util.Set;
033    
034    public class NoSonarFilterLoader extends Bridge {
035      private NoSonarFilter noSonarFilter;
036    
037      protected NoSonarFilterLoader(NoSonarFilter noSonarFilter) {
038        super(false);
039        this.noSonarFilter = noSonarFilter;
040      }
041    
042      @Override
043      public void onFile(SourceFile squidFile, Resource sonarFile) {
044        if (noSonarFilter != null) {
045          Set<Integer> ignoredLines = new HashSet<Integer>(squidFile.getNoSonarTagLines());
046          new SquidVisitorNotifier(squidFile, new SuppressWarningsVisitor(ignoredLines)).notifyVisitors();
047    
048          noSonarFilter.addResource(sonarFile, ignoredLines);
049        }
050      }
051    
052      private static class SuppressWarningsVisitor implements SquidVisitor {
053        private Set<Integer> ignoredLines;
054    
055        public SuppressWarningsVisitor(Set<Integer> ignoredLines) {
056          this.ignoredLines = ignoredLines;
057        }
058    
059        public void visitFile(SourceFile sourceFile) {
060        }
061    
062        public void visitClass(SourceClass sourceClass) {
063          if (sourceClass.isSuppressWarnings()) {
064            visitLines(sourceClass);
065          }
066        }
067    
068        public void visitMethod(SourceMethod sourceMethod) {
069          if (sourceMethod.isSuppressWarnings()) {
070            visitLines(sourceMethod);
071          }
072        }
073    
074        private void visitLines(SourceCode sourceCode) {
075          for (int line = sourceCode.getStartAtLine(); line <= sourceCode.getEndAtLine(); line++) {
076            ignoredLines.add(line);
077          }
078        }
079      }
080    
081    }