001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005:
006: package javax.xml.bind.annotation;
007:
008: import static java.lang.annotation.ElementType.TYPE;
009: import java.lang.annotation.Retention;
010: import static java.lang.annotation.RetentionPolicy.RUNTIME;
011: import java.lang.annotation.Target;
012:
013: /**
014: * <p>
015: * Maps a class or an enum type to a XML Schema type.
016: *
017: * <p><b>Usage</b></p>
018: * <p> The <tt>@XmlType</tt> annnotation can be used with the following program
019: * elements:
020: * <ul>
021: * <li> a top level class </li>
022: * <li> an enum type </li>
023: * </ul>
024: *
025: * <p>See "Package Specification" in javax.xml.bind.package javadoc for
026: * additional common information.</p>
027: *
028: * <h3> Mapping a Class </h3>
029: * <p>
030: * A class maps to a XML Schema type. A class is a data container for
031: * values represented by properties and fields. A schema type is a
032: * data container for values represented by schema components within a
033: * schema type's content model (e.g. model groups, attributes etc).
034: * <p> To be mapped, a class must either have a public no-arg
035: * constructor or a static no-arg factory method. The static factory
036: * method can be specified in <tt>factoryMethod()</tt> and
037: * <tt>factoryClass()</tt> annotation elements. The static factory
038: * method or the no-arg constructor is used during unmarshalling to
039: * create an instance of this class. If both are present, the static
040: * factory method overrides the no-arg constructor.
041: * <p>
042: * A class maps to either a XML Schema complex type or a XML Schema simple
043: * type. The XML Schema type is derived based on the
044: * mapping of JavaBean properties and fields contained within the
045: * class. The schema type to which the class is mapped can either be
046: * named or anonymous. A class can be mapped to an anonymous schema
047: * type by annotating the class with <tt>@XmlType(name="")</tt>.
048: * <p>
049: * Either a global element, local element or a local attribute can be
050: * associated with an anonymous type as follows:
051: * <ul>
052: * <li><b>global element: </b> A global element of an anonymous
053: * type can be derived by annotating the class with @{@link
054: * XmlRootElement}. See Example 3 below. </li>
055: *
056: * <li><b>local element: </b> A JavaBean property that references
057: * a class annotated with @XmlType(name="") and is mapped to the
058: * element associated with the anonymous type. See Example 4
059: * below.</li>
060: *
061: * <li><b>attribute: </b> A JavaBean property that references
062: * a class annotated with @XmlType(name="") and is mapped to the
063: * attribute associated with the anonymous type. See Example 5 below. </li>
064: * </ul>
065: * <b> Mapping to XML Schema Complex Type </b>
066: * <ul>
067: * <li>If class is annotated with <tt>@XmlType(name="") </tt>, it
068: * is mapped to an anonymous type otherwise, the class name maps
069: * to a complex type name. The <tt>XmlName()</tt> annotation element
070: * can be used to customize the name.</li>
071: *
072: * <li> Properties and fields that are mapped to elements are mapped to a
073: * content model within a complex type. The annotation element
074: * <tt>propOrder()</tt> can be used to customize the content model to be
075: * <tt>xs:all</tt> or <tt>xs:sequence</tt>. It is used for specifying
076: * the order of XML elements in <tt>xs:sequence</tt>. </li>
077: *
078: * <li> Properties and fields can be mapped to attributes within the
079: * complex type. </li>
080: *
081: * <li> The targetnamespace of the XML Schema type can be customized
082: * using the annotation element <tt>namespace()</tt>. </li>
083: * </ul>
084: *
085: * <p>
086: * <b> Mapping class to XML Schema simple type </b>
087: * <p>
088: * A class can be mapped to a XML Schema simple type using the
089: * <tt>@XmlValue</tt> annotation. For additional details and examples,
090: * see @{@link XmlValue} annotation type.
091: * <p>
092: * The following table shows the mapping of the class to a XML Schema
093: * complex type or simple type. The notational symbols used in the table are:
094: * <ul>
095: * <li> -> : represents a mapping </li>
096: * <li> [x]+ : one or more occurances of x </li>
097: * <li> [ <tt>@XmlValue</tt> property ]: JavaBean property annotated with
098: * <tt>@XmlValue</tt></li>
099: * <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: * @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: * <!-- XML Schema mapping for USAddress -->
196: * <xs:complexType name="USAddress">
197: * <xs:sequence>
198: * <xs:element name="street" type="xs:string"/>
199: * <xs:element name="city" type="xs:string"/>
200: * <xs:element name="state" type="xs:string"/>
201: * <xs:element name="zip" type="xs:decimal"/>
202: * <xs:element name="name" type="xs:string"/>
203: * </xs:all>
204: * </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: * @XmlType(propOrder={})
210: * public class USAddress { ...}
211: *
212: * <!-- XML Schema mapping for USAddress -->
213: * <xs:complexType name="USAddress">
214: * <xs:all>
215: * <xs:element name="name" type="xs:string"/>
216: * <xs:element name="street" type="xs:string"/>
217: * <xs:element name="city" type="xs:string"/>
218: * <xs:element name="state" type="xs:string"/>
219: * <xs:element name="zip" type="xs:decimal"/>
220: * </xs:sequence>
221: * </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: * @XmlRootElement
228: * @XmlType(name="")
229: * public class USAddress { ...}
230: *
231: * <!-- XML Schema mapping for USAddress -->
232: * <xs:element name="USAddress">
233: * <xs:complexType>
234: * <xs:sequence>
235: * <xs:element name="name" type="xs:string"/>
236: * <xs:element name="street" type="xs:string"/>
237: * <xs:element name="city" type="xs:string"/>
238: * <xs:element name="state" type="xs:string"/>
239: * <xs:element name="zip" type="xs:decimal"/>
240: * </xs:sequence>
241: * </xs:complexType>
242: * </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: * @XmlType(name="")
255: * public class USAddress { ... }
256: * }
257: *
258: * <!-- XML Schema mapping for USAddress -->
259: * <xs:complexType name="Invoice">
260: * <xs:sequence>
261: * <xs:element name="addr">
262: * <xs:complexType>
263: * <xs:element name="name", type="xs:string"/>
264: * <xs:element name="city", type="xs:string"/>
265: * <xs:element name="city" type="xs:string"/>
266: * <xs:element name="state" type="xs:string"/>
267: * <xs:element name="zip" type="xs:decimal"/>
268: * </xs:complexType>
269: * ...
270: * </xs:sequence>
271: * </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: * @XmlAttribute
283: * public USPrice price;
284: * }
285: *
286: * // map class to anonymous simple type.
287: * @XmlType(name="")
288: * public class USPrice {
289: * @XmlValue
290: * public java.math.BigDecimal price;
291: * }
292: *
293: * <!-- Example: XML Schema fragment -->
294: * <xs:complexType name="Item">
295: * <xs:sequence>
296: * <xs:element name="name" type="xs:string"/>
297: * <xs:attribute name="price">
298: * <xs:simpleType>
299: * <xs:restriction base="xs:decimal"/>
300: * </xs:simpleType>
301: * </xs:attribute>
302: * </xs:sequence>
303: * </xs:complexType>
304: * </pre>
305: *
306: * <p> <b> Example 6: </b> Define a factoryClass and factoryMethod
307: *
308: * <pre>
309: * @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: * @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.20 $
364: */
365:
366: @Retention(RUNTIME)
367: @Target({TYPE})
368: public @interface XmlType {
369: /**
370: * Name of the XML Schema type which the class is mapped.
371: */
372: String name() default "##default";
373:
374: /**
375: * Specifies the order for XML Schema elements when class is
376: * mapped to a XML Schema complex type.
377: *
378: * <p> Refer to the table for how the propOrder affects the
379: * mapping of class </p>
380: *
381: * <p> The propOrder is a list of names of JavaBean properties in
382: * the class. Each name in the list is the name of a Java
383: * identifier of the JavaBean property. The order in which
384: * JavaBean properties are listed is the order of XML Schema
385: * elements to which the JavaBean properties are mapped. </p>
386: * <p> All of the JavaBean properties being mapped to XML Schema elements
387: * must be listed.
388: * <p> A JavaBean property or field listed in propOrder must not
389: * be transient or annotated with <tt>@XmlTransient</tt>.
390: * <p> The default ordering of JavaBean properties is determined
391: * by @{@link XmlAccessorOrder}.
392: */
393: String[] propOrder() default { "" };
394:
395: /**
396: * Name of the target namespace of the XML Schema type. By
397: * default, this is the target namespace to which the package
398: * containing the class is mapped.
399: */
400: String namespace() default "##default";
401:
402: /**
403: * Class containing a no-arg factory method for creating an
404: * instance of this class. The default is this class.
405: *
406: * <p>If <tt>factoryClass</tt> is DEFAULT.class and
407: * <tt>factoryMethod</tt> is "", then there is no static factory
408: * method.
409: *
410: * <p>If <tt>factoryClass</tt> is DEFAULT.class and
411: * <tt>factoryMethod</tt> is not "", then
412: * <tt>factoryMethod</tt> is the name of a static factory method
413: * in this class.
414: *
415: * <p>If <tt>factoryClass</tt> is not DEFAULT.class, then
416: * <tt>factoryMethod</tt> must not be "" and must be the name of
417: * a static factory method specified in <tt>factoryClass</tt>.
418: */
419: Class factoryClass() default DEFAULT.class;
420:
421: /**
422: * Used in {@link XmlType#factoryClass()} to
423: * signal that either factory mehod is not used or
424: * that it's in the class with this {@link XmlType} itself.
425: */
426: static final class DEFAULT {
427: }
428:
429: /**
430: * Name of a no-arg factory method in the class specified in
431: * <tt>factoryClass</tt> factoryClass().
432: *
433: */
434: String factoryMethod() default "";
435: }
|