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());
}
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;
}
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
}
});
}
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;
}
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();
}
Aggregations