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.

ScannerReportReader.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.File;
  22. import javax.annotation.CheckForNull;
  23. import org.sonar.core.util.CloseableIterator;
  24. import org.sonar.core.util.Protobuf;
  25. import static org.sonar.core.util.CloseableIterator.emptyCloseableIterator;
  26. public class ScannerReportReader {
  27. private final FileStructure fileStructure;
  28. public ScannerReportReader(File dir) {
  29. this.fileStructure = new FileStructure(dir);
  30. }
  31. public ScannerReport.Metadata readMetadata() {
  32. File file = fileStructure.metadataFile();
  33. if (!fileExists(file)) {
  34. throw new IllegalStateException("Metadata file is missing in analysis report: " + file);
  35. }
  36. return Protobuf.read(file, ScannerReport.Metadata.parser());
  37. }
  38. public CloseableIterator<ScannerReport.ActiveRule> readActiveRules() {
  39. File file = fileStructure.activeRules();
  40. if (!fileExists(file)) {
  41. return emptyCloseableIterator();
  42. }
  43. return Protobuf.readStream(file, ScannerReport.ActiveRule.parser());
  44. }
  45. public CloseableIterator<ScannerReport.AdHocRule> readAdHocRules() {
  46. File file = fileStructure.adHocRules();
  47. if (!fileExists(file)) {
  48. return emptyCloseableIterator();
  49. }
  50. return Protobuf.readStream(file, ScannerReport.AdHocRule.parser());
  51. }
  52. public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
  53. File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef);
  54. if (fileExists(file)) {
  55. return Protobuf.readStream(file, ScannerReport.Measure.parser());
  56. }
  57. return emptyCloseableIterator();
  58. }
  59. @CheckForNull
  60. public ScannerReport.Changesets readChangesets(int componentRef) {
  61. File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, componentRef);
  62. if (fileExists(file)) {
  63. return Protobuf.read(file, ScannerReport.Changesets.parser());
  64. }
  65. return null;
  66. }
  67. public ScannerReport.Component readComponent(int componentRef) {
  68. File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef);
  69. if (!fileExists(file)) {
  70. throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file);
  71. }
  72. return Protobuf.read(file, ScannerReport.Component.parser());
  73. }
  74. public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) {
  75. File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
  76. if (fileExists(file)) {
  77. return Protobuf.readStream(file, ScannerReport.Issue.parser());
  78. }
  79. return emptyCloseableIterator();
  80. }
  81. public CloseableIterator<ScannerReport.ExternalIssue> readComponentExternalIssues(int componentRef) {
  82. File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef);
  83. if (fileExists(file)) {
  84. return Protobuf.readStream(file, ScannerReport.ExternalIssue.parser());
  85. }
  86. return emptyCloseableIterator();
  87. }
  88. public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) {
  89. File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef);
  90. if (fileExists(file)) {
  91. return Protobuf.readStream(file, ScannerReport.Duplication.parser());
  92. }
  93. return emptyCloseableIterator();
  94. }
  95. public CloseableIterator<ScannerReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
  96. File file = fileStructure.fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, componentRef);
  97. if (fileExists(file)) {
  98. return Protobuf.readStream(file, ScannerReport.CpdTextBlock.parser());
  99. }
  100. return emptyCloseableIterator();
  101. }
  102. public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) {
  103. File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef);
  104. if (fileExists(file)) {
  105. return Protobuf.readStream(file, ScannerReport.Symbol.parser());
  106. }
  107. return emptyCloseableIterator();
  108. }
  109. public boolean hasSyntaxHighlighting(int componentRef) {
  110. File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef);
  111. return file.exists();
  112. }
  113. @CheckForNull
  114. public CloseableIterator<ScannerReport.LineSgnificantCode> readComponentSignificantCode(int fileRef) {
  115. File file = fileStructure.fileFor(FileStructure.Domain.SGNIFICANT_CODE, fileRef);
  116. if (fileExists(file)) {
  117. return Protobuf.readStream(file, ScannerReport.LineSgnificantCode.parser());
  118. }
  119. return null;
  120. }
  121. @CheckForNull
  122. public ScannerReport.ChangedLines readComponentChangedLines(int fileRef) {
  123. File file = fileStructure.fileFor(FileStructure.Domain.CHANGED_LINES, fileRef);
  124. if (fileExists(file)) {
  125. return Protobuf.read(file, ScannerReport.ChangedLines.parser());
  126. }
  127. return null;
  128. }
  129. public boolean hasSignificantCode(int fileRef) {
  130. File file = fileStructure.fileFor(FileStructure.Domain.SGNIFICANT_CODE, fileRef);
  131. return fileExists(file);
  132. }
  133. public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) {
  134. File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, fileRef);
  135. if (fileExists(file)) {
  136. return Protobuf.readStream(file, ScannerReport.SyntaxHighlightingRule.parser());
  137. }
  138. return emptyCloseableIterator();
  139. }
  140. public boolean hasCoverage(int componentRef) {
  141. File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, componentRef);
  142. return file.exists();
  143. }
  144. public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) {
  145. File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, fileRef);
  146. if (fileExists(file)) {
  147. return Protobuf.readStream(file, ScannerReport.LineCoverage.parser());
  148. }
  149. return emptyCloseableIterator();
  150. }
  151. @CheckForNull
  152. public File readFileSource(int fileRef) {
  153. File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef);
  154. if (fileExists(file)) {
  155. return file;
  156. }
  157. return null;
  158. }
  159. public CloseableIterator<ScannerReport.ContextProperty> readContextProperties() {
  160. File file = fileStructure.contextProperties();
  161. if (!fileExists(file)) {
  162. return emptyCloseableIterator();
  163. }
  164. return Protobuf.readStream(file, ScannerReport.ContextProperty.parser());
  165. }
  166. public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() {
  167. File file = fileStructure.analysisWarnings();
  168. if (!fileExists(file)) {
  169. return emptyCloseableIterator();
  170. }
  171. return Protobuf.readStream(file, ScannerReport.AnalysisWarning.parser());
  172. }
  173. private static boolean fileExists(File file) {
  174. return file.exists() && file.isFile();
  175. }
  176. public FileStructure getFileStructure() {
  177. return fileStructure;
  178. }
  179. }