Package eu.maveniverse.domtrip
Class TreeWalker
- java.lang.Object
-
- eu.maveniverse.domtrip.TreeWalker
-
public class TreeWalker extends java.lang.ObjectLambda-friendly builder for configuring and executing tree traversals.TreeWalker provides a fluent API alternative to implementing the
DomTripVisitorinterface 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:
DomTripVisitor,Element.walk()
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute()Executes the traversal with the configured callbacks.TreeWalkeronComment(java.util.function.Function<Comment,DomTripVisitor.Action> handler)Sets the callback invoked for each comment node.TreeWalkeronEnter(java.util.function.Function<Element,DomTripVisitor.Action> handler)Sets the callback invoked when entering each element.TreeWalkeronExit(java.util.function.Consumer<Element> handler)Sets the callback invoked when exiting each element.TreeWalkeronProcessingInstruction(java.util.function.Function<ProcessingInstruction,DomTripVisitor.Action> handler)Sets the callback invoked for each processing instruction node.TreeWalkeronText(java.util.function.Function<Text,DomTripVisitor.Action> handler)Sets the callback invoked for each text node.
-
-
-
Method Detail
-
onEnter
public TreeWalker onEnter(java.util.function.Function<Element,DomTripVisitor.Action> handler)
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
public TreeWalker onExit(java.util.function.Consumer<Element> handler)
Sets the callback invoked when exiting each element.- Parameters:
handler- the exit callback- Returns:
- this walker for method chaining
-
onText
public TreeWalker onText(java.util.function.Function<Text,DomTripVisitor.Action> handler)
Sets the callback invoked for each text node.- Parameters:
handler- the text callback- Returns:
- this walker for method chaining
-
onComment
public TreeWalker onComment(java.util.function.Function<Comment,DomTripVisitor.Action> handler)
Sets the callback invoked for each comment node.- Parameters:
handler- the comment callback- Returns:
- this walker for method chaining
-
onProcessingInstruction
public TreeWalker onProcessingInstruction(java.util.function.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.
-
-