users@jaxb.java.net

Re: XJC and the visitor pattern

From: Christian Schulte <cs_at_schulte.it>
Date: Mon, 26 Oct 2009 19:49:52 +0100

Wolfgang Laun schrieb:
> On Mon, Oct 26, 2009 at 5:58 PM, Christian Schulte <cs_at_schulte.it> wrote:
>
>> Hello,
>>
>> I would like XJC to add visitor support to schema derived classes.
>> Looking at jaxb2-commons and hyperjaxb3 I could not find a plugin
>> supporting this. Does such a plugin exist ?
>>
>>
> Not specifically.
>
> There is one for general purpose code injection. Adding the "accept" to any
> number of complexType definitions in the schema is probably abhorrent,
> depending on the nature of your schema.
>
> A simple XSLT might take care of inserting the required annotation in all
> required places.
>
> -W

Maybe the visitor pattern isn't the correct solution to what I am trying
to solve. I thought about something like the following:

interface Visitor
{
  accept(A a);
  accept(B b);
  // One accept method for each schema derived class.
}

class A
{
  List<B> getB()

  accept(Visitor v)
  {
    v.accept(this); // Can be done with XSLT.

    for(B b : getB()) // Cannot be done with XSLT, I think.
    {
      v.accept(b);
    }
  }
}

So that whenever the schema changes structurally, the visitor would not
have to be changed. The problem is adding the accept methods to the
classes. The body of those methods will change whenever the schema
changes and I thought about some XJC plugin which could handle that. Any
ideas ?

-- 
Christian