You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputFileBuilder.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.filesystem;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.nio.charset.Charset;
  26. import java.nio.charset.StandardCharsets;
  27. import javax.annotation.CheckForNull;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.api.CoreProperties;
  31. import org.sonar.api.batch.fs.FileSystem;
  32. import org.sonar.api.batch.fs.InputFile;
  33. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  34. import org.sonar.api.batch.fs.internal.FileMetadata;
  35. import org.sonar.api.config.Settings;
  36. import org.sonar.api.scan.filesystem.PathResolver;
  37. class InputFileBuilder {
  38. private static final Logger LOG = LoggerFactory.getLogger(InputFileBuilder.class);
  39. @VisibleForTesting
  40. static final Charset UTF_32BE = Charset.forName("UTF-32BE");
  41. @VisibleForTesting
  42. static final Charset UTF_32LE = Charset.forName("UTF-32LE");
  43. private final String moduleKey;
  44. private final PathResolver pathResolver;
  45. private final LanguageDetection langDetection;
  46. private final StatusDetection statusDetection;
  47. private final DefaultModuleFileSystem fs;
  48. private final Settings settings;
  49. private final FileMetadata fileMetadata;
  50. InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection,
  51. StatusDetection statusDetection, DefaultModuleFileSystem fs, Settings settings, FileMetadata fileMetadata) {
  52. this.moduleKey = moduleKey;
  53. this.pathResolver = pathResolver;
  54. this.langDetection = langDetection;
  55. this.statusDetection = statusDetection;
  56. this.fs = fs;
  57. this.settings = settings;
  58. this.fileMetadata = fileMetadata;
  59. }
  60. String moduleKey() {
  61. return moduleKey;
  62. }
  63. PathResolver pathResolver() {
  64. return pathResolver;
  65. }
  66. LanguageDetection langDetection() {
  67. return langDetection;
  68. }
  69. StatusDetection statusDetection() {
  70. return statusDetection;
  71. }
  72. FileSystem fs() {
  73. return fs;
  74. }
  75. @CheckForNull
  76. DefaultInputFile create(File file) {
  77. String relativePath = pathResolver.relativePath(fs.baseDir(), file);
  78. if (relativePath == null) {
  79. LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.getAbsolutePath(), fs.baseDir());
  80. return null;
  81. }
  82. return new DefaultInputFile(moduleKey, relativePath);
  83. }
  84. /**
  85. * Optimization to not compute InputFile metadata if the file is excluded from analysis.
  86. */
  87. @CheckForNull
  88. DefaultInputFile completeAndComputeMetadata(DefaultInputFile inputFile, InputFile.Type type) {
  89. inputFile.setType(type);
  90. inputFile.setModuleBaseDir(fs.baseDir().toPath());
  91. String lang = langDetection.language(inputFile);
  92. if (lang == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) {
  93. // Return fast to skip costly metadata computation
  94. LOG.debug("'{}' language is not supported by any analyzer. Skipping it.", inputFile.relativePath());
  95. return null;
  96. }
  97. inputFile.setLanguage(lang);
  98. Charset charset = detectCharset(inputFile.file(), fs.encoding());
  99. inputFile.setCharset(charset);
  100. inputFile.initMetadata(fileMetadata.readMetadata(inputFile.file(), charset));
  101. inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), inputFile.hash()));
  102. return inputFile;
  103. }
  104. /**
  105. * @return charset detected from BOM in given file or given defaultCharset
  106. * @throws IllegalStateException if an I/O error occurs
  107. */
  108. private static Charset detectCharset(File file, Charset defaultCharset) {
  109. try (FileInputStream inputStream = new FileInputStream(file)) {
  110. byte[] bom = new byte[4];
  111. int n = inputStream.read(bom, 0, bom.length);
  112. if ((n >= 3) && (bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
  113. return StandardCharsets.UTF_8;
  114. } else if ((n >= 4) && (bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
  115. return UTF_32BE;
  116. } else if ((n >= 4) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
  117. return UTF_32LE;
  118. } else if ((n >= 2) && (bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
  119. return StandardCharsets.UTF_16BE;
  120. } else if ((n >= 2) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
  121. return StandardCharsets.UTF_16LE;
  122. } else {
  123. return defaultCharset;
  124. }
  125. } catch (IOException e) {
  126. throw new IllegalStateException("Unable to read file " + file.getAbsolutePath(), e);
  127. }
  128. }
  129. }