javax.xml.bind.annotation.XmlType Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2004-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
* or LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package javax.xml.bind.annotation;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
/**
*
* Maps a class or an enum type to a XML Schema type.
*
*
Usage
* The {@code @XmlType} annnotation can be used with the following program
* elements:
*
* - a top level class
* - an enum type
*
*
* See "Package Specification" in javax.xml.bind.package javadoc for
* additional common information.
*
* Mapping a Class
*
* A class maps to a XML Schema type. A class is a data container for
* values represented by properties and fields. A schema type is a
* data container for values represented by schema components within a
* schema type's content model (e.g. model groups, attributes etc).
*
To be mapped, a class must either have a public no-arg
* constructor or a static no-arg factory method. The static factory
* method can be specified in {@code factoryMethod()} and
* {@code factoryClass()} annotation elements. The static factory
* method or the no-arg constructor is used during unmarshalling to
* create an instance of this class. If both are present, the static
* factory method overrides the no-arg constructor.
*
* A class maps to either a XML Schema complex type or a XML Schema simple
* type. The XML Schema type is derived based on the
* mapping of JavaBean properties and fields contained within the
* class. The schema type to which the class is mapped can either be
* named or anonymous. A class can be mapped to an anonymous schema
* type by annotating the class with {@code @XmlType(name="")}.
*
* Either a global element, local element or a local attribute can be
* associated with an anonymous type as follows:
*
* - global element: A global element of an anonymous
* type can be derived by annotating the class with @{@link
* XmlRootElement}. See Example 3 below.
*
* - local element: A JavaBean property that references
* a class annotated with @XmlType(name="") and is mapped to the
* element associated with the anonymous type. See Example 4
* below.
*
* - attribute: A JavaBean property that references
* a class annotated with @XmlType(name="") and is mapped to the
* attribute associated with the anonymous type. See Example 5 below.
*
* Mapping to XML Schema Complex Type
*
* - If class is annotated with {@code @XmlType(name="") }, it
* is mapped to an anonymous type otherwise, the class name maps
* to a complex type name. The {@code XmlName()} annotation element
* can be used to customize the name.
*
* - Properties and fields that are mapped to elements are mapped to a
* content model within a complex type. The annotation element
* {@code propOrder()} can be used to customize the content model to be
* {@code xs:all} or {@code xs:sequence}. It is used for specifying
* the order of XML elements in {@code xs:sequence}.
*
* - Properties and fields can be mapped to attributes within the
* complex type.
*
* - The targetnamespace of the XML Schema type can be customized
* using the annotation element {@code namespace()}.
*
*
*
* Mapping class to XML Schema simple type
*
* A class can be mapped to a XML Schema simple type using the
* {@code @XmlValue} annotation. For additional details and examples,
* see @{@link XmlValue} annotation type.
*
* The following table shows the mapping of the class to a XML Schema
* complex type or simple type. The notational symbols used in the table are:
*
* - {@literal ->} : represents a mapping
* - [x]+ : one or more occurrences of x
* - [ {@code @XmlValue} property ]: JavaBean property annotated with
* {@code @XmlValue}
* - X : don't care
*
*
*
*
*
* Target
* propOrder
* ClassBody
* ComplexType
* SimpleType
*
*
*
*
*
* Class
* {}
* [property]+ {@literal ->} elements
* complexcontent
xs:all
*
*
*
*
* Class
* non empty
* [property]+ {@literal ->} elements
* complexcontent
xs:sequence
*
*
*
*
* Class
* X
* no property {@literal ->} element
* complexcontent
empty sequence
*
*
*
*
* Class
* X
* 1 [{@code @XmlValue} property] {@literal &&}
[property]+ {@literal ->} attributes
* simplecontent
*
*
*
*
* Class
* X
* 1 [{@code @XmlValue} property] {@literal &&}
no properties {@literal ->} attribute
*
* simpletype
*
*
*
*
*
* Mapping an enum type
*
* An enum type maps to a XML schema simple type with enumeration
* facets. The following annotation elements are ignored since they
* are not meaningful: {@code propOrder()} , {@code factoryMethod()} ,
* {@code factoryClass()} .
*
* Usage with other annotations
* This annotation can be used with the following annotations:
* {@link XmlRootElement}, {@link XmlAccessorOrder}, {@link XmlAccessorType},
* {@link XmlEnum}. However, {@link
* XmlAccessorOrder} and {@link XmlAccessorType} are ignored when this
* annotation is used on an enum type.
*
*
Example 1: Map a class to a complex type with
* xs:sequence with a customized ordering of JavaBean properties.
*
*
*
* @XmlType(propOrder={"street", "city" , "state", "zip", "name" })
* public class USAddress {
* String getName() {..};
* void setName(String) {..};
*
* String getStreet() {..};
* void setStreet(String) {..};
*
* String getCity() {..};
* void setCity(String) {..};
*
* String getState() {..};
* void setState(String) {..};
*
* java.math.BigDecimal getZip() {..};
* void setZip(java.math.BigDecimal) {..};
* }
* {@code
*
*
*
*
*
*
*
*
*
*
*
* }
* Example 2: Map a class to a complex type with
* xs:all
*
* @XmlType(propOrder={})
* public class USAddress { ...}
* {@code
*
*
*
*
*
*
*
*
*
*
*
*}
* Example 3: Map a class to a global element with an
* anonymous type.
*
*
* @XmlRootElement
* @XmlType(name="")
* public class USAddress { ...}
* {@code
*
*
*
*
*
*
*
*
*
*
*
*
*
* }
*
* Example 4: Map a property to a local element with
* anonymous type.
*
* //Example: Code fragment
* public class Invoice {
* USAddress addr;
* ...
* }
*
* @XmlType(name="")
* public class USAddress { ... }
* }
* {@code
*
*
*
*
*
*
*
*
*
*
*
*
* ...
*
*
* }
*
* Example 5: Map a property to an attribute with
* anonymous type.
*
*
*
* //Example: Code fragment
* public class Item {
* public String name;
* @XmlAttribute
* public USPrice price;
* }
*
* // map class to anonymous simple type.
* @XmlType(name="")
* public class USPrice {
* @XmlValue
* public java.math.BigDecimal price;
* }
* {@code
*
*
*
*
*
*
*
*
*
*
*
*
* }
*
* Example 6: Define a factoryClass and factoryMethod
*
*
* @XmlType(name="USAddressType", factoryClass=USAddressFactory.class,
* factoryMethod="getUSAddress")
* public class USAddress {
*
* private String city;
* private String name;
* private String state;
* private String street;
* private int zip;
*
* public USAddress(String name, String street, String city,
* String state, int zip) {
* this.name = name;
* this.street = street;
* this.city = city;
* this.state = state;
* this.zip = zip;
* }
* }
*
* public class USAddressFactory {
* public static USAddress getUSAddress(){
* return new USAddress("Mark Baker", "23 Elm St",
* "Dayton", "OH", 90952);
* }
*
*
*
* Example 7: Define factoryMethod and use the default factoryClass
*
*
* @XmlType(name="USAddressType", factoryMethod="getNewInstance")
* public class USAddress {
*
* private String city;
* private String name;
* private String state;
* private String street;
* private int zip;
*
* private USAddress() {}
*
* public static USAddress getNewInstance(){
* return new USAddress();
* }
* }
*
*
* @author Sekhar Vajjhala, Sun Microsystems, Inc.
* @see XmlElement
* @see XmlAttribute
* @see XmlValue
* @see XmlSchema
* @since 1.6, JAXB 2.0
*/
@Retention(RUNTIME) @Target({TYPE})
public @interface XmlType {
/**
* Name of the XML Schema type which the class is mapped.
*/
String name() default "##default" ;
/**
* Specifies the order for XML Schema elements when class is
* mapped to a XML Schema complex type.
*
* Refer to the table for how the propOrder affects the
* mapping of class
*
* The propOrder is a list of names of JavaBean properties in
* the class. Each name in the list is the name of a Java
* identifier of the JavaBean property. The order in which
* JavaBean properties are listed is the order of XML Schema
* elements to which the JavaBean properties are mapped.
* All of the JavaBean properties being mapped to XML Schema elements
* must be listed.
*
A JavaBean property or field listed in propOrder must not
* be transient or annotated with {@code @XmlTransient}.
*
The default ordering of JavaBean properties is determined
* by @{@link XmlAccessorOrder}.
*/
String[] propOrder() default {""};
/**
* Name of the target namespace of the XML Schema type. By
* default, this is the target namespace to which the package
* containing the class is mapped.
*/
String namespace() default "##default" ;
/**
* Class containing a no-arg factory method for creating an
* instance of this class. The default is this class.
*
*
If {@code factoryClass} is DEFAULT.class and
* {@code factoryMethod} is "", then there is no static factory
* method.
*
*
If {@code factoryClass} is DEFAULT.class and
* {@code factoryMethod} is not "", then
* {@code factoryMethod} is the name of a static factory method
* in this class.
*
*
If {@code factoryClass} is not DEFAULT.class, then
* {@code factoryMethod} must not be "" and must be the name of
* a static factory method specified in {@code factoryClass}.
*/
Class factoryClass() default DEFAULT.class;
/**
* Used in {@link XmlType#factoryClass()} to
* signal that either factory mehod is not used or
* that it's in the class with this {@link XmlType} itself.
*/
static final class DEFAULT {}
/**
* Name of a no-arg factory method in the class specified in
* {@code factoryClass} factoryClass().
*
*/
String factoryMethod() default "";
}