1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/LeafBeanInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,163 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime; 1.30 + 1.31 +import java.io.IOException; 1.32 + 1.33 +import javax.xml.bind.ValidationEvent; 1.34 +import javax.xml.bind.helpers.ValidationEventImpl; 1.35 +import javax.xml.namespace.QName; 1.36 +import javax.xml.stream.XMLStreamException; 1.37 + 1.38 +import com.sun.xml.internal.bind.api.AccessorException; 1.39 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeLeafInfo; 1.40 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.41 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TextLoader; 1.42 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.43 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader; 1.44 + 1.45 +import org.xml.sax.SAXException; 1.46 + 1.47 +/** 1.48 + * {@link JaxBeanInfo} implementation for immutable leaf classes. 1.49 + * 1.50 + * <p> 1.51 + * Leaf classes are always bound to a text and they are often immutable. 1.52 + * The JAXB spec allows this binding for a few special Java classes plus 1.53 + * type-safe enums. 1.54 + * 1.55 + * <p> 1.56 + * This implementation obtains necessary information from {@link RuntimeLeafInfo}. 1.57 + * 1.58 + * @author Kohsuke Kawaguchi 1.59 + */ 1.60 +final class LeafBeanInfoImpl<BeanT> extends JaxBeanInfo<BeanT> { 1.61 + 1.62 + private final Loader loader; 1.63 + private final Loader loaderWithSubst; 1.64 + 1.65 + private final Transducer<BeanT> xducer; 1.66 + 1.67 + /** 1.68 + * Non-null only if the leaf is also an element. 1.69 + */ 1.70 + private final Name tagName; 1.71 + 1.72 + public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) { 1.73 + super(grammar,li,li.getClazz(),li.getTypeNames(),li.isElement(),true,false); 1.74 + 1.75 + xducer = li.getTransducer(); 1.76 + loader = new TextLoader(xducer); 1.77 + loaderWithSubst = new XsiTypeLoader(this); 1.78 + 1.79 + if(isElement()) 1.80 + tagName = grammar.nameBuilder.createElementName(li.getElementName()); 1.81 + else 1.82 + tagName = null; 1.83 + } 1.84 + 1.85 + @Override 1.86 + public QName getTypeName(BeanT instance) { 1.87 + QName tn = xducer.getTypeName(instance); 1.88 + if(tn!=null) return tn; 1.89 + // rely on default 1.90 + return super.getTypeName(instance); 1.91 + } 1.92 + 1.93 + public final String getElementNamespaceURI(BeanT t) { 1.94 + return tagName.nsUri; 1.95 + } 1.96 + 1.97 + public final String getElementLocalName(BeanT t) { 1.98 + return tagName.localName; 1.99 + } 1.100 + 1.101 + public BeanT createInstance(UnmarshallingContext context) { 1.102 + throw new UnsupportedOperationException(); 1.103 + } 1.104 + 1.105 + public final boolean reset(BeanT bean, UnmarshallingContext context) { 1.106 + return false; 1.107 + } 1.108 + 1.109 + public final String getId(BeanT bean, XMLSerializer target) { 1.110 + return null; 1.111 + } 1.112 + 1.113 + public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException { 1.114 + // most of the times leaves are printed as leaf element/attribute property, 1.115 + // so this code is only used for example when you have multiple XmlElement on a property 1.116 + // and some of them are leaves. Hence this doesn't need to be super-fast. 1.117 + try { 1.118 + xducer.writeText(w,bean,null); 1.119 + } catch (AccessorException e) { 1.120 + w.reportError(null,e); 1.121 + } 1.122 + } 1.123 + 1.124 + public final void serializeAttributes(BeanT bean, XMLSerializer target) { 1.125 + // noop 1.126 + } 1.127 + 1.128 + public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.129 + if(tagName==null) { 1.130 + target.reportError( 1.131 + new ValidationEventImpl( 1.132 + ValidationEvent.ERROR, 1.133 + Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()), 1.134 + null, 1.135 + null)); 1.136 + } 1.137 + else { 1.138 + target.startElement(tagName,bean); 1.139 + target.childAsSoleContent(bean,null); 1.140 + target.endElement(); 1.141 + } 1.142 + } 1.143 + 1.144 + public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { 1.145 + // TODO: maybe we should create another LeafBeanInfoImpl class for 1.146 + // context-dependent xducers? 1.147 + if(xducer.useNamespace()) { 1.148 + try { 1.149 + xducer.declareNamespace(bean,target); 1.150 + } catch (AccessorException e) { 1.151 + target.reportError(null,e); 1.152 + } 1.153 + } 1.154 + } 1.155 + 1.156 + public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.157 + if(typeSubstitutionCapable) 1.158 + return loaderWithSubst; 1.159 + else 1.160 + return loader; 1.161 + } 1.162 + 1.163 + public Transducer<BeanT> getTransducer() { 1.164 + return xducer; 1.165 + } 1.166 +}