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