From f5cce14fe7749183a766e6335ee511d8918a81d4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 16 Apr 2014 23:08:32 +0200 Subject: [PATCH] XStreamMarshaller supports custom NameCoder strategy Issue: SPR-11702 --- .../oxm/xstream/XStreamMarshaller.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java index 2f6ee330618..14b114efffb 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -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. *

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