KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.annotation.Retention JavaDoc;
9 import java.lang.annotation.Target JavaDoc;
10
11 import static java.lang.annotation.ElementType JavaDoc.*;
12 import static java.lang.annotation.RetentionPolicy JavaDoc.*;
13
14 /**
15  * <p>
16  * Maps a JavaBean property to a XML attribute.
17  *
18  * <p> <b>Usage</b> </p>
19  * <p>
20  * The <tt>@XmlAttribute</tt> annotation can be used with the
21  * following program elements:
22  * <ul>
23  * <li> JavaBean property </li>
24  * <li> field </li>
25  * </ul>
26  *
27  * <p> A static final field is mapped to a XML fixed attribute.
28  *
29  * <p>See "Package Specification" in javax.xml.bind.package javadoc for
30  * additional common information.</p>
31  *
32  * The usage is subject to the following constraints:
33  * <ul>
34  * <li> If type of the field or the property is a collection
35  * type, then the collection item type must be mapped to schema
36  * simple type.
37  * <pre>
38  * // Examples
39  * &#64;XmlAttribute List&lt;Integer> items; //legal
40  * &#64;XmlAttribute List&lt;Bar> foo; // illegal if Bar does not map to a schema simple type
41  * </pre>
42  * </li>
43  * <li> If the type of the field or the property is a non
44  * collection type, then the type of the property or field
45  * must map to a simple schema type.
46  * <pre>
47  * // Examples
48  * &#64;XmlAttribute int foo; // legal
49  * &#64;XmlAttribute Foo foo; // illegal if Foo does not map to a schema simple type
50  * </pre>
51  * </li>
52  * <li> This annotation can be used with the following annotations:
53  * {@link XmlID},
54  * {@link XmlIDREF},
55  * {@link XmlList},
56  * {@link XmlSchemaType},
57  * {@link XmlValue},
58  * {@link XmlAttachmentRef},
59  * {@link XmlMimeType},
60  * {@link XmlInlineBinaryData},
61  * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}</li>.
62  * </ul>
63  * </p>
64  *
65  * <p> <b>Example 1: </b>Map a JavaBean property to an XML attribute.</p>
66  * <pre>
67  * //Example: Code fragment
68  * public class USPrice {
69  * &#64;XmlAttribute
70  * public java.math.BigDecimal getPrice() {...} ;
71  * public void setPrice(java.math.BigDecimal ) {...};
72  * }
73  *
74  * &lt;!-- Example: XML Schema fragment -->
75  * &lt;xs:complexType name="USPrice">
76  * &lt;xs:sequence>
77  * &lt;/xs:sequence>
78  * &lt;xs:attribute name="price" type="xs:decimal"/>
79  * &lt;/xs:complexType>
80  * </pre>
81  *
82  * <p> <b>Example 2: </b>Map a JavaBean property to an XML attribute with anonymous type.</p>
83  * See Example 7 in @{@link XmlType}.
84  *
85  * <p> <b>Example 3: </b>Map a JavaBean collection property to an XML attribute.</p>
86  * <pre>
87  * // Example: Code fragment
88  * class Foo {
89  * ...
90  * &#64;XmlAttribute List&lt;Integer> items;
91  * }
92  *
93  * &lt;!-- Example: XML Schema fragment -->
94  * &lt;xs:complexType name="foo">
95  * ...
96  * &lt;xs:attribute name="items">
97  * &lt;xs:simpleType>
98  * &lt;xs:list itemType="xs:int"/>
99  * &lt;/xs:simpleType>
100  * &lt;/xs:complexType>
101  *
102  * </pre>
103  * @author Sekhar Vajjhala, Sun Microsystems, Inc.
104  * @version $Revision: 1.13 $
105  * @see XmlType
106  * @since JAXB2.0
107  */

108
109 @Retention JavaDoc(RUNTIME) @Target JavaDoc({FIELD, METHOD})
110 public @interface XmlAttribute {
111     /**
112      * Name of the XML Schema attribute. By default, the XML Schema
113      * attribute name is derived from the JavaBean property name.
114      *
115      */

116     String JavaDoc name() default "##default";
117  
118     /**
119      * Specifies if the XML Schema attribute is optional or
120      * required. If true, then the JavaBean property is mapped to a
121      * XML Schema attribute that is required. Otherwise it is mapped
122      * to a XML Schema attribute that is optional.
123      *
124      */

125      boolean required() default false;
126
127     /**
128      * Specifies the XML target namespace of the XML Schema
129      * attribute.
130      *
131      */

132     String JavaDoc namespace() default "##default" ;
133 }
134
Popular Tags