ohair@286: /*
alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.bind.v2.runtime;
ohair@286:
ohair@286: import java.io.IOException;
ohair@286:
ohair@286: import javax.xml.bind.ValidationEvent;
ohair@286: import javax.xml.bind.helpers.ValidationEventImpl;
ohair@286: import javax.xml.namespace.QName;
ohair@286: import javax.xml.stream.XMLStreamException;
ohair@286:
ohair@286: import com.sun.xml.internal.bind.api.AccessorException;
ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeLeafInfo;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TextLoader;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader;
ohair@286:
ohair@286: import org.xml.sax.SAXException;
ohair@286:
ohair@286: /**
ohair@286: * {@link JaxBeanInfo} implementation for immutable leaf classes.
ohair@286: *
ohair@286: *
ohair@286: * Leaf classes are always bound to a text and they are often immutable.
ohair@286: * The JAXB spec allows this binding for a few special Java classes plus
ohair@286: * type-safe enums.
ohair@286: *
ohair@286: *
ohair@286: * This implementation obtains necessary information from {@link RuntimeLeafInfo}.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi
ohair@286: */
ohair@286: final class LeafBeanInfoImpl extends JaxBeanInfo {
ohair@286:
ohair@286: private final Loader loader;
ohair@286: private final Loader loaderWithSubst;
ohair@286:
ohair@286: private final Transducer xducer;
ohair@286:
ohair@286: /**
ohair@286: * Non-null only if the leaf is also an element.
ohair@286: */
ohair@286: private final Name tagName;
ohair@286:
ohair@286: public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) {
ohair@286: super(grammar,li,li.getClazz(),li.getTypeNames(),li.isElement(),true,false);
ohair@286:
ohair@286: xducer = li.getTransducer();
ohair@286: loader = new TextLoader(xducer);
ohair@286: loaderWithSubst = new XsiTypeLoader(this);
ohair@286:
ohair@286: if(isElement())
ohair@286: tagName = grammar.nameBuilder.createElementName(li.getElementName());
ohair@286: else
ohair@286: tagName = null;
ohair@286: }
ohair@286:
alanb@368: @Override
ohair@286: public QName getTypeName(BeanT instance) {
ohair@286: QName tn = xducer.getTypeName(instance);
ohair@286: if(tn!=null) return tn;
ohair@286: // rely on default
ohair@286: return super.getTypeName(instance);
ohair@286: }
ohair@286:
alanb@368: public final String getElementNamespaceURI(BeanT t) {
ohair@286: return tagName.nsUri;
ohair@286: }
ohair@286:
alanb@368: public final String getElementLocalName(BeanT t) {
ohair@286: return tagName.localName;
ohair@286: }
ohair@286:
ohair@286: public BeanT createInstance(UnmarshallingContext context) {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public final boolean reset(BeanT bean, UnmarshallingContext context) {
ohair@286: return false;
ohair@286: }
ohair@286:
ohair@286: public final String getId(BeanT bean, XMLSerializer target) {
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException {
ohair@286: // most of the times leaves are printed as leaf element/attribute property,
ohair@286: // so this code is only used for example when you have multiple XmlElement on a property
ohair@286: // and some of them are leaves. Hence this doesn't need to be super-fast.
ohair@286: try {
ohair@286: xducer.writeText(w,bean,null);
ohair@286: } catch (AccessorException e) {
ohair@286: w.reportError(null,e);
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public final void serializeAttributes(BeanT bean, XMLSerializer target) {
ohair@286: // noop
ohair@286: }
ohair@286:
ohair@286: public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286: if(tagName==null) {
ohair@286: target.reportError(
ohair@286: new ValidationEventImpl(
ohair@286: ValidationEvent.ERROR,
ohair@286: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()),
ohair@286: null,
ohair@286: null));
ohair@286: }
ohair@286: else {
ohair@286: target.startElement(tagName,bean);
ohair@286: target.childAsSoleContent(bean,null);
ohair@286: target.endElement();
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
ohair@286: // TODO: maybe we should create another LeafBeanInfoImpl class for
ohair@286: // context-dependent xducers?
ohair@286: if(xducer.useNamespace()) {
ohair@286: try {
ohair@286: xducer.declareNamespace(bean,target);
ohair@286: } catch (AccessorException e) {
ohair@286: target.reportError(null,e);
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
ohair@286: if(typeSubstitutionCapable)
ohair@286: return loaderWithSubst;
ohair@286: else
ohair@286: return loader;
ohair@286: }
ohair@286:
ohair@286: public Transducer getTransducer() {
ohair@286: return xducer;
ohair@286: }
ohair@286: }