src/share/jaxws_classes/javax/xml/bind/annotation/XmlType.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.annotation;
ohair@286 27
ohair@286 28 import static java.lang.annotation.ElementType.TYPE;
ohair@286 29 import java.lang.annotation.Retention;
ohair@286 30 import static java.lang.annotation.RetentionPolicy.RUNTIME;
ohair@286 31 import java.lang.annotation.Target;
ohair@286 32
ohair@286 33 /**
ohair@286 34 * <p>
ohair@286 35 * Maps a class or an enum type to a XML Schema type.
ohair@286 36 *
ohair@286 37 * <p><b>Usage</b></p>
ohair@286 38 * <p> The <tt>@XmlType</tt> annnotation can be used with the following program
ohair@286 39 * elements:
ohair@286 40 * <ul>
ohair@286 41 * <li> a top level class </li>
ohair@286 42 * <li> an enum type </li>
ohair@286 43 * </ul>
ohair@286 44 *
ohair@286 45 * <p>See "Package Specification" in javax.xml.bind.package javadoc for
ohair@286 46 * additional common information.</p>
ohair@286 47 *
ohair@286 48 * <h3> Mapping a Class </h3>
ohair@286 49 * <p>
ohair@286 50 * A class maps to a XML Schema type. A class is a data container for
ohair@286 51 * values represented by properties and fields. A schema type is a
ohair@286 52 * data container for values represented by schema components within a
ohair@286 53 * schema type's content model (e.g. model groups, attributes etc).
ohair@286 54 * <p> To be mapped, a class must either have a public no-arg
ohair@286 55 * constructor or a static no-arg factory method. The static factory
ohair@286 56 * method can be specified in <tt>factoryMethod()</tt> and
ohair@286 57 * <tt>factoryClass()</tt> annotation elements. The static factory
ohair@286 58 * method or the no-arg constructor is used during unmarshalling to
ohair@286 59 * create an instance of this class. If both are present, the static
ohair@286 60 * factory method overrides the no-arg constructor.
ohair@286 61 * <p>
ohair@286 62 * A class maps to either a XML Schema complex type or a XML Schema simple
ohair@286 63 * type. The XML Schema type is derived based on the
ohair@286 64 * mapping of JavaBean properties and fields contained within the
ohair@286 65 * class. The schema type to which the class is mapped can either be
ohair@286 66 * named or anonymous. A class can be mapped to an anonymous schema
ohair@286 67 * type by annotating the class with <tt>&#64XmlType(name="")</tt>.
ohair@286 68 * <p>
ohair@286 69 * Either a global element, local element or a local attribute can be
ohair@286 70 * associated with an anonymous type as follows:
ohair@286 71 * <ul>
ohair@286 72 * <li><b>global element: </b> A global element of an anonymous
ohair@286 73 * type can be derived by annotating the class with @{@link
ohair@286 74 * XmlRootElement}. See Example 3 below. </li>
ohair@286 75 *
ohair@286 76 * <li><b>local element: </b> A JavaBean property that references
ohair@286 77 * a class annotated with @XmlType(name="") and is mapped to the
ohair@286 78 * element associated with the anonymous type. See Example 4
ohair@286 79 * below.</li>
ohair@286 80 *
ohair@286 81 * <li><b>attribute: </b> A JavaBean property that references
ohair@286 82 * a class annotated with @XmlType(name="") and is mapped to the
ohair@286 83 * attribute associated with the anonymous type. See Example 5 below. </li>
ohair@286 84 * </ul>
ohair@286 85 * <b> Mapping to XML Schema Complex Type </b>
ohair@286 86 * <ul>
ohair@286 87 * <li>If class is annotated with <tt>@XmlType(name="") </tt>, it
ohair@286 88 * is mapped to an anonymous type otherwise, the class name maps
ohair@286 89 * to a complex type name. The <tt>XmlName()</tt> annotation element
ohair@286 90 * can be used to customize the name.</li>
ohair@286 91 *
ohair@286 92 * <li> Properties and fields that are mapped to elements are mapped to a
ohair@286 93 * content model within a complex type. The annotation element
ohair@286 94 * <tt>propOrder()</tt> can be used to customize the content model to be
ohair@286 95 * <tt>xs:all</tt> or <tt>xs:sequence</tt>. It is used for specifying
ohair@286 96 * the order of XML elements in <tt>xs:sequence</tt>. </li>
ohair@286 97 *
ohair@286 98 * <li> Properties and fields can be mapped to attributes within the
ohair@286 99 * complex type. </li>
ohair@286 100 *
ohair@286 101 * <li> The targetnamespace of the XML Schema type can be customized
ohair@286 102 * using the annotation element <tt>namespace()</tt>. </li>
ohair@286 103 * </ul>
ohair@286 104 *
ohair@286 105 * <p>
ohair@286 106 * <b> Mapping class to XML Schema simple type </b>
ohair@286 107 * <p>
ohair@286 108 * A class can be mapped to a XML Schema simple type using the
ohair@286 109 * <tt>@XmlValue</tt> annotation. For additional details and examples,
ohair@286 110 * see @{@link XmlValue} annotation type.
ohair@286 111 * <p>
ohair@286 112 * The following table shows the mapping of the class to a XML Schema
ohair@286 113 * complex type or simple type. The notational symbols used in the table are:
ohair@286 114 * <ul>
ohair@286 115 * <li> -> : represents a mapping </li>
ohair@286 116 * <li> [x]+ : one or more occurances of x </li>
ohair@286 117 * <li> [ <tt>@XmlValue</tt> property ]: JavaBean property annotated with
ohair@286 118 * <tt>@XmlValue</tt></li>
ohair@286 119 * <li> X : don't care
ohair@286 120 * </ul>
ohair@286 121 * <blockquote>
ohair@286 122 * <table border="1" cellpadding="4" cellspacing="3">
ohair@286 123 * <tbody>
ohair@286 124 * <tr>
ohair@286 125 * <td><b>Target</b></td>
ohair@286 126 * <td><b>propOrder</b></td>
ohair@286 127 * <td><b>ClassBody</b></td>
ohair@286 128 * <td><b>ComplexType</b></td>
ohair@286 129 * <td><b>SimpleType</b></td>
ohair@286 130 * </tr>
ohair@286 131 *
ohair@286 132 * <tr valign="top">
ohair@286 133 * <td>Class</td>
ohair@286 134 * <td>{}</td>
ohair@286 135 * <td>[property]+ -> elements</td>
ohair@286 136 * <td>complexcontent<br>xs:all</td>
ohair@286 137 * <td> </td>
ohair@286 138 * </tr>
ohair@286 139 *
ohair@286 140 * <tr valign="top">
ohair@286 141 * <td>Class</td>
ohair@286 142 * <td>non empty</td>
ohair@286 143 * <td>[property]+ -> elements</td>
ohair@286 144 * <td>complexcontent<br>xs:sequence</td>
ohair@286 145 * <td> </td>
ohair@286 146 * </tr>
ohair@286 147 *
ohair@286 148 * <tr valign="top">
ohair@286 149 * <td>Class</td>
ohair@286 150 * <td>X</td>
ohair@286 151 * <td>no property -> element</td>
ohair@286 152 * <td>complexcontent<br>empty sequence</td>
ohair@286 153 * <td> </td>
ohair@286 154 * </tr>
ohair@286 155 *
ohair@286 156 * <tr valign="top">
ohair@286 157 * <td>Class</td>
ohair@286 158 * <td>X</td>
ohair@286 159 * <td>1 [ <tt>@XmlValue</tt> property] && <br> [property]+
ohair@286 160 * ->attributes</td>
ohair@286 161 * <td>simplecontent</td>
ohair@286 162 * <td> </td>
ohair@286 163 * </tr>
ohair@286 164 *
ohair@286 165 * <tr valign="top">
ohair@286 166 * <td>Class</td>
ohair@286 167 * <td>X</td>
ohair@286 168 * <td>1 [ <tt>@XmlValue</tt> property ]&& <br> no properties
ohair@286 169 * -> attribute</td>
ohair@286 170 * <td> </td>
ohair@286 171 * <td>simpletype</td>
ohair@286 172 * <td> </td>
ohair@286 173 * </tr>
ohair@286 174 * </tbody>
ohair@286 175 * </table>
ohair@286 176 * </blockquote>
ohair@286 177 *
ohair@286 178 * <h3> Mapping an enum type </h3>
ohair@286 179 *
ohair@286 180 * An enum type maps to a XML schema simple type with enumeration
ohair@286 181 * facets. The following annotation elements are ignored since they
ohair@286 182 * are not meaningful: <tt>propOrder()</tt> , <tt>factoryMethod()</tt> ,
ohair@286 183 * <tt>factoryClass()</tt> .
ohair@286 184 *
ohair@286 185 * <h3> Usage with other annotations </h3>
ohair@286 186 * <p> This annotation can be used with the following annotations:
ohair@286 187 * {@link XmlRootElement}, {@link XmlAccessorOrder}, {@link XmlAccessorType},
ohair@286 188 * {@link XmlEnum}. However, {@link
ohair@286 189 * XmlAccessorOrder} and {@link XmlAccessorType} are ignored when this
ohair@286 190 * annotation is used on an enum type.
ohair@286 191 *
ohair@286 192 * <p> <b> Example 1: </b> Map a class to a complex type with
ohair@286 193 * xs:sequence with a customized ordering of JavaBean properties.
ohair@286 194 * </p>
ohair@286 195 *
ohair@286 196 * <pre>
ohair@286 197 * &#64;XmlType(propOrder={"street", "city" , "state", "zip", "name" })
ohair@286 198 * public class USAddress {
ohair@286 199 * String getName() {..};
ohair@286 200 * void setName(String) {..};
ohair@286 201 *
ohair@286 202 * String getStreet() {..};
ohair@286 203 * void setStreet(String) {..};
ohair@286 204 *
ohair@286 205 * String getCity() {..};
ohair@286 206 * void setCity(String) {..};
ohair@286 207 *
ohair@286 208 * String getState() {..};
ohair@286 209 * void setState(String) {..};
ohair@286 210 *
ohair@286 211 * java.math.BigDecimal getZip() {..};
ohair@286 212 * void setZip(java.math.BigDecimal) {..};
ohair@286 213 * }
ohair@286 214 *
ohair@286 215 * &lt;!-- XML Schema mapping for USAddress -->
ohair@286 216 * &lt;xs:complexType name="USAddress">
ohair@286 217 * &lt;xs:sequence>
ohair@286 218 * &lt;xs:element name="street" type="xs:string"/>
ohair@286 219 * &lt;xs:element name="city" type="xs:string"/>
ohair@286 220 * &lt;xs:element name="state" type="xs:string"/>
ohair@286 221 * &lt;xs:element name="zip" type="xs:decimal"/>
ohair@286 222 * &lt;xs:element name="name" type="xs:string"/>
ohair@286 223 * &lt;/xs:all>
ohair@286 224 * &lt;/xs:complexType>
ohair@286 225 * </pre>
ohair@286 226 * <p> <b> Example 2: </b> Map a class to a complex type with
ohair@286 227 * xs:all </p>
ohair@286 228 * <pre>
ohair@286 229 * &#64;XmlType(propOrder={})
ohair@286 230 * public class USAddress { ...}
ohair@286 231 *
ohair@286 232 * &lt;!-- XML Schema mapping for USAddress -->
ohair@286 233 * &lt;xs:complexType name="USAddress">
ohair@286 234 * &lt;xs:all>
ohair@286 235 * &lt;xs:element name="name" type="xs:string"/>
ohair@286 236 * &lt;xs:element name="street" type="xs:string"/>
ohair@286 237 * &lt;xs:element name="city" type="xs:string"/>
ohair@286 238 * &lt;xs:element name="state" type="xs:string"/>
ohair@286 239 * &lt;xs:element name="zip" type="xs:decimal"/>
ohair@286 240 * &lt;/xs:sequence>
ohair@286 241 * &lt;/xs:complexType>
ohair@286 242 *</pre>
ohair@286 243 * <p> <b> Example 3: </b> Map a class to a global element with an
ohair@286 244 * anonymous type.
ohair@286 245 * </p>
ohair@286 246 * <pre>
ohair@286 247 * &#64;XmlRootElement
ohair@286 248 * &#64;XmlType(name="")
ohair@286 249 * public class USAddress { ...}
ohair@286 250 *
ohair@286 251 * &lt;!-- XML Schema mapping for USAddress -->
ohair@286 252 * &lt;xs:element name="USAddress">
ohair@286 253 * &lt;xs:complexType>
ohair@286 254 * &lt;xs:sequence>
ohair@286 255 * &lt;xs:element name="name" type="xs:string"/>
ohair@286 256 * &lt;xs:element name="street" type="xs:string"/>
ohair@286 257 * &lt;xs:element name="city" type="xs:string"/>
ohair@286 258 * &lt;xs:element name="state" type="xs:string"/>
ohair@286 259 * &lt;xs:element name="zip" type="xs:decimal"/>
ohair@286 260 * &lt;/xs:sequence>
ohair@286 261 * &lt;/xs:complexType>
ohair@286 262 * &lt;/xs:element>
ohair@286 263 * </pre>
ohair@286 264 *
ohair@286 265 * <p> <b> Example 4: </b> Map a property to a local element with
ohair@286 266 * anonmyous type.
ohair@286 267 * <pre>
ohair@286 268 * //Example: Code fragment
ohair@286 269 * public class Invoice {
ohair@286 270 * USAddress addr;
ohair@286 271 * ...
ohair@286 272 * }
ohair@286 273 *
ohair@286 274 * &#64;XmlType(name="")
ohair@286 275 * public class USAddress { ... }
ohair@286 276 * }
ohair@286 277 *
ohair@286 278 * &lt;!-- XML Schema mapping for USAddress -->
ohair@286 279 * &lt;xs:complexType name="Invoice">
ohair@286 280 * &lt;xs:sequence>
ohair@286 281 * &lt;xs:element name="addr">
ohair@286 282 * &lt;xs:complexType>
ohair@286 283 * &lt;xs:element name="name", type="xs:string"/>
ohair@286 284 * &lt;xs:element name="city", type="xs:string"/>
ohair@286 285 * &lt;xs:element name="city" type="xs:string"/>
ohair@286 286 * &lt;xs:element name="state" type="xs:string"/>
ohair@286 287 * &lt;xs:element name="zip" type="xs:decimal"/>
ohair@286 288 * &lt;/xs:complexType>
ohair@286 289 * ...
ohair@286 290 * &lt;/xs:sequence>
ohair@286 291 * &lt;/xs:complexType>
ohair@286 292 * </pre>
ohair@286 293 *
ohair@286 294 * <p> <b> Example 5: </b> Map a property to an attribute with
ohair@286 295 * anonymous type.
ohair@286 296 *
ohair@286 297 * <pre>
ohair@286 298 *
ohair@286 299 * //Example: Code fragment
ohair@286 300 * public class Item {
ohair@286 301 * public String name;
ohair@286 302 * &#64;XmlAttribute
ohair@286 303 * public USPrice price;
ohair@286 304 * }
ohair@286 305 *
ohair@286 306 * // map class to anonymous simple type.
ohair@286 307 * &#64;XmlType(name="")
ohair@286 308 * public class USPrice {
ohair@286 309 * &#64;XmlValue
ohair@286 310 * public java.math.BigDecimal price;
ohair@286 311 * }
ohair@286 312 *
ohair@286 313 * &lt;!-- Example: XML Schema fragment -->
ohair@286 314 * &lt;xs:complexType name="Item">
ohair@286 315 * &lt;xs:sequence>
ohair@286 316 * &lt;xs:element name="name" type="xs:string"/>
ohair@286 317 * &lt;xs:attribute name="price">
ohair@286 318 * &lt;xs:simpleType>
ohair@286 319 * &lt;xs:restriction base="xs:decimal"/>
ohair@286 320 * &lt;/xs:simpleType>
ohair@286 321 * &lt;/xs:attribute>
ohair@286 322 * &lt;/xs:sequence>
ohair@286 323 * &lt;/xs:complexType>
ohair@286 324 * </pre>
ohair@286 325 *
ohair@286 326 * <p> <b> Example 6: </b> Define a factoryClass and factoryMethod
ohair@286 327 *
ohair@286 328 * <pre>
ohair@286 329 * &#64;XmlType(name="USAddressType", factoryClass=USAddressFactory.class,
ohair@286 330 * factoryMethod="getUSAddress")
ohair@286 331 * public class USAddress {
ohair@286 332 *
ohair@286 333 * private String city;
ohair@286 334 * private String name;
ohair@286 335 * private String state;
ohair@286 336 * private String street;
ohair@286 337 * private int zip;
ohair@286 338 *
ohair@286 339 * public USAddress(String name, String street, String city,
ohair@286 340 * String state, int zip) {
ohair@286 341 * this.name = name;
ohair@286 342 * this.street = street;
ohair@286 343 * this.city = city;
ohair@286 344 * this.state = state;
ohair@286 345 * this.zip = zip;
ohair@286 346 * }
ohair@286 347 * }
ohair@286 348 *
ohair@286 349 * public class USAddressFactory {
ohair@286 350 * public static USAddress getUSAddress(){
ohair@286 351 * return new USAddress("Mark Baker", "23 Elm St",
ohair@286 352 * "Dayton", "OH", 90952);
ohair@286 353 * }
ohair@286 354 *
ohair@286 355 * </pre>
ohair@286 356 *
ohair@286 357 * <p> <b> Example 7: </b> Define factoryMethod and use the default factoryClass
ohair@286 358 *
ohair@286 359 * <pre>
ohair@286 360 * &#64;XmlType(name="USAddressType", factoryMethod="getNewInstance")
ohair@286 361 * public class USAddress {
ohair@286 362 *
ohair@286 363 * private String city;
ohair@286 364 * private String name;
ohair@286 365 * private String state;
ohair@286 366 * private String street;
ohair@286 367 * private int zip;
ohair@286 368 *
ohair@286 369 * private USAddress() {}
ohair@286 370 *
ohair@286 371 * public static USAddress getNewInstance(){
ohair@286 372 * return new USAddress();
ohair@286 373 * }
ohair@286 374 * }
ohair@286 375 * </pre>
ohair@286 376 *
ohair@286 377 * @author Sekhar Vajjhala, Sun Microsystems, Inc.
ohair@286 378 * @see XmlElement
ohair@286 379 * @see XmlAttribute
ohair@286 380 * @see XmlValue
ohair@286 381 * @see XmlSchema
ohair@286 382 * @since JAXB2.0
ohair@286 383 */
ohair@286 384
ohair@286 385 @Retention(RUNTIME) @Target({TYPE})
ohair@286 386 public @interface XmlType {
ohair@286 387 /**
ohair@286 388 * Name of the XML Schema type which the class is mapped.
ohair@286 389 */
ohair@286 390 String name() default "##default" ;
ohair@286 391
ohair@286 392 /**
ohair@286 393 * Specifies the order for XML Schema elements when class is
ohair@286 394 * mapped to a XML Schema complex type.
ohair@286 395 *
ohair@286 396 * <p> Refer to the table for how the propOrder affects the
ohair@286 397 * mapping of class </p>
ohair@286 398 *
ohair@286 399 * <p> The propOrder is a list of names of JavaBean properties in
ohair@286 400 * the class. Each name in the list is the name of a Java
ohair@286 401 * identifier of the JavaBean property. The order in which
ohair@286 402 * JavaBean properties are listed is the order of XML Schema
ohair@286 403 * elements to which the JavaBean properties are mapped. </p>
ohair@286 404 * <p> All of the JavaBean properties being mapped to XML Schema elements
ohair@286 405 * must be listed.
ohair@286 406 * <p> A JavaBean property or field listed in propOrder must not
ohair@286 407 * be transient or annotated with <tt>@XmlTransient</tt>.
ohair@286 408 * <p> The default ordering of JavaBean properties is determined
ohair@286 409 * by @{@link XmlAccessorOrder}.
ohair@286 410 */
ohair@286 411 String[] propOrder() default {""};
ohair@286 412
ohair@286 413 /**
ohair@286 414 * Name of the target namespace of the XML Schema type. By
ohair@286 415 * default, this is the target namespace to which the package
ohair@286 416 * containing the class is mapped.
ohair@286 417 */
ohair@286 418 String namespace() default "##default" ;
ohair@286 419
ohair@286 420 /**
ohair@286 421 * Class containing a no-arg factory method for creating an
ohair@286 422 * instance of this class. The default is this class.
ohair@286 423 *
ohair@286 424 * <p>If <tt>factoryClass</tt> is DEFAULT.class and
ohair@286 425 * <tt>factoryMethod</tt> is "", then there is no static factory
ohair@286 426 * method.
ohair@286 427 *
ohair@286 428 * <p>If <tt>factoryClass</tt> is DEFAULT.class and
ohair@286 429 * <tt>factoryMethod</tt> is not "", then
ohair@286 430 * <tt>factoryMethod</tt> is the name of a static factory method
ohair@286 431 * in this class.
ohair@286 432 *
ohair@286 433 * <p>If <tt>factoryClass</tt> is not DEFAULT.class, then
ohair@286 434 * <tt>factoryMethod</tt> must not be "" and must be the name of
ohair@286 435 * a static factory method specified in <tt>factoryClass</tt>.
ohair@286 436 */
ohair@286 437 Class factoryClass() default DEFAULT.class;
ohair@286 438
ohair@286 439 /**
ohair@286 440 * Used in {@link XmlType#factoryClass()} to
ohair@286 441 * signal that either factory mehod is not used or
ohair@286 442 * that it's in the class with this {@link XmlType} itself.
ohair@286 443 */
ohair@286 444 static final class DEFAULT {}
ohair@286 445
ohair@286 446 /**
ohair@286 447 * Name of a no-arg factory method in the class specified in
ohair@286 448 * <tt>factoryClass</tt> factoryClass().
ohair@286 449 *
ohair@286 450 */
ohair@286 451 String factoryMethod() default "";
ohair@286 452 }

mercurial