com.sun.xml.bind.v2.runtime.LeafBeanInfoImpl Maven / Gradle / Ivy
Show all versions of jaxb-runtime Show documentation
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.bind.v2.runtime;
import java.io.IOException;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.helpers.ValidationEventImpl;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import com.sun.xml.bind.api.AccessorException;
import com.sun.xml.bind.v2.model.runtime.RuntimeLeafInfo;
import com.sun.xml.bind.v2.runtime.unmarshaller.Loader;
import com.sun.xml.bind.v2.runtime.unmarshaller.TextLoader;
import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
import com.sun.xml.bind.v2.runtime.unmarshaller.XsiTypeLoader;
import org.xml.sax.SAXException;
/**
* {@link JaxBeanInfo} implementation for immutable leaf classes.
*
*
* Leaf classes are always bound to a text and they are often immutable.
* The JAXB spec allows this binding for a few special Java classes plus
* type-safe enums.
*
*
* This implementation obtains necessary information from {@link RuntimeLeafInfo}.
*
* @author Kohsuke Kawaguchi
*/
final class LeafBeanInfoImpl extends JaxBeanInfo {
private final Loader loader;
private final Loader loaderWithSubst;
private final Transducer xducer;
/**
* Non-null only if the leaf is also an element.
*/
private final Name tagName;
public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) {
super(grammar,li,li.getClazz(),li.getTypeNames(),li.isElement(),true,false);
xducer = li.getTransducer();
loader = new TextLoader(xducer);
loaderWithSubst = new XsiTypeLoader(this);
if(isElement())
tagName = grammar.nameBuilder.createElementName(li.getElementName());
else
tagName = null;
}
@Override
public QName getTypeName(BeanT instance) {
QName tn = xducer.getTypeName(instance);
if(tn!=null) return tn;
// rely on default
return super.getTypeName(instance);
}
public final String getElementNamespaceURI(BeanT t) {
return tagName.nsUri;
}
public final String getElementLocalName(BeanT t) {
return tagName.localName;
}
public BeanT createInstance(UnmarshallingContext context) {
throw new UnsupportedOperationException();
}
public final boolean reset(BeanT bean, UnmarshallingContext context) {
return false;
}
public final String getId(BeanT bean, XMLSerializer target) {
return null;
}
public final void serializeBody(BeanT bean, XMLSerializer w) throws SAXException, IOException, XMLStreamException {
// most of the times leaves are printed as leaf element/attribute property,
// so this code is only used for example when you have multiple XmlElement on a property
// and some of them are leaves. Hence this doesn't need to be super-fast.
try {
xducer.writeText(w,bean,null);
} catch (AccessorException e) {
w.reportError(null,e);
}
}
public final void serializeAttributes(BeanT bean, XMLSerializer target) {
// noop
}
public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
if(tagName==null) {
target.reportError(
new ValidationEventImpl(
ValidationEvent.ERROR,
Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()),
null,
null));
}
else {
target.startElement(tagName,bean);
target.childAsSoleContent(bean,null);
target.endElement();
}
}
public final void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
// TODO: maybe we should create another LeafBeanInfoImpl class for
// context-dependent xducers?
if(xducer.useNamespace()) {
try {
xducer.declareNamespace(bean,target);
} catch (AccessorException e) {
target.reportError(null,e);
}
}
}
public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
if(typeSubstitutionCapable)
return loaderWithSubst;
else
return loader;
}
public Transducer getTransducer() {
return xducer;
}
}