private void processFileError(InputFile inputFile, SensorContext context) { Path ioFile = inputFile.file().toPath(); Path errorFile = ioFile.resolveSibling(ioFile.getFileName() + ERROR_EXTENSION).toAbsolutePath(); if (Files.exists(errorFile) && Files.isRegularFile(errorFile)) { LOG.debug("Processing " + errorFile.toString()); try { List<String> lines = Files.readAllLines(errorFile, context.fileSystem().encoding()); for (String line : lines) { if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(line, inputFile, context); } } catch (IOException e) { throw new IllegalStateException(e); } } }
private void processFileMeasures(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); if (measureFile.exists()) { LOG.debug("Processing " + measureFile.getAbsolutePath()); try { FileLinesContext linesContext = contextFactory.createFor(inputFile); List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name()); int lineNumber = 0; for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processMeasure(linesContext, measureFile, lineNumber, line); } linesContext.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
private void processFileHighlighting(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION); if (highlightingFile.exists()) { LOG.debug("Processing " + highlightingFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewHighlighting highlighting = context.newHighlighting() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(highlightingFile, lineNumber, highlighting, line); } highlighting.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
private void processFileSymbol(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION); if (symbolFile.exists()) { LOG.debug("Processing " + symbolFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewSymbolTable symbolTable = context.newSymbolTable() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(symbolFile, lineNumber, symbolTable, line); } symbolTable.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
try { StringBuilder sb = new StringBuilder(); for (String line : FileUtils.readLines(inputFile.file(), inputFile.charset())) { int startOffset = 0; int endOffset = 0;
private void createIndex(Iterable<InputFile> sourceFiles) { TokenChunker tokenChunker = JavaTokenProducer.build(); StatementChunker statementChunker = JavaStatementBuilder.build(); BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE); for (InputFile inputFile : sourceFiles) { LOG.debug("Populating index from {}", inputFile); String resourceEffectiveKey = inputFile.key(); List<Statement> statements; try (InputStream is = inputFile.inputStream(); Reader reader = new InputStreamReader(is, inputFile.charset())) { statements = statementChunker.chunk(tokenChunker.chunk(reader)); } catch (FileNotFoundException e) { throw new IllegalStateException("Cannot find file " + inputFile.file(), e); } catch (IOException e) { throw new IllegalStateException("Exception handling file: " + inputFile.file(), e); } List<Block> blocks; try { blocks = blockChunker.chunk(resourceEffectiveKey, statements); } catch (Exception e) { throw new IllegalStateException("Cannot process file " + inputFile.file(), e); } index.insert(inputFile, blocks); } }
@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY, Xoo2.KEY))) { File ioFile = file.file(); File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); processFileMeasures(file, measureFile, context); InputDir inputDir = context.fileSystem().inputDir(ioFile.getParentFile()); if (inputDir != null) { processFileMeasures(inputDir, new File(ioFile.getParentFile(), "folder" + MEASURES_EXTENSION), context); } } processFileMeasures(context.module(), new File(context.fileSystem().baseDir(), "module" + MEASURES_EXTENSION), context); } }
/** * Compute a MD5 hash of each line of the file after removing of all blank chars */ public static void computeLineHashesForIssueTracking(InputFile f, LineHashConsumer consumer) { try { readFile(f.inputStream(), f.charset(), f.absolutePath(), new CharHandler[] {new LineHashComputer(consumer, f.file())}); } catch (IOException e) { throw new IllegalStateException("Failed to compute line hashes for " + f.absolutePath(), e); } } }
@VisibleForTesting protected void processFile(InputFile inputFile, BlameOutput result) { File ioFile = inputFile.file(); File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION); if (!scmDataFile.exists()) {
private static void analyse(SensorContext context) { FileSystem fs = context.fileSystem(); FilePredicates p = fs.predicates(); RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); StreamSupport.stream(fs.inputFiles(p.hasType(Type.MAIN)).spliterator(), false) .map(file -> fs.inputDir(file.file().getParentFile())) .filter(Objects::nonNull) .distinct() .forEach(inputDir -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputDir) .message("This issue is generated for any non-empty directory")) .save(); }); } }
@Test public void is_file() throws Exception { // relative file assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue(); // absolute file assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue(); assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue(); assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue(); assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse(); }
@Test public void has_absolute_path() throws Exception { String path = javaFile.file().getAbsolutePath(); assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue(); assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue(); assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse(); assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse(); }
@Test public void has_path() throws Exception { // is relative path assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue(); assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse(); // is absolute path String path = javaFile.file().getAbsolutePath(); assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue(); assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse(); }
private static void createIssues(InputFile file, SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); InputDir inputDir = context.fileSystem().inputDir(file.file().getParentFile()); if (inputDir != null) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputDir) .message("This issue is generated for file " + file.relativePath())) .save(); } } }
@Override public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) { String path = inputFile.relativePath(); if (!caseSensitiveFileExtension) { String extension = super.sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName())); if (StringUtils.isNotBlank(extension)) { path = StringUtils.removeEndIgnoreCase(path, extension); path = path + extension; } } return path != null && pattern.match(path); }
@Override public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) { String path = inputFile.absolutePath(); if (!caseSensitiveFileExtension) { String extension = super.sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName())); if (StringUtils.isNotBlank(extension)) { path = StringUtils.removeEndIgnoreCase(path, extension); path = path + extension; } } return pattern.match(path); }
/** * Compute a MD5 hash of each line of the file after removing of all blank chars */ public static void computeLineHashesForIssueTracking(InputFile f, LineHashConsumer consumer) { try { readFile(f.inputStream(), f.charset(), f.absolutePath(), new CharHandler[] {new LineHashComputer(consumer, f.file())}); } catch (IOException e) { throw new IllegalStateException("Failed to compute line hashes for " + f.absolutePath(), e); } } }