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.

MetadataGenerator.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info 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.InputStream;
  23. import java.nio.charset.Charset;
  24. import org.sonar.api.batch.fs.InputFile.Type;
  25. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  26. import org.sonar.api.batch.fs.internal.FileMetadata;
  27. import org.sonar.api.batch.fs.internal.Metadata;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader;
  31. public class MetadataGenerator {
  32. private static final Logger LOG = Loggers.get(MetadataGenerator.class);
  33. @VisibleForTesting
  34. static final Charset UTF_32BE = Charset.forName("UTF-32BE");
  35. @VisibleForTesting
  36. static final Charset UTF_32LE = Charset.forName("UTF-32LE");
  37. private final StatusDetection statusDetection;
  38. private final FileMetadata fileMetadata;
  39. private final IssueExclusionsLoader exclusionsScanner;
  40. public MetadataGenerator(StatusDetection statusDetection, FileMetadata fileMetadata, IssueExclusionsLoader exclusionsScanner) {
  41. this.statusDetection = statusDetection;
  42. this.fileMetadata = fileMetadata;
  43. this.exclusionsScanner = exclusionsScanner;
  44. }
  45. /**
  46. * Sets all metadata in the file, including charset and status.
  47. * It is an expensive computation, reading the entire file.
  48. */
  49. public void setMetadata(String moduleKeyWithBranch, final DefaultInputFile inputFile, Charset defaultEncoding) {
  50. CharsetDetector charsetDetector = new CharsetDetector(inputFile.path(), defaultEncoding);
  51. try {
  52. Charset charset;
  53. if (charsetDetector.run()) {
  54. charset = charsetDetector.charset();
  55. } else {
  56. LOG.debug("Failed to detect a valid charset for file '{}'. Using default charset.", inputFile);
  57. charset = defaultEncoding;
  58. }
  59. InputStream is = charsetDetector.inputStream();
  60. inputFile.setCharset(charset);
  61. Metadata metadata = fileMetadata.readMetadata(is, charset, inputFile.absolutePath(), exclusionsScanner.createCharHandlerFor(inputFile.key()));
  62. inputFile.setMetadata(metadata);
  63. inputFile.setStatus(statusDetection.status(moduleKeyWithBranch, inputFile, metadata.hash()));
  64. LOG.debug("'{}' generated metadata{} with charset '{}'", inputFile, inputFile.type() == Type.TEST ? " as test " : "", charset);
  65. } catch (Exception e) {
  66. throw new IllegalStateException(e);
  67. }
  68. }
  69. }