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

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
child 760
e530533619ec
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

ohair@286 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.bind.v2.runtime;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.lang.reflect.Constructor;
ohair@286 30 import java.lang.reflect.InvocationTargetException;
ohair@286 31
ohair@286 32 import javax.xml.bind.JAXBElement;
ohair@286 33 import javax.xml.bind.JAXBException;
ohair@286 34 import javax.xml.namespace.QName;
ohair@286 35 import javax.xml.stream.XMLStreamException;
ohair@286 36
ohair@286 37 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 38 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
ohair@286 39 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo;
ohair@286 40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
ohair@286 41 import com.sun.xml.internal.bind.v2.runtime.property.Property;
ohair@286 42 import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory;
ohair@286 43 import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain;
ohair@286 44 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
ohair@286 45 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
ohair@286 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder;
ohair@286 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter;
ohair@286 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
ohair@286 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
ohair@286 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286 51 import com.sun.xml.internal.bind.v2.util.QNameMap;
ohair@286 52
ohair@286 53 import org.xml.sax.SAXException;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}.
ohair@286 57 *
ohair@286 58 * @author Kohsuke Kawaguchi
ohair@286 59 */
ohair@286 60 public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> {
ohair@286 61
ohair@286 62 private Loader loader;
ohair@286 63
ohair@286 64 private final Property property;
ohair@286 65
ohair@286 66 // used to create new instances of JAXBElement.
ohair@286 67 private final QName tagName;
ohair@286 68 public final Class expectedType;
ohair@286 69 private final Class scope;
ohair@286 70
ohair@286 71 /**
ohair@286 72 * If non-null, use this to create an instance.
ohair@286 73 * It takes one value.
ohair@286 74 */
ohair@286 75 private final Constructor<? extends JAXBElement> constructor;
ohair@286 76
ohair@286 77 ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) {
ohair@286 78 super(grammar,rei,(Class<JAXBElement>)rei.getType(),true,false,true);
ohair@286 79
ohair@286 80 this.property = PropertyFactory.create(grammar,rei.getProperty());
ohair@286 81
ohair@286 82 tagName = rei.getElementName();
mkos@450 83 expectedType = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getContentInMemoryType());
ohair@286 84 scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz();
ohair@286 85
mkos@450 86 Class type = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getType());
ohair@286 87 if(type==JAXBElement.class)
ohair@286 88 constructor = null;
ohair@286 89 else {
ohair@286 90 try {
ohair@286 91 constructor = type.getConstructor(expectedType);
ohair@286 92 } catch (NoSuchMethodException e) {
ohair@286 93 NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType);
ohair@286 94 x.initCause(e);
ohair@286 95 throw x;
ohair@286 96 }
ohair@286 97 }
ohair@286 98 }
ohair@286 99
ohair@286 100 /**
ohair@286 101 * The constructor for the sole instanceof {@link JaxBeanInfo} for
ohair@286 102 * handling user-created {@link JAXBElement}.
ohair@286 103 *
ohair@286 104 * Such {@link JaxBeanInfo} is used only for marshalling.
ohair@286 105 *
ohair@286 106 * This is a hack.
ohair@286 107 */
ohair@286 108 protected ElementBeanInfoImpl(final JAXBContextImpl grammar) {
ohair@286 109 super(grammar,null,JAXBElement.class,true,false,true);
ohair@286 110 tagName = null;
ohair@286 111 expectedType = null;
ohair@286 112 scope = null;
ohair@286 113 constructor = null;
ohair@286 114
ohair@286 115 this.property = new Property<JAXBElement>() {
ohair@286 116 public void reset(JAXBElement o) {
ohair@286 117 throw new UnsupportedOperationException();
ohair@286 118 }
ohair@286 119
ohair@286 120 public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException {
ohair@286 121 Class scope = e.getScope();
ohair@286 122 if(e.isGlobalScope()) scope = null;
ohair@286 123 QName n = e.getName();
ohair@286 124 ElementBeanInfoImpl bi = grammar.getElement(scope,n);
ohair@286 125 if(bi==null) {
ohair@286 126 // infer what to do from the type
ohair@286 127 JaxBeanInfo tbi;
ohair@286 128 try {
ohair@286 129 tbi = grammar.getBeanInfo(e.getDeclaredType(),true);
ohair@286 130 } catch (JAXBException x) {
ohair@286 131 // if e.getDeclaredType() isn't known to this JAXBContext
ohair@286 132 target.reportError(null,x);
ohair@286 133 return;
ohair@286 134 }
ohair@286 135 Object value = e.getValue();
ohair@286 136 target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null);
ohair@286 137 if(value==null) {
ohair@286 138 target.writeXsiNilTrue();
ohair@286 139 } else {
ohair@286 140 target.childAsXsiType(value,"value",tbi, false);
ohair@286 141 }
ohair@286 142 target.endElement();
ohair@286 143 } else {
ohair@286 144 try {
ohair@286 145 bi.property.serializeBody(e,target,e);
ohair@286 146 } catch (AccessorException x) {
ohair@286 147 target.reportError(null,x);
ohair@286 148 }
ohair@286 149 }
ohair@286 150 }
ohair@286 151
ohair@286 152 public void serializeURIs(JAXBElement o, XMLSerializer target) {
ohair@286 153 }
ohair@286 154
ohair@286 155 public boolean hasSerializeURIAction() {
ohair@286 156 return false;
ohair@286 157 }
ohair@286 158
ohair@286 159 public String getIdValue(JAXBElement o) {
ohair@286 160 return null;
ohair@286 161 }
ohair@286 162
ohair@286 163 public PropertyKind getKind() {
ohair@286 164 return PropertyKind.ELEMENT;
ohair@286 165 }
ohair@286 166
ohair@286 167 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
ohair@286 168 }
ohair@286 169
ohair@286 170 public Accessor getElementPropertyAccessor(String nsUri, String localName) {
ohair@286 171 throw new UnsupportedOperationException();
ohair@286 172 }
ohair@286 173
ohair@286 174 public void wrapUp() {
ohair@286 175 }
ohair@286 176
ohair@286 177 public RuntimePropertyInfo getInfo() {
ohair@286 178 return property.getInfo();
ohair@286 179 }
ohair@286 180
ohair@286 181 public boolean isHiddenByOverride() {
ohair@286 182 return false;
ohair@286 183 }
ohair@286 184
ohair@286 185 public void setHiddenByOverride(boolean hidden) {
ohair@286 186 throw new UnsupportedOperationException("Not supported on jaxbelements.");
ohair@286 187 }
ohair@286 188
ohair@286 189 public String getFieldName() {
ohair@286 190 return null;
ohair@286 191 }
ohair@286 192
ohair@286 193 };
ohair@286 194 }
ohair@286 195
ohair@286 196 /**
ohair@286 197 * Use the previous {@link UnmarshallingContext.State}'s target to store
ohair@286 198 * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader}
ohair@286 199 * to correctly find the parent object.
ohair@286 200 * This is a hack.
ohair@286 201 */
ohair@286 202 private final class IntercepterLoader extends Loader implements Intercepter {
ohair@286 203 private final Loader core;
ohair@286 204
ohair@286 205 public IntercepterLoader(Loader core) {
ohair@286 206 this.core = core;
ohair@286 207 }
ohair@286 208
ohair@286 209 @Override
ohair@286 210 public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aefimov@650 211 state.setLoader(core);
aefimov@650 212 state.setIntercepter(this);
ohair@286 213
ohair@286 214 // TODO: make sure there aren't too many duplicate of this code
ohair@286 215 // create the object to unmarshal
ohair@286 216 Object child;
ohair@286 217 UnmarshallingContext context = state.getContext();
ohair@286 218
ohair@286 219 // let's see if we can reuse the existing peer object
ohair@286 220 child = context.getOuterPeer();
ohair@286 221
ohair@286 222 if(child!=null && jaxbType!=child.getClass())
ohair@286 223 child = null; // unexpected type.
ohair@286 224
ohair@286 225 if(child!=null)
ohair@286 226 reset((JAXBElement)child,context);
ohair@286 227
ohair@286 228 if(child==null)
ohair@286 229 child = context.createInstance(ElementBeanInfoImpl.this);
ohair@286 230
ohair@286 231 fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state);
ohair@286 232
ohair@286 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);
ohair@286 237
ohair@286 238 core.startElement(state,ea);
ohair@286 239 }
ohair@286 240
ohair@286 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);
ohair@286 245
aefimov@650 246 if (state.isNil()) {
ohair@286 247 e.setNil(true);
aefimov@650 248 state.setNil(false);
ohair@286 249 }
ohair@286 250
ohair@286 251 if(o!=null)
ohair@286 252 // if the value is a leaf type, it's often already set to the element
ohair@286 253 // through Accessor.
ohair@286 254 e.setValue(o);
ohair@286 255
ohair@286 256 fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state);
ohair@286 257
ohair@286 258 return e;
ohair@286 259 }
ohair@286 260 }
ohair@286 261
ohair@286 262 public String getElementNamespaceURI(JAXBElement e) {
ohair@286 263 return e.getName().getNamespaceURI();
ohair@286 264 }
ohair@286 265
ohair@286 266 public String getElementLocalName(JAXBElement e) {
ohair@286 267 return e.getName().getLocalPart();
ohair@286 268 }
ohair@286 269
ohair@286 270 public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
ohair@286 271 if(loader==null) {
ohair@286 272 // this has to be done lazily to avoid cyclic reference issue
ohair@286 273 UnmarshallerChain c = new UnmarshallerChain(context);
ohair@286 274 QNameMap<ChildLoader> result = new QNameMap<ChildLoader>();
ohair@286 275 property.buildChildElementUnmarshallers(c,result);
ohair@286 276 if(result.size()==1)
ohair@286 277 // for ElementBeanInfoImpl created from RuntimeElementInfo
ohair@286 278 this.loader = new IntercepterLoader(result.getOne().getValue().loader);
ohair@286 279 else
ohair@286 280 // for special ElementBeanInfoImpl only used for marshalling
ohair@286 281 this.loader = Discarder.INSTANCE;
ohair@286 282 }
ohair@286 283 return loader;
ohair@286 284 }
ohair@286 285
ohair@286 286 public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException {
ohair@286 287 return createInstanceFromValue(null);
ohair@286 288 }
ohair@286 289
ohair@286 290 public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException {
ohair@286 291 if(constructor==null)
ohair@286 292 return new JAXBElement(tagName,expectedType,scope,o);
ohair@286 293 else
ohair@286 294 return constructor.newInstance(o);
ohair@286 295 }
ohair@286 296
ohair@286 297 public boolean reset(JAXBElement e, UnmarshallingContext context) {
ohair@286 298 e.setValue(null);
ohair@286 299 return true;
ohair@286 300 }
ohair@286 301
ohair@286 302 public String getId(JAXBElement e, XMLSerializer target) {
ohair@286 303 // TODO: is this OK? Should we be returning the ID value of the type property?
ohair@286 304 /*
ohair@286 305 There's one case where we JAXBElement needs to be designated as ID,
ohair@286 306 and that is when there's a global element whose type is ID.
ohair@286 307 */
ohair@286 308 Object o = e.getValue();
ohair@286 309 if(o instanceof String)
ohair@286 310 return (String)o;
ohair@286 311 else
ohair@286 312 return null;
ohair@286 313 }
ohair@286 314
ohair@286 315 public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 316 try {
ohair@286 317 property.serializeBody(element,target,null);
ohair@286 318 } catch (AccessorException x) {
ohair@286 319 target.reportError(null,x);
ohair@286 320 }
ohair@286 321 }
ohair@286 322
ohair@286 323 public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 324 serializeBody(e,target);
ohair@286 325 }
ohair@286 326
ohair@286 327 public void serializeAttributes(JAXBElement e, XMLSerializer target) {
ohair@286 328 // noop
ohair@286 329 }
ohair@286 330
ohair@286 331 public void serializeURIs(JAXBElement e, XMLSerializer target) {
ohair@286 332 // noop
ohair@286 333 }
ohair@286 334
ohair@286 335 public final Transducer<JAXBElement> getTransducer() {
ohair@286 336 return null;
ohair@286 337 }
ohair@286 338
ohair@286 339 @Override
ohair@286 340 public void wrapUp() {
ohair@286 341 super.wrapUp();
ohair@286 342 property.wrapUp();
ohair@286 343 }
ohair@286 344
ohair@286 345 @Override
ohair@286 346 public void link(JAXBContextImpl grammar) {
ohair@286 347 super.link(grammar);
ohair@286 348 getLoader(grammar,true); // make sure to build them, if we hadn't done so
ohair@286 349 }
ohair@286 350 }

mercurial