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 private Language language; 042 043 public AbstractSourceImporter(Language language) { 044 this.language = language; 045 } 046 047 public boolean shouldExecuteOnProject(Project project) { 048 return isEnabled(project) && language.equals(project.getLanguage()); 049 } 050 051 public void analyse(Project project, SensorContext context) { 052 try { 053 analyse(project.getFileSystem(), context); 054 055 } catch (IOException e) { 056 throw new SonarException("Parsing source files", e); 057 } 058 } 059 060 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) throws IOException { 061 parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset()); 062 parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset()); 063 } 064 065 protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) throws IOException { 066 for (File file : files) { 067 Resource resource = createResource(file, sourceDirs, unitTest); 068 if (resource != null) { 069 String source = FileUtils.readFileToString(file, sourcesEncoding.name()); 070 context.saveSource(resource, source); 071 } 072 } 073 } 074 075 protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) { 076 org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs); 077 if (resource != null) { 078 resource.setLanguage(language); 079 } 080 return resource; 081 } 082 083 protected boolean isEnabled(Project project) { 084 return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE); 085 } 086 }