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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial