Search in sources :

Example 16 with Node

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);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Node(org.sonar.plugins.html.node.Node) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) File(java.io.File) Test(org.junit.Test)

Example 17 with Node

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();
                    }
                }
            }
        }
    }
}
Also used : Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode) TagNode(org.sonar.plugins.html.node.TagNode) ArrayDeque(java.util.ArrayDeque)

Example 18 with Node

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;
}
Also used : Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode) Channel(org.sonar.channel.Channel) ArrayList(java.util.ArrayList) CodeReader(org.sonar.channel.CodeReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with Node

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;
}
Also used : Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) TagNode(org.sonar.plugins.html.node.TagNode)

Example 20 with Node

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;
    }
}
Also used : Node(org.sonar.plugins.html.node.Node)

Aggregations

Node (org.sonar.plugins.html.node.Node)39 Test (org.junit.Test)28 TagNode (org.sonar.plugins.html.node.TagNode)24 StringReader (java.io.StringReader)23 TextNode (org.sonar.plugins.html.node.TextNode)21 CommentNode (org.sonar.plugins.html.node.CommentNode)19 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)19 FileReader (java.io.FileReader)4 Attribute (org.sonar.plugins.html.node.Attribute)3 HtmlSourceCode (org.sonar.plugins.html.visitor.HtmlSourceCode)3 ArrayList (java.util.ArrayList)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 CodeReader (org.sonar.channel.CodeReader)2 File (java.io.File)1 Reader (java.io.Reader)1 ArrayDeque (java.util.ArrayDeque)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1