Using the generated code
Classes generated by XVisitor
XVisitor adds the following classes to the JAXB model:
Visitable
public interface Visitable {
public VisitorAction accept(Visitor visitor);
}
For any complex type of your schema, the corresponding Java class generated by xjc implements this interface. XVisitor adds an accept(Visitor) method implementation to each complex type class.
Visitor
public interface Visitor {
public VisitorAction enter(Foo bean);
public VisitorAction leave(Foo bean);
public VisitorAction visit(String text);
}
The Visitor interface has an enter() and a leave() for each complex type from the schema and a visit() method for plain text children of complex types with mixed content.
VisitorAction
public enum VisitorAction {
CONTINUE,
SKIP,
TERMINATE;
}
VisitorAction is the return type of all Visitor and Visitable methods. By default, accept() traverses all descendants of the current node in depth-first order, calling enter() before visiting the first child and calling leave() after visiting the last child.
If you return SKIP from an enter() method, XVisitor will skip the children of the current node and directly invoke the leave() method of the current node. The traversal then continues with the next sibling of the current node.
If you return TERMINATE from any Visitor method, the Visitor will terminate its traversal, but cleanly unwind the call stack by invoking the leave() method of any node that has been entered.
BaseVisitor
BaseVisitor is a default no-op Visitor implementation. For creating your own Visitor classes, you can simply extend this class and override some of its methods as needed.
Example
See the DocbookVisitorTest for a not-so-trivial example. This integration test implements a SectionTitleVisitor which traverses (the JAXB model of) a DocBook document, collecting all section titles and counting the number of paragraphs and sections.