src/share/jaxws_classes/javax/xml/bind/JAXBElement.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
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;
27
28 import javax.xml.namespace.QName;
29 import java.io.Serializable;
30
31 /**
32 * <p>JAXB representation of an Xml Element.</p>
33 *
34 * <p>This class represents information about an Xml Element from both the element
35 * declaration within a schema and the element instance value within an xml document
36 * with the following properties
37 * <ul>
38 * <li>element's xml tag <b><tt>name</tt></b></li>
39 * <li><b><tt>value</tt></b> represents the element instance's atttribute(s) and content model</li>
40 * <li>element declaration's <b><tt>declaredType</tt></b> (<tt>xs:element @type</tt> attribute)</li>
41 * <li><b><tt>scope</tt></b> of element declaration</li>
42 * <li>boolean <b><tt>nil</tt></b> property. (element instance's <tt><b>xsi:nil</b></tt> attribute)</li>
43 * </ul>
44 *
45 * <p>The <tt>declaredType</tt> and <tt>scope</tt> property are the
46 * JAXB class binding for the xml type definition.
47 * </p>
48 *
49 * <p><b><tt>Scope</tt></b> is either {@link GlobalScope} or the Java class representing the
50 * complex type definition containing the schema element declaration.
51 * </p>
52 *
53 * <p>There is a property constraint that if <b><tt>value</tt></b> is <tt>null</tt>,
54 * then <tt>nil</tt> must be <tt>true</tt>. The converse is not true to enable
55 * representing a nil element with attribute(s). If <tt>nil</tt> is true, it is possible
56 * that <tt>value</tt> is non-null so it can hold the value of the attributes
57 * associated with a nil element.
58 * </p>
59 *
60 * @author Kohsuke Kawaguchi, Joe Fialli
61 * @since JAXB 2.0
62 */
63
64 public class JAXBElement<T> implements Serializable {
65
66 /** xml element tag name */
67 final protected QName name;
68
69 /** Java datatype binding for xml element declaration's type. */
70 final protected Class<T> declaredType;
71
72 /** Scope of xml element declaration representing this xml element instance.
73 * Can be one of the following values:
74 * - {@link GlobalScope} for global xml element declaration.
75 * - local element declaration has a scope set to the Java class
76 * representation of complex type defintion containing
77 * xml element declaration.
78 */
79 final protected Class scope;
80
81 /** xml element value.
82 Represents content model and attributes of an xml element instance. */
83 protected T value;
84
85 /** true iff the xml element instance has xsi:nil="true". */
86 protected boolean nil = false;
87
88 /**
89 * Designates global scope for an xml element.
90 */
91 public static final class GlobalScope {}
92
93 /**
94 * <p>Construct an xml element instance.</p>
95 *
96 * @param name Java binding of xml element tag name
97 * @param declaredType Java binding of xml element declaration's type
98 * @param scope
99 * Java binding of scope of xml element declaration.
100 * Passing null is the same as passing <tt>GlobalScope.class</tt>
101 * @param value
102 * Java instance representing xml element's value.
103 * @see #getScope()
104 * @see #isTypeSubstituted()
105 */
106 public JAXBElement(QName name,
107 Class<T> declaredType,
108 Class scope,
109 T value) {
110 if(declaredType==null || name==null)
111 throw new IllegalArgumentException();
112 this.declaredType = declaredType;
113 if(scope==null) scope = GlobalScope.class;
114 this.scope = scope;
115 this.name = name;
116 setValue(value);
117 }
118
119 /**
120 * Construct an xml element instance.
121 *
122 * This is just a convenience method for <tt>new JAXBElement(name,declaredType,GlobalScope.class,value)</tt>
123 */
124 public JAXBElement(QName name, Class<T> declaredType, T value ) {
125 this(name,declaredType,GlobalScope.class,value);
126 }
127
128 /**
129 * Returns the Java binding of the xml element declaration's type attribute.
130 */
131 public Class<T> getDeclaredType() {
132 return declaredType;
133 }
134
135 /**
136 * Returns the xml element tag name.
137 */
138 public QName getName() {
139 return name;
140 }
141
142 /**
143 * <p>Set the content model and attributes of this xml element.</p>
144 *
145 * <p>When this property is set to <tt>null</tt>, <tt>isNil()</tt> must by <tt>true</tt>.
146 * Details of constraint are described at {@link #isNil()}.</p>
147 *
148 * @see #isTypeSubstituted()
149 */
150 public void setValue(T t) {
151 this.value = t;
152 }
153
154 /**
155 * <p>Return the content model and attribute values for this element.</p>
156 *
157 * <p>See {@link #isNil()} for a description of a property constraint when
158 * this value is <tt>null</tt></p>
159 */
160 public T getValue() {
161 return value;
162 }
163
164 /**
165 * Returns scope of xml element declaration.
166 *
167 * @see #isGlobalScope()
168 * @return <tt>GlobalScope.class</tt> if this element is of global scope.
169 */
170 public Class getScope() {
171 return scope;
172 }
173
174 /**
175 * <p>Returns <tt>true</tt> iff this element instance content model
176 * is nil.</p>
177 *
178 * <p>This property always returns <tt>true</tt> when {@link #getValue()} is null.
179 * Note that the converse is not true, when this property is <tt>true</tt>,
180 * {@link #getValue()} can contain a non-null value for attribute(s). It is
181 * valid for a nil xml element to have attribute(s).</p>
182 */
183 public boolean isNil() {
184 return (value == null) || nil;
185 }
186
187 /**
188 * <p>Set whether this element has nil content.</p>
189 *
190 * @see #isNil()
191 */
192 public void setNil(boolean value) {
193 this.nil = value;
194 }
195
196 /* Convenience methods
197 * (Not necessary but they do unambiguously conceptualize
198 * the rationale behind this class' fields.)
199 */
200
201 /**
202 * Returns true iff this xml element declaration is global.
203 */
204 public boolean isGlobalScope() {
205 return this.scope == GlobalScope.class;
206 }
207
208 /**
209 * Returns true iff this xml element instance's value has a different
210 * type than xml element declaration's declared type.
211 */
212 public boolean isTypeSubstituted() {
213 if(value==null) return false;
214 return value.getClass() != declaredType;
215 }
216
217 private static final long serialVersionUID = 1L;
218 }

mercurial