001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.api.batch;
021
022 import com.google.common.base.CharMatcher;
023 import com.google.common.io.Files;
024 import org.sonar.api.CoreProperties;
025 import org.sonar.api.resources.Language;
026 import org.sonar.api.resources.Project;
027 import org.sonar.api.resources.ProjectFileSystem;
028 import org.sonar.api.resources.Qualifiers;
029 import org.sonar.api.resources.Resource;
030 import org.sonar.api.utils.SonarException;
031
032 import java.io.File;
033 import java.nio.charset.Charset;
034 import java.util.List;
035
036 /**
037 * A pre-implementation for a sensor that imports sources.
038 * It became too much ugly because of extensability. Methods can't be
039 * refactored because they are heavily overridden in plugins.
040 *
041 * @since 1.10
042 */
043 @Phase(name = Phase.Name.PRE)
044 public abstract class AbstractSourceImporter implements Sensor {
045
046 private Language language;
047 private boolean enabled = false;
048
049 public AbstractSourceImporter(Language language) {
050 this.language = language;
051 }
052
053 /**
054 * Generally this method should not be overridden in subclasses, but if it is, then it should be executed anyway (see SONAR-3419).
055 */
056 public boolean shouldExecuteOnProject(Project project) {
057 enabled = isEnabled(project);
058 return language.equals(project.getLanguage());
059 }
060
061 /**
062 * {@inheritDoc}
063 */
064 public void analyse(Project project, SensorContext context) {
065 analyse(project.getFileSystem(), context);
066 onFinished();
067 }
068
069 protected void onFinished() {
070
071 }
072
073 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) {
074 parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset());
075 parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset());
076 }
077
078 protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) {
079 for (File file : files) {
080 Resource resource = createResource(file, sourceDirs, unitTest);
081 if (resource != null) {
082 try {
083 context.index(resource);
084 if (enabled) {
085 String source = Files.toString(file, Charset.forName(sourcesEncoding.name()));
086 // SONAR-3860 Remove BOM character from source
087 source = CharMatcher.anyOf("\uFEFF").removeFrom(source);
088 context.saveSource(resource, source);
089 }
090 } catch (Exception e) {
091 throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
092 + sourcesEncoding.name() + "'.", e);
093 }
094 }
095 }
096 }
097
098 protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) {
099 org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs);
100 if (resource != null) {
101 resource.setLanguage(language);
102 if (unitTest) {
103 resource.setQualifier(Qualifiers.UNIT_TEST_FILE);
104 }
105 }
106 return resource;
107 }
108
109 protected boolean isEnabled(Project project) {
110 return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
111 CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
112 }
113
114 /**
115 * @return the language
116 */
117 public Language getLanguage() {
118 return language;
119 }
120 }