Search in sources :

Example 1 with NodeVisitor

use of org.jsoup.select.NodeVisitor in project jsoup by jhy.

the class NodeTest method traverse.

@Test
public void traverse() {
    Document doc = Jsoup.parse("<div><p>Hello</p></div><div>There</div>");
    final StringBuilder accum = new StringBuilder();
    doc.select("div").first().traverse(new NodeVisitor() {

        public void head(Node node, int depth) {
            accum.append("<" + node.nodeName() + ">");
        }

        public void tail(Node node, int depth) {
            accum.append("</" + node.nodeName() + ">");
        }
    });
    assertEquals("<div><p><#text></#text></p></div>", accum.toString());
}
Also used : NodeVisitor(org.jsoup.select.NodeVisitor) Test(org.junit.Test)

Example 2 with NodeVisitor

use of org.jsoup.select.NodeVisitor in project flow by vaadin.

the class TemplateParser method collectIncludeNodes.

private static List<TextNode> collectIncludeNodes(Element element) {
    List<TextNode> includeNodes = new ArrayList<>();
    new NodeTraversor(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
        // nop
        }

        @Override
        public void tail(Node node, int depth) {
            if (node instanceof TextNode) {
                TextNode textNode = (TextNode) node;
                String text = textNode.getWholeText();
                if (text.contains(INCLUDE_PREFIX)) {
                    includeNodes.add(textNode);
                }
            }
        }
    }).traverse(element);
    return includeNodes;
}
Also used : TextNode(org.jsoup.nodes.TextNode) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) Node(org.jsoup.nodes.Node) ArrayList(java.util.ArrayList) TextNode(org.jsoup.nodes.TextNode) NodeTraversor(org.jsoup.select.NodeTraversor) NodeVisitor(org.jsoup.select.NodeVisitor)

Example 3 with NodeVisitor

use of org.jsoup.select.NodeVisitor in project flow by vaadin.

the class TemplateDataAnalyzer method inspectTwoWayBindings.

private void inspectTwoWayBindings(org.jsoup.nodes.Element element) {
    Matcher matcher = TWO_WAY_BINDING_PATTERN.matcher("");
    element.traverse(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
            // inside text content.
            for (Attribute attribute : node.attributes()) {
                matcher.reset(attribute.getValue());
                if (matcher.matches()) {
                    String path = matcher.group(1);
                    addTwoWayBindingPath(path);
                }
            }
        }

        @Override
        public void tail(Node node, int depth) {
        // Nop
        }
    });
}
Also used : Matcher(java.util.regex.Matcher) Attribute(org.jsoup.nodes.Attribute) Node(org.jsoup.nodes.Node) NodeVisitor(org.jsoup.select.NodeVisitor)

Example 4 with NodeVisitor

use of org.jsoup.select.NodeVisitor in project structr by structr.

the class MicroformatParser method extractChildContent.

private Object extractChildContent(final Element element) {
    final List<String> parts = new LinkedList<>();
    element.traverse(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
            if (node instanceof Element) {
                final Element element = (Element) node;
                final Set<String> classes = element.classNames();
                removeEmpty(classes);
                if (classes.isEmpty()) {
                    parts.add(element.ownText());
                }
            }
        }

        @Override
        public void tail(Node node, int depth) {
        }
    });
    if (parts.isEmpty()) {
        final String ownText = element.ownText();
        if (StringUtils.isNotBlank(ownText)) {
            parts.add(element.ownText());
        }
    }
    if (parts.isEmpty()) {
        return null;
    }
    if (parts.size() == 1) {
        return parts.get(0);
    }
    return parts;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Node(org.jsoup.nodes.Node) Element(org.jsoup.nodes.Element) LinkedList(java.util.LinkedList) NodeVisitor(org.jsoup.select.NodeVisitor)

Example 5 with NodeVisitor

use of org.jsoup.select.NodeVisitor in project jsoup by jhy.

the class Element method text.

/**
     * Gets the combined text of this element and all its children. Whitespace is normalized and trimmed.
     * <p>
     * For example, given HTML {@code <p>Hello  <b>there</b> now! </p>}, {@code p.text()} returns {@code "Hello there now!"}
     *
     * @return unencoded text, or empty string if none.
     * @see #ownText()
     * @see #textNodes()
     */
public String text() {
    final StringBuilder accum = new StringBuilder();
    new NodeTraversor(new NodeVisitor() {

        public void head(Node node, int depth) {
            if (node instanceof TextNode) {
                TextNode textNode = (TextNode) node;
                appendNormalisedText(accum, textNode);
            } else if (node instanceof Element) {
                Element element = (Element) node;
                if (accum.length() > 0 && (element.isBlock() || element.tag.getName().equals("br")) && !TextNode.lastCharIsWhitespace(accum))
                    accum.append(" ");
            }
        }

        public void tail(Node node, int depth) {
        }
    }).traverse(this);
    return accum.toString().trim();
}
Also used : NodeTraversor(org.jsoup.select.NodeTraversor) NodeVisitor(org.jsoup.select.NodeVisitor)

Aggregations

NodeVisitor (org.jsoup.select.NodeVisitor)8 Node (org.jsoup.nodes.Node)4 Set (java.util.Set)3 Element (org.jsoup.nodes.Element)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 Extensions (com.vladsch.flexmark.profiles.pegdown.Extensions)1 PegdownOptionsAdapter (com.vladsch.flexmark.profiles.pegdown.PegdownOptionsAdapter)1 DataHolder (com.vladsch.flexmark.util.options.DataHolder)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLEncoder (java.net.URLEncoder)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 java.util.concurrent (java.util.concurrent)1 Matcher (java.util.regex.Matcher)1