KickJava   Java API By Example, From Geeks To Geeks.

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


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 static java.lang.annotation.ElementType.TYPE JavaDoc;
9 import java.lang.annotation.Retention JavaDoc;
10 import static java.lang.annotation.RetentionPolicy.RUNTIME JavaDoc;
11 import java.lang.annotation.Target JavaDoc;
12
13 /**
14  * <p>
15  * Maps a class or an enum type to a XML Schema type.
16  *
17  * <p><b>Usage</b></p>
18  * <p> The <tt>@XmlType</tt> annnotation can be used with the following program
19  * elements:
20  * <ul>
21  * <li> a top level class </li>
22  * <li> an enum type </li>
23  * </ul>
24  *
25  * <p>See "Package Specification" in javax.xml.bind.package javadoc for
26  * additional common information.</p>
27  *
28  * <h3> Mapping a Class </h3>
29  * <p>
30  * A class maps to a XML Schema type. A class is a data container for
31  * values represented by properties and fields. A schema type is a
32  * data container for values represented by schema components within a
33  * schema type's content model (e.g. model groups, attributes etc).
34  * <p> To be mapped, a class must either have a public no-arg
35  * constructor or a static no-arg factory method. The static factory
36  * method can be specified in <tt>factoryMethod()</tt> and
37  * <tt>factoryClass()</tt> annotation elements. The static factory
38  * method or the no-arg constructor is used during unmarshalling to
39  * create an instance of this class. If both are present, the static
40  * factory method overrides the no-arg constructor.
41  * <p>
42  * A class maps to either a XML Schema complex type or a XML Schema simple
43  * type. The XML Schema type is derived based on the
44  * mapping of JavaBean properties and fields contained within the
45  * class. The schema type to which the class is mapped can either be
46  * named or anonymous. A class can be mapped to an anonymous schema
47  * type by annotating the class with <tt>&#64XmlType(name="")</tt>.
48  * <p>
49  * Either a global element, local element or a local attribute can be
50  * associated with an anonymous type as follows:
51  * <ul>
52  * <li><b>global element: </b> A global element of an anonymous
53  * type can be derived by annotating the class with @{@link
54  * XmlRootElement}. See Example 3 below. </li>
55  *
56  * <li><b>local element: </b> A JavaBean property that references
57  * a class annotated with @XmlType(name="") and is mapped to the
58  * element associated with the anonymous type. See Example 4
59  * below.</li>
60  *
61  * <li><b>attribute: </b> A JavaBean property that references
62  * a class annotated with @XmlType(name="") and is mapped to the
63  * attribute associated with the anonymous type. See Example 5 below. </li>
64  * </ul>
65  * <b> Mapping to XML Schema Complex Type </b>
66  * <ul>
67  * <li>If class is annotated with <tt>@XmlType(name="") </tt>, it
68  * is mapped to an anonymous type otherwise, the class name maps
69  * to a complex type name. The <tt>XmlName()</tt> annotation element
70  * can be used to customize the name.</li>
71  *
72  * <li> Properties and fields that are mapped to elements are mapped to a
73  * content model within a complex type. The annotation element
74  * <tt>propOrder()</tt> can be used to customize the content model to be
75  * <tt>xs:all</tt> or <tt>xs:sequence</tt>. It is used for specifying
76  * the order of XML elements in <tt>xs:sequence</tt>. </li>
77  *
78  * <li> Properties and fields can be mapped to attributes within the
79  * complex type. </li>
80  *
81  * <li> The targetnamespace of the XML Schema type can be customized
82  * using the annotation element <tt>namespace()</tt>. </li>
83  * </ul>
84  *
85  * <p>
86  * <b> Mapping class to XML Schema simple type </b>
87  * <p>
88  * A class can be mapped to a XML Schema simple type using the
89  * <tt>@XmlValue</tt> annotation. For additional details and examples,
90  * see @{@link XmlValue} annotation type.
91  * <p>
92  * The following table shows the mapping of the class to a XML Schema
93  * complex type or simple type. The notational symbols used in the table are:
94  * <ul>
95  * <li> -> : represents a mapping </li>
96  * <li> [x]+ : one or more occurances of x </li>
97  * <li> [ <tt>@XmlValue</tt> property ]: JavaBean property annotated with
98  * <tt>@XmlValue</tt></li>
99  * <li> X : don't care
100  * </ul>
101  * <blockquote>
102  * <table border="1" cellpadding="4" cellspacing="3">
103  * <tbody>
104  * <tr>
105  * <td><b>Target</td>
106  * <td><b>propOrder</b></td>
107  * <td><b>ClassBody</b></td>
108  * <td><b>ComplexType</b></td>
109  * <td><b>SimpleType</b></td>
110  * </tr>
111  *
112  * <tr valign="top">
113  * <td>Class</td>
114  * <td>{}</td>
115  * <td>[property]+ -> elements</td>
116  * <td>complexcontent<br>xs:all</td>
117  * <td> </td>
118  * </tr>
119  *
120  * <tr valign="top">
121  * <td>Class</td>
122  * <td>non empty</td>
123  * <td>[property]+ -> elements</td>
124  * <td>complexcontent<br>xs:sequence</td>
125  * <td> </td>
126  * </tr>
127  *
128  * <tr valign="top">
129  * <td>Class</td>
130  * <td>X</td>
131  * <td>no property -> element</td>
132  * <td>complexcontent<br>empty sequence</td>
133  * <td> </td>
134  * </tr>
135  *
136  * <tr valign="top">
137  * <td>Class</td>
138  * <td>X</td>
139  * <td>1 [ <tt>@XmlValue</tt> property] && <br> [property]+
140  * ->attributes</td>
141  * <td>simplecontent</td>
142  * <td> </td>
143  * </tr>
144  *
145  * <tr valign="top">
146  * <td>Class</td>
147  * <td>X</td>
148  * <td>1 [ <tt>@XmlValue</tt> property ]&& <br> no properties
149  * -> attribute</td>
150  * <td> </td>
151  * <td>simpletype</td>
152  * <td> </td>
153  * </tr>
154  * </tbody>
155  * </table>
156  * </blockquote>
157  *
158  * <h3> Mapping an enum type </h3>
159  *
160  * An enum type maps to a XML schema simple type with enumeration
161  * facets. The following annotation elements are ignored since they
162  * are not meaningful: <tt>propOrder()</tt> , <tt>factoryMethod()</tt> ,
163  * <tt>factoryClass()</tt> .
164  *
165  * <h3> Usage with other annotations </h3>
166  * <p> This annotation can be used with the following annotations:
167  * {@link XmlRootElement}, {@link XmlAccessorOrder}, {@link XmlAccessorType},
168  * {@link XmlEnum}. However, {@link
169  * XmlAccessorOrder} and {@link XmlAccessorType} are ignored when this
170  * annotation is used on an enum type.
171  *
172  * <p> <b> Example 1: </b> Map a class to a complex type with
173  * xs:sequence with a customized ordering of JavaBean properties.
174  * </p>
175  *
176  * <pre>
177  * &#64;XmlType(propOrder={"street", "city" , "state", "zip", "name" })
178  * public class USAddress {
179  * String getName() {..};
180  * void setName(String) {..};
181  *
182  * String getStreet() {..};
183  * void setStreet(String) {..};
184  *
185  * String getCity() {..};
186  * void setCity(String) {..};
187  *
188  * String getState() {..};
189  * void setState(String) {..};
190  *
191  * java.math.BigDecimal getZip() {..};
192  * void setZip(java.math.BigDecimal) {..};
193  * }
194  *
195  * &lt;!-- XML Schema mapping for USAddress -->
196  * &lt;xs:complexType name="USAddress">
197  * &lt;xs:sequence>
198  * &lt;xs:element name="street" type="xs:string"/>
199  * &lt;xs:element name="city" type="xs:string"/>
200  * &lt;xs:element name="state" type="xs:string"/>
201  * &lt;xs:element name="zip" type="xs:decimal"/>
202  * &lt;xs:element name="name" type="xs:string"/>
203  * &lt;/xs:all>
204  * &lt;/xs:complexType>
205  * </pre>
206  * <p> <b> Example 2: </b> Map a class to a complex type with
207  * xs:all </p>
208  * <pre>
209  * &#64;XmlType(propOrder={})
210  * public class USAddress { ...}
211  *
212  * &lt;!-- XML Schema mapping for USAddress -->
213  * &lt;xs:complexType name="USAddress">
214  * &lt;xs:all>
215  * &lt;xs:element name="name" type="xs:string"/>
216  * &lt;xs:element name="street" type="xs:string"/>
217  * &lt;xs:element name="city" type="xs:string"/>
218  * &lt;xs:element name="state" type="xs:string"/>
219  * &lt;xs:element name="zip" type="xs:decimal"/>
220  * &lt;/xs:sequence>
221  * &lt;/xs:complexType>
222  *</pre>
223  * <p> <b> Example 3: </b> Map a class to a global element with an
224  * anonymous type.
225  * </p>
226  * <pre>
227  * &#64;XmlRootElement
228  * &#64;XmlType(name="")
229  * public class USAddress { ...}
230  *
231  * &lt;!-- XML Schema mapping for USAddress -->
232  * &lt;xs:element name="USAddress">
233  * &lt;xs:complexType>
234  * &lt;xs:sequence>
235  * &lt;xs:element name="name" type="xs:string"/>
236  * &lt;xs:element name="street" type="xs:string"/>
237  * &lt;xs:element name="city" type="xs:string"/>
238  * &lt;xs:element name="state" type="xs:string"/>
239  * &lt;xs:element name="zip" type="xs:decimal"/>
240  * &lt;/xs:sequence>
241  * &lt;/xs:complexType>
242  * &lt;/xs:element>
243  * </pre>
244  *
245  * <p> <b> Example 4: </b> Map a property to a local element with
246  * anonmyous type.
247  * <pre>
248  * //Example: Code fragment
249  * public class Invoice {
250  * USAddress addr;
251  * ...
252  * }
253  *
254  * &#64;XmlType(name="")
255  * public class USAddress { ... }
256  * }
257  *
258  * &lt;!-- XML Schema mapping for USAddress -->
259  * &lt;xs:complexType name="Invoice">
260  * &lt;xs:sequence>
261  * &lt;xs:element name="addr">
262  * &lt;xs:complexType>
263  * &lt;xs:element name="name", type="xs:string"/>
264  * &lt;xs:element name="city", type="xs:string"/>
265  * &lt;xs:element name="city" type="xs:string"/>
266  * &lt;xs:element name="state" type="xs:string"/>
267  * &lt;xs:element name="zip" type="xs:decimal"/>
268  * &lt;/xs:complexType>
269  * ...
270  * &lt;/xs:sequence>
271  * &lt;/xs:complexType>
272  * </pre>
273  *
274  * <p> <b> Example 5: </b> Map a property to an attribute with
275  * anonymous type.
276  *
277  * <pre>
278  *
279  * //Example: Code fragment
280  * public class Item {
281  * public String name;
282  * &#64;XmlAttribute
283  * public USPrice price;
284  * }
285  *
286  * // map class to anonymous simple type.
287  * &#64;XmlType(name="")
288  * public class USPrice {
289  * &#64;XmlValue
290  * public java.math.BigDecimal price;
291  * }
292  *
293  * &lt;!-- Example: XML Schema fragment -->
294  * &lt;xs:complexType name="Item">
295  * &lt;xs:sequence>
296  * &lt;xs:element name="name" type="xs:string"/>
297  * &lt;xs:attribute name="price">
298  * &lt;xs:simpleType>
299  * &lt;xs:restriction base="xs:decimal"/>
300  * &lt;/xs:simpleType>
301  * &lt;/xs:attribute>
302  * &lt;/xs:sequence>
303  * &lt;/xs:complexType>
304  * </pre>
305  *
306  * <p> <b> Example 6: </b> Define a factoryClass and factoryMethod
307  *
308  * <pre>
309  * &#64;XmlType(name="USAddressType", factoryClass=USAddressFactory.class,
310  * factoryMethod="getUSAddress")
311  * public class USAddress {
312  *
313  * private String city;
314  * private String name;
315  * private String state;
316  * private String street;
317  * private int zip;
318  *
319  * public USAddress(String name, String street, String city,
320  * String state, int zip) {
321  * this.name = name;
322  * this.street = street;
323  * this.city = city;
324  * this.state = state;
325  * this.zip = zip;
326  * }
327  * }
328  *
329  * public class USAddressFactory {
330  * public static USAddress getUSAddress(){
331  * return new USAddress("Mark Baker", "23 Elm St",
332  * "Dayton", "OH", 90952);
333  * }
334  *
335  * </pre>
336  *
337  * <p> <b> Example 7: </b> Define factoryMethod and use the default factoryClass
338  *
339  * <pre>
340  * &#64;XmlType(name="USAddressType", factoryMethod="getNewInstance")
341  * public class USAddress {
342  *
343  * private String city;
344  * private String name;
345  * private String state;
346  * private String street;
347  * private int zip;
348  *
349  * private USAddress() {}
350  *
351  * public static USAddress getNewInstance(){
352  * return new USAddress();
353  * }
354  * }
355  * </pre>
356  *
357  * @author Sekhar Vajjhala, Sun Microsystems, Inc.
358  * @see XmlElement
359  * @see XmlAttribute
360  * @see XmlValue
361  * @see XmlSchema
362  * @since JAXB2.0
363  * @version $Revision: 1.19 $
364  */

