All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.scanner.protocol.output.ScannerReportReader Maven / Gradle / Ivy

The newest version!
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.protocol.output;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.annotation.CheckForNull;
import org.sonar.core.util.CloseableIterator;
import org.sonar.core.util.Protobuf;

import static org.sonar.core.util.CloseableIterator.emptyCloseableIterator;

public class ScannerReportReader {
  private final FileStructure fileStructure;

  public ScannerReportReader(FileStructure fileStructure) {
    this.fileStructure = fileStructure;
  }

  public ScannerReport.Metadata readMetadata() {
    File file = fileStructure.metadataFile();
    if (!fileExists(file)) {
      throw new IllegalStateException("Metadata file is missing in analysis report: " + file);
    }
    return Protobuf.read(file, ScannerReport.Metadata.parser());
  }

  public CloseableIterator readActiveRules() {
    File file = fileStructure.activeRules();
    if (!fileExists(file)) {
      return emptyCloseableIterator();
    }
    return Protobuf.readStream(file, ScannerReport.ActiveRule.parser());
  }

  public CloseableIterator readAdHocRules() {
    File file = fileStructure.adHocRules();
    if (!fileExists(file)) {
      return emptyCloseableIterator();
    }
    return Protobuf.readStream(file, ScannerReport.AdHocRule.parser());
  }

  public CloseableIterator readCves() {
    File file = fileStructure.cves();
    if (!fileExists(file)) {
      return emptyCloseableIterator();
    }
    return Protobuf.readStream(file, ScannerReport.Cve.parser());
  }

  public CloseableIterator readComponentMeasures(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.Measure.parser());
    }
    return emptyCloseableIterator();
  }

  @CheckForNull
  public ScannerReport.Changesets readChangesets(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, componentRef);
    if (fileExists(file)) {
      return Protobuf.read(file, ScannerReport.Changesets.parser());
    }
    return null;
  }

  @CheckForNull
  public InputStream getAnalysisCache() {
    File file = fileStructure.analysisCache();
    if (fileExists(file)) {
      try {
        return new BufferedInputStream(new FileInputStream(fileStructure.analysisCache()));
      } catch (FileNotFoundException e) {
        throw new IllegalStateException("Unable to open file " + fileStructure.analysisCache(), e);
      }
    }
    return null;
  }

  public ScannerReport.Component readComponent(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef);
    if (!fileExists(file)) {
      throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file);
    }
    return Protobuf.read(file, ScannerReport.Component.parser());
  }

  public CloseableIterator readComponentIssues(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.Issue.parser());
    }
    return emptyCloseableIterator();
  }

  public boolean hasIssues(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
    return fileExists(file);
  }

  public CloseableIterator readComponentExternalIssues(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.ExternalIssue.parser());
    }
    return emptyCloseableIterator();
  }

  public CloseableIterator readComponentDuplications(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.Duplication.parser());
    }
    return emptyCloseableIterator();
  }

  public CloseableIterator readCpdTextBlocks(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.CpdTextBlock.parser());
    }
    return emptyCloseableIterator();
  }

  public CloseableIterator readComponentSymbols(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.Symbol.parser());
    }
    return emptyCloseableIterator();
  }

  public boolean hasSyntaxHighlighting(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef);
    return file.exists();
  }

  @CheckForNull
  public CloseableIterator readComponentSignificantCode(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SGNIFICANT_CODE, fileRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.LineSgnificantCode.parser());
    }
    return null;
  }

  @CheckForNull
  public ScannerReport.ChangedLines readComponentChangedLines(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.CHANGED_LINES, fileRef);
    if (fileExists(file)) {
      return Protobuf.read(file, ScannerReport.ChangedLines.parser());
    }
    return null;
  }

  public boolean hasSignificantCode(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SGNIFICANT_CODE, fileRef);
    return fileExists(file);
  }

  public CloseableIterator readComponentSyntaxHighlighting(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, fileRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.SyntaxHighlightingRule.parser());
    }
    return emptyCloseableIterator();
  }

  public boolean hasCoverage(int componentRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, componentRef);
    return file.exists();
  }

  public CloseableIterator readComponentCoverage(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, fileRef);
    if (fileExists(file)) {
      return Protobuf.readStream(file, ScannerReport.LineCoverage.parser());
    }
    return emptyCloseableIterator();
  }

  @CheckForNull
  public File readFileSource(int fileRef) {
    File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef);
    if (fileExists(file)) {
      return file;
    }
    return null;
  }

  public CloseableIterator readContextProperties() {
    File file = fileStructure.contextProperties();
    if (!fileExists(file)) {
      return emptyCloseableIterator();
    }
    return Protobuf.readStream(file, ScannerReport.ContextProperty.parser());
  }

  public CloseableIterator readAnalysisWarnings() {
    File file = fileStructure.analysisWarnings();
    if (!fileExists(file)) {
      return emptyCloseableIterator();
    }
    return Protobuf.readStream(file, ScannerReport.AnalysisWarning.parser());
  }

  private static boolean fileExists(File file) {
    return file.exists() && file.isFile();
  }

  public FileStructure getFileStructure() {
    return fileStructure;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy