1 /*
2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.xml.bind.annotation;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.Target;
30 
31 import static java.lang.annotation.RetentionPolicy.RUNTIME;
32 import static java.lang.annotation.ElementType.TYPE;
33 
34 /**
35  * Maps a class or an enum type to an XML element.
36  *
37  * <p> <b>Usage</b> </p>
38  * <p>
39  * The &#64;XmlRootElement annotation can be used with the following program
40  * elements:
41  * <ul>
42  *   <li> a top level class </li>
43  *   <li> an enum type </li>
44  * </ul>
45  *
46  * <p>See "Package Specification" in javax.xml.bind.package javadoc for
47  * additional common information.</p>
48  *
49  * <p>
50  * When a top level class or an enum type is annotated with the
51  * &#64;XmlRootElement annotation, then its value is represented
52  * as XML element in an XML document.
53  *
54  * <p> This annotation can be used with the following annotations:
55  * {@link XmlType}, {@link XmlEnum}, {@link XmlAccessorType},
56  * {@link XmlAccessorOrder}.
57  * <p>
58 
59  * <p>
60  * <b>Example 1: </b> Associate an element with XML Schema type
61  * <pre>
62  *     // Example: Code fragment
63  *     &#64;XmlRootElement
64  *     class Point {
65  *        int x;
66  *        int y;
67  *        Point(int _x,int _y) {x=_x;y=_y;}
68  *     }
69  * </pre>
70  *
71  * <pre>
72  *     //Example: Code fragment corresponding to XML output
73  *     marshal( new Point(3,5), System.out);
74  * </pre>
75  *
76  * <pre>
77  *     &lt;!-- Example: XML output -->
78  *     &lt;point>
79  *       &lt;x> 3 </x>
80  *       &lt;y> 5 </y>
81  *     &lt;/point>
82  * </pre>
83  *
84  * The annotation causes an global element declaration to be produced
85  * in the schema. The global element declaration is associated with
86  * the XML schema type to which the class is mapped.
87  *
88  * <pre>
89  *     &lt;!-- Example: XML schema definition -->
90  *     &lt;xs:element name="point" type="point"/>
91  *     &lt;xs:complexType name="point">
92  *       &lt;xs:sequence>
93  *         &lt;xs:element name="x" type="xs:int"/>
94  *         &lt;xs:element name="y" type="xs:int"/>
95  *       &lt;/xs:sequence>
96  *     &lt;/xs:complexType>
97  * </pre>
98  *
99  * <p>
100  *
101  * <b>Example 2: Orthogonality to type inheritance </b>
102  *
103  * <p>
104  * An element declaration annotated on a type is not inherited by its
105  * derived types. The following example shows this.
106  * <pre>
107  *     // Example: Code fragment
108  *     &#64;XmlRootElement
109  *     class Point3D extends Point {
110  *         int z;
111  *         Point3D(int _x,int _y,int _z) {super(_x,_y);z=_z;}
112  *     }
113  *
114  *     //Example: Code fragment corresponding to XML output *
115  *     marshal( new Point3D(3,5,0), System.out );
116  *
117  *     &lt;!-- Example: XML output -->
118  *     &lt;!-- The element name is point3D not point -->
119  *     &lt;point3D>
120  *       &lt;x>3&lt;/x>
121  *       &lt;y>5&lt;/y>
122  *       &lt;z>0&lt;/z>
123  *     &lt;/point3D>
124  *
125  *     &lt;!-- Example: XML schema definition -->
126  *     &lt;xs:element name="point3D" type="point3D"/>
127  *     &lt;xs:complexType name="point3D">
128  *       &lt;xs:complexContent>
129  *         &lt;xs:extension base="point">
130  *           &lt;xs:sequence>
131  *             &lt;xs:element name="z" type="xs:int"/>
132  *           &lt;/xs:sequence>
133  *         &lt;/xs:extension>
134  *       &lt;/xs:complexContent>
135  *     &lt;/xs:complexType>
136  * </pre>
137  *
138  * <b>Example 3: </b> Associate a global element with XML Schema type
139  * to which the class is mapped.
140  * <pre>
141  *     //Example: Code fragment
142  *     &#64;XmlRootElement(name="PriceElement")
143  *     public class USPrice {
144  *         &#64;XmlElement
145  *         public java.math.BigDecimal price;
146  *     }
147  *
148  *     &lt;!-- Example: XML schema definition -->
149  *     &lt;xs:element name="PriceElement" type="USPrice"/>
150  *     &lt;xs:complexType name="USPrice">
151  *       &lt;xs:sequence>
152  *         &lt;xs:element name="price" type="xs:decimal"/>
153  *       &lt;/sequence>
154  *     &lt;/xs:complexType>
155  * </pre>
156  *
157  * @author Sekhar Vajjhala, Sun Microsystems, Inc.
158  * @since JAXB2.0
159  */
160 @Retention(RUNTIME)
161 @Target({TYPE})
162 public @interface XmlRootElement {
163     /**
164      * namespace name of the XML element.
165      * <p>
166      * If the value is "##default", then the XML namespace name is derived
167      * from the package of the class ( {@link XmlSchema} ). If the
168      * package is unnamed, then the XML namespace is the default empty
169      * namespace.
170      */
namespace()171     String namespace() default "##default";
172 
173     /**
174      * local name of the XML element.
175      * <p>
176      * If the value is "##default", then the name is derived from the
177      * class name.
178      *
179      */
name()180     String name() default "##default";
181 
182 }
183