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