Sun, 31 Aug 2014 16:14:36 +0400
8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos
ohair@286 | 1 | /* |
aefimov@650 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime.unmarshaller; |
ohair@286 | 27 | |
ohair@286 | 28 | import javax.xml.namespace.QName; |
ohair@286 | 29 | |
ohair@286 | 30 | import com.sun.xml.internal.bind.DatatypeConverterImpl; |
ohair@286 | 31 | import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
ohair@286 | 32 | import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; |
ohair@286 | 33 | import com.sun.istack.internal.Nullable; |
ohair@286 | 34 | |
ohair@286 | 35 | import java.util.Collection; |
ohair@286 | 36 | import java.util.Collections; |
ohair@286 | 37 | import java.util.HashSet; |
ohair@286 | 38 | import org.xml.sax.Attributes; |
ohair@286 | 39 | import org.xml.sax.SAXException; |
ohair@286 | 40 | |
ohair@286 | 41 | /** |
ohair@286 | 42 | * Looks at @xsi:type and forwards to the right {@link Loader}. |
ohair@286 | 43 | * |
ohair@286 | 44 | * @author Kohsuke Kawaguchi |
ohair@286 | 45 | */ |
ohair@286 | 46 | public class XsiTypeLoader extends Loader { |
ohair@286 | 47 | |
ohair@286 | 48 | /** |
ohair@286 | 49 | * Use this when no @xsi:type was found. |
ohair@286 | 50 | */ |
ohair@286 | 51 | private final JaxBeanInfo defaultBeanInfo; |
ohair@286 | 52 | |
ohair@286 | 53 | public XsiTypeLoader(JaxBeanInfo defaultBeanInfo) { |
ohair@286 | 54 | super(true); |
ohair@286 | 55 | this.defaultBeanInfo = defaultBeanInfo; |
ohair@286 | 56 | } |
ohair@286 | 57 | |
ohair@286 | 58 | public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
ohair@286 | 59 | JaxBeanInfo beanInfo = parseXsiType(state,ea,defaultBeanInfo); |
ohair@286 | 60 | if(beanInfo==null) |
ohair@286 | 61 | beanInfo = defaultBeanInfo; |
ohair@286 | 62 | |
ohair@286 | 63 | Loader loader = beanInfo.getLoader(null,false); |
aefimov@650 | 64 | state.setLoader(loader); |
ohair@286 | 65 | loader.startElement(state,ea); |
ohair@286 | 66 | } |
ohair@286 | 67 | |
ohair@286 | 68 | /*pacakge*/ static JaxBeanInfo parseXsiType(UnmarshallingContext.State state, TagName ea, @Nullable JaxBeanInfo defaultBeanInfo) throws SAXException { |
ohair@286 | 69 | UnmarshallingContext context = state.getContext(); |
ohair@286 | 70 | JaxBeanInfo beanInfo = null; |
ohair@286 | 71 | |
ohair@286 | 72 | // look for @xsi:type |
ohair@286 | 73 | Attributes atts = ea.atts; |
ohair@286 | 74 | int idx = atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type"); |
ohair@286 | 75 | |
ohair@286 | 76 | if(idx>=0) { |
ohair@286 | 77 | // we'll consume the value only when it's a recognized value, |
ohair@286 | 78 | // so don't consume it just yet. |
ohair@286 | 79 | String value = atts.getValue(idx); |
ohair@286 | 80 | |
ohair@286 | 81 | QName type = DatatypeConverterImpl._parseQName(value,context); |
ohair@286 | 82 | if(type==null) { |
ohair@286 | 83 | reportError(Messages.NOT_A_QNAME.format(value),true); |
ohair@286 | 84 | } else { |
ohair@286 | 85 | if(defaultBeanInfo!=null && defaultBeanInfo.getTypeNames().contains(type)) |
ohair@286 | 86 | // if this xsi:type is something that the default type can already handle, |
ohair@286 | 87 | // 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 | 88 | // where a redundant xsi:type="xs:dateTime" causes JAXB to unmarshal XMLGregorianCalendar, |
ohair@286 | 89 | // where Date is expected. |
ohair@286 | 90 | // this is not a complete fix, as we still won't be able to handle simple type substitution in general, |
ohair@286 | 91 | // but none-the-less |
ohair@286 | 92 | return defaultBeanInfo; |
ohair@286 | 93 | |
ohair@286 | 94 | beanInfo = context.getJAXBContext().getGlobalType(type); |
mkos@397 | 95 | if(beanInfo==null) { // let's report an error |
mkos@397 | 96 | if (context.parent.hasEventHandler() // is somebody listening? |
mkos@397 | 97 | && context.shouldErrorBeReported()) { // should we report error? |
mkos@397 | 98 | String nearest = context.getJAXBContext().getNearestTypeName(type); |
mkos@397 | 99 | if(nearest!=null) |
mkos@397 | 100 | reportError(Messages.UNRECOGNIZED_TYPE_NAME_MAYBE.format(type,nearest),true); |
mkos@397 | 101 | else |
mkos@397 | 102 | reportError(Messages.UNRECOGNIZED_TYPE_NAME.format(type),true); |
mkos@397 | 103 | } |
ohair@286 | 104 | } |
ohair@286 | 105 | // TODO: resurrect the following check |
ohair@286 | 106 | // else |
ohair@286 | 107 | // if(!target.isAssignableFrom(actual)) { |
ohair@286 | 108 | // reportError(context, |
ohair@286 | 109 | // Messages.UNSUBSTITUTABLE_TYPE.format(value,actual.getName(),target.getName()), |
ohair@286 | 110 | // true); |
ohair@286 | 111 | // actual = targetBeanInfo; // ditto |
ohair@286 | 112 | // } |
ohair@286 | 113 | } |
ohair@286 | 114 | } |
ohair@286 | 115 | return beanInfo; |
ohair@286 | 116 | } |
ohair@286 | 117 | |
ohair@286 | 118 | static final QName XsiTypeQNAME = new QName(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type"); |
ohair@286 | 119 | |
ohair@286 | 120 | @Override |
ohair@286 | 121 | public Collection<QName> getExpectedAttributes() { |
ohair@286 | 122 | final Collection<QName> expAttrs = new HashSet<QName>(); |
ohair@286 | 123 | expAttrs.addAll(super.getExpectedAttributes()); |
ohair@286 | 124 | expAttrs.add(XsiTypeQNAME); |
ohair@286 | 125 | return Collections.unmodifiableCollection(expAttrs); |
ohair@286 | 126 | } |
ohair@286 | 127 | } |