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     * It became too much ugly because of extensability. Methods can't be
035     * refactored because they are heavily overridden in plugins.
036     *
037     * @since 1.10
038     */
039    @Phase(name = Phase.Name.PRE)
040    public abstract class AbstractSourceImporter implements Sensor {
041    
042      private Language language;
043      private boolean enabled = false;
044    
045      public AbstractSourceImporter(Language language) {
046        this.language = language;
047      }
048    
049      /**
050       * {@inheritDoc}
051       */
052      public boolean shouldExecuteOnProject(Project project) {
053        return language.equals(project.getLanguage());
054      }
055    
056      /**
057       * {@inheritDoc}
058       */
059      public void analyse(Project project, SensorContext context) {
060        enabled = isEnabled(project);
061        analyse(project.getFileSystem(), context);
062        onFinished();
063      }
064    
065      protected void onFinished() {
066    
067      }
068    
069      protected void analyse(ProjectFileSystem fileSystem, SensorContext context) {
070        parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset());
071        parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset());
072      }
073    
074      protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) {
075        for (File file : files) {
076          Resource resource = createResource(file, sourceDirs, unitTest);
077          if (resource != null) {
078            try {
079              context.index(resource);
080              if (enabled) {
081                String source = FileUtils.readFileToString(file, sourcesEncoding.name());
082                context.saveSource(resource, source);
083              }
084            } catch (IOException e) {
085              throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
086                  + sourcesEncoding.name() + "'. You should check the property " + CoreProperties.ENCODING_PROPERTY, e);
087            }
088          }
089        }
090      }
091    
092      protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) {
093        org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs);
094        if (resource != null) {
095          resource.setLanguage(language);
096          if (unitTest) {
097            resource.setQualifier(Qualifiers.UNIT_TEST_FILE);
098          }
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    }