Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/bind/JAXBElement.java
38890 views
/*1* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.bind;2627import javax.xml.namespace.QName;28import java.io.Serializable;2930/**31* <p>JAXB representation of an Xml Element.</p>32*33* <p>This class represents information about an Xml Element from both the element34* declaration within a schema and the element instance value within an xml document35* with the following properties36* <ul>37* <li>element's xml tag <b><tt>name</tt></b></li>38* <li><b><tt>value</tt></b> represents the element instance's atttribute(s) and content model</li>39* <li>element declaration's <b><tt>declaredType</tt></b> (<tt>xs:element @type</tt> attribute)</li>40* <li><b><tt>scope</tt></b> of element declaration</li>41* <li>boolean <b><tt>nil</tt></b> property. (element instance's <tt><b>xsi:nil</b></tt> attribute)</li>42* </ul>43*44* <p>The <tt>declaredType</tt> and <tt>scope</tt> property are the45* JAXB class binding for the xml type definition.46* </p>47*48* <p><b><tt>Scope</tt></b> is either {@link GlobalScope} or the Java class representing the49* complex type definition containing the schema element declaration.50* </p>51*52* <p>There is a property constraint that if <b><tt>value</tt></b> is <tt>null</tt>,53* then <tt>nil</tt> must be <tt>true</tt>. The converse is not true to enable54* representing a nil element with attribute(s). If <tt>nil</tt> is true, it is possible55* that <tt>value</tt> is non-null so it can hold the value of the attributes56* associated with a nil element.57* </p>58*59* @author Kohsuke Kawaguchi, Joe Fialli60* @since JAXB 2.061*/6263public class JAXBElement<T> implements Serializable {6465/** xml element tag name */66final protected QName name;6768/** Java datatype binding for xml element declaration's type. */69final protected Class<T> declaredType;7071/** Scope of xml element declaration representing this xml element instance.72* Can be one of the following values:73* - {@link GlobalScope} for global xml element declaration.74* - local element declaration has a scope set to the Java class75* representation of complex type defintion containing76* xml element declaration.77*/78final protected Class scope;7980/** xml element value.81Represents content model and attributes of an xml element instance. */82protected T value;8384/** true iff the xml element instance has xsi:nil="true". */85protected boolean nil = false;8687/**88* Designates global scope for an xml element.89*/90public static final class GlobalScope {}9192/**93* <p>Construct an xml element instance.</p>94*95* @param name Java binding of xml element tag name96* @param declaredType Java binding of xml element declaration's type97* @param scope98* Java binding of scope of xml element declaration.99* Passing null is the same as passing <tt>GlobalScope.class</tt>100* @param value101* Java instance representing xml element's value.102* @see #getScope()103* @see #isTypeSubstituted()104*/105public JAXBElement(QName name,106Class<T> declaredType,107Class scope,108T value) {109if(declaredType==null || name==null)110throw new IllegalArgumentException();111this.declaredType = declaredType;112if(scope==null) scope = GlobalScope.class;113this.scope = scope;114this.name = name;115setValue(value);116}117118/**119* Construct an xml element instance.120*121* This is just a convenience method for <tt>new JAXBElement(name,declaredType,GlobalScope.class,value)</tt>122*/123public JAXBElement(QName name, Class<T> declaredType, T value ) {124this(name,declaredType,GlobalScope.class,value);125}126127/**128* Returns the Java binding of the xml element declaration's type attribute.129*/130public Class<T> getDeclaredType() {131return declaredType;132}133134/**135* Returns the xml element tag name.136*/137public QName getName() {138return name;139}140141/**142* <p>Set the content model and attributes of this xml element.</p>143*144* <p>When this property is set to <tt>null</tt>, <tt>isNil()</tt> must by <tt>true</tt>.145* Details of constraint are described at {@link #isNil()}.</p>146*147* @see #isTypeSubstituted()148*/149public void setValue(T t) {150this.value = t;151}152153/**154* <p>Return the content model and attribute values for this element.</p>155*156* <p>See {@link #isNil()} for a description of a property constraint when157* this value is <tt>null</tt></p>158*/159public T getValue() {160return value;161}162163/**164* Returns scope of xml element declaration.165*166* @see #isGlobalScope()167* @return <tt>GlobalScope.class</tt> if this element is of global scope.168*/169public Class getScope() {170return scope;171}172173/**174* <p>Returns <tt>true</tt> iff this element instance content model175* is nil.</p>176*177* <p>This property always returns <tt>true</tt> when {@link #getValue()} is null.178* Note that the converse is not true, when this property is <tt>true</tt>,179* {@link #getValue()} can contain a non-null value for attribute(s). It is180* valid for a nil xml element to have attribute(s).</p>181*/182public boolean isNil() {183return (value == null) || nil;184}185186/**187* <p>Set whether this element has nil content.</p>188*189* @see #isNil()190*/191public void setNil(boolean value) {192this.nil = value;193}194195/* Convenience methods196* (Not necessary but they do unambiguously conceptualize197* the rationale behind this class' fields.)198*/199200/**201* Returns true iff this xml element declaration is global.202*/203public boolean isGlobalScope() {204return this.scope == GlobalScope.class;205}206207/**208* Returns true iff this xml element instance's value has a different209* type than xml element declaration's declared type.210*/211public boolean isTypeSubstituted() {212if(value==null) return false;213return value.getClass() != declaredType;214}215216private static final long serialVersionUID = 1L;217}218219220