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.plugins.squid;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.io.FileUtils;
024 import org.sonar.api.CoreProperties;
025 import org.sonar.api.batch.DependedUpon;
026 import org.sonar.api.batch.Phase;
027 import org.sonar.api.batch.Sensor;
028 import org.sonar.api.batch.SensorContext;
029 import org.sonar.api.resources.*;
030 import org.sonar.api.utils.SonarException;
031 import org.sonar.java.api.JavaUtils;
032
033 import java.nio.charset.Charset;
034 import java.util.List;
035
036 @Phase(name = Phase.Name.PRE)
037 @DependedUpon(JavaUtils.BARRIER_BEFORE_SQUID)
038 public final class JavaSourceImporter implements Sensor {
039
040 private boolean importSources = false;
041
042 public JavaSourceImporter(Configuration conf) {
043 this.importSources = conf.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
044 CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
045 }
046
047 JavaSourceImporter(boolean importSources) {
048 this.importSources = importSources;
049 }
050
051 /**
052 * {@inheritDoc}
053 */
054 public boolean shouldExecuteOnProject(Project project) {
055 return Java.KEY.equals(project.getLanguageKey());
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 public void analyse(Project project, SensorContext context) {
062 analyse(project.getFileSystem(), context);
063 }
064
065 void analyse(ProjectFileSystem fileSystem, SensorContext context) {
066 parseDirs(context, fileSystem.mainFiles(Java.KEY), false, fileSystem.getSourceCharset());
067 parseDirs(context, fileSystem.testFiles(Java.KEY), true, fileSystem.getSourceCharset());
068 }
069
070 void parseDirs(SensorContext context, List<InputFile> inputFiles, boolean unitTest, Charset sourcesEncoding) {
071 for (InputFile inputFile : inputFiles) {
072 JavaFile javaFile = JavaFile.fromRelativePath(inputFile.getRelativePath(), unitTest);
073 importSource(context, javaFile, inputFile, sourcesEncoding);
074 }
075 }
076
077 void importSource(SensorContext context, JavaFile javaFile, InputFile inputFile, Charset sourcesEncoding) {
078 try {
079 context.index(javaFile);
080
081 if (importSources) {
082 String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
083 context.saveSource(javaFile, source);
084 }
085
086 } catch (Exception e) {
087 throw new SonarException("Unable to read and import the source file : '" + inputFile.getFile().getAbsolutePath() + "' with the charset : '"
088 + sourcesEncoding.name() + "'.", e);
089 }
090 }
091
092 @Override
093 public String toString() {
094 return getClass().getSimpleName();
095 }
096
097 }