ohair@286: /* aefimov@650: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.bind.v2.runtime; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.lang.reflect.Constructor; ohair@286: import java.lang.reflect.InvocationTargetException; ohair@286: ohair@286: import javax.xml.bind.JAXBElement; ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: ohair@286: import com.sun.xml.internal.bind.api.AccessorException; ohair@286: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo; ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; ohair@286: import com.sun.xml.internal.bind.v2.runtime.property.Property; ohair@286: import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory; ohair@286: import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain; ohair@286: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; ohair@286: import com.sun.xml.internal.bind.v2.util.QNameMap; ohair@286: ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: /** ohair@286: * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: public final class ElementBeanInfoImpl extends JaxBeanInfo { ohair@286: ohair@286: private Loader loader; ohair@286: ohair@286: private final Property property; ohair@286: ohair@286: // used to create new instances of JAXBElement. ohair@286: private final QName tagName; ohair@286: public final Class expectedType; ohair@286: private final Class scope; ohair@286: ohair@286: /** ohair@286: * If non-null, use this to create an instance. ohair@286: * It takes one value. ohair@286: */ ohair@286: private final Constructor constructor; ohair@286: ohair@286: ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) { ohair@286: super(grammar,rei,(Class)rei.getType(),true,false,true); ohair@286: ohair@286: this.property = PropertyFactory.create(grammar,rei.getProperty()); ohair@286: ohair@286: tagName = rei.getElementName(); mkos@450: expectedType = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getContentInMemoryType()); ohair@286: scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz(); ohair@286: mkos@450: Class type = (Class) Utils.REFLECTION_NAVIGATOR.erasure(rei.getType()); ohair@286: if(type==JAXBElement.class) ohair@286: constructor = null; ohair@286: else { ohair@286: try { ohair@286: constructor = type.getConstructor(expectedType); ohair@286: } catch (NoSuchMethodException e) { ohair@286: NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType); ohair@286: x.initCause(e); ohair@286: throw x; ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * The constructor for the sole instanceof {@link JaxBeanInfo} for ohair@286: * handling user-created {@link JAXBElement}. ohair@286: * ohair@286: * Such {@link JaxBeanInfo} is used only for marshalling. ohair@286: * ohair@286: * This is a hack. ohair@286: */ ohair@286: protected ElementBeanInfoImpl(final JAXBContextImpl grammar) { ohair@286: super(grammar,null,JAXBElement.class,true,false,true); ohair@286: tagName = null; ohair@286: expectedType = null; ohair@286: scope = null; ohair@286: constructor = null; ohair@286: ohair@286: this.property = new Property() { ohair@286: public void reset(JAXBElement o) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException { ohair@286: Class scope = e.getScope(); ohair@286: if(e.isGlobalScope()) scope = null; ohair@286: QName n = e.getName(); ohair@286: ElementBeanInfoImpl bi = grammar.getElement(scope,n); ohair@286: if(bi==null) { ohair@286: // infer what to do from the type ohair@286: JaxBeanInfo tbi; ohair@286: try { ohair@286: tbi = grammar.getBeanInfo(e.getDeclaredType(),true); ohair@286: } catch (JAXBException x) { ohair@286: // if e.getDeclaredType() isn't known to this JAXBContext ohair@286: target.reportError(null,x); ohair@286: return; ohair@286: } ohair@286: Object value = e.getValue(); ohair@286: target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null); ohair@286: if(value==null) { ohair@286: target.writeXsiNilTrue(); ohair@286: } else { ohair@286: target.childAsXsiType(value,"value",tbi, false); ohair@286: } ohair@286: target.endElement(); ohair@286: } else { ohair@286: try { ohair@286: bi.property.serializeBody(e,target,e); ohair@286: } catch (AccessorException x) { ohair@286: target.reportError(null,x); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: public void serializeURIs(JAXBElement o, XMLSerializer target) { ohair@286: } ohair@286: ohair@286: public boolean hasSerializeURIAction() { ohair@286: return false; ohair@286: } ohair@286: ohair@286: public String getIdValue(JAXBElement o) { ohair@286: return null; ohair@286: } ohair@286: ohair@286: public PropertyKind getKind() { ohair@286: return PropertyKind.ELEMENT; ohair@286: } ohair@286: ohair@286: public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap handlers) { ohair@286: } ohair@286: ohair@286: public Accessor getElementPropertyAccessor(String nsUri, String localName) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: public void wrapUp() { ohair@286: } ohair@286: ohair@286: public RuntimePropertyInfo getInfo() { ohair@286: return property.getInfo(); ohair@286: } ohair@286: ohair@286: public boolean isHiddenByOverride() { ohair@286: return false; ohair@286: } ohair@286: ohair@286: public void setHiddenByOverride(boolean hidden) { ohair@286: throw new UnsupportedOperationException("Not supported on jaxbelements."); ohair@286: } ohair@286: ohair@286: public String getFieldName() { ohair@286: return null; ohair@286: } ohair@286: ohair@286: }; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Use the previous {@link UnmarshallingContext.State}'s target to store ohair@286: * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader} ohair@286: * to correctly find the parent object. ohair@286: * This is a hack. ohair@286: */ ohair@286: private final class IntercepterLoader extends Loader implements Intercepter { ohair@286: private final Loader core; ohair@286: ohair@286: public IntercepterLoader(Loader core) { ohair@286: this.core = core; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aefimov@650: state.setLoader(core); aefimov@650: state.setIntercepter(this); ohair@286: ohair@286: // TODO: make sure there aren't too many duplicate of this code ohair@286: // create the object to unmarshal ohair@286: Object child; ohair@286: UnmarshallingContext context = state.getContext(); ohair@286: ohair@286: // let's see if we can reuse the existing peer object ohair@286: child = context.getOuterPeer(); ohair@286: ohair@286: if(child!=null && jaxbType!=child.getClass()) ohair@286: child = null; // unexpected type. ohair@286: ohair@286: if(child!=null) ohair@286: reset((JAXBElement)child,context); ohair@286: ohair@286: if(child==null) ohair@286: child = context.createInstance(ElementBeanInfoImpl.this); ohair@286: ohair@286: fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state); ohair@286: ohair@286: context.recordOuterPeer(child); aefimov@650: UnmarshallingContext.State p = state.getPrev(); aefimov@650: p.setBackup(p.getTarget()); aefimov@650: p.setTarget(child); ohair@286: ohair@286: core.startElement(state,ea); ohair@286: } ohair@286: ohair@286: public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException { aefimov@650: JAXBElement e = (JAXBElement)state.getTarget(); aefimov@650: state.setTarget(state.getBackup()); aefimov@650: state.setBackup(null); ohair@286: aefimov@650: if (state.isNil()) { ohair@286: e.setNil(true); aefimov@650: state.setNil(false); ohair@286: } ohair@286: ohair@286: if(o!=null) ohair@286: // if the value is a leaf type, it's often already set to the element ohair@286: // through Accessor. ohair@286: e.setValue(o); ohair@286: ohair@286: fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state); ohair@286: ohair@286: return e; ohair@286: } ohair@286: } ohair@286: ohair@286: public String getElementNamespaceURI(JAXBElement e) { ohair@286: return e.getName().getNamespaceURI(); ohair@286: } ohair@286: ohair@286: public String getElementLocalName(JAXBElement e) { ohair@286: return e.getName().getLocalPart(); ohair@286: } ohair@286: ohair@286: public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { ohair@286: if(loader==null) { ohair@286: // this has to be done lazily to avoid cyclic reference issue ohair@286: UnmarshallerChain c = new UnmarshallerChain(context); ohair@286: QNameMap result = new QNameMap(); ohair@286: property.buildChildElementUnmarshallers(c,result); ohair@286: if(result.size()==1) ohair@286: // for ElementBeanInfoImpl created from RuntimeElementInfo ohair@286: this.loader = new IntercepterLoader(result.getOne().getValue().loader); ohair@286: else ohair@286: // for special ElementBeanInfoImpl only used for marshalling ohair@286: this.loader = Discarder.INSTANCE; ohair@286: } ohair@286: return loader; ohair@286: } ohair@286: ohair@286: public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException { ohair@286: return createInstanceFromValue(null); ohair@286: } ohair@286: ohair@286: public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException { ohair@286: if(constructor==null) ohair@286: return new JAXBElement(tagName,expectedType,scope,o); ohair@286: else ohair@286: return constructor.newInstance(o); ohair@286: } ohair@286: ohair@286: public boolean reset(JAXBElement e, UnmarshallingContext context) { ohair@286: e.setValue(null); ohair@286: return true; ohair@286: } ohair@286: ohair@286: public String getId(JAXBElement e, XMLSerializer target) { ohair@286: // TODO: is this OK? Should we be returning the ID value of the type property? ohair@286: /* ohair@286: There's one case where we JAXBElement needs to be designated as ID, ohair@286: and that is when there's a global element whose type is ID. ohair@286: */ ohair@286: Object o = e.getValue(); ohair@286: if(o instanceof String) ohair@286: return (String)o; ohair@286: else ohair@286: return null; ohair@286: } ohair@286: ohair@286: public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException { ohair@286: try { ohair@286: property.serializeBody(element,target,null); ohair@286: } catch (AccessorException x) { ohair@286: target.reportError(null,x); ohair@286: } ohair@286: } ohair@286: ohair@286: public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException { ohair@286: serializeBody(e,target); ohair@286: } ohair@286: ohair@286: public void serializeAttributes(JAXBElement e, XMLSerializer target) { ohair@286: // noop ohair@286: } ohair@286: ohair@286: public void serializeURIs(JAXBElement e, XMLSerializer target) { ohair@286: // noop ohair@286: } ohair@286: ohair@286: public final Transducer getTransducer() { ohair@286: return null; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void wrapUp() { ohair@286: super.wrapUp(); ohair@286: property.wrapUp(); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void link(JAXBContextImpl grammar) { ohair@286: super.link(grammar); ohair@286: getLoader(grammar,true); // make sure to build them, if we hadn't done so ohair@286: } ohair@286: }