325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.xml.bind;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/Aimport java.io.Serializable;
325N/A
325N/A/**
325N/A * <p>JAXB representation of an Xml Element.</p>
325N/A *
325N/A * <p>This class represents information about an Xml Element from both the element
325N/A * declaration within a schema and the element instance value within an xml document
325N/A * with the following properties
325N/A * <ul>
325N/A * <li>element's xml tag <b><tt>name</tt></b></li>
325N/A * <li><b><tt>value</tt></b> represents the element instance's atttribute(s) and content model</li>
325N/A * <li>element declaration's <b><tt>declaredType</tt></b> (<tt>xs:element @type</tt> attribute)</li>
325N/A * <li><b><tt>scope</tt></b> of element declaration</li>
325N/A * <li>boolean <b><tt>nil</tt></b> property. (element instance's <tt><b>xsi:nil</b></tt> attribute)</li>
325N/A * </ul>
325N/A *
325N/A * <p>The <tt>declaredType</tt> and <tt>scope</tt> property are the
325N/A * JAXB class binding for the xml type definition.
325N/A * </p>
325N/A *
325N/A * <p><b><tt>Scope</tt></b> is either {@link GlobalScope} or the Java class representing the
325N/A * complex type definition containing the schema element declaration.
325N/A * </p>
325N/A *
325N/A * <p>There is a property constraint that if <b><tt>value</tt></b> is <tt>null</tt>,
325N/A * then <tt>nil</tt> must be <tt>true</tt>. The converse is not true to enable
325N/A * representing a nil element with attribute(s). If <tt>nil</tt> is true, it is possible
325N/A * that <tt>value</tt> is non-null so it can hold the value of the attributes
325N/A * associated with a nil element.
325N/A * </p>
325N/A *
325N/A * @author Kohsuke Kawaguchi, Joe Fialli
325N/A * @since JAXB 2.0
325N/A */
325N/A
325N/Apublic class JAXBElement<T> implements Serializable {
325N/A
325N/A /** xml element tag name */
325N/A final protected QName name;
325N/A
325N/A /** Java datatype binding for xml element declaration's type. */
325N/A final protected Class<T> declaredType;
325N/A
325N/A /** Scope of xml element declaration representing this xml element instance.
325N/A * Can be one of the following values:
325N/A * - {@link GlobalScope} for global xml element declaration.
325N/A * - local element declaration has a scope set to the Java class
325N/A * representation of complex type defintion containing
325N/A * xml element declaration.
325N/A */
325N/A final protected Class scope;
325N/A
325N/A /** xml element value.
325N/A Represents content model and attributes of an xml element instance. */
325N/A protected T value;
325N/A
325N/A /** true iff the xml element instance has xsi:nil="true". */
325N/A protected boolean nil = false;
325N/A
325N/A /**
325N/A * Designates global scope for an xml element.
325N/A */
325N/A public static final class GlobalScope {}
325N/A
325N/A /**
325N/A * <p>Construct an xml element instance.</p>
325N/A *
325N/A * @param name Java binding of xml element tag name
325N/A * @param declaredType Java binding of xml element declaration's type
325N/A * @param scope
325N/A * Java binding of scope of xml element declaration.
325N/A * Passing null is the same as passing <tt>GlobalScope.class</tt>
325N/A * @param value
325N/A * Java instance representing xml element's value.
325N/A * @see #getScope()
325N/A * @see #isTypeSubstituted()
325N/A */
325N/A public JAXBElement(QName name,
325N/A Class<T> declaredType,
325N/A Class scope,
325N/A T value) {
325N/A if(declaredType==null || name==null)
325N/A throw new IllegalArgumentException();
325N/A this.declaredType = declaredType;
325N/A if(scope==null) scope = GlobalScope.class;
325N/A this.scope = scope;
325N/A this.name = name;
325N/A setValue(value);
325N/A }
325N/A
325N/A /**
325N/A * Construct an xml element instance.
325N/A *
325N/A * This is just a convenience method for <tt>new JAXBElement(name,declaredType,GlobalScope.class,value)</tt>
325N/A */
325N/A public JAXBElement(QName name, Class<T> declaredType, T value ) {
325N/A this(name,declaredType,GlobalScope.class,value);
325N/A }
325N/A
325N/A /**
325N/A * Returns the Java binding of the xml element declaration's type attribute.
325N/A */
325N/A public Class<T> getDeclaredType() {
325N/A return declaredType;
325N/A }
325N/A
325N/A /**
325N/A * Returns the xml element tag name.
325N/A */
325N/A public QName getName() {
325N/A return name;
325N/A }
325N/A
325N/A /**
325N/A * <p>Set the content model and attributes of this xml element.</p>
325N/A *
325N/A * <p>When this property is set to <tt>null</tt>, <tt>isNil()</tt> must by <tt>true</tt>.
325N/A * Details of constraint are described at {@link #isNil()}.</p>
325N/A *
325N/A * @see #isTypeSubstituted()
325N/A */
325N/A public void setValue(T t) {
325N/A this.value = t;
325N/A }
325N/A
325N/A /**
325N/A * <p>Return the content model and attribute values for this element.</p>
325N/A *
325N/A * <p>See {@link #isNil()} for a description of a property constraint when
325N/A * this value is <tt>null</tt></p>
325N/A */
325N/A public T getValue() {
325N/A return value;
325N/A }
325N/A
325N/A /**
325N/A * Returns scope of xml element declaration.
325N/A *
325N/A * @see #isGlobalScope()
325N/A * @return <tt>GlobalScope.class</tt> if this element is of global scope.
325N/A */
325N/A public Class getScope() {
325N/A return scope;
325N/A }
325N/A
325N/A /**
325N/A * <p>Returns <tt>true</tt> iff this element instance content model
325N/A * is nil.</p>
325N/A *
325N/A * <p>This property always returns <tt>true</tt> when {@link #getValue()} is null.
325N/A * Note that the converse is not true, when this property is <tt>true</tt>,
325N/A * {@link #getValue()} can contain a non-null value for attribute(s). It is
325N/A * valid for a nil xml element to have attribute(s).</p>
325N/A */
325N/A public boolean isNil() {
325N/A return (value == null) || nil;
325N/A }
325N/A
325N/A /**
325N/A * <p>Set whether this element has nil content.</p>
325N/A *
325N/A * @see #isNil()
325N/A */
325N/A public void setNil(boolean value) {
325N/A this.nil = value;
325N/A }
325N/A
325N/A /* Convenience methods
325N/A * (Not necessary but they do unambiguously conceptualize
325N/A * the rationale behind this class' fields.)
325N/A */
325N/A
325N/A /**
325N/A * Returns true iff this xml element declaration is global.
325N/A */
325N/A public boolean isGlobalScope() {
325N/A return this.scope == GlobalScope.class;
325N/A }
325N/A
325N/A /**
325N/A * Returns true iff this xml element instance's value has a different
325N/A * type than xml element declaration's declared type.
325N/A */
325N/A public boolean isTypeSubstituted() {
325N/A if(value==null) return false;
325N/A return value.getClass() != declaredType;
325N/A }
325N/A
325N/A private static final long serialVersionUID = 1L;
325N/A}