Search in sources :

Example 1 with TokenChunker

use of org.sonar.duplications.token.TokenChunker in project sonarqube by SonarSource.

the class JavaCpdBlockIndexer method createIndex.

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 = ((DefaultInputFile) inputFile).key();
        List<Statement> statements;
        try (InputStream is = new FileInputStream(inputFile.file());
            Reader reader = new InputStreamReader(is, fs.encoding())) {
            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 = blockChunker.chunk(resourceEffectiveKey, statements);
        index.insert(inputFile, blocks);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TokenChunker(org.sonar.duplications.token.TokenChunker) Statement(org.sonar.duplications.statement.Statement) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) StatementChunker(org.sonar.duplications.statement.StatementChunker) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Example 2 with TokenChunker

use of org.sonar.duplications.token.TokenChunker in project sonarqube by SonarSource.

the class JavaCpdBlockIndexerSensor method createIndex.

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TokenChunker(org.sonar.duplications.token.TokenChunker) Statement(org.sonar.duplications.statement.Statement) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) StatementChunker(org.sonar.duplications.statement.StatementChunker) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 InputFile (org.sonar.api.batch.fs.InputFile)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 Block (org.sonar.duplications.block.Block)2 BlockChunker (org.sonar.duplications.block.BlockChunker)2 Statement (org.sonar.duplications.statement.Statement)2 StatementChunker (org.sonar.duplications.statement.StatementChunker)2 TokenChunker (org.sonar.duplications.token.TokenChunker)2 FileInputStream (java.io.FileInputStream)1