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 org.apache.commons.io.FileUtils; 023 import org.sonar.api.CoreProperties; 024 import org.sonar.api.resources.Language; 025 import org.sonar.api.resources.Project; 026 import org.sonar.api.resources.ProjectFileSystem; 027 import org.sonar.api.resources.Resource; 028 import org.sonar.api.utils.SonarException; 029 030 import java.io.File; 031 import java.io.IOException; 032 import java.nio.charset.Charset; 033 import java.util.List; 034 035 /** 036 * @since 1.10 037 */ 038 @Phase(name = Phase.Name.PRE) 039 public abstract class AbstractSourceImporter implements Sensor { 040 041 /** 042 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_PROPERTY since 1.11 043 */ 044 @Deprecated public static final String KEY_IMPORT_SOURCES = "sonar.importSources"; 045 046 /** 047 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE since 1.11 048 */ 049 @Deprecated public static final boolean DEFAULT_IMPORT_SOURCES = true; 050 051 private Language language; 052 053 public AbstractSourceImporter(Language language) { 054 this.language = language; 055 } 056 057 public boolean shouldExecuteOnProject(Project project) { 058 return isEnabled(project) && language.equals(project.getLanguage()); 059 } 060 061 public void analyse(Project project, SensorContext context) { 062 try { 063 analyse(project.getFileSystem(), context); 064 065 } catch (IOException e) { 066 throw new SonarException("Parsing source files", e); 067 } 068 } 069 070 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) throws IOException { 071 parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset()); 072 parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset()); 073 } 074 075 protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) throws IOException { 076 for (File file : files) { 077 Resource resource = createResource(file, sourceDirs, unitTest); 078 if (resource != null) { 079 String source = FileUtils.readFileToString(file, sourcesEncoding.name()); 080 context.saveSource(resource, source); 081 } 082 } 083 } 084 085 protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) { 086 org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs); 087 if (resource != null) { 088 resource.setLanguage(language); 089 } 090 return resource; 091 } 092 093 protected boolean isEnabled(Project project) { 094 return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE); 095 } 096 }