1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ 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; /** * <p> * Maps a class or an enum type to a XML Schema type. * * <p><b>Usage</b></p> * <p> The <tt>@XmlType</tt> annnotation can be used with the following program * elements: * <ul> * <li> a top level class </li> * <li> an enum type </li> * </ul> * * <p>See "Package Specification" in javax.xml.bind.package javadoc for * additional common information.</p> * * <h3> Mapping a Class </h3> * <p> * 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). * <p> 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 <tt>factoryMethod()</tt> and * <tt>factoryClass()</tt> 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. * <p> * 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 <tt>@XmlType(name="")</tt>. * <p> * Either a global element, local element or a local attribute can be * associated with an anonymous type as follows: * <ul> * <li><b>global element: </b> A global element of an anonymous * type can be derived by annotating the class with @{@link * XmlRootElement}. See Example 3 below. </li> * * <li><b>local element: </b> 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.</li> * * <li><b>attribute: </b> 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. </li> * </ul> * <b> Mapping to XML Schema Complex Type </b> * <ul> * <li>If class is annotated with <tt>@XmlType(name="") </tt>, it * is mapped to an anonymous type otherwise, the class name maps * to a complex type name. The <tt>XmlName()</tt> annotation element * can be used to customize the name.</li> * * <li> Properties and fields that are mapped to elements are mapped to a * content model within a complex type. The annotation element * <tt>propOrder()</tt> can be used to customize the content model to be * <tt>xs:all</tt> or <tt>xs:sequence</tt>. It is used for specifying * the order of XML elements in <tt>xs:sequence</tt>. </li> * * <li> Properties and fields can be mapped to attributes within the * complex type. </li> * * <li> The targetnamespace of the XML Schema type can be customized * using the annotation element <tt>namespace()</tt>. </li> * </ul> * * <p> * <b> Mapping class to XML Schema simple type </b> * <p> * A class can be mapped to a XML Schema simple type using the * <tt>@XmlValue</tt> annotation. For additional details and examples, * see @{@link XmlValue} annotation type. * <p> * 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: * <ul> * <li> -> : represents a mapping </li> * <li> [x]+ : one or more occurances of x </li> * <li> [ <tt>@XmlValue</tt> property ]: JavaBean property annotated with * <tt>@XmlValue</tt></li> * <li> X : don't care * </ul> * <blockquote> * <table border="1" cellpadding="4" cellspacing="3"> * <tbody> * <tr> * <td><b>Target</b></td> * <td><b>propOrder</b></td> * <td><b>ClassBody</b></td> * <td><b>ComplexType</b></td> * <td><b>SimpleType</b></td> * </tr> * * <tr valign="top"> * <td>Class</td> * <td>{}</td> * <td>[property]+ -> elements</td> * <td>complexcontent<br>xs:all</td> * <td> </td> * </tr> * * <tr valign="top"> * <td>Class</td> * <td>non empty</td> * <td>[property]+ -> elements</td> * <td>complexcontent<br>xs:sequence</td> * <td> </td> * </tr> * * <tr valign="top"> * <td>Class</td> * <td>X</td> * <td>no property -> element</td> * <td>complexcontent<br>empty sequence</td> * <td> </td> * </tr> * * <tr valign="top"> * <td>Class</td> * <td>X</td> * <td>1 [ <tt>@XmlValue</tt> property] && <br> [property]+ * ->attributes</td> * <td>simplecontent</td> * <td> </td> * </tr> * * <tr valign="top"> * <td>Class</td> * <td>X</td> * <td>1 [ <tt>@XmlValue</tt> property ]&& <br> no properties * -> attribute</td> * <td> </td> * <td>simpletype</td> * <td> </td> * </tr> * </tbody> * </table> * </blockquote> * * <h3> Mapping an enum type </h3> * * An enum type maps to a XML schema simple type with enumeration * facets. The following annotation elements are ignored since they * are not meaningful: <tt>propOrder()</tt> , <tt>factoryMethod()</tt> , * <tt>factoryClass()</tt> . * * <h3> Usage with other annotations </h3> * <p> 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. * * <p> <b> Example 1: </b> Map a class to a complex type with * xs:sequence with a customized ordering of JavaBean properties. * </p> * * <pre> * @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) {..}; * } * * <!-- XML Schema mapping for USAddress --> * <xs:complexType name="USAddress"> * <xs:sequence> * <xs:element name="street" type="xs:string"/> * <xs:element name="city" type="xs:string"/> * <xs:element name="state" type="xs:string"/> * <xs:element name="zip" type="xs:decimal"/> * <xs:element name="name" type="xs:string"/> * </xs:all> * </xs:complexType> * </pre> * <p> <b> Example 2: </b> Map a class to a complex type with * xs:all </p> * <pre> * @XmlType(propOrder={}) * public class USAddress { ...} * * <!-- XML Schema mapping for USAddress --> * <xs:complexType name="USAddress"> * <xs:all> * <xs:element name="name" type="xs:string"/> * <xs:element name="street" type="xs:string"/> * <xs:element name="city" type="xs:string"/> * <xs:element name="state" type="xs:string"/> * <xs:element name="zip" type="xs:decimal"/> * </xs:sequence> * </xs:complexType> *</pre> * <p> <b> Example 3: </b> Map a class to a global element with an * anonymous type. * </p> * <pre> * @XmlRootElement * @XmlType(name="") * public class USAddress { ...} * * <!-- XML Schema mapping for USAddress --> * <xs:element name="USAddress"> * <xs:complexType> * <xs:sequence> * <xs:element name="name" type="xs:string"/> * <xs:element name="street" type="xs:string"/> * <xs:element name="city" type="xs:string"/> * <xs:element name="state" type="xs:string"/> * <xs:element name="zip" type="xs:decimal"/> * </xs:sequence> * </xs:complexType> * </xs:element> * </pre> * * <p> <b> Example 4: </b> Map a property to a local element with * anonmyous type. * <pre> * //Example: Code fragment * public class Invoice { * USAddress addr; * ... * } * * @XmlType(name="") * public class USAddress { ... } * } * * <!-- XML Schema mapping for USAddress --> * <xs:complexType name="Invoice"> * <xs:sequence> * <xs:element name="addr"> * <xs:complexType> * <xs:element name="name", type="xs:string"/> * <xs:element name="city", type="xs:string"/> * <xs:element name="city" type="xs:string"/> * <xs:element name="state" type="xs:string"/> * <xs:element name="zip" type="xs:decimal"/> * </xs:complexType> * ... * </xs:sequence> * </xs:complexType> * </pre> * * <p> <b> Example 5: </b> Map a property to an attribute with * anonymous type. * * <pre> * * //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; * } * * <!-- Example: XML Schema fragment --> * <xs:complexType name="Item"> * <xs:sequence> * <xs:element name="name" type="xs:string"/> * <xs:attribute name="price"> * <xs:simpleType> * <xs:restriction base="xs:decimal"/> * </xs:simpleType> * </xs:attribute> * </xs:sequence> * </xs:complexType> * </pre> * * <p> <b> Example 6: </b> Define a factoryClass and factoryMethod * * <pre> * @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); * } * * </pre> * * <p> <b> Example 7: </b> Define factoryMethod and use the default factoryClass * * <pre> * @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(); * } * } * </pre> * * @author Sekhar Vajjhala, Sun Microsystems, Inc. * @see XmlElement * @see XmlAttribute * @see XmlValue * @see XmlSchema * @since JAXB2.0 * @version $Revision: 1.19 $ */ @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. * * <p> Refer to the table for how the propOrder affects the * mapping of class </p> * * <p> 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. </p> * <p> All of the JavaBean properties being mapped to XML Schema elements * must be listed. * <p> A JavaBean property or field listed in propOrder must not * be transient or annotated with <tt>@XmlTransient</tt>. * <p> 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. * * <p>If <tt>factoryClass</tt> is DEFAULT.class and * <tt>factoryMethod</tt> is "", then there is no static factory * method. * * <p>If <tt>factoryClass</tt> is DEFAULT.class and * <tt>factoryMethod</tt> is not "", then * <tt>factoryMethod</tt> is the name of a static factory method * in this class. * * <p>If <tt>factoryClass</tt> is not DEFAULT.class, then * <tt>factoryMethod</tt> must not be "" and must be the name of * a static factory method specified in <tt>factoryClass</tt>. */ 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 * <tt>factoryClass</tt> factoryClass(). * */ String factoryMethod() default ""; }