src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/property/SingleElementLeafProperty.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2010, 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 com.sun.xml.internal.bind.v2.runtime.property;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.lang.reflect.Modifier;
aoqi@0 30
aoqi@0 31 import javax.xml.bind.JAXBElement;
aoqi@0 32 import javax.xml.stream.XMLStreamException;
aoqi@0 33
aoqi@0 34 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 35 import com.sun.xml.internal.bind.v2.model.core.ID;
aoqi@0 36 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 37 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
aoqi@0 38 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
aoqi@0 39 import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl;
aoqi@0 40 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 41 import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0 42 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
aoqi@0 45 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
aoqi@0 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator;
aoqi@0 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyLoader;
aoqi@0 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyXsiLoader;
aoqi@0 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
aoqi@0 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
aoqi@0 51 import com.sun.xml.internal.bind.v2.util.QNameMap;
aoqi@0 52
aoqi@0 53 import org.xml.sax.SAXException;
aoqi@0 54
aoqi@0 55 /**
aoqi@0 56 * {@link Property} that contains a leaf value.
aoqi@0 57 *
aoqi@0 58 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
aoqi@0 59 */
aoqi@0 60 final class SingleElementLeafProperty<BeanT> extends PropertyImpl<BeanT> {
aoqi@0 61
aoqi@0 62 private final Name tagName;
aoqi@0 63 private final boolean nillable;
aoqi@0 64 private final Accessor acc;
aoqi@0 65 private final String defaultValue;
aoqi@0 66 private final TransducedAccessor<BeanT> xacc;
aoqi@0 67 private final boolean improvedXsiTypeHandling;
aoqi@0 68 private final boolean idRef;
aoqi@0 69
aoqi@0 70 public SingleElementLeafProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) {
aoqi@0 71 super(context, prop);
aoqi@0 72 RuntimeTypeRef ref = prop.getTypes().get(0);
aoqi@0 73 tagName = context.nameBuilder.createElementName(ref.getTagName());
aoqi@0 74 assert tagName != null;
aoqi@0 75 nillable = ref.isNillable();
aoqi@0 76 defaultValue = ref.getDefaultValue();
aoqi@0 77 this.acc = prop.getAccessor().optimize(context);
aoqi@0 78
aoqi@0 79 xacc = TransducedAccessor.get(context, ref);
aoqi@0 80 assert xacc != null;
aoqi@0 81
aoqi@0 82 improvedXsiTypeHandling = context.improvedXsiTypeHandling;
aoqi@0 83 idRef = ref.getSource().id() == ID.IDREF;
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 public void reset(BeanT o) throws AccessorException {
aoqi@0 87 acc.set(o, null);
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 public String getIdValue(BeanT bean) throws AccessorException, SAXException {
aoqi@0 91 return xacc.print(bean).toString();
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 @Override
aoqi@0 95 public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 96 boolean hasValue = xacc.hasValue(o);
aoqi@0 97
aoqi@0 98 Object obj = null;
aoqi@0 99
aoqi@0 100 try {
aoqi@0 101 obj = acc.getUnadapted(o);
aoqi@0 102 } catch (AccessorException ae) {
aoqi@0 103 ; // noop
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 Class valueType = acc.getValueType();
aoqi@0 107
aoqi@0 108 // check for different type than expected. If found, add xsi:type declaration
aoqi@0 109 if (xsiTypeNeeded(o, w, obj, valueType)) {
aoqi@0 110 w.startElement(tagName, outerPeer);
aoqi@0 111 w.childAsXsiType(obj, fieldName, w.grammar.getBeanInfo(valueType), false);
aoqi@0 112 w.endElement();
aoqi@0 113 } else { // current type is expected
aoqi@0 114 if (hasValue) {
aoqi@0 115 xacc.writeLeafElement(w, tagName, o, fieldName);
aoqi@0 116 } else if (nillable) {
aoqi@0 117 w.startElement(tagName, null);
aoqi@0 118 w.writeXsiNilTrue();
aoqi@0 119 w.endElement();
aoqi@0 120 }
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 /**
aoqi@0 125 * Checks if xsi type needed to be specified
aoqi@0 126 */
aoqi@0 127 private boolean xsiTypeNeeded(BeanT bean, XMLSerializer w, Object value, Class valueTypeClass) {
aoqi@0 128 if (!improvedXsiTypeHandling) // improved xsi type set
aoqi@0 129 return false;
aoqi@0 130 if (acc.isAdapted()) // accessor is not adapted
aoqi@0 131 return false;
aoqi@0 132 if (value == null) // value is not null
aoqi@0 133 return false;
aoqi@0 134 if (value.getClass().equals(valueTypeClass)) // value represented by different class
aoqi@0 135 return false;
aoqi@0 136 if (idRef) // IDREF
aoqi@0 137 return false;
aoqi@0 138 if (valueTypeClass.isPrimitive()) // is not primitive
aoqi@0 139 return false;
aoqi@0 140 return acc.isValueTypeAbstractable() || isNillableAbstract(bean, w.grammar, value, valueTypeClass);
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 /**
aoqi@0 144 * Checks if element is nillable and represented by abstract class.
aoqi@0 145 */
aoqi@0 146 private boolean isNillableAbstract(BeanT bean, JAXBContextImpl context, Object value, Class valueTypeClass) {
aoqi@0 147 if (!nillable) // check if element is nillable
aoqi@0 148 return false;
aoqi@0 149 if (valueTypeClass != Object.class) // required type wasn't recognized
aoqi@0 150 return false;
aoqi@0 151 if (bean.getClass() != JAXBElement.class) // is JAXBElement
aoqi@0 152 return false;
aoqi@0 153 JAXBElement jaxbElement = (JAXBElement) bean;
aoqi@0 154 Class valueClass = value.getClass();
aoqi@0 155 Class declaredTypeClass = jaxbElement.getDeclaredType();
aoqi@0 156 if (declaredTypeClass.equals(valueClass)) // JAXBElement<class> is different from unadapted class)
aoqi@0 157 return false;
aoqi@0 158 if (!declaredTypeClass.isAssignableFrom(valueClass)) // and is subclass from it
aoqi@0 159 return false;
aoqi@0 160 if (!Modifier.isAbstract(declaredTypeClass.getModifiers())) // declared class is abstract
aoqi@0 161 return false;
aoqi@0 162 return acc.isAbstractable(declaredTypeClass); // and is not builtin type
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
aoqi@0 166 Loader l = new LeafPropertyLoader(xacc);
aoqi@0 167 if (defaultValue != null)
aoqi@0 168 l = new DefaultValueLoaderDecorator(l, defaultValue);
aoqi@0 169 if (nillable || chain.context.allNillable)
aoqi@0 170 l = new XsiNilLoader.Single(l, acc);
aoqi@0 171
aoqi@0 172 // LeafPropertyXsiLoader doesn't work well with nillable elements
aoqi@0 173 if (improvedXsiTypeHandling)
aoqi@0 174 l = new LeafPropertyXsiLoader(l, xacc, acc);
aoqi@0 175
aoqi@0 176 handlers.put(tagName, new ChildLoader(l, null));
aoqi@0 177 }
aoqi@0 178
aoqi@0 179
aoqi@0 180 public PropertyKind getKind() {
aoqi@0 181 return PropertyKind.ELEMENT;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 @Override
aoqi@0 185 public Accessor getElementPropertyAccessor(String nsUri, String localName) {
aoqi@0 186 if (tagName.equals(nsUri, localName))
aoqi@0 187 return acc;
aoqi@0 188 else
aoqi@0 189 return null;
aoqi@0 190 }
aoqi@0 191 }

mercurial