1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/ElementBeanInfoImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,351 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.lang.reflect.Constructor; 1.33 +import java.lang.reflect.InvocationTargetException; 1.34 + 1.35 +import javax.xml.bind.JAXBElement; 1.36 +import javax.xml.bind.JAXBException; 1.37 +import javax.xml.namespace.QName; 1.38 +import javax.xml.stream.XMLStreamException; 1.39 + 1.40 +import com.sun.xml.internal.bind.api.AccessorException; 1.41 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 1.42 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.43 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo; 1.44 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; 1.45 +import com.sun.xml.internal.bind.v2.runtime.property.Property; 1.46 +import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory; 1.47 +import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain; 1.48 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.49 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; 1.50 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder; 1.51 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter; 1.52 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.53 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; 1.54 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.55 +import com.sun.xml.internal.bind.v2.util.QNameMap; 1.56 + 1.57 +import org.xml.sax.SAXException; 1.58 + 1.59 +/** 1.60 + * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}. 1.61 + * 1.62 + * @author Kohsuke Kawaguchi 1.63 + */ 1.64 +public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> { 1.65 + 1.66 + private Loader loader; 1.67 + 1.68 + private final Property property; 1.69 + 1.70 + // used to create new instances of JAXBElement. 1.71 + private final QName tagName; 1.72 + public final Class expectedType; 1.73 + private final Class scope; 1.74 + 1.75 + /** 1.76 + * If non-null, use this to create an instance. 1.77 + * It takes one value. 1.78 + */ 1.79 + private final Constructor<? extends JAXBElement> constructor; 1.80 + 1.81 + ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) { 1.82 + super(grammar,rei,(Class<JAXBElement>)rei.getType(),true,false,true); 1.83 + 1.84 + this.property = PropertyFactory.create(grammar,rei.getProperty()); 1.85 + 1.86 + tagName = rei.getElementName(); 1.87 + expectedType = Navigator.REFLECTION.erasure(rei.getContentInMemoryType()); 1.88 + scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz(); 1.89 + 1.90 + Class type = Navigator.REFLECTION.erasure(rei.getType()); 1.91 + if(type==JAXBElement.class) 1.92 + constructor = null; 1.93 + else { 1.94 + try { 1.95 + constructor = type.getConstructor(expectedType); 1.96 + } catch (NoSuchMethodException e) { 1.97 + NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType); 1.98 + x.initCause(e); 1.99 + throw x; 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + /** 1.105 + * The constructor for the sole instanceof {@link JaxBeanInfo} for 1.106 + * handling user-created {@link JAXBElement}. 1.107 + * 1.108 + * Such {@link JaxBeanInfo} is used only for marshalling. 1.109 + * 1.110 + * This is a hack. 1.111 + */ 1.112 + protected ElementBeanInfoImpl(final JAXBContextImpl grammar) { 1.113 + super(grammar,null,JAXBElement.class,true,false,true); 1.114 + tagName = null; 1.115 + expectedType = null; 1.116 + scope = null; 1.117 + constructor = null; 1.118 + 1.119 + this.property = new Property<JAXBElement>() { 1.120 + public void reset(JAXBElement o) { 1.121 + throw new UnsupportedOperationException(); 1.122 + } 1.123 + 1.124 + public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException { 1.125 + Class scope = e.getScope(); 1.126 + if(e.isGlobalScope()) scope = null; 1.127 + QName n = e.getName(); 1.128 + ElementBeanInfoImpl bi = grammar.getElement(scope,n); 1.129 + if(bi==null) { 1.130 + // infer what to do from the type 1.131 + JaxBeanInfo tbi; 1.132 + try { 1.133 + tbi = grammar.getBeanInfo(e.getDeclaredType(),true); 1.134 + } catch (JAXBException x) { 1.135 + // if e.getDeclaredType() isn't known to this JAXBContext 1.136 + target.reportError(null,x); 1.137 + return; 1.138 + } 1.139 + Object value = e.getValue(); 1.140 + target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null); 1.141 + if(value==null) { 1.142 + target.writeXsiNilTrue(); 1.143 + } else { 1.144 + target.childAsXsiType(value,"value",tbi, false); 1.145 + } 1.146 + target.endElement(); 1.147 + } else { 1.148 + try { 1.149 + bi.property.serializeBody(e,target,e); 1.150 + } catch (AccessorException x) { 1.151 + target.reportError(null,x); 1.152 + } 1.153 + } 1.154 + } 1.155 + 1.156 + public void serializeURIs(JAXBElement o, XMLSerializer target) { 1.157 + } 1.158 + 1.159 + public boolean hasSerializeURIAction() { 1.160 + return false; 1.161 + } 1.162 + 1.163 + public String getIdValue(JAXBElement o) { 1.164 + return null; 1.165 + } 1.166 + 1.167 + public PropertyKind getKind() { 1.168 + return PropertyKind.ELEMENT; 1.169 + } 1.170 + 1.171 + public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { 1.172 + } 1.173 + 1.174 + public Accessor getElementPropertyAccessor(String nsUri, String localName) { 1.175 + throw new UnsupportedOperationException(); 1.176 + } 1.177 + 1.178 + public void wrapUp() { 1.179 + } 1.180 + 1.181 + public RuntimePropertyInfo getInfo() { 1.182 + return property.getInfo(); 1.183 + } 1.184 + 1.185 + public boolean isHiddenByOverride() { 1.186 + return false; 1.187 + } 1.188 + 1.189 + public void setHiddenByOverride(boolean hidden) { 1.190 + throw new UnsupportedOperationException("Not supported on jaxbelements."); 1.191 + } 1.192 + 1.193 + public String getFieldName() { 1.194 + return null; 1.195 + } 1.196 + 1.197 + }; 1.198 + } 1.199 + 1.200 + /** 1.201 + * Use the previous {@link UnmarshallingContext.State}'s target to store 1.202 + * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader} 1.203 + * to correctly find the parent object. 1.204 + * This is a hack. 1.205 + */ 1.206 + private final class IntercepterLoader extends Loader implements Intercepter { 1.207 + private final Loader core; 1.208 + 1.209 + public IntercepterLoader(Loader core) { 1.210 + this.core = core; 1.211 + } 1.212 + 1.213 + @Override 1.214 + public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.215 + state.loader = core; 1.216 + state.intercepter = this; 1.217 + 1.218 + // TODO: make sure there aren't too many duplicate of this code 1.219 + // create the object to unmarshal 1.220 + Object child; 1.221 + UnmarshallingContext context = state.getContext(); 1.222 + 1.223 + // let's see if we can reuse the existing peer object 1.224 + child = context.getOuterPeer(); 1.225 + 1.226 + if(child!=null && jaxbType!=child.getClass()) 1.227 + child = null; // unexpected type. 1.228 + 1.229 + if(child!=null) 1.230 + reset((JAXBElement)child,context); 1.231 + 1.232 + if(child==null) 1.233 + child = context.createInstance(ElementBeanInfoImpl.this); 1.234 + 1.235 + fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state); 1.236 + 1.237 + context.recordOuterPeer(child); 1.238 + UnmarshallingContext.State p = state.prev; 1.239 + p.backup = p.target; 1.240 + p.target = child; 1.241 + 1.242 + core.startElement(state,ea); 1.243 + } 1.244 + 1.245 + public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException { 1.246 + JAXBElement e = (JAXBElement)state.target; 1.247 + state.target = state.backup; 1.248 + state.backup = null; 1.249 + 1.250 + if (state.nil) { 1.251 + e.setNil(true); 1.252 + state.nil = false; 1.253 + } 1.254 + 1.255 + if(o!=null) 1.256 + // if the value is a leaf type, it's often already set to the element 1.257 + // through Accessor. 1.258 + e.setValue(o); 1.259 + 1.260 + fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state); 1.261 + 1.262 + return e; 1.263 + } 1.264 + } 1.265 + 1.266 + public String getElementNamespaceURI(JAXBElement e) { 1.267 + return e.getName().getNamespaceURI(); 1.268 + } 1.269 + 1.270 + public String getElementLocalName(JAXBElement e) { 1.271 + return e.getName().getLocalPart(); 1.272 + } 1.273 + 1.274 + public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.275 + if(loader==null) { 1.276 + // this has to be done lazily to avoid cyclic reference issue 1.277 + UnmarshallerChain c = new UnmarshallerChain(context); 1.278 + QNameMap<ChildLoader> result = new QNameMap<ChildLoader>(); 1.279 + property.buildChildElementUnmarshallers(c,result); 1.280 + if(result.size()==1) 1.281 + // for ElementBeanInfoImpl created from RuntimeElementInfo 1.282 + this.loader = new IntercepterLoader(result.getOne().getValue().loader); 1.283 + else 1.284 + // for special ElementBeanInfoImpl only used for marshalling 1.285 + this.loader = Discarder.INSTANCE; 1.286 + } 1.287 + return loader; 1.288 + } 1.289 + 1.290 + public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException { 1.291 + return createInstanceFromValue(null); 1.292 + } 1.293 + 1.294 + public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException { 1.295 + if(constructor==null) 1.296 + return new JAXBElement(tagName,expectedType,scope,o); 1.297 + else 1.298 + return constructor.newInstance(o); 1.299 + } 1.300 + 1.301 + public boolean reset(JAXBElement e, UnmarshallingContext context) { 1.302 + e.setValue(null); 1.303 + return true; 1.304 + } 1.305 + 1.306 + public String getId(JAXBElement e, XMLSerializer target) { 1.307 + // TODO: is this OK? Should we be returning the ID value of the type property? 1.308 + /* 1.309 + There's one case where we JAXBElement needs to be designated as ID, 1.310 + and that is when there's a global element whose type is ID. 1.311 + */ 1.312 + Object o = e.getValue(); 1.313 + if(o instanceof String) 1.314 + return (String)o; 1.315 + else 1.316 + return null; 1.317 + } 1.318 + 1.319 + public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.320 + try { 1.321 + property.serializeBody(element,target,null); 1.322 + } catch (AccessorException x) { 1.323 + target.reportError(null,x); 1.324 + } 1.325 + } 1.326 + 1.327 + public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.328 + serializeBody(e,target); 1.329 + } 1.330 + 1.331 + public void serializeAttributes(JAXBElement e, XMLSerializer target) { 1.332 + // noop 1.333 + } 1.334 + 1.335 + public void serializeURIs(JAXBElement e, XMLSerializer target) { 1.336 + // noop 1.337 + } 1.338 + 1.339 + public final Transducer<JAXBElement> getTransducer() { 1.340 + return null; 1.341 + } 1.342 + 1.343 + @Override 1.344 + public void wrapUp() { 1.345 + super.wrapUp(); 1.346 + property.wrapUp(); 1.347 + } 1.348 + 1.349 + @Override 1.350 + public void link(JAXBContextImpl grammar) { 1.351 + super.link(grammar); 1.352 + getLoader(grammar,true); // make sure to build them, if we hadn't done so 1.353 + } 1.354 +}