users@jaxb.java.net

Defining the XML Schema Type for XmlElementDecl

From: Shelley <randomshelley_at_gmail.com>
Date: Mon, 20 Aug 2007 10:47:20 -0500

How can an xml schema type (XmlSchemaType) be defined for an
XmlElementRef/Decl with a custom binding?

The schema/bindings are as follows:

Schema:
    <xs:element name="arguments" minOccurs="0">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="char" type="xs:unsignedShort" />
                    . . .
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

Bindings:
    <jxb:bindings

node="//xs:element[@name='arguments']//xs:complexType//xs:choice//xs:element[@name='char']">

        <jxb:javaType name="java.lang.Character"
            parseMethod="com.test.CharacterConverter.parse"
            printMethod="com.test.CharacterConverter.print" />
    </jxb:bindings>

The classes are as follows:

    @XmlRegistry
    class ObjectFactory {

        @XmlElementDecl(namespace = "urn:com:test:namespace", name = "char",
scope = com.test.Arguments.class)
        // XmlSchemaType is desired, but cannot be used with
XmlElementDecl/Ref: @XmlSchemaType(name = "unsignedShort")
        @XmlJavaTypeAdapter(com.test.CharacterAdapter.class )
        public JAXBElement<Character> createArgumentsChar(Character value) {
            return new JAXBElement<Character>(_ArgumentsChar_QNAME,
Character.class, com.test.Arguments.class, value);
        }

        . . .
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "arguments" })
    class Arguments {

        @XmlElementRefs( {
                @XmlElementRef(name = "char", namespace =
"urn:com:test:namespace", type = JAXBElement.class),
                . . . })
        protected List<JAXBElement<?>> arguments;

        . . .
    }

When generating the schema from these classes (schemagen), the resulting
"char" element type is " xs:string," not "xs:unsignedShort" as desired. What
annotation can be used to indicate that the type should be
xs:unsignedShort? If the addition of an annotation is not possible, are
there any other ways to accomplish this?

(To simplify and address a similar problem/question that may result in the
same solution, how can an XmlElementRef/Decl be bound to the xml schema type
"xs:unsignedShort"? By default, when an unsignedShort is desired, since the
Java classes deal only with Integers, "schemagen" uses " xs:int" by default
for the element type.)