ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, 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.unmarshaller; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: ohair@286: import com.sun.xml.internal.bind.DatatypeConverterImpl; ohair@286: import com.sun.xml.internal.bind.v2.WellKnownNamespace; ohair@286: import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: ohair@286: import java.util.Collection; ohair@286: import java.util.Collections; ohair@286: import java.util.HashSet; ohair@286: import org.xml.sax.Attributes; ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: /** ohair@286: * Looks at @xsi:type and forwards to the right {@link Loader}. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: public class XsiTypeLoader extends Loader { ohair@286: ohair@286: /** ohair@286: * Use this when no @xsi:type was found. ohair@286: */ ohair@286: private final JaxBeanInfo defaultBeanInfo; ohair@286: ohair@286: public XsiTypeLoader(JaxBeanInfo defaultBeanInfo) { ohair@286: super(true); ohair@286: this.defaultBeanInfo = defaultBeanInfo; ohair@286: } ohair@286: ohair@286: public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { ohair@286: JaxBeanInfo beanInfo = parseXsiType(state,ea,defaultBeanInfo); ohair@286: if(beanInfo==null) ohair@286: beanInfo = defaultBeanInfo; ohair@286: ohair@286: Loader loader = beanInfo.getLoader(null,false); ohair@286: state.loader = loader; ohair@286: loader.startElement(state,ea); ohair@286: } ohair@286: ohair@286: /*pacakge*/ static JaxBeanInfo parseXsiType(UnmarshallingContext.State state, TagName ea, @Nullable JaxBeanInfo defaultBeanInfo) throws SAXException { ohair@286: UnmarshallingContext context = state.getContext(); ohair@286: JaxBeanInfo beanInfo = null; ohair@286: ohair@286: // look for @xsi:type ohair@286: Attributes atts = ea.atts; ohair@286: int idx = atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type"); ohair@286: ohair@286: if(idx>=0) { ohair@286: // we'll consume the value only when it's a recognized value, ohair@286: // so don't consume it just yet. ohair@286: String value = atts.getValue(idx); ohair@286: ohair@286: QName type = DatatypeConverterImpl._parseQName(value,context); ohair@286: if(type==null) { ohair@286: reportError(Messages.NOT_A_QNAME.format(value),true); ohair@286: } else { ohair@286: if(defaultBeanInfo!=null && defaultBeanInfo.getTypeNames().contains(type)) ohair@286: // if this xsi:type is something that the default type can already handle, ohair@286: // let it do so. This is added as a work around to bug https://jax-ws.dev.java.net/issues/show_bug.cgi?id=195 ohair@286: // where a redundant xsi:type="xs:dateTime" causes JAXB to unmarshal XMLGregorianCalendar, ohair@286: // where Date is expected. ohair@286: // this is not a complete fix, as we still won't be able to handle simple type substitution in general, ohair@286: // but none-the-less ohair@286: return defaultBeanInfo; ohair@286: ohair@286: beanInfo = context.getJAXBContext().getGlobalType(type); mkos@397: if(beanInfo==null) { // let's report an error mkos@397: if (context.parent.hasEventHandler() // is somebody listening? mkos@397: && context.shouldErrorBeReported()) { // should we report error? mkos@397: String nearest = context.getJAXBContext().getNearestTypeName(type); mkos@397: if(nearest!=null) mkos@397: reportError(Messages.UNRECOGNIZED_TYPE_NAME_MAYBE.format(type,nearest),true); mkos@397: else mkos@397: reportError(Messages.UNRECOGNIZED_TYPE_NAME.format(type),true); mkos@397: } ohair@286: } ohair@286: // TODO: resurrect the following check ohair@286: // else ohair@286: // if(!target.isAssignableFrom(actual)) { ohair@286: // reportError(context, ohair@286: // Messages.UNSUBSTITUTABLE_TYPE.format(value,actual.getName(),target.getName()), ohair@286: // true); ohair@286: // actual = targetBeanInfo; // ditto ohair@286: // } ohair@286: } ohair@286: } ohair@286: return beanInfo; ohair@286: } ohair@286: ohair@286: static final QName XsiTypeQNAME = new QName(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type"); ohair@286: ohair@286: @Override ohair@286: public Collection getExpectedAttributes() { ohair@286: final Collection expAttrs = new HashSet(); ohair@286: expAttrs.addAll(super.getExpectedAttributes()); ohair@286: expAttrs.add(XsiTypeQNAME); ohair@286: return Collections.unmodifiableCollection(expAttrs); ohair@286: } ohair@286: }