2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package org.sonar.plugins.core.issue.ignore.scanner;
23 import org.sonar.api.batch.DependedUpon;
24 import org.sonar.api.batch.Phase;
25 import org.sonar.api.batch.Sensor;
26 import org.sonar.api.batch.SensorContext;
27 import org.sonar.api.resources.Project;
28 import org.sonar.api.scan.filesystem.FileQuery;
29 import org.sonar.api.scan.filesystem.InputFile;
30 import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
31 import org.sonar.api.utils.SonarException;
32 import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
33 import org.sonar.java.api.JavaUtils;
34 import org.sonar.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
35 import org.sonar.plugins.core.issue.ignore.pattern.InclusionPatternInitializer;
37 import java.nio.charset.Charset;
39 @Phase(name = Phase.Name.PRE)
40 // Issue Exclusions must be computed before JavaSquidSensor is executed and creates issues
41 @DependedUpon(value = JavaUtils.BARRIER_BEFORE_SQUID)
42 public final class IgnoreIssuesSourceScanner implements Sensor {
44 private final IgnoreIssuesRegexpScanner regexpScanner;
45 private final ExclusionPatternInitializer exclusionPatternInitializer;
46 private final InclusionPatternInitializer inclusionPatternInitializer;
47 private final DefaultModuleFileSystem fileSystem;
49 public IgnoreIssuesSourceScanner(IgnoreIssuesRegexpScanner regexpScanner, ExclusionPatternInitializer exclusionPatternInitializer, InclusionPatternInitializer inclusionPatternInitializer,
50 DefaultModuleFileSystem fileSystem) {
51 this.regexpScanner = regexpScanner;
52 this.exclusionPatternInitializer = exclusionPatternInitializer;
53 this.inclusionPatternInitializer = inclusionPatternInitializer;
54 this.fileSystem = fileSystem;
57 public boolean shouldExecuteOnProject(Project project) {
58 return inclusionPatternInitializer.hasConfiguredPatterns()
59 || exclusionPatternInitializer.hasConfiguredPatterns();
65 public void analyse(Project project, SensorContext context) {
66 Charset sourcesEncoding = fileSystem.sourceCharset();
68 for (InputFile inputFile : fileSystem.inputFiles(FileQuery.all())) {
70 String componentEffectiveKey = inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY);
71 if (componentEffectiveKey != null) {
72 String path = inputFile.path();
73 inclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
74 exclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
75 if (exclusionPatternInitializer.hasFileContentPattern()) {
76 regexpScanner.scan(componentEffectiveKey, inputFile.file(), sourcesEncoding);
79 } catch (Exception e) {
80 throw new SonarException("Unable to read the source file : '" + inputFile.absolutePath() + "' with the charset : '"
81 + sourcesEncoding.name() + "'.", e);
87 public String toString() {
88 return "Issues Exclusions - Source Scanner";