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 * A pre-implementation for a sensor that imports sources 037 * @since 1.10 038 */ 039 @Phase(name = Phase.Name.PRE) 040 public abstract class AbstractSourceImporter implements Sensor { 041 042 /** 043 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_PROPERTY since 1.11 044 */ 045 @Deprecated public static final String KEY_IMPORT_SOURCES = "sonar.importSources"; 046 047 /** 048 * @deprecated replaced by CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE since 1.11 049 */ 050 @Deprecated public static final boolean DEFAULT_IMPORT_SOURCES = true; 051 052 private Language language; 053 054 public AbstractSourceImporter(Language language) { 055 this.language = language; 056 } 057 058 /** 059 * {@inheritDoc} 060 */ 061 public boolean shouldExecuteOnProject(Project project) { 062 return isEnabled(project) && language.equals(project.getLanguage()); 063 } 064 065 /** 066 * {@inheritDoc} 067 */ 068 public void analyse(Project project, SensorContext context) { 069 try { 070 analyse(project.getFileSystem(), context); 071 072 } catch (IOException e) { 073 throw new SonarException("Parsing source files", e); 074 } 075 } 076 077 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) throws IOException { 078 parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset()); 079 parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset()); 080 } 081 082 protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) throws IOException { 083 for (File file : files) { 084 Resource resource = createResource(file, sourceDirs, unitTest); 085 if (resource != null) { 086 String source = FileUtils.readFileToString(file, sourcesEncoding.name()); 087 context.saveSource(resource, source); 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 } 097 return resource; 098 } 099 100 protected boolean isEnabled(Project project) { 101 return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE); 102 } 103 104 /** 105 * @return the language 106 */ 107 public Language getLanguage() { 108 return language; 109 } 110 }