Skip to content

Commit 090ab4b

Browse files
committed
XStreamMarshaller supports custom NameCoder strategy
Issue: SPR-11702 (cherry picked from commit f5cce14)
1 parent 18ef1d4 commit 090ab4b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
5151
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
5252
import com.thoughtworks.xstream.io.StreamException;
53+
import com.thoughtworks.xstream.io.naming.NameCoder;
5354
import com.thoughtworks.xstream.io.xml.CompactWriter;
5455
import com.thoughtworks.xstream.io.xml.DomReader;
5556
import com.thoughtworks.xstream.io.xml.DomWriter;
@@ -160,6 +161,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
160161

161162
private String encoding = DEFAULT_ENCODING;
162163

164+
private NameCoder nameCoder = new XmlFriendlyNameCoder();
165+
163166
private Class<?>[] supportedClasses;
164167

165168
private ClassLoader beanClassLoader = new CompositeClassLoader();
@@ -356,6 +359,15 @@ protected String getDefaultEncoding() {
356359
return this.encoding;
357360
}
358361

362+
/**
363+
* Set a custom XStream {@link NameCoder} to use.
364+
* The default is an {@link XmlFriendlyNameCoder}.
365+
* @since 4.0.4
366+
*/
367+
public void setNameCoder(NameCoder nameCoder) {
368+
this.nameCoder = nameCoder;
369+
}
370+
359371
/**
360372
* Set the classes supported by this marshaller.
361373
* <p>If this property is empty (the default), all classes are supported.
@@ -622,10 +634,10 @@ public boolean supports(Class<?> clazz) {
622634
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
623635
HierarchicalStreamWriter streamWriter;
624636
if (node instanceof Document) {
625-
streamWriter = new DomWriter((Document) node);
637+
streamWriter = new DomWriter((Document) node, this.nameCoder);
626638
}
627639
else if (node instanceof Element) {
628-
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), new XmlFriendlyNameCoder());
640+
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), this.nameCoder);
629641
}
630642
else {
631643
throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
@@ -646,7 +658,7 @@ protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) t
646658
@Override
647659
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
648660
try {
649-
doMarshal(graph, new StaxWriter(new QNameMap(), streamWriter), null);
661+
doMarshal(graph, new StaxWriter(new QNameMap(), streamWriter, this.nameCoder), null);
650662
}
651663
catch (XMLStreamException ex) {
652664
throw convertXStreamException(ex, true);
@@ -657,7 +669,7 @@ protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter
657669
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
658670
throws XmlMappingException {
659671

660-
SaxWriter saxWriter = new SaxWriter();
672+
SaxWriter saxWriter = new SaxWriter(this.nameCoder);
661673
saxWriter.setContentHandler(contentHandler);
662674
doMarshal(graph, saxWriter, null);
663675
}
@@ -729,10 +741,10 @@ protected Object unmarshalStreamSourceNoExternalEntitities(StreamSource streamSo
729741
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
730742
HierarchicalStreamReader streamReader;
731743
if (node instanceof Document) {
732-
streamReader = new DomReader((Document) node);
744+
streamReader = new DomReader((Document) node, this.nameCoder);
733745
}
734746
else if (node instanceof Element) {
735-
streamReader = new DomReader((Element) node);
747+
streamReader = new DomReader((Element) node, this.nameCoder);
736748
}
737749
else {
738750
throw new IllegalArgumentException("DOMSource contains neither Document nor Element");
@@ -753,7 +765,7 @@ protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlM
753765

754766
@Override
755767
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
756-
return doUnmarshal(new StaxReader(new QNameMap(), streamReader), null);
768+
return doUnmarshal(new StaxReader(new QNameMap(), streamReader, this.nameCoder), null);
757769
}
758770

759771
@Override

0 commit comments

Comments
 (0)