Java Code Examples for org.sonar.api.batch.sensor.SensorContext#fileSystem()

The following examples show how to use org.sonar.api.batch.sensor.SensorContext#fileSystem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TraceSensor.java    From sonar-esql-plugin with Apache License 2.0 6 votes vote down vote up
public void execute(SensorContext context, Map<InputFile, Set<Integer>> executableLines,  String[] traces) {
	
	for (String trace : traces) {
		FileSystem fs = context.fileSystem();
		File report = fs.resolvePath(trace);
		if (report.isFile()) {
			new UnitTestsAnalyzer(report, executableLines).analyse(context);
		}else if(report.isDirectory()){
			for (File reportFile:report.listFiles()){
				new UnitTestsAnalyzer(reportFile, executableLines).analyse(context);
			}
		} else {
			LOG.info("Trace not found: '{}'", trace);
		}
	} 
}
 
Example 2
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void execute(SensorContext context) {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  List<SquidAstVisitor<LexerlessGrammar>> visitors = new ArrayList<>(checks.all());
  visitors.add(new FileLinesVisitor(fileLinesContextFactory, fileSystem));
  LuaConfiguration configuration = new LuaConfiguration(fileSystem.encoding());

  scanner = LuaAstScanner.create(configuration, visitors);

  Iterable<java.io.File> files = fileSystem.files(
    predicates.and(
      predicates.hasType(InputFile.Type.MAIN),
      predicates.hasLanguage(Lua.KEY),
      inputFile -> !inputFile.absolutePath().endsWith("mxml")
    ));
  scanner.scanFiles(ImmutableList.copyOf(files));

  Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class));
  save(context, squidSourceFiles);
}
 
Example 3
Source File: CloverageMetricParser.java    From sonar-clojure with MIT License 5 votes vote down vote up
private static Optional<FileAnalysis> findFileBySources(SensorContext context, String filename) {
    FileSystem fs = context.fileSystem();
    FilePredicate pattern = fs.predicates().matchesPathPattern("**/" + filename);
    InputFile potentialFile = fs.inputFile(pattern);

    if (potentialFile != null) {
        LOG.debug("Found file");
        FileAnalysis foundSource = new FileAnalysis();
        foundSource.setInputFile(potentialFile);
        return Optional.of(foundSource);
    }

    return Optional.empty();
}
 
Example 4
Source File: FlowSquidSensor.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void execute(SensorContext context) {
  logger.debug("FlowSquidSensor analysis called");
  AstScanner<Grammar> flowScanner = FlowAstScanner.create(createConfiguration(), flowChecks.all(),
      null);
  logger.debug("Scanning Flow Files with " + flowChecks.all().size() + " checks active");
  FileSystem fs = context.fileSystem();
  Iterable<InputFile> flowFiles = fs
      .inputFiles(fs.predicates().and(fs.predicates().hasLanguage(FlowLanguage.KEY),
          fs.predicates().matchesPathPatterns(FlowLanguage.getFlowFilePatterns())));
  ArrayList<File> files = new ArrayList<File>();
  flowFiles.forEach(file -> files.add(file.file()));
  try {
    flowScanner.scanFiles(files);
  } catch (Exception e) {
    if (config.getBoolean(FlowLanguageProperties.FAIL_ON_SCANERROR).get()) {
      throw e;
    } else {
      logger.error("** * Exception while scanning file, skipping.", e);
    }
  }
  Collection<SourceCode> squidSourceFiles = flowScanner.getIndex()
      .search(new QueryByType(SourceFile.class));
  logger.debug("** Done Scanning");
  // Process sourceFiles
  logger.debug("** Getting Interface Files");
  getInterfaceFiles(squidSourceFiles, context);
  logger.debug("** Setting Top Level Services");
  setTopLevelServices(squidSourceFiles);
  logger.debug("** Saving Source Files");
  save(context, flowScanner, squidSourceFiles);

}
 
Example 5
Source File: FlowSquidSensor.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void getInterfaceFiles(Collection<SourceCode> squidSourceFiles, SensorContext context) {
  // Scan node.ndf files
  AstScanner<Grammar> scanner = NodeAstScanner.create(createConfiguration(), nodeChecks.all(),
      null);
  logger.debug("Scanning Interface Files with " + nodeChecks.all().size() + " checks active");
  FileSystem fs = context.fileSystem();
  Iterable<InputFile> nodeFiles = fs
      .inputFiles(fs.predicates().matchesPathPatterns(FlowLanguage.getNodeFilePatterns()));
  try {
    ArrayList<File> files = new ArrayList<File>();
    nodeFiles.forEach(file -> files.add(file.file()));
    scanner.scanFiles(files);
  } catch (Exception e) {
    if (config.getBoolean(FlowLanguageProperties.FAIL_ON_SCANERROR).get()) {
      throw e;
    } else {
      logger.error("** * Exception while scanning file, skipping.", e);
    }
  }
  Collection<SourceCode> nodeSources = scanner.getIndex()
      .search(new QueryByType(SourceFile.class));
  logger.debug("*NODE* nodes found:" + nodeSources.size() + " *");
  for (SourceCode squidSourceFile : squidSourceFiles) {
    for (SourceCode nodeSource : nodeSources) {
      if ((new File(nodeSource.getKey())).getParent()
          .equals((new File(squidSourceFile.getKey())).getParent())) {
        squidSourceFile.addChild(nodeSource);
        String relativePath = pathResolver.relativePath(fileSystem.baseDir(),
            new java.io.File(nodeSource.getKey()));
        InputFile inputFile = fileSystem
            .inputFile(fileSystem.predicates().hasRelativePath(relativePath));
        saveViolations(context, inputFile, (SourceFile) nodeSource);
      }

    }
  }
}
 
