users@jaxb.java.net

Advice for serializing classes I cannot annotate ?

From: Herve Bitteur <Herve.Bitteur_at_Sun.COM>
Date: Wed, 25 Oct 2006 14:17:46 +0200

  Hello, here is a simplified example of what I mean

//Java file:
import java.awt.Point; // Note the 'Point' class is not under my control
@XmlRootElement(name="foo")
public class Foo {
   @XmlElement
   Point location;
   // ... other stuff ...
}

//(expected) XML file:
<foo>
   <location x="123" y="345"/>
   ...
</foo>

I can annotate Foo (it's my source code), but I cannot annotate the
java.awt.Point source.

My "solution" so far has been to define a PointFacade class (which I can
annotate) which encapsulates the real Point. Something like the following:

import java.awt.Point;
public class PointFacade {
   private Point point;

   public PointFacade() {}
   public PointFacade(Point point) {this.point = point;}

   @XmlAttribute
   public void setX(int x) { point.x = x;} public int getX() {return
point.x;}

   @XmlAttribute
   public void setY(int y) { point.y = y;} public int getY() {return
point.y;}
}

And then in the application class Foo, use this construction:

public class Foo {
   @XmlTransient
   Point location; // Real data, but not directly accessed by the marshaller

   @XmlElement (name="location")
   public void setLocation(PointFacade pf) { location = pf.getPoint(); }
   public PointFacade getLocation() { return new PointFacade(location); }
}

OK, it works, but it looks a bit awkward, not to speak of the runtime
overhead.
Moreover, I suspect this to be a rather frequent situation : marshalling
classes that belong to the JRE. And I'm not going to write this kind of
class facade for each and every JRE class I use!
In the case of this 'Point' class, how could I tell the marshaller to
marshal the public 'x' and 'y' fields of the Point class, how could I
define the attribute/element names for these fields. The problem boils
down to the fact that one cannot annotate these JRE files...
So what would be your advices here?

Thanks for any hint
/Hervé

-- 
<http://www.sun.com> 	*Hervé Bitteur*
Software Program Manager
*Sun Microsystems, Inc.*
13, Avenue Morane Saulnier
78142 Vélizy Cedex FRANCE
Phone +33 1 34 03 01 12
Mobile +33 6 86 49 04 14
Fax     +33 1 34 03 07 56
Email   herve.bitteur_at_sun.com
Skype hbitteur <skype:hbitteur?call>
-- 
<http://www.sun.com> 	*Hervé Bitteur*
Software Program Manager
*Sun Microsystems, Inc.*
13, Avenue Morane Saulnier
78142 Vélizy Cedex FRANCE
Phone +33 1 34 03 01 12
Mobile +33 6 86 49 04 14
Fax     +33 1 34 03 07 56
Email   herve.bitteur_at_sun.com
Skype hbitteur <skype:hbitteur?call>