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