001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.batch.protocol.output; 021 022import java.io.File; 023import javax.annotation.CheckForNull; 024import org.sonar.core.util.CloseableIterator; 025import org.sonar.core.util.Protobuf; 026 027import static org.sonar.core.util.CloseableIterator.emptyCloseableIterator; 028 029public class BatchReportReader { 030 031 private final FileStructure fileStructure; 032 033 public BatchReportReader(File dir) { 034 this.fileStructure = new FileStructure(dir); 035 } 036 037 public BatchReport.Metadata readMetadata() { 038 File file = fileStructure.metadataFile(); 039 if (!fileExists(file)) { 040 throw new IllegalStateException("Metadata file is missing in analysis report: " + file); 041 } 042 return Protobuf.read(file, BatchReport.Metadata.PARSER); 043 } 044 045 public CloseableIterator<BatchReport.ActiveRule> readActiveRules() { 046 File file = fileStructure.activeRules(); 047 if (!fileExists(file)) { 048 return emptyCloseableIterator(); 049 } 050 return Protobuf.readStream(file, BatchReport.ActiveRule.PARSER); 051 } 052 053 public CloseableIterator<BatchReport.Measure> readComponentMeasures(int componentRef) { 054 File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef); 055 if (fileExists(file)) { 056 return Protobuf.readStream(file, BatchReport.Measure.PARSER); 057 } 058 return emptyCloseableIterator(); 059 } 060 061 @CheckForNull 062 public BatchReport.Changesets readChangesets(int componentRef) { 063 File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, componentRef); 064 if (fileExists(file)) { 065 return Protobuf.read(file, BatchReport.Changesets.PARSER); 066 } 067 return null; 068 } 069 070 public BatchReport.Component readComponent(int componentRef) { 071 File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef); 072 if (!fileExists(file)) { 073 throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file); 074 } 075 return Protobuf.read(file, BatchReport.Component.PARSER); 076 } 077 078 public CloseableIterator<BatchReport.Issue> readComponentIssues(int componentRef) { 079 File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); 080 if (fileExists(file)) { 081 return Protobuf.readStream(file, BatchReport.Issue.PARSER); 082 } 083 return emptyCloseableIterator(); 084 } 085 086 public CloseableIterator<BatchReport.Duplication> readComponentDuplications(int componentRef) { 087 File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef); 088 if (fileExists(file)) { 089 return Protobuf.readStream(file, BatchReport.Duplication.PARSER); 090 } 091 return emptyCloseableIterator(); 092 } 093 094 public CloseableIterator<BatchReport.Symbol> readComponentSymbols(int componentRef) { 095 File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef); 096 if (fileExists(file)) { 097 return Protobuf.readStream(file, BatchReport.Symbol.PARSER); 098 } 099 return emptyCloseableIterator(); 100 } 101 102 public boolean hasSyntaxHighlighting(int componentRef) { 103 File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef); 104 return file.exists(); 105 } 106 107 public CloseableIterator<BatchReport.SyntaxHighlighting> readComponentSyntaxHighlighting(int fileRef) { 108 File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, fileRef); 109 if (fileExists(file)) { 110 return Protobuf.readStream(file, BatchReport.SyntaxHighlighting.PARSER); 111 } 112 return emptyCloseableIterator(); 113 } 114 115 public CloseableIterator<BatchReport.Coverage> readComponentCoverage(int fileRef) { 116 File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, fileRef); 117 if (fileExists(file)) { 118 return Protobuf.readStream(file, BatchReport.Coverage.PARSER); 119 } 120 return emptyCloseableIterator(); 121 } 122 123 @CheckForNull 124 public File readFileSource(int fileRef) { 125 File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef); 126 if (fileExists(file)) { 127 return file; 128 } 129 return null; 130 } 131 132 @CheckForNull 133 public File readTests(int testFileRef) { 134 File file = fileStructure.fileFor(FileStructure.Domain.TESTS, testFileRef); 135 if (fileExists(file)) { 136 return file; 137 } 138 139 return null; 140 } 141 142 @CheckForNull 143 public File readCoverageDetails(int testFileRef) { 144 File file = fileStructure.fileFor(FileStructure.Domain.COVERAGE_DETAILS, testFileRef); 145 if (fileExists(file)) { 146 return file; 147 } 148 149 return null; 150 } 151 152 private static boolean fileExists(File file) { 153 return file.exists() && file.isFile(); 154 } 155 156 public FileStructure getFileStructure() { 157 return fileStructure; 158 } 159}