Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsd:list of custom type is generated into List<String>

Tags:

java

jaxb

xsd

We have an xsd schema with a declaration like this:

<xsd:simpleType name="customId">
    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:restriction base="xsd:int" />
</xsd:simpleType>

Then, I want to have a list of this type in a generated Java class:

<xsd:complexType name="SomeMessage">
    ...
<xsd:attribute name="customIds" use="optional">
        <xsd:simpleType>
            <xsd:list itemType="customId" />
        </xsd:simpleType>
</xsd:attribute>
    ...
</xsd:complexType>

But the field customIds, for some reason, is generated as List<String>.

I guess, xsd:sequence could be used instead of xsd:list, but SomeMessage already has an xsd:choice, and as far as I get, it is illegal to have xsd:sequence in the same declaration.

Thanks!

like image 679
gregvonbautt Avatar asked Sep 24 '12 11:09

gregvonbautt


1 Answers

Code generated using NetBeans 7.1.2, running on Java 1.7.0_02.

If you want to map simple types to Java classes, one way to do is to globally set mapSimpleTypeDef="true"

<xsd:annotation>
    <xsd:appinfo>
        <jaxb:globalBindings mapSimpleTypeDef="true"/>
    </xsd:appinfo>
</xsd:annotation>

Generated code:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeMessage")
public class SomeMessage {

    @XmlAttribute(name = "customIds")
    protected List<CustomId> customIds;

    /**
     * Gets the value of the customIds property.
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link CustomId }
     * 
     * 
     */
    public List<CustomId> getCustomIds() {
        if (customIds == null) {
            customIds = new ArrayList<CustomId>();
        }
        return this.customIds;
    }

}

If you want to refer your pre-existing CustomId class, then the following works in my case:

<xsd:simpleType name="customId">
    <xsd:restriction base="xsd:int"/>
</xsd:simpleType>
<xsd:complexType name="SomeMessage">
    <xsd:attribute name="customIds" use="optional">
        <xsd:simpleType>
            <xsd:annotation>
                <xsd:appinfo>
                    <jaxb:javaType name="java.util.List&lt;com.company.identifiers.CustomId>" parseMethod="Class1.fromString" printMethod="Class1.toString"/>
                </xsd:appinfo>
            </xsd:annotation>
            <xsd:list itemType="customId"/>
        </xsd:simpleType>
    </xsd:attribute>
</xsd:complexType>

You'll get the following:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeMessage")
public class SomeMessage {

    @XmlAttribute(name = "customIds")
    @XmlJavaTypeAdapter(Adapter1 .class)
    protected List<CustomId> customIds;

    /**
     * Gets the value of the customIds property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public List<CustomId> getCustomIds() {
        return customIds;
    }

    /**
     * Sets the value of the customIds property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCustomIds(List<CustomId> value) {
        this.customIds = value;
    }

}

And the generated Adapter1:

public class Adapter1
    extends XmlAdapter<String, List<CustomId>>
{


    public List<CustomId> unmarshal(String value) {
        return (Class1.fromString(value));
    }

    public String marshal(List<CustomId> value) {
        return (Class1.toString(value));
    }

}
like image 148
Petru Gardea Avatar answered Oct 26 '22 18:10

Petru Gardea