Search in sources :

Example 6 with TagNode

use of org.sonar.plugins.web.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.web.node.TagNode)

Example 7 with TagNode

use of org.sonar.plugins.web.node.TagNode 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()));
    }
}
Also used : Attribute(org.sonar.plugins.web.node.Attribute) TagNode(org.sonar.plugins.web.node.TagNode) Node(org.sonar.plugins.web.node.Node)

Example 8 with TagNode

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

Example 9 with TagNode

use of org.sonar.plugins.web.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));
    assertTrue(nodeList.size() > 50);
    // check tagnodes
    for (Node node : nodeList) {
        if (node instanceof TagNode) {
            assertTrue(node.getCode().startsWith("<"));
            assertTrue(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")) {
                    assertTrue("Tag should have children: " + tagNode.getCode(), tagNode.getChildren().size() > 0);
                } else if (tagNode.equalsElementName("outputText")) {
                    assertThat(tagNode.getChildren().size()).isEqualTo(0);
                }
            }
        }
    }
}
Also used : TagNode(org.sonar.plugins.web.node.TagNode) TextNode(org.sonar.plugins.web.node.TextNode) CommentNode(org.sonar.plugins.web.node.CommentNode) Node(org.sonar.plugins.web.node.Node) DirectiveNode(org.sonar.plugins.web.node.DirectiveNode) FileReader(java.io.FileReader) TagNode(org.sonar.plugins.web.node.TagNode) Test(org.junit.Test)

Example 10 with TagNode

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

the class PageLexerTest method testNestedTagInAttribute.

@Test
public void testNestedTagInAttribute() {
    String fragment = "<td id=\"typeCellHeader\"<c:if test='${param.typeNormalOrError == \"error\"}'>" + "style=\"display:none;\"</c:if>>Type" + "</td>";
    StringReader reader = new StringReader(fragment);
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(reader);
    assertEquals(3, nodeList.size());
    assertTrue(nodeList.get(0) instanceof TagNode);
    assertTrue(nodeList.get(1) instanceof TextNode);
    assertTrue(nodeList.get(2) instanceof TagNode);
    TagNode tagNode = (TagNode) nodeList.get(0);
    assertEquals(4, tagNode.getAttributes().size());
    // the embedded tags are added as attributes
    assertThat(tagNode.getAttributes().get(1).getValue()).isEmpty();
    assertThat(tagNode.getAttributes().get(3).getValue()).isEmpty();
}
Also used : TagNode(org.sonar.plugins.web.node.TagNode) TextNode(org.sonar.plugins.web.node.TextNode) CommentNode(org.sonar.plugins.web.node.CommentNode) Node(org.sonar.plugins.web.node.Node) DirectiveNode(org.sonar.plugins.web.node.DirectiveNode) StringReader(java.io.StringReader) TextNode(org.sonar.plugins.web.node.TextNode) TagNode(org.sonar.plugins.web.node.TagNode) Test(org.junit.Test)

Aggregations

TagNode (org.sonar.plugins.web.node.TagNode)16 Node (org.sonar.plugins.web.node.Node)13 CommentNode (org.sonar.plugins.web.node.CommentNode)11 DirectiveNode (org.sonar.plugins.web.node.DirectiveNode)11 TextNode (org.sonar.plugins.web.node.TextNode)11 Test (org.junit.Test)10 StringReader (java.io.StringReader)8 FileReader (java.io.FileReader)2 Attribute (org.sonar.plugins.web.node.Attribute)2 ArrayList (java.util.ArrayList)1 CodeReader (org.sonar.channel.CodeReader)1