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        onFinished();
074      }
075    
076      protected void onFinished() {
077    
078      }
079    
080      protected void analyse(ProjectFileSystem fileSystem, SensorContext context) {
081        parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset());
082        parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset());
083      }
084    
085      protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) {
086        for (File file : files) {
087          Resource resource = createResource(file, sourceDirs, unitTest);
088          if (resource != null) {
089            try {
090              String source = FileUtils.readFileToString(file, sourcesEncoding.name());
091              context.saveSource(resource, source);
092            } catch (IOException e) {
093              throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
094                  + sourcesEncoding.name() + "'.", e);
095            }
096          }
097        }
098      }
099    
100      protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) {
101        org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs);
102        if (resource != null) {
103          resource.setLanguage(language);
104          if (unitTest) {
105            resource.setQualifier(Resource.QUALIFIER_UNIT_TEST_CLASS);
106          }
107        }
108        return resource;
109      }
110    
111      protected boolean isEnabled(Project project) {
112        return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
113            CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
114      }
115    
116      /**
117       * @return the language
118       */
119      public Language getLanguage() {
120        return language;
121      }
122    }