Mon, 28 May 2018 10:36:45 +0800
Merge
aoqi@0 | 1 | /* |
aefimov@650 | 2 | * Copyright (c) 1997, 2014, 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; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.lang.reflect.Constructor; |
aoqi@0 | 30 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import javax.xml.bind.JAXBElement; |
aoqi@0 | 33 | import javax.xml.bind.JAXBException; |
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.runtime.RuntimeElementInfo; |
aoqi@0 | 40 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; |
aoqi@0 | 41 | import com.sun.xml.internal.bind.v2.runtime.property.Property; |
aoqi@0 | 42 | import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory; |
aoqi@0 | 43 | import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain; |
aoqi@0 | 44 | import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
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.Discarder; |
aoqi@0 | 47 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter; |
aoqi@0 | 48 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
aoqi@0 | 49 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; |
aoqi@0 | 50 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
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 JaxBeanInfo} implementation for {@link RuntimeElementInfo}. |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * @author Kohsuke Kawaguchi |
aoqi@0 | 59 | */ |
aoqi@0 | 60 | public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> { |
aoqi@0 | 61 | |
aoqi@0 | 62 | private Loader loader; |
aoqi@0 | 63 | |
aoqi@0 | 64 | private final Property property; |
aoqi@0 | 65 | |
aoqi@0 | 66 | // used to create new instances of JAXBElement. |
aoqi@0 | 67 | private final QName tagName; |
aoqi@0 | 68 | public final Class expectedType; |
aoqi@0 | 69 | private final Class scope; |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * If non-null, use this to create an instance. |
aoqi@0 | 73 | * It takes one value. |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | private final Constructor<? extends JAXBElement> constructor; |
aoqi@0 | 76 | |
aoqi@0 | 77 | ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) { |
aoqi@0 | 78 | super(grammar,rei,(Class<JAXBElement>)rei.getType(),true,false,true); |
aoqi@0 | 79 | |
aoqi@0 | 80 | this.property = PropertyFactory.create(grammar,rei.getProperty()); |
aoqi@0 | 81 | |
aoqi@0 | 82 | tagName = rei.getElementName(); |
aoqi@0 | 83 | expectedType = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getContentInMemoryType()); |
aoqi@0 | 84 | scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz(); |
aoqi@0 | 85 | |
aoqi@0 | 86 | Class type = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getType()); |
aoqi@0 | 87 | if(type==JAXBElement.class) |
aoqi@0 | 88 | constructor = null; |
aoqi@0 | 89 | else { |
aoqi@0 | 90 | try { |
aoqi@0 | 91 | constructor = type.getConstructor(expectedType); |
aoqi@0 | 92 | } catch (NoSuchMethodException e) { |
aoqi@0 | 93 | NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType); |
aoqi@0 | 94 | x.initCause(e); |
aoqi@0 | 95 | throw x; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | } |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | /** |
aoqi@0 | 101 | * The constructor for the sole instanceof {@link JaxBeanInfo} for |
aoqi@0 | 102 | * handling user-created {@link JAXBElement}. |
aoqi@0 | 103 | * |
aoqi@0 | 104 | * Such {@link JaxBeanInfo} is used only for marshalling. |
aoqi@0 | 105 | * |
aoqi@0 | 106 | * This is a hack. |
aoqi@0 | 107 | */ |
aoqi@0 | 108 | protected ElementBeanInfoImpl(final JAXBContextImpl grammar) { |
aoqi@0 | 109 | super(grammar,null,JAXBElement.class,true,false,true); |
aoqi@0 | 110 | tagName = null; |
aoqi@0 | 111 | expectedType = null; |
aoqi@0 | 112 | scope = null; |
aoqi@0 | 113 | constructor = null; |
aoqi@0 | 114 | |
aoqi@0 | 115 | this.property = new Property<JAXBElement>() { |
aoqi@0 | 116 | public void reset(JAXBElement o) { |
aoqi@0 | 117 | throw new UnsupportedOperationException(); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 121 | Class scope = e.getScope(); |
aoqi@0 | 122 | if(e.isGlobalScope()) scope = null; |
aoqi@0 | 123 | QName n = e.getName(); |
aoqi@0 | 124 | ElementBeanInfoImpl bi = grammar.getElement(scope,n); |
aoqi@0 | 125 | if(bi==null) { |
aoqi@0 | 126 | // infer what to do from the type |
aoqi@0 | 127 | JaxBeanInfo tbi; |
aoqi@0 | 128 | try { |
aoqi@0 | 129 | tbi = grammar.getBeanInfo(e.getDeclaredType(),true); |
aoqi@0 | 130 | } catch (JAXBException x) { |
aoqi@0 | 131 | // if e.getDeclaredType() isn't known to this JAXBContext |
aoqi@0 | 132 | target.reportError(null,x); |
aoqi@0 | 133 | return; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | Object value = e.getValue(); |
aoqi@0 | 136 | target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null); |
aoqi@0 | 137 | if(value==null) { |
aoqi@0 | 138 | target.writeXsiNilTrue(); |
aoqi@0 | 139 | } else { |
aoqi@0 | 140 | target.childAsXsiType(value,"value",tbi, false); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | target.endElement(); |
aoqi@0 | 143 | } else { |
aoqi@0 | 144 | try { |
aoqi@0 | 145 | bi.property.serializeBody(e,target,e); |
aoqi@0 | 146 | } catch (AccessorException x) { |
aoqi@0 | 147 | target.reportError(null,x); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | public void serializeURIs(JAXBElement o, XMLSerializer target) { |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public boolean hasSerializeURIAction() { |
aoqi@0 | 156 | return false; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | public String getIdValue(JAXBElement o) { |
aoqi@0 | 160 | return null; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | public PropertyKind getKind() { |
aoqi@0 | 164 | return PropertyKind.ELEMENT; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | public Accessor getElementPropertyAccessor(String nsUri, String localName) { |
aoqi@0 | 171 | throw new UnsupportedOperationException(); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | public void wrapUp() { |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | public RuntimePropertyInfo getInfo() { |
aoqi@0 | 178 | return property.getInfo(); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | public boolean isHiddenByOverride() { |
aoqi@0 | 182 | return false; |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | public void setHiddenByOverride(boolean hidden) { |
aoqi@0 | 186 | throw new UnsupportedOperationException("Not supported on jaxbelements."); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | public String getFieldName() { |
aoqi@0 | 190 | return null; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | }; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | /** |
aoqi@0 | 197 | * Use the previous {@link UnmarshallingContext.State}'s target to store |
aoqi@0 | 198 | * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader} |
aoqi@0 | 199 | * to correctly find the parent object. |
aoqi@0 | 200 | * This is a hack. |
aoqi@0 | 201 | */ |
aoqi@0 | 202 | private final class IntercepterLoader extends Loader implements Intercepter { |
aoqi@0 | 203 | private final Loader core; |
aoqi@0 | 204 | |
aoqi@0 | 205 | public IntercepterLoader(Loader core) { |
aoqi@0 | 206 | this.core = core; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | @Override |
aoqi@0 | 210 | public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
aefimov@650 | 211 | state.setLoader(core); |
aefimov@650 | 212 | state.setIntercepter(this); |
aoqi@0 | 213 | |
aoqi@0 | 214 | // TODO: make sure there aren't too many duplicate of this code |
aoqi@0 | 215 | // create the object to unmarshal |
aoqi@0 | 216 | Object child; |
aoqi@0 | 217 | UnmarshallingContext context = state.getContext(); |
aoqi@0 | 218 | |
aoqi@0 | 219 | // let's see if we can reuse the existing peer object |
aoqi@0 | 220 | child = context.getOuterPeer(); |
aoqi@0 | 221 | |
aoqi@0 | 222 | if(child!=null && jaxbType!=child.getClass()) |
aoqi@0 | 223 | child = null; // unexpected type. |
aoqi@0 | 224 | |
aoqi@0 | 225 | if(child!=null) |
aoqi@0 | 226 | reset((JAXBElement)child,context); |
aoqi@0 | 227 | |
aoqi@0 | 228 | if(child==null) |
aoqi@0 | 229 | child = context.createInstance(ElementBeanInfoImpl.this); |
aoqi@0 | 230 | |
aoqi@0 | 231 | fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state); |
aoqi@0 | 232 | |
aoqi@0 | 233 | context.recordOuterPeer(child); |
aefimov@650 | 234 | UnmarshallingContext.State p = state.getPrev(); |
aefimov@650 | 235 | p.setBackup(p.getTarget()); |
aefimov@650 | 236 | p.setTarget(child); |
aoqi@0 | 237 | |
aoqi@0 | 238 | core.startElement(state,ea); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException { |
aefimov@650 | 242 | JAXBElement e = (JAXBElement)state.getTarget(); |
aefimov@650 | 243 | state.setTarget(state.getBackup()); |
aefimov@650 | 244 | state.setBackup(null); |
aoqi@0 | 245 | |
aefimov@650 | 246 | if (state.isNil()) { |
aoqi@0 | 247 | e.setNil(true); |
aefimov@650 | 248 | state.setNil(false); |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | if(o!=null) |
aoqi@0 | 252 | // if the value is a leaf type, it's often already set to the element |
aoqi@0 | 253 | // through Accessor. |
aoqi@0 | 254 | e.setValue(o); |
aoqi@0 | 255 | |
aoqi@0 | 256 | fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state); |
aoqi@0 | 257 | |
aoqi@0 | 258 | return e; |
aoqi@0 | 259 | } |
aoqi@0 | 260 | } |
aoqi@0 | 261 | |
aoqi@0 | 262 | public String getElementNamespaceURI(JAXBElement e) { |
aoqi@0 | 263 | return e.getName().getNamespaceURI(); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | public String getElementLocalName(JAXBElement e) { |
aoqi@0 | 267 | return e.getName().getLocalPart(); |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { |
aoqi@0 | 271 | if(loader==null) { |
aoqi@0 | 272 | // this has to be done lazily to avoid cyclic reference issue |
aoqi@0 | 273 | UnmarshallerChain c = new UnmarshallerChain(context); |
aoqi@0 | 274 | QNameMap<ChildLoader> result = new QNameMap<ChildLoader>(); |
aoqi@0 | 275 | property.buildChildElementUnmarshallers(c,result); |
aoqi@0 | 276 | if(result.size()==1) |
aoqi@0 | 277 | // for ElementBeanInfoImpl created from RuntimeElementInfo |
aoqi@0 | 278 | this.loader = new IntercepterLoader(result.getOne().getValue().loader); |
aoqi@0 | 279 | else |
aoqi@0 | 280 | // for special ElementBeanInfoImpl only used for marshalling |
aoqi@0 | 281 | this.loader = Discarder.INSTANCE; |
aoqi@0 | 282 | } |
aoqi@0 | 283 | return loader; |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException { |
aoqi@0 | 287 | return createInstanceFromValue(null); |
aoqi@0 | 288 | } |
aoqi@0 | 289 | |
aoqi@0 | 290 | public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException { |
aoqi@0 | 291 | if(constructor==null) |
aoqi@0 | 292 | return new JAXBElement(tagName,expectedType,scope,o); |
aoqi@0 | 293 | else |
aoqi@0 | 294 | return constructor.newInstance(o); |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | public boolean reset(JAXBElement e, UnmarshallingContext context) { |
aoqi@0 | 298 | e.setValue(null); |
aoqi@0 | 299 | return true; |
aoqi@0 | 300 | } |
aoqi@0 | 301 | |
aoqi@0 | 302 | public String getId(JAXBElement e, XMLSerializer target) { |
aoqi@0 | 303 | // TODO: is this OK? Should we be returning the ID value of the type property? |
aoqi@0 | 304 | /* |
aoqi@0 | 305 | There's one case where we JAXBElement needs to be designated as ID, |
aoqi@0 | 306 | and that is when there's a global element whose type is ID. |
aoqi@0 | 307 | */ |
aoqi@0 | 308 | Object o = e.getValue(); |
aoqi@0 | 309 | if(o instanceof String) |
aoqi@0 | 310 | return (String)o; |
aoqi@0 | 311 | else |
aoqi@0 | 312 | return null; |
aoqi@0 | 313 | } |
aoqi@0 | 314 | |
aoqi@0 | 315 | public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 316 | try { |
aoqi@0 | 317 | property.serializeBody(element,target,null); |
aoqi@0 | 318 | } catch (AccessorException x) { |
aoqi@0 | 319 | target.reportError(null,x); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 324 | serializeBody(e,target); |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | public void serializeAttributes(JAXBElement e, XMLSerializer target) { |
aoqi@0 | 328 | // noop |
aoqi@0 | 329 | } |
aoqi@0 | 330 | |
aoqi@0 | 331 | public void serializeURIs(JAXBElement e, XMLSerializer target) { |
aoqi@0 | 332 | // noop |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | public final Transducer<JAXBElement> getTransducer() { |
aoqi@0 | 336 | return null; |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | @Override |
aoqi@0 | 340 | public void wrapUp() { |
aoqi@0 | 341 | super.wrapUp(); |
aoqi@0 | 342 | property.wrapUp(); |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | @Override |
aoqi@0 | 346 | public void link(JAXBContextImpl grammar) { |
aoqi@0 | 347 | super.link(grammar); |
aoqi@0 | 348 | getLoader(grammar,true); // make sure to build them, if we hadn't done so |
aoqi@0 | 349 | } |
aoqi@0 | 350 | } |