users@jaxb.java.net

Re: SAX-like JAXB?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 28 Aug 2006 10:21:56 -0700

Thanks. I saw your blog on this. Nice write up.

I liked your blog version better. You could have done this:

   public class Parent {
     List<Child> children = new CallbackList<Child>(this);
   }

to pass the parent reference to the CallbackList, then CallbackList can
invoke the Parent.handleNewChild(); No need to use ThreadLocal.


The other thing you might find useful for this is per-class listener
method. Instead of using Unmarshaller.Listener which receives events for
every object, you can instead define the before/afterUnmarshal method on
your PurcharseOrders class. (See Unmarshaller javadoc for the signature
of the methods and semantics.)


Would you be interested in turning something like that into a complete
example that we can ship?



Ernst, Matthias wrote:
> What I'm doing is the following: in the Unmarshaller.Listener I override
> the list in the parent object with an implementation that calls a
> "newChildUnmarshalled" handler on #add but doesn't remember the child:
>
> (purchase order example:)
>
> unmarshaller.setListener(new Unmarshaller.Listener() {
> public void beforeUnmarshal(Object target, Object parent) {
> if (target instanceof PurchaseOrders) {
> ((PurchaseOrders) target).setPurchaseOrders(new
> ArrayList<PurchaseOrder>() {
> public boolean add(PurchaseOrder po) {
> handleNewPurchaseOrder(po); // could pass purchaseorders as
> well.
> return false;
> }
> });
> }
> }
> public void beforeUnmarshal(Object target, Object parent) {
> if (target instanceof PurchaseOrders) {
> ((PurchaseOrders) target).setPurchaseOrders(null); // or whatever
> }
> }
> });
>
> I find this much more elegant than fiddling with the StAX stream or the
> partial-unmarshalling sample.
>
> Cheers
> Matthias
>


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com