Search in sources :

Example 11 with Node

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

Example 12 with Node

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

Example 13 with Node

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

Example 14 with Node

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

Example 15 with Node

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

Aggregations

Node (org.sonar.plugins.web.node.Node)25 TagNode (org.sonar.plugins.web.node.TagNode)19 Test (org.junit.Test)18 TextNode (org.sonar.plugins.web.node.TextNode)17 CommentNode (org.sonar.plugins.web.node.CommentNode)15 DirectiveNode (org.sonar.plugins.web.node.DirectiveNode)15 StringReader (java.io.StringReader)10 FileReader (java.io.FileReader)7 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 File (java.io.File)3 WebSourceCode (org.sonar.plugins.web.visitor.WebSourceCode)3 ArrayList (java.util.ArrayList)2 CodeReader (org.sonar.channel.CodeReader)2 Attribute (org.sonar.plugins.web.node.Attribute)2 List (java.util.List)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1 Channel (org.sonar.channel.Channel)1 PageLexer (org.sonar.plugins.web.lex.PageLexer)1