325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.xml.bind.annotation;
325N/A
325N/Aimport javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
325N/Aimport java.lang.annotation.Retention;
325N/Aimport java.lang.annotation.Target;
325N/A
325N/Aimport static java.lang.annotation.RetentionPolicy.RUNTIME;
325N/Aimport static java.lang.annotation.ElementType.FIELD;
325N/Aimport static java.lang.annotation.ElementType.METHOD;
325N/A
325N/A/**
325N/A * <p>
325N/A * Maps a JavaBean property to a XML element derived from property's type.
325N/A * <p>
325N/A * <b>Usage</b>
325N/A * <p>
325N/A * <tt>&#64;XmlElementRef</tt> annotation can be used with a
325N/A * JavaBean property or from within {@link XmlElementRefs}
325N/A * <p>
325N/A * This annotation dynamically associates an XML element name with the JavaBean
325N/A * property. When a JavaBean property is annotated with {@link
325N/A * XmlElement}, the XML element name is statically derived from the
325N/A * JavaBean property name. However, when this annotation is used, the
325N/A * XML element name is derived from the instance of the type of the
325N/A * JavaBean property at runtime.
325N/A *
325N/A * <h3> XML Schema substitution group support </h3>
325N/A * XML Schema allows a XML document author to use XML element names
325N/A * that were not statically specified in the content model of a
325N/A * schema using substitution groups. Schema derived code provides
325N/A * support for substitution groups using an <i>element property</i>,
325N/A * (section 5.5.5, "Element Property" of JAXB 2.0 specification). An
325N/A * element property method signature is of the form:
325N/A * <pre>
325N/A * public void setTerm(JAXBElement<? extends Operator>);
325N/A * public JAXBElement<? extends Operator> getTerm();
325N/A * </pre>
325N/A * <p>
325N/A * An element factory method annotated with {@link XmlElementDecl} is
325N/A * used to create a <tt>JAXBElement</tt> instance, containing an XML
325N/A * element name. The presence of &#64;XmlElementRef annotation on an
325N/A * element property indicates that the element name from <tt>JAXBElement</tt>
325N/A * instance be used instead of deriving an XML element name from the
325N/A * JavaBean property name.
325N/A *
325N/A * <p>
325N/A * The usage is subject to the following constraints:
325N/A * <ul>
325N/A * <li> If the collection item type (for collection property) or
325N/A * property type (for single valued property) is
325N/A * {@link javax.xml.bind.JAXBElement}, then
325N/A * <tt>&#64;XmlElementRef}.name()</tt> and <tt>&#64;XmlElementRef.namespace()</tt> must
325N/A * point an element factory method with an @XmlElementDecl
325N/A * annotation in a class annotated with @XmlRegistry (usually
325N/A * ObjectFactory class generated by the schema compiler) :
325N/A * <ul>
325N/A * <li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li>
325N/A * <li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li>
325N/A * </ul>
325N/A * </li>
325N/A * <li> If the collection item type (for collection property) or
325N/A * property type (for single valued property) is not
325N/A * {@link javax.xml.bind.JAXBElement}, then the type referenced by the
325N/A * property or field must be annotated with {@link XmlRootElement}. </li>
325N/A * <li> This annotation can be used with the following annotations:
325N/A * {@link XmlElementWrapper}, {@link XmlJavaTypeAdapter}.
325N/A * </ul>
325N/A *
325N/A * <p>See "Package Specification" in javax.xml.bind.package javadoc for
325N/A * additional common information.</p>
325N/A *
325N/A * <p><b>Example 1: Ant Task Example</b></p>
325N/A * The following Java class hierarchy models an Ant build
325N/A * script. An Ant task corresponds to a class in the class
325N/A * hierarchy. The XML element name of an Ant task is indicated by the
325N/A * &#64;XmlRootElement annotation on its corresponding class.
325N/A * <pre>
325N/A * &#64;XmlRootElement(name="target")
325N/A * class Target {
325N/A * // The presence of &#64;XmlElementRef indicates that the XML
325N/A * // element name will be derived from the &#64;XmlRootElement
325N/A * // annotation on the type (for e.g. "jar" for JarTask).
325N/A * &#64;XmlElementRef
325N/A * List&lt;Task> tasks;
325N/A * }
325N/A *
325N/A * abstract class Task {
325N/A * }
325N/A *
325N/A * &#64;XmlRootElement(name="jar")
325N/A * class JarTask extends Task {
325N/A * ...
325N/A * }
325N/A *
325N/A * &#64;XmlRootElement(name="javac")
325N/A * class JavacTask extends Task {
325N/A * ...
325N/A * }
325N/A *
325N/A * &lt;!-- XML Schema fragment -->
325N/A * &lt;xs:element name="target" type="Target">
325N/A * &lt;xs:complexType name="Target">
325N/A * &lt;xs:sequence>
325N/A * &lt;xs:choice maxOccurs="unbounded">
325N/A * &lt;xs:element ref="jar">
325N/A * &lt;xs:element ref="javac">
325N/A * &lt;/xs:choice>
325N/A * &lt;/xs:sequence>
325N/A * &lt;/xs:complexType>
325N/A *
325N/A * </pre>
325N/A * <p>
325N/A * Thus the following code fragment:
325N/A * <pre>
325N/A * Target target = new Target();
325N/A * target.tasks.add(new JarTask());
325N/A * target.tasks.add(new JavacTask());
325N/A * marshal(target);
325N/A * </pre>
325N/A * will produce the following XML output:
325N/A * <pre>
325N/A * &lt;target>
325N/A * &lt;jar>
325N/A * ....
325N/A * &lt;/jar>
325N/A * &lt;javac>
325N/A * ....
325N/A * &lt;/javac>
325N/A * &lt;/target>
325N/A * </pre>
325N/A * <p>
325N/A * It is not an error to have a class that extends <tt>Task</tt>
325N/A * that doesn't have {@link XmlRootElement}. But they can't show up in an
325N/A * XML instance (because they don't have XML element names).
325N/A *
325N/A * <p><b>Example 2: XML Schema Susbstitution group support</b>
325N/A * <p> The following example shows the annotations for XML Schema
325N/A * substitution groups. The annotations and the ObjectFactory are
325N/A * derived from the schema.
325N/A *
325N/A * <pre>
325N/A * &#64;XmlElement
325N/A * class Math {
325N/A * // The value of {@link #type()}is
325N/A * // JAXBElement.class , which indicates the XML
325N/A * // element name ObjectFactory - in general a class marked
325N/A * // with &#64;XmlRegistry. (See ObjectFactory below)
325N/A * //
325N/A * // The {@link #name()} is "operator", a pointer to a
325N/A * // factory method annotated with a
325N/A * // {@link XmlElementDecl} with the name "operator". Since
325N/A * // "operator" is the head of a substitution group that
325N/A * // contains elements "add" and "sub" elements, "operator"
325N/A * // element can be substituted in an instance document by
325N/A * // elements "add" or "sub". At runtime, JAXBElement
325N/A * // instance contains the element name that has been
325N/A * // substituted in the XML document.
325N/A * //
325N/A * &#64;XmlElementRef(type=JAXBElement.class,name="operator")
325N/A * JAXBElement&lt;? extends Operator> term;
325N/A * }
325N/A *
325N/A * &#64;XmlRegistry
325N/A * class ObjectFactory {
325N/A * &#64;XmlElementDecl(name="operator")
325N/A * JAXBElement&lt;Operator> createOperator(Operator o) {...}
325N/A * &#64;XmlElementDecl(name="add",substitutionHeadName="operator")
325N/A * JAXBElement&lt;Operator> createAdd(Operator o) {...}
325N/A * &#64;XmlElementDecl(name="sub",substitutionHeadName="operator")
325N/A * JAXBElement&lt;Operator> createSub(Operator o) {...}
325N/A * }
325N/A *
325N/A * class Operator {
325N/A * ...
325N/A * }
325N/A * </pre>
325N/A * <p>
325N/A * Thus, the following code fragment
325N/A * <pre>
325N/A * Math m = new Math();
325N/A * m.term = new ObjectFactory().createAdd(new Operator());
325N/A * marshal(m);
325N/A * </pre>
325N/A * will produce the following XML output:
325N/A * <pre>
325N/A * &lt;math>
325N/A * &lt;add>...&lt;/add>
325N/A * &lt;/math>
325N/A * </pre>
325N/A *
325N/A *
325N/A * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems,Inc. </li><li>Sekhar Vajjhala, Sun Microsystems, Inc.</li></ul>
325N/A * @see XmlElementRefs
325N/A * @since JAXB2.0
325N/A */
325N/A@Retention(RUNTIME)
325N/A@Target({FIELD,METHOD})
325N/Apublic @interface XmlElementRef {
325N/A /**
325N/A * The Java type being referenced.
325N/A * <p>
325N/A * If the value is DEFAULT.class, the type is inferred from the
325N/A * the type of the JavaBean property.
325N/A */
325N/A Class type() default DEFAULT.class;
325N/A
325N/A /**
325N/A * This parameter and {@link #name()} are used to determine the
325N/A * XML element for the JavaBean property.
325N/A *
325N/A * <p> If <tt>type()</tt> is <tt>JAXBElement.class</tt> , then
325N/A * <tt>namespace()</tt> and <tt>name()</tt>
325N/A * point to a factory method with {@link XmlElementDecl}. The XML
325N/A * element name is the element name from the factory method's
325N/A * {@link XmlElementDecl} annotation or if an element from its
325N/A * substitution group (of which it is a head element) has been
325N/A * substituted in the XML document, then the element name is from the
325N/A * {@link XmlElementDecl} on the substituted element.
325N/A *
325N/A * <p> If {@link #type()} is not <tt>JAXBElement.class</tt>, then
325N/A * the XML element name is the XML element name statically
325N/A * associated with the type using the annotation {@link
325N/A * XmlRootElement} on the type. If the type is not annotated with
325N/A * an {@link XmlElementDecl}, then it is an error.
325N/A *
325N/A * <p> If <tt>type()</tt> is not <tt>JAXBElement.class</tt>, then
325N/A * this value must be "".
325N/A *
325N/A */
325N/A String namespace() default "";
325N/A /**
325N/A *
325N/A * @see #namespace()
325N/A */
325N/A String name() default "##default";
325N/A
325N/A /**
325N/A * Used in {@link XmlElementRef#type()} to
325N/A * signal that the type be inferred from the signature
325N/A * of the property.
325N/A */
325N/A static final class DEFAULT {}
325N/A
325N/A /**
325N/A * Customize the element declaration to be required.
325N/A * <p>
325N/A * If required() is true, then Javabean property is mapped to
325N/A * an XML schema element declaration with minOccurs="1".
325N/A * maxOccurs is "1" for a single valued property and "unbounded"
325N/A * for a multivalued property.
325N/A *
325N/A * <p>
325N/A * If required() is false, then the Javabean property is mapped
325N/A * to XML Schema element declaration with minOccurs="0".
325N/A * maxOccurs is "1" for a single valued property and "unbounded"
325N/A * for a multivalued property.
325N/A *
325N/A * <p>
325N/A * For compatibility with JAXB 2.1, this property defaults to <tt>true</tt>,
325N/A * despite the fact that {@link XmlElement#required()} defaults to false.
325N/A *
325N/A * @since 2.2
325N/A */
325N/A boolean required() default true;
325N/A}