KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > annotation > XmlElementRef


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.bind.annotation;
7
8 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
9 import java.lang.annotation.Retention JavaDoc;
10 import java.lang.annotation.Target JavaDoc;
11
12 import static java.lang.annotation.RetentionPolicy.RUNTIME JavaDoc;
13 import static java.lang.annotation.ElementType.FIELD JavaDoc;
14 import static java.lang.annotation.ElementType.METHOD JavaDoc;
15
16 /**
17  * <p>
18  * Maps a JavaBean property to a XML element derived from property's type.
19  * <p>
20  * <b>Usage</b>
21  * <p>
22  * <tt>&#64;XmlElementRef</tt> annotation can be used with a
23  * JavaBean property or from within {@link XmlElementRefs}
24  * <p>
25  * This annotation dynamically associates an XML element name with the JavaBean
26  * property. When a JavaBean property is annotated with {@link
27  * XmlElement}, the XML element name is statically derived from the
28  * JavaBean property name. However, when this annotation is used, the
29  * XML element name is derived from the instance of the type of the
30  * JavaBean property at runtime.
31  *
32  * <h3> XML Schema substitution group support </h3>
33  * XML Schema allows a XML document author to use XML element names
34  * that were not statically specified in the content model of a
35  * schema using substitution groups. Schema derived code provides
36  * support for substitution groups using an <i>element property</i>,
37  * (section 5.5.5, "Element Property" of JAXB 2.0 specification). An
38  * element property method signature is of the form:
39  * <pre><xmp>
40  * public void setTerm(JAXBElement<? extends Operator>);
41  * public JAXBElement<? extends Operator> getTerm();
42  * </xmp></pre>
43  * <p>
44  * An element factory method annotated with {@link XmlElementDecl} is
45  * used to create a <tt>JAXBElement</tt> instance, containing an XML
46  * element name. The presence of &#64;XmlElementRef annotation on an
47  * element property indicates that the element name from <tt>JAXBElement</tt>
48  * instance be used instead of deriving an XML element name from the
49  * JavaBean property name.
50  *
51  * <p>
52  * The usage is subject to the following constraints:
53  * <ul>
54  * <li> If the collection item type (for collection property) or
55  * property type (for single valued property) is
56  * {@link javax.xml.bind.JAXBElement}, then
57  * <tt>&#64;XmlElementRef}.name()</tt> and <tt>&#64;XmlElementRef.namespace()</tt> must
58  * point an element factory method with an @XmlElementDecl
59  * annotation in a class annotated with @XmlRegistry (usually
60  * ObjectFactory class generated by the schema compiler) :
61  * <ul>
62  * <li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li>
63  * <li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li>
64  * </ul>
65  * </li>
66  * <li> If the collection item type (for collection property) or
67  * property type (for single valued property) is not
68  * {@link javax.xml.bind.JAXBElement}, then the type referenced by the
69  * property or field must be annotated with {@link XmlRootElement}. </li>
70  * <li> This annotation can be used with the following annotations:
71  * {@link XmlElementWrapper}, {@link XmlJavaTypeAdapter}.
72  * </ul>
73  *
74  * <p>See "Package Specification" in javax.xml.bind.package javadoc for
75  * additional common information.</p>
76  *
77  * <p><b>Example 1: </b>Ant Task Example</b></p>
78  * The following Java class hierarchy models an Ant build
79  * script. An Ant task corresponds to a class in the class
80  * hierarchy. The XML element name of an Ant task is indicated by the
81  * &#64;XmlRootElement annotation on its corresponding class.
82  * <pre>
83  * &#64;XmlRootElement(name="target")
84  * class Target {
85  * // The presence of &#64;XmlElementRef indicates that the XML
86  * // element name will be derived from the &#64;XmlRootElement
87  * // annotation on the type (for e.g. "jar" for JarTask).
88  * &#64;XmlElementRef
89  * List&lt;Task> tasks;
90  * }
91  *
92  * abstract class Task {
93  * }
94  *
95  * &#64;XmlRootElement(name="jar")
96  * class JarTask extends Task {
97  * ...
98  * }
99  *
100  * &#64;XmlRootElement(name="javac")
101  * class JavacTask extends Task {
102  * ...
103  * }
104  *
105  * &lt;!-- XML Schema fragment -->
106  * &lt;xs:element name="target" type="Target">
107  * &lt;xs:complexType name="Target">
108  * &lt;xs:sequence>
109  * &lt;xs:choice maxOccurs="unbounded">
110  * &lt;xs:element ref="jar">
111  * &lt;xs:element ref="javac">
112  * &lt;/xs:choice>
113  * &lt;/xs:sequence>
114  * &lt;/xs:complexType>
115  *
116  * </pre>
117  * <p>
118  * Thus the following code fragment:
119  * <pre>
120  * Target target = new Target();
121  * target.tasks.add(new JarTask());
122  * target.tasks.add(new JavacTask());
123  * marshal(target);
124  * </pre>
125  * will produce the following XML output:
126  * <pre><xmp>
127  * <target>
128  * <jar>
129  * ....
130  * </jar>
131  * <javac>
132  * ....
133  * </javac>
134  * </target>
135  * </xmp></pre>
136  * <p>
137  * It is not an error to have a class that extends <tt>Task</tt>
138  * that doesn't have {@link XmlRootElement}. But they can't show up in an
139  * XML instance (because they don't have XML element names).
140  *
141  * <p><b>Example 2: XML Schema Susbstitution group support</b>
142  * <p> The following example shows the annotations for XML Schema
143  * substitution groups. The annotations and the ObjectFactory are
144  * derived from the schema.
145  *
146  * <pre>
147  * &#64;XmlElement
148  * class Math {
149  * // The value of {@link #type()}is
150  * // JAXBElement.class , which indicates the XML
151  * // element name ObjectFactory - in general a class marked
152  * // with &#64;XmlRegistry. (See ObjectFactory below)
153  * //
154  * // The {@link #name()} is "operator", a pointer to a
155  * // factory method annotated with a
156  * // {@link XmlElementDecl} with the name "operator". Since
157  * // "operator" is the head of a substitution group that
158  * // contains elements "add" and "sub" elements, "operator"
159  * // element can be substituted in an instance document by
160  * // elements "add" or "sub". At runtime, JAXBElement
161  * // instance contains the element name that has been
162  * // substituted in the XML document.
163  * //
164  * &#64;XmlElementRef(type=JAXBElement.class,name="operator")
165  * JAXBElement&lt;? extends Operator> term;
166  * }
167  *
168  * &#64;XmlRegistry
169  * class ObjectFactory {
170  * &#64;XmlElementDecl(name="operator")
171  * JAXBElement&lt;Operator> createOperator(Operator o) {...}
172  * &#64;XmlElementDecl(name="add",substitutionHeadName="operator")
173  * JAXBElement&lt;Operator> createAdd(Operator o) {...}
174  * &#64;XmlElementDecl(name="sub",substitutionHeadName="operator")
175  * JAXBElement&lt;Operator> createSub(Operator o) {...}
176  * }
177  *
178  * class Operator {
179  * ...
180  * }
181  * </pre>
182  * <p>
183  * Thus, the following code fragment
184  * <pre>
185  * Math m = new Math();
186  * m.term = new ObjectFactory().createAdd(new Operator());
187  * marshal(m);
188  * </pre>
189  * will produce the following XML output:
190  * <pre>
191  * &lt;math>
192  * &lt;add>...&lt;/add>
193  * &lt;/math>
194  * </pre>
195  *
196  *
197  * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems,Inc. </li><li>Sekhar Vajjhala, Sun Microsystems, Inc.</li></ul>
198  * @see XmlElementRefs
199  * @since JAXB2.0
200  */

