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.

ScannerReportWriter.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.protocol.output;
  21. import java.io.BufferedOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.OutputStream;
  25. import javax.annotation.concurrent.Immutable;
  26. import org.sonar.core.util.ContextException;
  27. import org.sonar.core.util.Protobuf;
  28. @Immutable
  29. public class ScannerReportWriter {
  30. private final FileStructure fileStructure;
  31. public ScannerReportWriter(File dir) {
  32. if (!dir.exists() && !dir.mkdirs()) {
  33. throw new IllegalStateException("Unable to create directory: " + dir);
  34. }
  35. this.fileStructure = new FileStructure(dir);
  36. }
  37. public FileStructure getFileStructure() {
  38. return fileStructure;
  39. }
  40. public boolean hasComponentData(FileStructure.Domain domain, int componentRef) {
  41. File file = fileStructure.fileFor(domain, componentRef);
  42. return file.exists() && file.isFile();
  43. }
  44. /**
  45. * Metadata is mandatory
  46. */
  47. public File writeMetadata(ScannerReport.Metadata metadata) {
  48. Protobuf.write(metadata, fileStructure.metadataFile());
  49. return fileStructure.metadataFile();
  50. }
  51. public File writeActiveRules(Iterable<ScannerReport.ActiveRule> activeRules) {
  52. Protobuf.writeStream(activeRules, fileStructure.activeRules(), false);
  53. return fileStructure.metadataFile();
  54. }
  55. public File writeComponent(ScannerReport.Component component) {
  56. File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef());
  57. Protobuf.write(component, file);
  58. return file;
  59. }
  60. public File writeComponentIssues(int componentRef, Iterable<ScannerReport.Issue> issues) {
  61. File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
  62. Protobuf.writeStream(issues, file, false);
  63. return file;
  64. }
  65. public File writeComponentSignificantCode(int componentRef, Iterable<ScannerReport.LineSgnificantCode> lineSignificantCode) {
  66. File file = fileStructure.fileFor(FileStructure.Domain.SGNIFICANT_CODE, componentRef);
  67. Protobuf.writeStream(lineSignificantCode, file, false);
  68. return file;
  69. }
  70. public void appendComponentIssue(int componentRef, ScannerReport.Issue issue) {
  71. File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
  72. try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
  73. issue.writeDelimitedTo(out);
  74. } catch (Exception e) {
  75. throw ContextException.of("Unable to write issue", e).addContext("file", file);
  76. }
  77. }
  78. public File writeComponentChangedLines(int componentRef, ScannerReport.ChangedLines changedLines) {
  79. File file = fileStructure.fileFor(FileStructure.Domain.CHANGED_LINES, componentRef);
  80. Protobuf.write(changedLines, file);
  81. return file;
  82. }
  83. public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) {
  84. File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef);
  85. try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
  86. issue.writeDelimitedTo(out);
  87. } catch (Exception e) {
  88. throw ContextException.of("Unable to write external issue", e).addContext("file", file);
  89. }
  90. }
  91. public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) {
  92. File file = fileStructure.adHocRules();
  93. try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
  94. adHocRule.writeDelimitedTo(out);
  95. } catch (Exception e) {
  96. throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file);
  97. }
  98. }
  99. public File writeComponentMeasures(int componentRef, Iterable<ScannerReport.Measure> measures) {
  100. File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef);
  101. Protobuf.writeStream(measures, file, false);
  102. return file;
  103. }
  104. public File writeComponentChangesets(ScannerReport.Changesets changesets) {
  105. File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, changesets.getComponentRef());
  106. Protobuf.write(changesets, file);
  107. return file;
  108. }
  109. public File writeComponentDuplications(int componentRef, Iterable<ScannerReport.Duplication> duplications) {
  110. File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef);
  111. Protobuf.writeStream(duplications, file, false);
  112. return file;
  113. }
  114. public File writeCpdTextBlocks(int componentRef, Iterable<ScannerReport.CpdTextBlock> blocks) {
  115. File file = fileStructure.fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, componentRef);
  116. Protobuf.writeStream(blocks, file, false);
  117. return file;
  118. }
  119. public File writeComponentSymbols(int componentRef, Iterable<ScannerReport.Symbol> symbols) {
  120. File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef);
  121. Protobuf.writeStream(symbols, file, false);
  122. return file;
  123. }
  124. public File writeComponentSyntaxHighlighting(int componentRef, Iterable<ScannerReport.SyntaxHighlightingRule> syntaxHighlightingRules) {
  125. File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef);
  126. Protobuf.writeStream(syntaxHighlightingRules, file, false);
  127. return file;
  128. }
  129. public File writeComponentCoverage(int componentRef, Iterable<ScannerReport.LineCoverage> coverageList) {
  130. File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, componentRef);
  131. Protobuf.writeStream(coverageList, file, false);
  132. return file;
  133. }
  134. public File writeContextProperties(Iterable<ScannerReport.ContextProperty> properties) {
  135. File file = fileStructure.contextProperties();
  136. Protobuf.writeStream(properties, file, false);
  137. return file;
  138. }
  139. public File writeAnalysisWarnings(Iterable<ScannerReport.AnalysisWarning> analysisWarnings) {
  140. File file = fileStructure.analysisWarnings();
  141. Protobuf.writeStream(analysisWarnings, file, false);
  142. return file;
  143. }
  144. public File getSourceFile(int componentRef) {
  145. return fileStructure.fileFor(FileStructure.Domain.SOURCE, componentRef);
  146. }
  147. }