001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 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.api.batch;
021
022 import org.apache.commons.io.FileUtils;
023 import org.sonar.api.CoreProperties;
024 import org.sonar.api.resources.*;
025 import org.sonar.api.utils.SonarException;
026
027 import java.io.File;
028 import java.io.IOException;
029 import java.nio.charset.Charset;
030 import java.util.List;
031
032 /**
033 * A pre-implementation for a sensor that imports sources
034 *
035 * @since 1.10
036 */
037 @Phase(name = Phase.Name.PRE)
038 public abstract class AbstractSourceImporter implements Sensor {
039
040 /**
041 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_PROPERTY since 1.11
042 */
043 @Deprecated
044 public static final String KEY_IMPORT_SOURCES = "sonar.importSources";
045
046 /**
047 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE since 1.11
048 */
049 @Deprecated
050 public static final boolean DEFAULT_IMPORT_SOURCES = true;
051
052 private Language language;
053
054 public AbstractSourceImporter(Language language) {
055 this.language = language;
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 public boolean shouldExecuteOnProject(Project project) {
062 return isEnabled(project) && language.equals(project.getLanguage());
063 }
064
065 /**
066 * {@inheritDoc}
067 */
068 public void analyse(Project project, SensorContext context) {
069 analyse(project.getFileSystem(), context);
070 onFinished();
071 }
072
073 protected void onFinished() {
074
075 }
076
077 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) {
078 parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset());
079 parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset());
080 }
081
082 protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) {
083 for (File file : files) {
084 Resource resource = createResource(file, sourceDirs, unitTest);
085 if (resource != null) {
086 try {
087 String source = FileUtils.readFileToString(file, sourcesEncoding.name());
088 context.index(resource);
089 context.saveSource(resource, source);
090 } catch (IOException 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 }