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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, 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 import java.lang.reflect.Type;
aoqi@0 31 import java.util.HashMap;
aoqi@0 32 import java.util.Map;
aoqi@0 33
aoqi@0 34 import javax.xml.namespace.QName;
aoqi@0 35 import javax.xml.stream.XMLStreamException;
aoqi@0 36
aoqi@0 37 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 38 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 39 import com.sun.xml.internal.bind.v2.model.core.TypeRef;
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
aoqi@0 41 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
aoqi@0 42 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
aoqi@0 45 import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0 46 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0 47 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
aoqi@0 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
aoqi@0 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator;
aoqi@0 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
aoqi@0 51 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
aoqi@0 52 import com.sun.xml.internal.bind.v2.util.QNameMap;
aoqi@0 53
aoqi@0 54 import javax.xml.bind.JAXBElement;
aoqi@0 55 import org.xml.sax.SAXException;
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
aoqi@0 59 */
aoqi@0 60 final class SingleElementNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {
aoqi@0 61
aoqi@0 62 private final Accessor<BeanT,ValueT> acc;
aoqi@0 63
aoqi@0 64 private final boolean nillable;
aoqi@0 65
aoqi@0 66 private final QName[] acceptedElements;
aoqi@0 67
aoqi@0 68 private final Map<Class,TagAndType> typeNames = new HashMap<Class,TagAndType>();
aoqi@0 69
aoqi@0 70 private RuntimeElementPropertyInfo prop;
aoqi@0 71
aoqi@0 72 /**
aoqi@0 73 * The tag name used to produce xsi:nil. The first one in the list.
aoqi@0 74 */
aoqi@0 75 private final Name nullTagName;
aoqi@0 76
aoqi@0 77 public SingleElementNodeProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) {
aoqi@0 78 super(context,prop);
aoqi@0 79 acc = prop.getAccessor().optimize(context);
aoqi@0 80 this.prop = prop;
aoqi@0 81
aoqi@0 82 QName nt = null;
aoqi@0 83 boolean nil = false;
aoqi@0 84
aoqi@0 85 acceptedElements = new QName[prop.getTypes().size()];
aoqi@0 86 for( int i=0; i<acceptedElements.length; i++ )
aoqi@0 87 acceptedElements[i] = prop.getTypes().get(i).getTagName();
aoqi@0 88
aoqi@0 89 for (RuntimeTypeRef e : prop.getTypes()) {
aoqi@0 90 JaxBeanInfo beanInfo = context.getOrCreate(e.getTarget());
aoqi@0 91 if(nt==null) nt = e.getTagName();
aoqi@0 92 typeNames.put( beanInfo.jaxbType, new TagAndType(
aoqi@0 93 context.nameBuilder.createElementName(e.getTagName()),beanInfo) );
aoqi@0 94 nil |= e.isNillable();
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 nullTagName = context.nameBuilder.createElementName(nt);
aoqi@0 98
aoqi@0 99 nillable = nil;
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 @Override
aoqi@0 103 public void wrapUp() {
aoqi@0 104 super.wrapUp();
aoqi@0 105 prop = null;
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public void reset(BeanT bean) throws AccessorException {
aoqi@0 109 acc.set(bean,null);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public String getIdValue(BeanT beanT) {
aoqi@0 113 return null;
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 @Override
aoqi@0 117 public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 118 ValueT v = acc.get(o);
aoqi@0 119 if (v!=null) {
aoqi@0 120 Class vtype = v.getClass();
aoqi@0 121 TagAndType tt=typeNames.get(vtype); // quick way that usually works
aoqi@0 122
aoqi@0 123 if(tt==null) {// slow way that always works
aoqi@0 124 for (Map.Entry<Class,TagAndType> e : typeNames.entrySet()) {
aoqi@0 125 if(e.getKey().isAssignableFrom(vtype)) {
aoqi@0 126 tt = e.getValue();
aoqi@0 127 break;
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 boolean addNilDecl = (o instanceof JAXBElement) && ((JAXBElement)o).isNil();
aoqi@0 133 if(tt==null) {
aoqi@0 134 // actually this is an error, because the actual type was not a sub-type
aoqi@0 135 // of any of the types specified in the annotations,
aoqi@0 136 // but for the purpose of experimenting with simple type substitution,
aoqi@0 137 // it's convenient to marshal this anyway (for example so that classes
aoqi@0 138 // generated from simple types like String can be marshalled as expected.)
aoqi@0 139 w.startElement(typeNames.values().iterator().next().tagName,null);
aoqi@0 140 w.childAsXsiType(v,fieldName,w.grammar.getBeanInfo(Object.class), addNilDecl && nillable);
aoqi@0 141 } else {
aoqi@0 142 w.startElement(tt.tagName,null);
aoqi@0 143 w.childAsXsiType(v,fieldName,tt.beanInfo, addNilDecl && nillable);
aoqi@0 144 }
aoqi@0 145 w.endElement();
aoqi@0 146 } else if (nillable) {
aoqi@0 147 w.startElement(nullTagName,null);
aoqi@0 148 w.writeXsiNilTrue();
aoqi@0 149 w.endElement();
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
aoqi@0 154 JAXBContextImpl context = chain.context;
aoqi@0 155
aoqi@0 156 for (TypeRef<Type,Class> e : prop.getTypes()) {
aoqi@0 157 JaxBeanInfo bi = context.getOrCreate((RuntimeTypeInfo) e.getTarget());
aoqi@0 158 // if the expected Java type is already final, type substitution won't really work anyway.
aoqi@0 159 // this also traps cases like trying to substitute xsd:long element with xsi:type='xsd:int'
aoqi@0 160 Loader l = bi.getLoader(context,!Modifier.isFinal(bi.jaxbType.getModifiers()));
aoqi@0 161 if(e.getDefaultValue()!=null)
aoqi@0 162 l = new DefaultValueLoaderDecorator(l,e.getDefaultValue());
aoqi@0 163 if(nillable || chain.context.allNillable)
aoqi@0 164 l = new XsiNilLoader.Single(l,acc);
aoqi@0 165 handlers.put( e.getTagName(), new ChildLoader(l,acc));
aoqi@0 166 }
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 public PropertyKind getKind() {
aoqi@0 170 return PropertyKind.ELEMENT;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 @Override
aoqi@0 174 public Accessor getElementPropertyAccessor(String nsUri, String localName) {
aoqi@0 175 for( QName n : acceptedElements) {
aoqi@0 176 if(n.getNamespaceURI().equals(nsUri) && n.getLocalPart().equals(localName))
aoqi@0 177 return acc;
aoqi@0 178 }
aoqi@0 179 return null;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 }

mercurial