use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class ElementTokenizer method parseNestedTag.
/**
* Parse a nested tag with PageLexer.
* The nested tag is added as an attribute to its parent element.
*/
private static void parseNestedTag(CodeReader codeReader, TagNode element) {
PageLexer nestedPageLexer = new PageLexer();
List<Node> nodeList = nestedPageLexer.nestedParse(codeReader);
// add the nested tags as attribute.
for (Node node : nodeList) {
element.getAttributes().add(new Attribute(node.getCode()));
}
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageLexer method parse.
/**
* Parse the input into a list of tokens, with parent/child relations between the tokens.
*/
public List<Node> parse(Reader reader) {
// CodeReader reads the file stream
CodeReader codeReader = new CodeReader(reader);
// ArrayList collects the nodes
List<Node> nodeList = new ArrayList<>();
// ChannelDispatcher manages the tokenizers
ChannelDispatcher<List<Node>> channelDispatcher = ChannelDispatcher.builder().addChannels((Channel[]) tokenizers.toArray(new Channel[tokenizers.size()])).build();
channelDispatcher.consume(codeReader, nodeList);
createNodeHierarchy(nodeList);
return nodeList;
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageLexer method createNodeHierarchy.
/**
* Scan the nodes and build the hierarchy of parent and child nodes.
*/
private static void createNodeHierarchy(List<Node> nodeList) {
TagNode current = null;
for (Node node : nodeList) {
if (node.getNodeType() == NodeType.TAG) {
TagNode element = (TagNode) node;
// start element
if (!element.isEndElement()) {
element.setParent(current);
current = element;
}
// end element
if ((element.isEndElement() || element.hasEnd()) && current != null) {
current = current.getParent();
}
}
}
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageCountLinesTest method testCountLinesHtmlFile.
@Test
public void testCountLinesHtmlFile() throws FileNotFoundException {
List<Node> nodeList = lexer.parse(new FileReader(TestUtils.getResource("checks/AvoidHtmlCommentCheck/document.html")));
String relativePath = "test/document.html";
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(8);
assertThat(webSourceCode.getDetailedLinesOfCode()).containsOnly(1, 2, 3, 4, 6, 7, 8, 9);
assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(1);
assertThat(webSourceCode.getDetailedLinesOfComments()).containsOnly(5);
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageCountLinesTest method testCountLines.
@Test
public void testCountLines() throws FileNotFoundException {
java.io.File file = TestUtils.getResource("src/main/webapp/user-properties.jsp");
List<Node> nodeList = lexer.parse(new FileReader(file));
assertThat(nodeList.size()).isGreaterThan(100);
// new File("test", "user-properties.jsp");
String relativePath = "test/user-properties.jsp";
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(227);
assertThat(webSourceCode.getDetailedLinesOfCode().size()).isEqualTo(224);
assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(14);
assertThat(webSourceCode.getDetailedLinesOfComments().size()).isEqualTo(14);
}
Aggregations