Class TreeWalker

java.lang.Object
eu.maveniverse.domtrip.TreeWalker

public class TreeWalker extends Object
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 Details

    • onEnter

      public TreeWalker onEnter(Function<Element, DomTripVisitor.Action> handler)
      Sets the callback invoked when entering each element.

      The function receives the element and returns an DomTripVisitor.Action to control traversal flow.

      Parameters:
      handler - the enter callback
      Returns:
      this walker for method chaining
    • onExit

      public TreeWalker onExit(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(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(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(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.