XStreamMarshaller supports custom NameCoder strategy

Issue: SPR-11702
This commit is contained in:
Juergen Hoeller 2014-04-16 23:08:32 +02:00
parent 53162e78e4
commit f5cce14fe7
1 changed files with 19 additions and 7 deletions

View File

@ -50,6 +50,7 @@ import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomReader;
import com.thoughtworks.xstream.io.xml.DomWriter;
@ -160,6 +161,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
private String encoding = DEFAULT_ENCODING;
private NameCoder nameCoder = new XmlFriendlyNameCoder();
private Class<?>[] supportedClasses;
private ClassLoader beanClassLoader = new CompositeClassLoader();
@ -356,6 +359,15 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
return this.encoding;
}
/**
* Set a custom XStream {@link NameCoder} to use.
* The default is an {@link XmlFriendlyNameCoder}.
* @since 4.0.4
*/
public void setNameCoder(NameCoder nameCoder) {
this.nameCoder = nameCoder;
}
/**
* Set the classes supported by this marshaller.
* <p>If this property is empty (the default), all classes are supported.
@ -622,10 +634,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
HierarchicalStreamWriter streamWriter;
if (node instanceof Document) {
streamWriter = new DomWriter((Document) node);
streamWriter = new DomWriter((Document) node, this.nameCoder);
}
else if (node instanceof Element) {
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), new XmlFriendlyNameCoder());
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), this.nameCoder);
}
else {
throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
@ -646,7 +658,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
@Override
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
try {
doMarshal(graph, new StaxWriter(new QNameMap(), streamWriter), null);
doMarshal(graph, new StaxWriter(new QNameMap(), streamWriter, this.nameCoder), null);
}
catch (XMLStreamException ex) {
throw convertXStreamException(ex, true);
@ -657,7 +669,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
throws XmlMappingException {
SaxWriter saxWriter = new SaxWriter();
SaxWriter saxWriter = new SaxWriter(this.nameCoder);
saxWriter.setContentHandler(contentHandler);
doMarshal(graph, saxWriter, null);
}
@ -729,10 +741,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
HierarchicalStreamReader streamReader;
if (node instanceof Document) {
streamReader = new DomReader((Document) node);
streamReader = new DomReader((Document) node, this.nameCoder);
}
else if (node instanceof Element) {
streamReader = new DomReader((Element) node);
streamReader = new DomReader((Element) node, this.nameCoder);
}
else {
throw new IllegalArgumentException("DOMSource contains neither Document nor Element");
@ -753,7 +765,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
@Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
return doUnmarshal(new StaxReader(new QNameMap(), streamReader), null);
return doUnmarshal(new StaxReader(new QNameMap(), streamReader, this.nameCoder), null);
}
@Override