4

I want to generate the POJOs using the xml-binding with a different hierarchy that I have right now. Now I have an xsd like this one:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://manueldoncel.com" xmlns:tns="http://manueldoncel.com"  elementFormDefault="qualified">
    <!-- Base element that any element uses / extends  -->
    <complexType name="baseElement" abstract="true">
        <sequence>
            <element name="attributes" type="anyType"  minOccurs="0" />
        </sequence>
        <attribute name="id" type="string" />
    </complexType>

    <complexType name="Square">
        <complexContent><extension base="tns:baseElement" /></complexContent>
    </complexType>

    <complexType name="Triangle">
        <complexContent><extension base="tns:baseElement" /></complexContent>
    </complexType>

</schema>

An a xjb like this;

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <jxb:bindings schemaLocation="figures.xsd" node="/xs:schema">
    <jxb:globalBindings>
      <xjc:simple />
      <xjc:superClass name="org.manuel.metadata.Figure"/>     
    </jxb:globalBindings>
  </jxb:bindings>
</jxb:bindings>

But, I would like to have a better hierarchy with this approach

public abstract class Figure {
    // some methods for all the figures
}

public abstract class ThreeSideFigure extends Figure {
.... // some methods for only the Three side Figures
}

So, the Triangle POJO generated from the XSD should extend from ThreeSideFigure rather than from Figure.

In this particular xsd I only put 2 figures, but I can have much more. I would like to be able to specify in the xjb that all the complexType should extends from Figure but only a few of them, should extends from ThreeSideFigure.

Do you know how the xjb should look like?

Manuelarte
  • 1,658
  • 2
  • 28
  • 47

1 Answers1

2

I don't think the Metro JAXB RI extensions will let you do that.

From the 2.2.7 documentation:

3.1.3. Extending a Common Super Class

The <xjc:superClass> customization allows you to specify the fully qualified name of the Java class that is to be used as the super class of all the generated implementation classes. The <xjc:superClass> customization can only occur within your <jaxb:globalBindings> customization on the <xs:schema> element

That said, the answer to XJC superinterface and superclass only for all classes? suggests that you may be able to do it with 3rd party extensions. There are some details about plugins in the documentation.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267