Search in sources :

Example 11 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class ElementTokenizer method parseToken.

private void parseToken(Node node) {
    TagNode element = (TagNode) node;
    CodeReader codeReader = new CodeReader(node.getCode());
    ParseMode mode = ParseMode.BEFORE_NODE_NAME;
    for (int ch = codeReader.peek(); ch != -1; ch = codeReader.peek()) {
        // handle white space
        if (Character.isWhitespace(ch)) {
            codeReader.pop();
            continue;
        }
        // handle special characters
        switch(ch) {
            case '=':
                mode = ParseMode.BEFORE_ATTRIBUTE_VALUE;
                codeReader.pop();
                continue;
            case '<':
                nestedTag(element, codeReader, mode);
                continue;
            case '>':
            case '/':
            case '%':
            case '@':
                codeReader.pop();
                continue;
            default:
                break;
        }
        mode = parseToken(mode, codeReader, element);
    }
}
Also used : CodeReader(org.sonar.channel.CodeReader) TagNode(org.sonar.plugins.html.node.TagNode)

Example 12 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class TableHeaderHasIdOrScopeCheck method visitTrNode.

private void visitTrNode(TagNode node) {
    List<TagNode> row = node.getChildren();
    if (!tables.isEmpty() && !row.isEmpty()) {
        TableElement currentTable = tables.peek();
        for (TagNode element : row) {
            if (isThTag(element)) {
                currentTable.headers.add(element);
            }
        }
        currentTable.firstCol.add(row.get(0));
        if (currentTable.firstRow.isEmpty()) {
            currentTable.firstRow = row;
        }
    } else {
        // Sometimes rows are defined in separate files, and at this point we don't see them as part of a current table.
        // In this case we treat them as belonging to "not simple tables".
        raiseIssueOnTableHeadersWithoutScopeOrId(row);
    }
}
Also used : TagNode(org.sonar.plugins.html.node.TagNode)

Example 13 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class PageLexerTest method testLexer.

@Test
public void testLexer() throws FileNotFoundException {
    String fileName = "src/test/resources/src/main/webapp/create-salesorder.xhtml";
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(new FileReader(fileName));
    assertThat(nodeList.size()).isGreaterThan(50);
    // check tagnodes
    for (Node node : nodeList) {
        if (node instanceof TagNode) {
            assertThat(node.getCode()).startsWith("<");
            assertThat(node.getCode()).endsWith(">");
        }
    }
    showHierarchy(nodeList);
    // check hierarchy
    for (Node node : nodeList) {
        if (node instanceof TagNode) {
            TagNode tagNode = (TagNode) node;
            if (!tagNode.isEndElement()) {
                if (tagNode.equalsElementName("define")) {
                    assertThat(tagNode.getChildren()).as("Tag should have children: " + tagNode.getCode()).isNotEmpty();
                } else if (tagNode.equalsElementName("outputText")) {
                    assertThat(tagNode.getChildren()).isEmpty();
                }
            }
        }
    }
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) FileReader(java.io.FileReader) TagNode(org.sonar.plugins.html.node.TagNode) Test(org.junit.Test)

Example 14 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class PageLexerTest method testNestedTagInValue.

@Test
public void testNestedTagInValue() {
    String fragment = "<td label=\"Hello <c:if test='${param == true}'>World</c:if>\">Type</td>";
    StringReader reader = new StringReader(fragment);
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(reader);
    assertThat(nodeList).hasSize(3);
    assertThat(nodeList.get(0)).isInstanceOf(TagNode.class);
    assertThat(nodeList.get(1)).isInstanceOf(TextNode.class);
    assertThat(nodeList.get(2)).isInstanceOf(TagNode.class);
    TagNode tagNode = (TagNode) nodeList.get(0);
    assertThat(tagNode.getAttributes()).hasSize(1);
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) StringReader(java.io.StringReader) TagNode(org.sonar.plugins.html.node.TagNode) Test(org.junit.Test)

Example 15 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class PageLexerTest method showHierarchy.

private void showHierarchy(List<Node> nodeList) {
    StringBuilder sb = new StringBuilder();
    for (Node node : nodeList) {
        if (node.getClass() == TagNode.class && ((TagNode) node).getParent() == null) {
            TagNode root = (TagNode) node;
            printTag(sb, root, 0);
        // System.out.print(sb.toString());
        }
    }
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) TagNode(org.sonar.plugins.html.node.TagNode)

Aggregations

TagNode (org.sonar.plugins.html.node.TagNode)22 Node (org.sonar.plugins.html.node.Node)15 CommentNode (org.sonar.plugins.html.node.CommentNode)12 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)12 TextNode (org.sonar.plugins.html.node.TextNode)12 Test (org.junit.Test)10 StringReader (java.io.StringReader)9 Attribute (org.sonar.plugins.html.node.Attribute)3 FileReader (java.io.FileReader)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CodeReader (org.sonar.channel.CodeReader)1