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 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2013, 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; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.IOException; |
ohair@286 | 29 | |
ohair@286 | 30 | import javax.xml.bind.ValidationEvent; |
ohair@286 | 31 | import javax.xml.bind.helpers.ValidationEventImpl; |
ohair@286 | 32 | import javax.xml.namespace.QName; |
ohair@286 | 33 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 34 | |
ohair@286 | 35 | import com.sun.xml.internal.bind.api.AccessorException; |
ohair@286 | 36 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeLeafInfo; |
ohair@286 | 37 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
ohair@286 | 38 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TextLoader; |
ohair@286 | 39 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
ohair@286 | 40 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader; |
ohair@286 | 41 | |
ohair@286 | 42 | import org.xml.sax.SAXException; |
ohair@286 | 43 | |
ohair@286 | 44 | /** |
ohair@286 | 45 | * {@link JaxBeanInfo} implementation for immutable leaf classes. |
ohair@286 | 46 | * |
ohair@286 | 47 | * <p> |
ohair@286 | 48 | * Leaf classes are always bound to a text and they are often immutable. |
ohair@286 | 49 | * The JAXB spec allows this binding for a few special Java classes plus |
ohair@286 | 50 | * type-safe enums. |
ohair@286 | 51 | * |
ohair@286 | 52 | * <p> |
ohair@286 | 53 | * This implementation obtains necessary information from {@link RuntimeLeafInfo}. |
ohair@286 | 54 | * |
ohair@286 | 55 | * @author Kohsuke Kawaguchi |
ohair@286 | 56 | */ |
ohair@286 | 57 | final class LeafBeanInfoImpl<BeanT> extends JaxBeanInfo<BeanT> { |
ohair@286 | 58 | |
ohair@286 | 59 | private final Loader loader; |
ohair@286 | 60 | private final Loader loaderWithSubst; |
ohair@286 | 61 | |
ohair@286 | 62 | private final Transducer<BeanT> xducer; |
ohair@286 | 63 | |
ohair@286 | 64 | /** |
ohair@286 | 65 | * Non-null only if the leaf is also an element. |
ohair@286 | 66 | */ |
ohair@286 | 67 | private final Name tagName; |
ohair@286 | 68 | |
ohair@286 | 69 | public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) { |
ohair@286 | 70 | super(grammar,li,li.getClazz(),li.getTypeNames(),li.isElement(),true,false); |
ohair@286 | 71 | |
ohair@286 | 72 | xducer = li.getTransducer(); |
ohair@286 | 73 | loader = new TextLoader(xducer); |
ohair@286 | 74 | loaderWithSubst = new XsiTypeLoader(this); |
ohair@286 | 75 | |
ohair@286 | 76 | if(isElement()) |
ohair@286 | 77 | tagName = grammar.nameBuilder.createElementName(li.getElementName()); |
ohair@286 | 78 | else |
ohair@286 | 79 | tagName = null; |
ohair@286 | 80 | } |
ohair@286 | 81 | |
alanb@368 | 82 | @Override |
ohair@286 | 83 | public QName getTypeName(BeanT instance) { |
ohair@286 | 84 | QName tn = xducer.getTypeName(instance); |
ohair@286 | 85 | if(tn!=null) return tn; |
ohair@286 | 86 | // rely on default |
ohair@286 | 87 | return super.getTypeName(instance); |
ohair@286 | 88 | } |
ohair@286 | 89 | |
alanb@368 | 90 | public final String getElementNamespaceURI(BeanT t) { |
ohair@286 | 91 | return tagName.nsUri; |
ohair@286 | 92 | } |
ohair@286 | 93 | |
alanb@368 | 94 | public final String getElementLocalName(BeanT t) { |
ohair@286 | 95 | return tagName.localName; |
ohair@286 | 96 | } |
ohair@286 | 97 | |
ohair@286 | 98 | public BeanT createInstance(UnmarshallingContext context) { |
ohair@286 | 99 | throw new UnsupportedOperationException(); |
ohair@286 | 100 | } |
ohair@286 | 101 | |
ohair@286 | 102 | public final boolean reset(BeanT bean, UnmarshallingContext context) { |
ohair@286 | 103 | return false; |
ohair@286 | 104 | } |
ohair@286 | 105 | |
ohair@286 | 106 | public final String getId(BeanT bean, XMLSerializer target) { |
ohair@286 | 107 | return null; |
ohair@286 | 108 | } |
ohair@286 | 109 | |
ohair@286 | 110 | public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException { |
ohair@286 | 111 | // most of the times leaves are printed as leaf element/attribute property, |
ohair@286 | 112 | // so this code is only used for example when you have multiple XmlElement on a property |
ohair@286 | 113 | // and some of them are leaves. Hence this doesn't need to be super-fast. |
ohair@286 | 114 | try { |
ohair@286 | 115 | xducer.writeText(w,bean,null); |
ohair@286 | 116 | } catch (AccessorException e) { |
ohair@286 | 117 | w.reportError(null,e); |
ohair@286 | 118 | } |
ohair@286 | 119 | } |
ohair@286 | 120 | |
ohair@286 | 121 | public final void serializeAttributes(BeanT bean, XMLSerializer target) { |
ohair@286 | 122 | // noop |
ohair@286 | 123 | } |
ohair@286 | 124 | |
ohair@286 | 125 | public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { |
ohair@286 | 126 | if(tagName==null) { |
ohair@286 | 127 | target.reportError( |
ohair@286 | 128 | new ValidationEventImpl( |
ohair@286 | 129 | ValidationEvent.ERROR, |
ohair@286 | 130 | Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()), |
ohair@286 | 131 | null, |
ohair@286 | 132 | null)); |
ohair@286 | 133 | } |
ohair@286 | 134 | else { |
ohair@286 | 135 | target.startElement(tagName,bean); |
ohair@286 | 136 | target.childAsSoleContent(bean,null); |
ohair@286 | 137 | target.endElement(); |
ohair@286 | 138 | } |
ohair@286 | 139 | } |
ohair@286 | 140 | |
ohair@286 | 141 | public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { |
ohair@286 | 142 | // TODO: maybe we should create another LeafBeanInfoImpl class for |
ohair@286 | 143 | // context-dependent xducers? |
ohair@286 | 144 | if(xducer.useNamespace()) { |
ohair@286 | 145 | try { |
ohair@286 | 146 | xducer.declareNamespace(bean,target); |
ohair@286 | 147 | } catch (AccessorException e) { |
ohair@286 | 148 | target.reportError(null,e); |
ohair@286 | 149 | } |
ohair@286 | 150 | } |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { |
ohair@286 | 154 | if(typeSubstitutionCapable) |
ohair@286 | 155 | return loaderWithSubst; |
ohair@286 | 156 | else |
ohair@286 | 157 | return loader; |
ohair@286 | 158 | } |
ohair@286 | 159 | |
ohair@286 | 160 | public Transducer<BeanT> getTransducer() { |
ohair@286 | 161 | return xducer; |
ohair@286 | 162 | } |
ohair@286 | 163 | } |