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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,162 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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 + public QName getTypeName(BeanT instance) { 1.86 + QName tn = xducer.getTypeName(instance); 1.87 + if(tn!=null) return tn; 1.88 + // rely on default 1.89 + return super.getTypeName(instance); 1.90 + } 1.91 + 1.92 + public final String getElementNamespaceURI(BeanT _) { 1.93 + return tagName.nsUri; 1.94 + } 1.95 + 1.96 + public final String getElementLocalName(BeanT _) { 1.97 + return tagName.localName; 1.98 + } 1.99 + 1.100 + public BeanT createInstance(UnmarshallingContext context) { 1.101 + throw new UnsupportedOperationException(); 1.102 + } 1.103 + 1.104 + public final boolean reset(BeanT bean, UnmarshallingContext context) { 1.105 + return false; 1.106 + } 1.107 + 1.108 + public final String getId(BeanT bean, XMLSerializer target) { 1.109 + return null; 1.110 + } 1.111 + 1.112 + public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException { 1.113 + // most of the times leaves are printed as leaf element/attribute property, 1.114 + // so this code is only used for example when you have multiple XmlElement on a property 1.115 + // and some of them are leaves. Hence this doesn't need to be super-fast. 1.116 + try { 1.117 + xducer.writeText(w,bean,null); 1.118 + } catch (AccessorException e) { 1.119 + w.reportError(null,e); 1.120 + } 1.121 + } 1.122 + 1.123 + public final void serializeAttributes(BeanT bean, XMLSerializer target) { 1.124 + // noop 1.125 + } 1.126 + 1.127 + public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.128 + if(tagName==null) { 1.129 + target.reportError( 1.130 + new ValidationEventImpl( 1.131 + ValidationEvent.ERROR, 1.132 + Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()), 1.133 + null, 1.134 + null)); 1.135 + } 1.136 + else { 1.137 + target.startElement(tagName,bean); 1.138 + target.childAsSoleContent(bean,null); 1.139 + target.endElement(); 1.140 + } 1.141 + } 1.142 + 1.143 + public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { 1.144 + // TODO: maybe we should create another LeafBeanInfoImpl class for 1.145 + // context-dependent xducers? 1.146 + if(xducer.useNamespace()) { 1.147 + try { 1.148 + xducer.declareNamespace(bean,target); 1.149 + } catch (AccessorException e) { 1.150 + target.reportError(null,e); 1.151 + } 1.152 + } 1.153 + } 1.154 + 1.155 + public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.156 + if(typeSubstitutionCapable) 1.157 + return loaderWithSubst; 1.158 + else 1.159 + return loader; 1.160 + } 1.161 + 1.162 + public Transducer<BeanT> getTransducer() { 1.163 + return xducer; 1.164 + } 1.165 +}