use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.
the class PageCountLinesTest method testCountLinesJspFile.
@Test
public void testCountLinesJspFile() {
List<Node> nodeList = lexer.parse(readFile("checks/AvoidHtmlCommentCheck/document.jsp"));
HtmlSourceCode htmlSourceCode = new HtmlSourceCode(new TestInputFileBuilder("key", "testdocument.jsp").setModuleBaseDir(new File(".").toPath()).build());
scanner.scan(nodeList, htmlSourceCode);
assertThat(htmlSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(2);
assertThat(htmlSourceCode.getDetailedLinesOfCode()).containsOnly(1, 3);
assertThat(htmlSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(6);
}
use of org.sonar.plugins.html.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) {
Deque<TagNode> openElementStack = new ArrayDeque<>();
for (Node node : nodeList) {
if (node.getNodeType() != NodeType.TAG) {
continue;
}
TagNode element = (TagNode) node;
// start element
if (!element.isEndElement()) {
TagNode parent = openElementStack.peek();
while (parent != null && (shouldCloseParent(nodeName(element), nodeName(parent)) || isVoidElement(parent))) {
openElementStack.pop();
parent = openElementStack.peek();
}
element.setParent(parent);
openElementStack.push(element);
}
// end element
if (isEndElement(element) && !openElementStack.isEmpty()) {
TagNode openElement = openElementStack.peek();
if (openElement.equalsElementName(element.getNodeName())) {
openElementStack.pop();
} else {
// non-well formed, close HTML elements if there is matching open element
if (openElementStack.stream().anyMatch(tag -> tag.equalsElementName(element.getNodeName()))) {
while (!openElement.equalsElementName(element.getNodeName()) && isHtmlElement(openElement)) {
openElement = openElementStack.pop();
}
}
}
}
}
}
use of org.sonar.plugins.html.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.html.node.Node in project sonar-web by SonarSource.
the class TextTokenizer method consume.
@Override
public boolean consume(CodeReader codeReader, List<Node> nodeList) {
Node node = createNode();
setStartPosition(codeReader, node);
StringBuilder stringBuilder = new StringBuilder();
if (inScript(nodeList)) {
codeReader.popTo(new EndScriptMatcher(codeReader), stringBuilder);
} else {
codeReader.popTo(endTokenMatcher, stringBuilder);
}
node.setCode(stringBuilder.toString());
setEndPosition(codeReader, node);
nodeList.add(node);
return true;
}
use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.
the class AbstractTokenizer method consume.
@Override
public boolean consume(CodeReader codeReader, T nodeList) {
if (equalsIgnoreCase(codeReader.peek(startChars.length), startChars)) {
Node node = createNode();
setStartPosition(codeReader, node);
StringBuilder stringBuilder = new StringBuilder();
codeReader.popTo(getEndMatcher(codeReader), stringBuilder);
for (int i = 0; i < endChars.length; i++) {
codeReader.pop(stringBuilder);
}
node.setCode(stringBuilder.toString());
setEndPosition(codeReader, node);
addNode(nodeList, node);
return true;
} else {
return false;
}
}
Aggregations