users@jaxb.java.net

schemagen and using _at_XmlElementRef on a non-collection property

From: James.Strachan <james.strachan_at_gmail.com>
Date: Tue, 10 Jul 2007 17:59:47 -0700 (PDT)

In the Apache Camel project I want to create an XSD something like the
following, so that a <filter> can contain a choice of <groovy> <ognl> or
<ruby> expressions etc.

<xs:complexType name="filterType">
  <xs:complexContent>
    <xs:extension base="tns:processorType">
      <xs:sequence>
        <xs:choice minOccurs="1" maxOccurs="1">
          <xs:element ref="tns:groovy"/>
          <xs:element ref="tns:ognl"/>
          <xs:element ref="tns:ruby"/>
          <xs:element ref="tns:xpath"/>
        </xs:choice>
        <xs:choice maxOccurs="unbounded">
          <xs:element ref="tns:choice"/>
          <xs:element ref="tns:filter"/>
          <xs:element ref="tns:process"/>
          <xs:element ref="tns:recipientList"/>
          <xs:element ref="tns:splitter"/>
          <xs:element ref="tns:to"/>
        </xs:choice>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

So I've a FilterType class like this...

@XmlRootElement(name = "filter")
@XmlAccessorType(XmlAccessType.FIELD)
public class FilterType extends ProcessorType {
    @XmlElementRef
    private ExpressionType expression;

    @XmlElementRef
    private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
...

However that doesn't work as it doesn't generate a choice on the
'expression' property, instead it generates a sequence of all possible
@XmlElementRef types, which seems a bit bizarre as the setter can only
contain one value; is this a bug?


<xs:complexType name="filterType">
  <xs:complexContent>
    <xs:extension base="tns:processorType">
      <xs:sequence>
        <xs:element ref="tns:groovy"/>
        <xs:element ref="tns:ognl"/>
        <xs:element ref="tns:ruby"/>
        <xs:element ref="tns:xpath"/>
        <xs:choice maxOccurs="unbounded">
          <xs:element ref="tns:route"/>
          <xs:element ref="tns:choice"/>
          <xs:element ref="tns:otherwise"/>
          <xs:element ref="tns:when"/>
          <xs:element ref="tns:filter"/>
          <xs:element ref="tns:process"/>
          <xs:element ref="tns:recipientList"/>
          <xs:element ref="tns:splitter"/>
          <xs:element ref="tns:to"/>
        </xs:choice>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>


Though @XmlElementRef works great on collections. So as a dirty hack I've
done this...

                @XmlRootElement(name = "filter")
                @XmlAccessorType(XmlAccessType.FIELD)
                public class FilterType extends ProcessorType {

                    // a dirty hack to fix schemagen...
                    @XmlElementRef
                    private List<ExpressionType> expressions;
                    @XmlTransient
                    private ExpressionType expression;

                    @XmlElementRef
                    private List<ProcessorType> outputs = new ArrayList<ProcessorType>();


i.e. I added an extra collection-of-one field.

I just wondered; is there a proper way to specify that an @XmlElementRef
really is a choice; in addition is there any way to set the min/max occurs?

BTW the reason I like the @XmlElementRef is it nicely does the extension
mechanism, without having to cut and paste a chunk of
@XmlElements({_at_XmlElement(), ...}) declarations whenever a common base class
is used in my POJO model.

James
-- 
View this message in context: http://www.nabble.com/schemagen-and-using-%40XmlElementRef-on-a-non-collection-property-tf4059330.html#a11532413
Sent from the java.net - jaxb users mailing list archive at Nabble.com.