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

Tue, 07 Nov 2017 18:54:04 -0800

author
asaha
date
Tue, 07 Nov 2017 18:54:04 -0800
changeset 1528
f453f4eaf8b4
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
permissions
-rw-r--r--

Added tag jdk8u162-b06 for changeset 6095742f8034

ohair@286 1 /*
mkos@397 2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind;
ohair@286 27
ohair@286 28 import javax.xml.namespace.QName;
ohair@286 29 import java.io.Serializable;
ohair@286 30
ohair@286 31 /**
ohair@286 32 * <p>JAXB representation of an Xml Element.</p>
ohair@286 33 *
ohair@286 34 * <p>This class represents information about an Xml Element from both the element
ohair@286 35 * declaration within a schema and the element instance value within an xml document
ohair@286 36 * with the following properties
ohair@286 37 * <ul>
ohair@286 38 * <li>element's xml tag <b><tt>name</tt></b></li>
ohair@286 39 * <li><b><tt>value</tt></b> represents the element instance's atttribute(s) and content model</li>
ohair@286 40 * <li>element declaration's <b><tt>declaredType</tt></b> (<tt>xs:element @type</tt> attribute)</li>
ohair@286 41 * <li><b><tt>scope</tt></b> of element declaration</li>
ohair@286 42 * <li>boolean <b><tt>nil</tt></b> property. (element instance's <tt><b>xsi:nil</b></tt> attribute)</li>
ohair@286 43 * </ul>
ohair@286 44 *
ohair@286 45 * <p>The <tt>declaredType</tt> and <tt>scope</tt> property are the
ohair@286 46 * JAXB class binding for the xml type definition.
ohair@286 47 * </p>
ohair@286 48 *
ohair@286 49 * <p><b><tt>Scope</tt></b> is either {@link GlobalScope} or the Java class representing the
ohair@286 50 * complex type definition containing the schema element declaration.
ohair@286 51 * </p>
ohair@286 52 *
ohair@286 53 * <p>There is a property constraint that if <b><tt>value</tt></b> is <tt>null</tt>,
ohair@286 54 * then <tt>nil</tt> must be <tt>true</tt>. The converse is not true to enable
ohair@286 55 * representing a nil element with attribute(s). If <tt>nil</tt> is true, it is possible
ohair@286 56 * that <tt>value</tt> is non-null so it can hold the value of the attributes
ohair@286 57 * associated with a nil element.
ohair@286 58 * </p>
ohair@286 59 *
ohair@286 60 * @author Kohsuke Kawaguchi, Joe Fialli
ohair@286 61 * @since JAXB 2.0
ohair@286 62 */
ohair@286 63
ohair@286 64 public class JAXBElement<T> implements Serializable {
ohair@286 65
ohair@286 66 /** xml element tag name */
ohair@286 67 final protected QName name;
ohair@286 68
ohair@286 69 /** Java datatype binding for xml element declaration's type. */
ohair@286 70 final protected Class<T> declaredType;
ohair@286 71
ohair@286 72 /** Scope of xml element declaration representing this xml element instance.
ohair@286 73 * Can be one of the following values:
ohair@286 74 * - {@link GlobalScope} for global xml element declaration.
ohair@286 75 * - local element declaration has a scope set to the Java class
ohair@286 76 * representation of complex type defintion containing
ohair@286 77 * xml element declaration.
ohair@286 78 */
ohair@286 79 final protected Class scope;
ohair@286 80
ohair@286 81 /** xml element value.
ohair@286 82 Represents content model and attributes of an xml element instance. */
ohair@286 83 protected T value;
ohair@286 84
ohair@286 85 /** true iff the xml element instance has xsi:nil="true". */
ohair@286 86 protected boolean nil = false;
ohair@286 87
ohair@286 88 /**
ohair@286 89 * Designates global scope for an xml element.
ohair@286 90 */
ohair@286 91 public static final class GlobalScope {}
ohair@286 92
ohair@286 93 /**
ohair@286 94 * <p>Construct an xml element instance.</p>
ohair@286 95 *
ohair@286 96 * @param name Java binding of xml element tag name
ohair@286 97 * @param declaredType Java binding of xml element declaration's type
ohair@286 98 * @param scope
ohair@286 99 * Java binding of scope of xml element declaration.
ohair@286 100 * Passing null is the same as passing <tt>GlobalScope.class</tt>
ohair@286 101 * @param value
ohair@286 102 * Java instance representing xml element's value.
ohair@286 103 * @see #getScope()
ohair@286 104 * @see #isTypeSubstituted()
ohair@286 105 */
ohair@286 106 public JAXBElement(QName name,
ohair@286 107 Class<T> declaredType,
ohair@286 108 Class scope,
ohair@286 109 T value) {
ohair@286 110 if(declaredType==null || name==null)
ohair@286 111 throw new IllegalArgumentException();
ohair@286 112 this.declaredType = declaredType;
ohair@286 113 if(scope==null) scope = GlobalScope.class;
ohair@286 114 this.scope = scope;
ohair@286 115 this.name = name;
ohair@286 116 setValue(value);
ohair@286 117 }
ohair@286 118
ohair@286 119 /**
ohair@286 120 * Construct an xml element instance.
ohair@286 121 *
ohair@286 122 * This is just a convenience method for <tt>new JAXBElement(name,declaredType,GlobalScope.class,value)</tt>
ohair@286 123 */
ohair@286 124 public JAXBElement(QName name, Class<T> declaredType, T value ) {
ohair@286 125 this(name,declaredType,GlobalScope.class,value);
ohair@286 126 }
ohair@286 127
ohair@286 128 /**
ohair@286 129 * Returns the Java binding of the xml element declaration's type attribute.
ohair@286 130 */
ohair@286 131 public Class<T> getDeclaredType() {
ohair@286 132 return declaredType;
ohair@286 133 }
ohair@286 134
ohair@286 135 /**
ohair@286 136 * Returns the xml element tag name.
ohair@286 137 */
ohair@286 138 public QName getName() {
ohair@286 139 return name;
ohair@286 140 }
ohair@286 141
ohair@286 142 /**
ohair@286 143 * <p>Set the content model and attributes of this xml element.</p>
ohair@286 144 *
ohair@286 145 * <p>When this property is set to <tt>null</tt>, <tt>isNil()</tt> must by <tt>true</tt>.
ohair@286 146 * Details of constraint are described at {@link #isNil()}.</p>
ohair@286 147 *
ohair@286 148 * @see #isTypeSubstituted()
ohair@286 149 */
ohair@286 150 public void setValue(T t) {
ohair@286 151 this.value = t;
ohair@286 152 }
ohair@286 153
ohair@286 154 /**
ohair@286 155 * <p>Return the content model and attribute values for this element.</p>
ohair@286 156 *
ohair@286 157 * <p>See {@link #isNil()} for a description of a property constraint when
ohair@286 158 * this value is <tt>null</tt></p>
ohair@286 159 */
ohair@286 160 public T getValue() {
ohair@286 161 return value;
ohair@286 162 }
ohair@286 163
ohair@286 164 /**
ohair@286 165 * Returns scope of xml element declaration.
ohair@286 166 *
ohair@286 167 * @see #isGlobalScope()
ohair@286 168 * @return <tt>GlobalScope.class</tt> if this element is of global scope.
ohair@286 169 */
ohair@286 170 public Class getScope() {
ohair@286 171 return scope;
ohair@286 172 }
ohair@286 173
ohair@286 174 /**
ohair@286 175 * <p>Returns <tt>true</tt> iff this element instance content model
ohair@286 176 * is nil.</p>
ohair@286 177 *
ohair@286 178 * <p>This property always returns <tt>true</tt> when {@link #getValue()} is null.
ohair@286 179 * Note that the converse is not true, when this property is <tt>true</tt>,
ohair@286 180 * {@link #getValue()} can contain a non-null value for attribute(s). It is
ohair@286 181 * valid for a nil xml element to have attribute(s).</p>
ohair@286 182 */
ohair@286 183 public boolean isNil() {
ohair@286 184 return (value == null) || nil;
ohair@286 185 }
ohair@286 186
ohair@286 187 /**
ohair@286 188 * <p>Set whether this element has nil content.</p>
ohair@286 189 *
ohair@286 190 * @see #isNil()
ohair@286 191 */
ohair@286 192 public void setNil(boolean value) {
ohair@286 193 this.nil = value;
ohair@286 194 }
ohair@286 195
ohair@286 196 /* Convenience methods
ohair@286 197 * (Not necessary but they do unambiguously conceptualize
ohair@286 198 * the rationale behind this class' fields.)
ohair@286 199 */
ohair@286 200
ohair@286 201 /**
ohair@286 202 * Returns true iff this xml element declaration is global.
ohair@286 203 */
ohair@286 204 public boolean isGlobalScope() {
ohair@286 205 return this.scope == GlobalScope.class;
ohair@286 206 }
ohair@286 207
ohair@286 208 /**
ohair@286 209 * Returns true iff this xml element instance's value has a different
ohair@286 210 * type than xml element declaration's declared type.
ohair@286 211 */
ohair@286 212 public boolean isTypeSubstituted() {
ohair@286 213 if(value==null) return false;
ohair@286 214 return value.getClass() != declaredType;
ohair@286 215 }
ohair@286 216
ohair@286 217 private static final long serialVersionUID = 1L;
ohair@286 218 }

mercurial