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