365
366 @Retention JavaDoc(RUNTIME) @Target JavaDoc({TYPE})
367 public @interface XmlType {
368     /**
369      * Name of the XML Schema type which the class is mapped.
370      */

371     String JavaDoc name() default "##default" ;
372  
373     /**
374      * Specifies the order for XML Schema elements when class is
375      * mapped to a XML Schema complex type.
376      *
377      * <p> Refer to the table for how the propOrder affects the
378      * mapping of class </p>
379      *
380      * <p> The propOrder is a list of names of JavaBean properties in
381      * the class. Each name in the list is the name of a Java
382      * identifier of the JavaBean property. The order in which
383      * JavaBean properties are listed is the order of XML Schema
384      * elements to which the JavaBean properties are mapped. </p>
385      * <p> All of the JavaBean properties being mapped to XML Schema elements
386      * must be listed.
387      * <p> A JavaBean property or field listed in propOrder must not
388      * be transient or annotated with <tt>@XmlTransient</tt>.
389      * <p> The default ordering of JavaBean properties is determined
390      * by @{@link XmlAccessorOrder}.
391      */

392     String JavaDoc[] propOrder() default {""};
393
394     /**
395      * Name of the target namespace of the XML Schema type. By
396      * default, this is the target namespace to which the package
397      * containing the class is mapped.
398      */

399     String JavaDoc namespace() default "##default" ;
400    
401     /**
402      * Class containing a no-arg factory method for creating an
403      * instance of this class. The default is this class.
404      *
405      * <p>If <tt>factoryClass</tt> is DEFAULT.class and
406      * <tt>factoryMethod</tt> is "", then there is no static factory
407      * method.
408      *
409      * <p>If <tt>factoryClass</tt> is DEFAULT.class and
410      * <tt>factoryMethod</tt> is not "", then
411      * <tt>factoryMethod</tt> is the name of a static factory method
412      * in this class.
413      *
414      * <p>If <tt>factoryClass</tt> is not DEFAULT.class, then
415      * <tt>factoryMethod</tt> must not be "" and must be the name of
416      * a static factory method specified in <tt>factoryClass</tt>.
417      */

418     Class JavaDoc factoryClass() default DEFAULT.class;
419
420     /**
421      * Used in {@link XmlType#factoryClass()} to
422      * signal that either factory mehod is not used or
423      * that it's in the class with this {@link XmlType} itself.
424      */

425     static final class DEFAULT {}
426
427     /**
428      * Name of a no-arg factory method in the class specified in
429      * <tt>factoryClass</tt> factoryClass().
430      *
431      */

432     String JavaDoc factoryMethod() default "";
433 }
434
435
436
Popular Tags