201 @Retention JavaDoc(RUNTIME)
202 @Target JavaDoc({FIELD,METHOD})
203 public @interface XmlElementRef {
204     /**
205      * The Java type being referenced.
206      * <p>
207      * If the value is DEFAULT.class, the type is inferred from the
208      * the type of the JavaBean property.
209      */

210     Class JavaDoc type() default DEFAULT.class;
211
212     /**
213      * This parameter and {@link #name()} are used to determine the
214      * XML element for the JavaBean property.
215      *
216      * <p> If <tt>type()</tt> is <tt>JAXBElement.class</tt> , then
217      * <tt>namespace()</tt> and <tt>name()</tt>
218      * point to a factory method with {@link XmlElementDecl}. The XML
219      * element name is the element name from the factory method's
220      * {@link XmlElementDecl} annotation or if an element from its
221      * substitution group (of which it is a head element) has been
222      * substituted in the XML document, then the element name is from the
223      * {@link XmlElementDecl} on the substituted element.
224      *
225      * <p> If {@link #type()} is not <tt>JAXBElement.class</tt>, then
226      * the XML element name is the XML element name statically
227      * associated with the type using the annotation {@link
228      * XmlRootElement} on the type. If the type is not annotated with
229      * an {@link XmlElementDecl}, then it is an error.
230      *
231      * <p> If <tt>type()</tt> is not <tt>JAXBElement.class</tt>, then
232      * this value must be "".
233      *
234      */

235     String JavaDoc namespace() default "";
236     /**
237      *
238      * @see #namespace()
239      */

240     String JavaDoc name() default "##default";
241
242     /**
243      * Used in {@link XmlElementRef#type()} to
244      * signal that the type be inferred from the signature
245      * of the property.
246      */

247     static final class DEFAULT {}
248 }
249
Popular Tags