users@jaxb.java.net

Re: Anyone using JAXB2 Basics inheritance?

From: Colin Freas <colinfreas_at_gmail.com>
Date: Mon, 21 Mar 2011 10:37:56 -0400

Apologies is this is too off topic for the list...

So, part of my application's data model contains a simple parent-child
relationship, where you can think of the parent as an event (in the everyday
sense, not as in *onclick*) and the child as an attachment containing binary
data for that event, maybe a pdf or a Word document.

Anyway, my XSD defines CRUD requests and responses which are consumed by a
pre-existing web service.

Here's a snippet of XSD:
  <xs:element name="CreateEventRequest">
    <xs:complexType>
      <xs:all>
        <xs:element name="event" type="app:newEvent"/>
        <xs:element name="eventAttachment" type="app:newEventAttachment"
minOccurs="0"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
  ...
  ...
  <xs:element name="ReadEventResponse">
    <xs:complexType>
      <xs:all>
        <xs:element name="status" type="app:statusCode"/>
        <xs:element name="event" type="app:existingEvent" minOccurs="0"/>
        <xs:element name="eventAttachment"
type="app:existingEventAttachment" minOccurs="0"/>
      </xs:all>
    </xs:complexType>
  </xs:element>

What would be ideal is if I could have both of these implement base *
appRequest* and *appResponse* interfaces, which themselves could also be
defined in the XSD. I do use <extension> in a few places right now, for
instance *newEventAttachment *and *existingEventAttachment* extend *
baseEventAttachment*. But that mechanism doesn't feel like the best
solution here, which is why I am exploring the Inheritance plugin of JAXB2
Basics.

I don't really know what the best way to define interfaces "on the fly"
would be, if it isn't possible right now. Probably a new <annotation>.
 Defines maybe? Something like:
  <xs:complexType name="BaseAppResponseInterface">
    <xs:annotation>
      <xs:appinfo>
        <inheritance:*defines*>my.app.package.BaseAppInterface</inheritance:
*defines*>
      </xs:appinfo>
    </xs:annotation>
    <xs:all>
      <xs:element name="status" type="app:statusCode"/>
    </xs:all
  </xs:complexType>

Here, running a schema with this through XJC would produce a Java interface
called BaseAppResponseInterface which other classes could implement. The
interface would consist of a getter and setter for status using a statusCode
type (defined elsewhere in the XSD.)

Maybe there's a better way to implement the feature, or maybe there's
another way of doing already.

-Colin






On Fri, Mar 18, 2011 at 4:59 PM, Aleksei Valikov <valikov_at_gmx.net> wrote:

> Hi,
>
> > I have JAXB2 Basics setup in Eclipse to use the inheritance plug-in and
> it's
> > mostly working for what I need.
> > So, I have something like this:
> >
> > <?xml version="1.0"?>
> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> > xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
> >
> > xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
> > jaxb:version="2.1"
> > jaxb:extensionBindingPrefixes="inheritance">
> >
> > <xs:complexType name="WhateverImplementer">
> > <xs:annotation>
> > <xs:appinfo>
> >
> <inheritance:implements>my.package.Whatever</inheritance:implements>
> > </xs:appinfo>
> > </xs:annotation>
> > </xs:complexType>
> > </xs:schema>
> >
> > Is there a way to define the my.package.Whatever interface somewhere else
> in
> > XSD?
>
> Would you give an example? How would you want this interface to be defined?
>
> Bye,
> /lexi
>