KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > JAXBElement


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;
7
8 import javax.xml.namespace.QName JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * <p>JAXB representation of an Xml Element.</p>
13  *
14  * <p>This class represents information about an Xml Element from both the element
15  * declaration within a schema and the element instance value within an xml document
16  * with the following properties
17  * <ul>
18  * <li>element's xml tag <b><tt>name</tt></b></li>
19  * <li><b><tt>value</tt></b> represents the element instance's atttribute(s) and content model</li>
20  * <li>element declaration's <b><tt>declaredType</tt></b> (<tt>xs:element @type</tt> attribute)</li>
21  * <li><b><tt>scope</tt></b> of element declaration</li>
22  * <li>boolean <b><tt>nil</tt></b> property. (element instance's <tt><b>xsi:nil</b></tt> attribute)</li>
23  * </ul>
24  *
25  * <p>The <tt>declaredType</tt> and <tt>scope</tt> property are the
26  * JAXB class binding for the xml type definition.
27  * </p>
28  *
29  * <p><b><tt>Scope</tt></b> is either {@link GlobalScope} or the Java class representing the
30  * complex type definition containing the schema element declaration.
31  * </p>
32  *
33  * <p>There is a property constraint that if <b><tt>value</tt></b> is <tt>null</tt>,
34  * then <tt>nil</tt> must be <tt>true</tt>. The converse is not true to enable
35  * representing a nil element with attribute(s). If <tt>nil</tt> is true, it is possible
36  * that <tt>value</tt> is non-null so it can hold the value of the attributes
37  * associated with a nil element.
38  * </p>
39  *
40  * @author Kohsuke Kawaguchi, Joe Fialli
41  * @since JAXB 2.0
42  */

43
44 public class JAXBElement<T> implements Serializable JavaDoc {
45
46     /** xml element tag name */
47     final protected QName JavaDoc name;
48
49     /** Java datatype binding for xml element declaration's type. */
50     final protected Class JavaDoc<T> declaredType;
51
52     /** Scope of xml element declaration representing this xml element instance.
53      * Can be one of the following values:
54      * - {@link GlobalScope} for global xml element declaration.
55      * - local element declaration has a scope set to the Java class
56      * representation of complex type defintion containing
57      * xml element declaration.
58      */

59     final protected Class JavaDoc scope;
60
61     /** xml element value.
62         Represents content model and attributes of an xml element instance. */

63     protected T value;
64
65     /** true iff the xml element instance has xsi:nil="true". */
66     protected boolean nil = false;
67
68     /**
69      * Designates global scope for an xml element.
70      */

71     public static final class GlobalScope {}
72
73     /**
74      * <p>Construct an xml element instance.</p>
75      *
76      * @param name Java binding of xml element tag name
77      * @param declaredType Java binding of xml element declaration's type
78      * @param scope
79      * Java binding of scope of xml element declaration.
80      * Passing null is the same as passing <tt>GlobalScope.class</tt>
81      * @param value
82      * Java instance representing xml element's value.
83      * @see #getScope()
84      * @see #isTypeSubstituted()
85      */

86     public JAXBElement(QName JavaDoc name,
87                Class JavaDoc<T> declaredType,
88                Class JavaDoc scope,
89                T value) {
90         if(declaredType==null || name==null)
91             throw new IllegalArgumentException JavaDoc();
92         this.declaredType = declaredType;
93         if(scope==null) scope = GlobalScope.class;
94         this.scope = scope;
95         this.name = name;
96         setValue(value);
97     }
98
99     /**
100      * Construct an xml element instance.
101      *
102      * This is just a convenience method for <tt>new JAXBElement(name,declaredType,GlobalScope.class,value)</tt>
103      */

104     public JAXBElement(QName JavaDoc name, Class JavaDoc<T> declaredType, T value ) {
105         this(name,declaredType,GlobalScope.class,value);
106     }
107
108     /**
109      * Returns the Java binding of the xml element declaration's type attribute.
110      */

111     public Class JavaDoc<T> getDeclaredType() {
112         return declaredType;
113     }
114
115     /**
116      * Returns the xml element tag name.
117      */

118     public QName JavaDoc getName() {
119         return name;
120     }
121
122     /**
123      * <p>Set the content model and attributes of this xml element.</p>
124      *
125      * <p>When this property is set to <tt>null</tt>, <tt>isNil()</tt> must by <tt>true</tt>.
126      * Details of constraint are described at {@link #isNil()}.</pp>
127      *
128      * @see #isTypeSubstituted()
129      */

130     public void setValue(T t) {
131         this.value = t;
132     }
133
134     /**
135      * <p>Return the content model and attribute values for this element.</p>
136      *
137      * <p>See {@link #isNil()} for a description of a property constraint when
138      * this value is <tt>null</tt></p>
139      */

140     public T getValue() {
141         return value;
142     }
143
144     /**
145      * Returns scope of xml element declaration.
146      *
147      * @see #isGlobalScope()
148      * @return <tt>GlobalScope.class</tt> if this element is of global scope.
149      */

150     public Class JavaDoc getScope() {
151         return scope;
152     }
153     
154     /**
155      * <p>Returns <tt>true</tt> iff this element instance content model
156      * is nil.</p>
157      *
158      * <p>This property always returns <tt>true</tt> when {@link #getValue()} is null.
159      * Note that the converse is not true, when this property is <tt>true</tt>,
160      * {@link #getValue()} can contain a non-null value for attribute(s). It is
161      * valid for a nil xml element to have attribute(s).</p>
162      */

163     public boolean isNil() {
164         return (value == null) || nil;
165     }
166
167     /**
168      * <p>Set whether this element has nil content.</p>
169      *
170      * @see #isNil()
171      */

172     public void setNil(boolean value) {
173         this.nil = value;
174     }
175     
176     /* Convenience methods
177      * (Not necessary but they do unambiguously conceptualize
178      * the rationale behind this class' fields.)
179      */

180
181     /**
182      * Returns true iff this xml element declaration is global.
183      */

184     public boolean isGlobalScope() {
185         return this.scope == GlobalScope.class;
186     }
187
188     /**
189      * Returns true iff this xml element instance's value has a different
190      * type than xml element declaration's declared type.
191      */

192     public boolean isTypeSubstituted() {
193         if(value==null) return false;
194         return value.getClass() != declaredType;
195     }
196
197     private static final long serialVersionUID = 1L;
198 }
199
Popular Tags