Search in sources :

Example 6 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project sonarqube by SonarSource.

the class XooTokenizer method tokenize.

@Override
public final void tokenize(SourceCode source, Tokens cpdTokens) {
    String fileName = source.getFileName();
    LOG.info("Using deprecated tokenizer extension point to tokenize {}", fileName);
    int lineIdx = 1;
    try {
        for (String line : FileUtils.readLines(new File(fileName), fs.encoding())) {
            for (String token : Splitter.on(" ").split(line)) {
                TokenEntry cpdToken = new TokenEntry(token, fileName, lineIdx);
                cpdTokens.add(cpdToken);
            }
            lineIdx++;
        }
    } catch (IOException e) {
        throw new IllegalStateException("Unable to tokenize", e);
    }
    cpdTokens.add(TokenEntry.getEOF());
}
Also used : TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) IOException(java.io.IOException) File(java.io.File)

Example 7 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project sonarqube by SonarSource.

the class XooTokenizerTest method testExecution.

@Test
public void testExecution() throws IOException {
    File source = new File(baseDir, "src/foo.xoo");
    FileUtils.write(source, "token1 token2 token3\ntoken4");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
    fileSystem.add(inputFile);
    XooTokenizer tokenizer = new XooTokenizer(fileSystem);
    SourceCode sourceCode = mock(SourceCode.class);
    when(sourceCode.getFileName()).thenReturn(inputFile.absolutePath());
    Tokens cpdTokens = new Tokens();
    tokenizer.tokenize(sourceCode, cpdTokens);
    // 4 tokens + EOF
    assertThat(cpdTokens.getTokens()).hasSize(5);
    assertThat(cpdTokens.getTokens().get(3)).isEqualTo(new TokenEntry("token4", "src/foo.xoo", 2));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SourceCode(net.sourceforge.pmd.cpd.SourceCode) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Tokens(net.sourceforge.pmd.cpd.Tokens) Test(org.junit.Test)

Aggregations

TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)7 File (java.io.File)3 SourceCode (net.sourceforge.pmd.cpd.SourceCode)3 Mark (net.sourceforge.pmd.cpd.Mark)2 Match (net.sourceforge.pmd.cpd.Match)2 Tokens (net.sourceforge.pmd.cpd.Tokens)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 CPD (net.sourceforge.pmd.cpd.CPD)1 CPDConfiguration (net.sourceforge.pmd.cpd.CPDConfiguration)1 JavaLanguage (net.sourceforge.pmd.cpd.JavaLanguage)1 Tokenizer (net.sourceforge.pmd.cpd.Tokenizer)1 Before (org.junit.Before)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 Document (org.w3c.dom.Document)1