Example 6
Source File: CoberturaReportParser.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void collectFileMeasures(SensorContext context, SMInputCursor clazz) throws XMLStreamException {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  Map<String, InputFile> inputFileByFilename = new HashMap<>();
  while (clazz.getNext() != null) {
    String fileName = clazz.getAttrValue("filename");

    InputFile inputFile;
    // mxml files are not supported by the plugin
    if (inputFileByFilename.containsKey(fileName)) {
      inputFile = inputFileByFilename.get(fileName);
    } else {
      String key = fileName.startsWith(File.separator) ? fileName : (File.separator + fileName);
      inputFile = fileSystem.inputFile(predicates.and(
        predicates.matchesPathPattern("file:**" + key.replace(File.separator, "/")),
        predicates.hasType(InputFile.Type.MAIN),
        predicates.hasLanguage(Lua.KEY)));
      inputFileByFilename.put(fileName, inputFile);
      if (inputFile == null && !fileName.endsWith(".mxml")) {
        LOG.warn("Cannot save coverage result for file: {}, because resource not found.", fileName);
      }
    }
    if (inputFile != null) {
      collectFileData(
        clazz,
        context.newCoverage()
          .onFile(inputFile)
          .ofType(CoverageType.UNIT)
      );
    } else {
      SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
      while (line.getNext() != null) {
        // advance
      }
    }
  }
}
 
Example 7
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void save(SensorContext context, Collection<SourceCode> squidSourceFiles) {
  FileSystem fileSystem = context.fileSystem();
  for (SourceCode squidSourceFile : squidSourceFiles) {
    SourceFile squidFile = (SourceFile) squidSourceFile;

    InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasPath(squidFile.getKey()));

    saveClassComplexity(context, inputFile, squidFile);
    saveMeasures(context, inputFile, squidFile);
    saveFunctionsComplexityDistribution(context, inputFile, squidFile);
    saveFilesComplexityDistribution(context, inputFile, squidFile);
    visitors.add(new LuaTokensVisitor(context, LuaLexer.create(configuration)));
    saveViolations(context, inputFile, squidFile);
  }
}
 
Example 8
Source File: CpdVisitor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public CpdVisitor(SensorContext sensorContext) {
  this.sensorContext = sensorContext;
  this.fileSystem = sensorContext.fileSystem();
}
 
Example 9
Source File: CssSyntaxHighlighterVisitor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public CssSyntaxHighlighterVisitor(SensorContext sensorContext) {
  this.sensorContext = sensorContext;
  fileSystem = sensorContext.fileSystem();
}
 
Example 10
Source File: AbstractMetricsVisitor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public AbstractMetricsVisitor(SensorContext sensorContext, NoSonarFilter noSonarFilter) {
  this.sensorContext = sensorContext;
  this.fileSystem = sensorContext.fileSystem();
  this.noSonarFilter = noSonarFilter;
}
 
Example 11
Source File: IssueSaver.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public IssueSaver(SensorContext sensorContext, Checks checks) {
  this.sensorContext = sensorContext;
  this.fileSystem = sensorContext.fileSystem();
  this.checks = checks;
}
 
Example 12
Source File: CFLintAnalyzer.java    From sonar-coldfusion with Apache License 2.0 4 votes vote down vote up
public CFLintAnalyzer(SensorContext sensorContext) {
    Preconditions.checkNotNull(sensorContext);

    this.settings = sensorContext.config();
    this.fs = sensorContext.fileSystem();
}
 
Example 13
Source File: MetricsVisitor.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public MetricsVisitor(SensorContext sensorContext) {
  this.sensorContext = sensorContext;
  this.fileSystem = sensorContext.fileSystem();
}
 
Example 14
Source File: SyntaxHighlighterVisitor.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public SyntaxHighlighterVisitor(SensorContext sensorContext) {
  this.sensorContext = sensorContext;
  fileSystem = sensorContext.fileSystem();
}
 
Example 15
Source File: IssueSaver.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
public IssueSaver(SensorContext sensorContext, GherkinChecks checks) {
  this.sensorContext = sensorContext;
  this.fileSystem = sensorContext.fileSystem();
  this.checks = checks;
}