Interface DomTripVisitor
This complements the existing stream-based navigation (Element.descendants(),
ElementQuery) with a pattern better suited for transformations needing context
tracking (depth, ancestors, accumulated state).
Traversal Order:
The visitor performs a depth-first traversal: for each element,
enterElement(Element) is called first, then all children are visited
recursively, and finally exitElement(Element) is called.
Flow Control:
Visit methods return an DomTripVisitor.Action to control traversal:
DomTripVisitor.Action.CONTINUE- Continue normal traversalDomTripVisitor.Action.SKIP- Skip children of current element (only meaningful fromenterElement)DomTripVisitor.Action.STOP- Abort traversal entirely
Usage Example:
element.accept(new DomTripVisitor() {
@Override
public Action enterElement(Element e) {
if ("metadata".equals(e.localName())) {
return Action.SKIP; // don't descend into metadata
}
System.out.println("Entering: " + e.name());
return Action.CONTINUE;
}
@Override
public void exitElement(Element e) {
System.out.println("Exiting: " + e.name());
}
@Override
public Action visitComment(Comment c) {
System.out.println("Comment: " + c.content());
return Action.CONTINUE;
}
});
Stateful Visitor Example:
class NamespaceCollector implements DomTripVisitor {
private final Map<String, String> namespaces = new LinkedHashMap<>();
private final Deque<String> path = new ArrayDeque<>();
@Override
public Action enterElement(Element e) {
path.push(e.localName());
String ns = e.namespaceURI();
if (ns != null && !namespaces.containsKey(ns)) {
namespaces.put(ns, String.join("/", path));
}
return Action.CONTINUE;
}
@Override
public void exitElement(Element e) {
path.pop();
}
public Map<String, String> result() { return namespaces; }
}
- Since:
- 1.3.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumControls traversal flow during a visitor walk. -
Method Summary
Modifier and TypeMethodDescriptiondefault DomTripVisitor.ActionenterElement(Element element) Called when entering an element during depth-first traversal.default voidexitElement(Element element) Called after all children of an element have been visited.default DomTripVisitor.ActionvisitComment(Comment comment) Called for comment nodes during traversal.default DomTripVisitor.ActionCalled for processing instruction nodes during traversal.default DomTripVisitor.ActionCalled for text nodes during traversal.
-
Method Details
-
enterElement
Called when entering an element during depth-first traversal.This is called before visiting any of the element's children. Return
DomTripVisitor.Action.SKIPto skip the element's children, orDomTripVisitor.Action.STOPto abort the entire traversal.- Parameters:
element- the element being entered- Returns:
- the action to take:
DomTripVisitor.Action.CONTINUE,DomTripVisitor.Action.SKIP, orDomTripVisitor.Action.STOP
-
exitElement
Called after all children of an element have been visited.This is always called for elements that were entered (even if children were skipped via
DomTripVisitor.Action.SKIP), unless the traversal was stopped viaDomTripVisitor.Action.STOP.- Parameters:
element- the element being exited
-
visitText
Called for text nodes during traversal.- Parameters:
text- the text node being visited- Returns:
- the action to take:
DomTripVisitor.Action.CONTINUEorDomTripVisitor.Action.STOP
-
visitComment
Called for comment nodes during traversal.- Parameters:
comment- the comment node being visited- Returns:
- the action to take:
DomTripVisitor.Action.CONTINUEorDomTripVisitor.Action.STOP
-
visitProcessingInstruction
Called for processing instruction nodes during traversal.- Parameters:
pi- the processing instruction being visited- Returns:
- the action to take:
DomTripVisitor.Action.CONTINUEorDomTripVisitor.Action.STOP
-