Class TreeWalker
java.lang.Object
eu.maveniverse.domtrip.TreeWalker
Lambda-friendly builder for configuring and executing tree traversals.
TreeWalker provides a fluent API alternative to implementing the
DomTripVisitor interface directly. It is particularly useful for
simple traversals where only a few visitor methods are needed.
Usage Example:
element.walk()
.onEnter(e -> {
if ("secret".equals(e.localName())) {
e.textContent("***");
return DomTripVisitor.Action.SKIP;
}
return DomTripVisitor.Action.CONTINUE;
})
.onExit(e -> System.out.println("Exiting: " + e.name()))
.onText(t -> {
System.out.println("Text: " + t.content());
return DomTripVisitor.Action.CONTINUE;
})
.execute();
- Since:
- 1.3.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Executes the traversal with the configured callbacks.onComment(Function<Comment, DomTripVisitor.Action> handler) Sets the callback invoked for each comment node.onEnter(Function<Element, DomTripVisitor.Action> handler) Sets the callback invoked when entering each element.Sets the callback invoked when exiting each element.Sets the callback invoked for each processing instruction node.onText(Function<Text, DomTripVisitor.Action> handler) Sets the callback invoked for each text node.
-
Method Details
-
onEnter
Sets the callback invoked when entering each element.The function receives the element and returns an
DomTripVisitor.Actionto control traversal flow.- Parameters:
handler- the enter callback- Returns:
- this walker for method chaining
-
onExit
Sets the callback invoked when exiting each element.- Parameters:
handler- the exit callback- Returns:
- this walker for method chaining
-
onText
Sets the callback invoked for each text node.- Parameters:
handler- the text callback- Returns:
- this walker for method chaining
-
onComment
Sets the callback invoked for each comment node.- Parameters:
handler- the comment callback- Returns:
- this walker for method chaining
-
onProcessingInstruction
public TreeWalker onProcessingInstruction(Function<ProcessingInstruction, DomTripVisitor.Action> handler) Sets the callback invoked for each processing instruction node.- Parameters:
handler- the processing instruction callback- Returns:
- this walker for method chaining
-
execute
public void execute()Executes the traversal with the configured callbacks.Performs a depth-first walk of the tree starting from the root node, invoking the configured callbacks for each node encountered.
-