Search in sources :

Example 1 with PageLexer

use of org.sonar.plugins.web.lex.PageLexer in project sonar-web by SonarSource.

the class PageCountLinesTest method setUp.

@Before
public void setUp() {
    lexer = new PageLexer();
    scanner = new HtmlAstScanner(Collections.emptyList());
    scanner.addVisitor(new PageCountLines());
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner) Before(org.junit.Before)

Example 2 with PageLexer

use of org.sonar.plugins.web.lex.PageLexer in project sonar-web by SonarSource.

the class TestHelper method scan.

public static WebSourceCode scan(File file, DefaultNodeVisitor visitor) {
    FileReader fileReader;
    try {
        fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
        throw Throwables.propagate(e);
    }
    WebSourceCode result = new WebSourceCode(new DefaultInputFile("key", file.getPath()).setLanguage(WebConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()));
    HtmlAstScanner walker = new HtmlAstScanner(ImmutableList.of(new PageCountLines(), new ComplexityVisitor()));
    walker.addVisitor(visitor);
    walker.scan(new PageLexer().parse(fileReader), result, Charsets.UTF_8);
    return result;
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) ComplexityVisitor(org.sonar.plugins.web.analyzers.ComplexityVisitor) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileNotFoundException(java.io.FileNotFoundException) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) PageCountLines(org.sonar.plugins.web.analyzers.PageCountLines) FileReader(java.io.FileReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner)

Example 3 with PageLexer

use of org.sonar.plugins.web.lex.PageLexer in project sonar-web by SonarSource.

the class HeaderCheckTest method scanWithWrongInputFile.

public static void scanWithWrongInputFile(File file, DefaultNodeVisitor visitor) {
    HtmlAstScanner walker = new HtmlAstScanner(Collections.emptyList());
    walker.addVisitor(visitor);
    FileReader reader;
    try {
        reader = new FileReader(file);
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to read file");
    }
    WebSourceCode result = new WebSourceCode(new DefaultInputFile("key", /* wrong path */
    ".").setLanguage(WebConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()));
    walker.scan(new PageLexer().parse(reader), // won't be able to resolve the file
    result, StandardCharsets.UTF_8);
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner) ExpectedException(org.junit.rules.ExpectedException)

Example 4 with PageLexer

use of org.sonar.plugins.web.lex.PageLexer in project sonar-web by SonarSource.

the class NoSonarScannerTest method scanNoSonar.

@Test
public void scanNoSonar() {
    List<Node> nodeList = new PageLexer().parse(new StringReader("<table>\n<!-- //NOSONAR --><td>\n</table>"));
    WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", "dummy.jsp"));
    NoSonarFilter noSonarFilter = spy(new NoSonarFilter());
    HtmlAstScanner pageScanner = new HtmlAstScanner(Collections.emptyList());
    pageScanner.addVisitor(new NoSonarScanner(noSonarFilter));
    pageScanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
    verify(noSonarFilter, times(1)).noSonarInFile(any(InputFile.class), isOnlyIgnoringLine2());
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Node(org.sonar.plugins.web.node.Node) StringReader(java.io.StringReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 5 with PageLexer

use of org.sonar.plugins.web.lex.PageLexer in project sonar-web by SonarSource.

the class WebSensor method execute.

@Override
public void execute(SensorContext sensorContext) {
    // configure the lexer
    final PageLexer lexer = new PageLexer();
    FileSystem fileSystem = sensorContext.fileSystem();
    // configure page scanner and the visitors
    final HtmlAstScanner scanner = setupScanner(sensorContext);
    FilePredicates predicates = fileSystem.predicates();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.and(predicates.hasType(InputFile.Type.MAIN), predicates.hasLanguage(WebConstants.LANGUAGE_KEY)));
    for (InputFile inputFile : inputFiles) {
        WebSourceCode sourceCode = new WebSourceCode(inputFile);
        try (FileReader reader = new FileReader(inputFile.file())) {
            scanner.scan(lexer.parse(reader), sourceCode, fileSystem.encoding());
            saveMetrics(sensorContext, sourceCode);
            saveLineLevelMeasures(inputFile, sourceCode);
        } catch (Exception e) {
            LOG.error("Cannot analyze file " + inputFile.file().getAbsolutePath(), e);
        }
    }
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

PageLexer (org.sonar.plugins.web.lex.PageLexer)5 InputFile (org.sonar.api.batch.fs.InputFile)4 HtmlAstScanner (org.sonar.plugins.web.visitor.HtmlAstScanner)4 FileReader (java.io.FileReader)3 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 WebSourceCode (org.sonar.plugins.web.visitor.WebSourceCode)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1 FilePredicates (org.sonar.api.batch.fs.FilePredicates)1 FileSystem (org.sonar.api.batch.fs.FileSystem)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1 ComplexityVisitor (org.sonar.plugins.web.analyzers.ComplexityVisitor)1 PageCountLines (org.sonar.plugins.web.analyzers.PageCountLines)1 Node (org.sonar.plugins.web.node.Node)1