aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, 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.property; aoqi@0: aoqi@0: import java.io.IOException; 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.bind.annotation.DomHandler; 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.ClassFactory; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.WildcardMode; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElement; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeReferencePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 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.WildcardLoader; 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: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: final class SingleReferenceNodeProperty extends PropertyImpl { aoqi@0: aoqi@0: private final Accessor acc; aoqi@0: aoqi@0: private final QNameMap expectedElements = new QNameMap(); aoqi@0: aoqi@0: private final DomHandler domHandler; aoqi@0: private final WildcardMode wcMode; aoqi@0: aoqi@0: public SingleReferenceNodeProperty(JAXBContextImpl context, RuntimeReferencePropertyInfo prop) { aoqi@0: super(context,prop); aoqi@0: acc = prop.getAccessor().optimize(context); aoqi@0: aoqi@0: for (RuntimeElement e : prop.getElements()) { aoqi@0: expectedElements.put( e.getElementName(), context.getOrCreate(e) ); aoqi@0: } aoqi@0: aoqi@0: if(prop.getWildcard()!=null) { aoqi@0: domHandler = (DomHandler) ClassFactory.create(prop.getDOMHandler()); aoqi@0: wcMode = prop.getWildcard(); aoqi@0: } else { aoqi@0: domHandler = null; aoqi@0: wcMode = null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void reset(BeanT bean) throws AccessorException { aoqi@0: acc.set(bean,null); aoqi@0: } aoqi@0: aoqi@0: public String getIdValue(BeanT beanT) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { aoqi@0: ValueT v = acc.get(o); aoqi@0: if(v!=null) { aoqi@0: try { aoqi@0: JaxBeanInfo bi = w.grammar.getBeanInfo(v,true); aoqi@0: if(bi.jaxbType==Object.class && domHandler!=null) aoqi@0: // even if 'v' is a DOM node, it always derive from Object, aoqi@0: // so the getBeanInfo returns BeanInfo for Object aoqi@0: w.writeDom(v,domHandler,o,fieldName); aoqi@0: else aoqi@0: bi.serializeRoot(v,w); aoqi@0: } catch (JAXBException e) { aoqi@0: w.reportError(fieldName,e); aoqi@0: // recover by ignoring this property aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap handlers) { aoqi@0: for (QNameMap.Entry n : expectedElements.entrySet()) aoqi@0: handlers.put(n.nsUri,n.localName, new ChildLoader(n.getValue().getLoader(chain.context,true),acc)); aoqi@0: aoqi@0: if(domHandler!=null) aoqi@0: handlers.put(CATCH_ALL,new ChildLoader(new WildcardLoader(domHandler,wcMode),acc)); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public PropertyKind getKind() { aoqi@0: return PropertyKind.REFERENCE; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Accessor getElementPropertyAccessor(String nsUri, String localName) { aoqi@0: JaxBeanInfo bi = expectedElements.get(nsUri, localName); aoqi@0: if(bi!=null) { aoqi@0: if(bi instanceof ElementBeanInfoImpl) { aoqi@0: final ElementBeanInfoImpl ebi = (ElementBeanInfoImpl) bi; aoqi@0: // a JAXBElement. We need to handle JAXBElement for JAX-WS aoqi@0: return new Accessor(ebi.expectedType) { aoqi@0: public Object get(BeanT bean) throws AccessorException { aoqi@0: ValueT r = acc.get(bean); aoqi@0: if(r instanceof JAXBElement) { aoqi@0: return ((JAXBElement)r).getValue(); aoqi@0: } else aoqi@0: // this is sloppy programming, but hey... aoqi@0: return r; aoqi@0: } aoqi@0: aoqi@0: public void set(BeanT bean, Object value) throws AccessorException { aoqi@0: if(value!=null) { aoqi@0: try { aoqi@0: value = ebi.createInstanceFromValue(value); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: throw new AccessorException(e); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw new AccessorException(e); aoqi@0: } catch (InstantiationException e) { aoqi@0: throw new AccessorException(e); aoqi@0: } aoqi@0: } aoqi@0: acc.set(bean,(ValueT)value); aoqi@0: } aoqi@0: }; aoqi@0: } else { aoqi@0: // a custom element type, like @XmlRootElement class Foo { ... } aoqi@0: return acc; aoqi@0: } aoqi@0: } else aoqi@0: return null; aoqi@0: } aoqi@0: }