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.Modifier; aoqi@0: import java.lang.reflect.Type; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Map; aoqi@0: 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.core.TypeRef; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef; 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.Name; 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.DefaultValueLoaderDecorator; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; aoqi@0: import com.sun.xml.internal.bind.v2.util.QNameMap; aoqi@0: aoqi@0: import javax.xml.bind.JAXBElement; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * @author Kohsuke Kawaguchi (kk@kohsuke.org) aoqi@0: */ aoqi@0: final class SingleElementNodeProperty extends PropertyImpl { aoqi@0: aoqi@0: private final Accessor acc; aoqi@0: aoqi@0: private final boolean nillable; aoqi@0: aoqi@0: private final QName[] acceptedElements; aoqi@0: aoqi@0: private final Map typeNames = new HashMap(); aoqi@0: aoqi@0: private RuntimeElementPropertyInfo prop; aoqi@0: aoqi@0: /** aoqi@0: * The tag name used to produce xsi:nil. The first one in the list. aoqi@0: */ aoqi@0: private final Name nullTagName; aoqi@0: aoqi@0: public SingleElementNodeProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) { aoqi@0: super(context,prop); aoqi@0: acc = prop.getAccessor().optimize(context); aoqi@0: this.prop = prop; aoqi@0: aoqi@0: QName nt = null; aoqi@0: boolean nil = false; aoqi@0: aoqi@0: acceptedElements = new QName[prop.getTypes().size()]; aoqi@0: for( int i=0; i e : typeNames.entrySet()) { aoqi@0: if(e.getKey().isAssignableFrom(vtype)) { aoqi@0: tt = e.getValue(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: boolean addNilDecl = (o instanceof JAXBElement) && ((JAXBElement)o).isNil(); aoqi@0: if(tt==null) { aoqi@0: // actually this is an error, because the actual type was not a sub-type aoqi@0: // of any of the types specified in the annotations, aoqi@0: // but for the purpose of experimenting with simple type substitution, aoqi@0: // it's convenient to marshal this anyway (for example so that classes aoqi@0: // generated from simple types like String can be marshalled as expected.) aoqi@0: w.startElement(typeNames.values().iterator().next().tagName,null); aoqi@0: w.childAsXsiType(v,fieldName,w.grammar.getBeanInfo(Object.class), addNilDecl && nillable); aoqi@0: } else { aoqi@0: w.startElement(tt.tagName,null); aoqi@0: w.childAsXsiType(v,fieldName,tt.beanInfo, addNilDecl && nillable); aoqi@0: } aoqi@0: w.endElement(); aoqi@0: } else if (nillable) { aoqi@0: w.startElement(nullTagName,null); aoqi@0: w.writeXsiNilTrue(); aoqi@0: w.endElement(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap handlers) { aoqi@0: JAXBContextImpl context = chain.context; aoqi@0: aoqi@0: for (TypeRef e : prop.getTypes()) { aoqi@0: JaxBeanInfo bi = context.getOrCreate((RuntimeTypeInfo) e.getTarget()); aoqi@0: // if the expected Java type is already final, type substitution won't really work anyway. aoqi@0: // this also traps cases like trying to substitute xsd:long element with xsi:type='xsd:int' aoqi@0: Loader l = bi.getLoader(context,!Modifier.isFinal(bi.jaxbType.getModifiers())); aoqi@0: if(e.getDefaultValue()!=null) aoqi@0: l = new DefaultValueLoaderDecorator(l,e.getDefaultValue()); aoqi@0: if(nillable || chain.context.allNillable) aoqi@0: l = new XsiNilLoader.Single(l,acc); aoqi@0: handlers.put( e.getTagName(), new ChildLoader(l,acc)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public PropertyKind getKind() { aoqi@0: return PropertyKind.ELEMENT; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Accessor getElementPropertyAccessor(String nsUri, String localName) { aoqi@0: for( QName n : acceptedElements) { aoqi@0: if(n.getNamespaceURI().equals(nsUri) && n.getLocalPart().equals(localName)) aoqi@0: return acc; aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: }