users@jaxb.java.net

Re: XJC and the visitor pattern

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 26 Oct 2009 21:53:00 +0100

Hi,

> Comparing the two java examples given at [0] and [1], I am not quite
> sure how those accept methods would need to be implemented (and if
> that's at all what I am looking for). If all there is to implement in
> those accept methods is a line of code like 'visitor.visit(this)',
> things should be really easy. If the accept method additionally iterates
> all referenced objects, things could become quite complex. I am a bit
> confused about that pattern now, I admit.
>
> [0] http://de.wikipedia.org/wiki/Visitor
> [1] http://en.wikipedia.org/wiki/Visitor_pattern

I don't think classic visitor is suitable since you'll end up with one
method per class in JAXB context and that can be quite long. Another
thing is that it is often helpful to traverse a tree in depth, classic
visitor would require a lot of programming to do that.

So I'm currently thinking of something like

public interface Visitor
{
  public void visitParent(Object parent);
  public void visitChild(Object parent, String fieldName, Object value);
}

and classes implement the Visitable interface:

public interface Visitable
{
  public void accept(Visitor visitor);
}

In the accept method all the schema-derived classes first call
visitParent and then iterate over fields, calling visitChild method.
Other possibilities are to use some kind of "locator" in place of
parent and fieldName.

What is your task, by the way?

Bye.
/lexi