aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: import javax.xml.bind.ValidationEvent; aoqi@0: import javax.xml.bind.helpers.ValidationEventImpl; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.api.AccessorException; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeLeafInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TextLoader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link JaxBeanInfo} implementation for immutable leaf classes. aoqi@0: * aoqi@0: *

aoqi@0: * Leaf classes are always bound to a text and they are often immutable. aoqi@0: * The JAXB spec allows this binding for a few special Java classes plus aoqi@0: * type-safe enums. aoqi@0: * aoqi@0: *

aoqi@0: * This implementation obtains necessary information from {@link RuntimeLeafInfo}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: final class LeafBeanInfoImpl extends JaxBeanInfo { aoqi@0: aoqi@0: private final Loader loader; aoqi@0: private final Loader loaderWithSubst; aoqi@0: aoqi@0: private final Transducer xducer; aoqi@0: aoqi@0: /** aoqi@0: * Non-null only if the leaf is also an element. aoqi@0: */ aoqi@0: private final Name tagName; aoqi@0: aoqi@0: public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) { aoqi@0: super(grammar,li,li.getClazz(),li.getTypeNames(),li.isElement(),true,false); aoqi@0: aoqi@0: xducer = li.getTransducer(); aoqi@0: loader = new TextLoader(xducer); aoqi@0: loaderWithSubst = new XsiTypeLoader(this); aoqi@0: aoqi@0: if(isElement()) aoqi@0: tagName = grammar.nameBuilder.createElementName(li.getElementName()); aoqi@0: else aoqi@0: tagName = null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public QName getTypeName(BeanT instance) { aoqi@0: QName tn = xducer.getTypeName(instance); aoqi@0: if(tn!=null) return tn; aoqi@0: // rely on default aoqi@0: return super.getTypeName(instance); aoqi@0: } aoqi@0: aoqi@0: public final String getElementNamespaceURI(BeanT t) { aoqi@0: return tagName.nsUri; aoqi@0: } aoqi@0: aoqi@0: public final String getElementLocalName(BeanT t) { aoqi@0: return tagName.localName; aoqi@0: } aoqi@0: aoqi@0: public BeanT createInstance(UnmarshallingContext context) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public final boolean reset(BeanT bean, UnmarshallingContext context) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public final String getId(BeanT bean, XMLSerializer target) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException { aoqi@0: // most of the times leaves are printed as leaf element/attribute property, aoqi@0: // so this code is only used for example when you have multiple XmlElement on a property aoqi@0: // and some of them are leaves. Hence this doesn't need to be super-fast. aoqi@0: try { aoqi@0: xducer.writeText(w,bean,null); aoqi@0: } catch (AccessorException e) { aoqi@0: w.reportError(null,e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void serializeAttributes(BeanT bean, XMLSerializer target) { aoqi@0: // noop aoqi@0: } aoqi@0: aoqi@0: public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { aoqi@0: if(tagName==null) { aoqi@0: target.reportError( aoqi@0: new ValidationEventImpl( aoqi@0: ValidationEvent.ERROR, aoqi@0: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()), aoqi@0: null, aoqi@0: null)); aoqi@0: } aoqi@0: else { aoqi@0: target.startElement(tagName,bean); aoqi@0: target.childAsSoleContent(bean,null); aoqi@0: target.endElement(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { aoqi@0: // TODO: maybe we should create another LeafBeanInfoImpl class for aoqi@0: // context-dependent xducers? aoqi@0: if(xducer.useNamespace()) { aoqi@0: try { aoqi@0: xducer.declareNamespace(bean,target); aoqi@0: } catch (AccessorException e) { aoqi@0: target.reportError(null,e); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { aoqi@0: if(typeSubstitutionCapable) aoqi@0: return loaderWithSubst; aoqi@0: else aoqi@0: return loader; aoqi@0: } aoqi@0: aoqi@0: public Transducer getTransducer() { aoqi@0: return xducer; aoqi@0: } aoqi@0: }