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.scan.filesystem.InputFile;
25 import org.sonar.api.batch.Phase;
26 import org.sonar.api.batch.Sensor;
27 import org.sonar.api.batch.SensorContext;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.scan.filesystem.FileQuery;
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.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
34 import org.sonar.plugins.core.issue.ignore.pattern.InclusionPatternInitializer;
36 import java.nio.charset.Charset;
38 @Phase(name = Phase.Name.PRE)
39 public final class SourceScanner implements Sensor {
41 private final RegexpScanner regexpScanner;
42 private final ExclusionPatternInitializer exclusionPatternInitializer;
43 private final InclusionPatternInitializer inclusionPatternInitializer;
44 private final DefaultModuleFileSystem fileSystem;
46 public SourceScanner(RegexpScanner regexpScanner, ExclusionPatternInitializer exclusionPatternInitializer, InclusionPatternInitializer inclusionPatternInitializer,
47 DefaultModuleFileSystem fileSystem) {
48 this.regexpScanner = regexpScanner;
49 this.exclusionPatternInitializer = exclusionPatternInitializer;
50 this.inclusionPatternInitializer = inclusionPatternInitializer;
51 this.fileSystem = fileSystem;
54 public boolean shouldExecuteOnProject(Project project) {
55 return inclusionPatternInitializer.hasConfiguredPatterns()
56 || exclusionPatternInitializer.hasConfiguredPatterns();
62 public void analyse(Project project, SensorContext context) {
63 Charset sourcesEncoding = fileSystem.sourceCharset();
65 for (InputFile inputFile : fileSystem.inputFiles(FileQuery.all())) {
67 String componentEffectiveKey = inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY);
68 if (componentEffectiveKey != null) {
69 String path = inputFile.path();
70 inclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
71 exclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
72 if (exclusionPatternInitializer.hasFileContentPattern()) {
73 regexpScanner.scan(componentEffectiveKey, inputFile.file(), sourcesEncoding);
76 } catch (Exception e) {
77 throw new SonarException("Unable to read the source file : '" + inputFile.absolutePath() + "' with the charset : '"
78 + sourcesEncoding.name() + "'.", e);
84 public String toString() {
85 return "Issues Exclusions - Source Scanner";