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());
}
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;
}
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);
}
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);
}
}
}
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;
}
Aggregations