Search in sources :

Example 1 with HtmlAstScanner

use of org.sonar.plugins.web.visitor.HtmlAstScanner 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 HtmlAstScanner

use of org.sonar.plugins.web.visitor.HtmlAstScanner 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 HtmlAstScanner

use of org.sonar.plugins.web.visitor.HtmlAstScanner 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 HtmlAstScanner

use of org.sonar.plugins.web.visitor.HtmlAstScanner 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)

Example 5 with HtmlAstScanner

use of org.sonar.plugins.web.visitor.HtmlAstScanner in project sonar-web by SonarSource.

the class WebSensor method setupScanner.

/**
 * Create PageScanner with Visitors.
 */
private HtmlAstScanner setupScanner(SensorContext context) {
    HtmlAstScanner scanner = new HtmlAstScanner(ImmutableList.of(new WebTokensVisitor(context), new PageCountLines(), new ComplexityVisitor(), new NoSonarScanner(noSonarFilter)));
    for (Object check : checks.all()) {
        ((AbstractPageCheck) check).setRuleKey(checks.ruleKey(check));
        scanner.addVisitor((AbstractPageCheck) check);
    }
    return scanner;
}
Also used : ComplexityVisitor(org.sonar.plugins.web.analyzers.ComplexityVisitor) PageCountLines(org.sonar.plugins.web.analyzers.PageCountLines) NoSonarScanner(org.sonar.plugins.web.visitor.NoSonarScanner) AbstractPageCheck(org.sonar.plugins.web.checks.AbstractPageCheck) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner)

Aggregations

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