50
50
import com .thoughtworks .xstream .io .HierarchicalStreamReader ;
51
51
import com .thoughtworks .xstream .io .HierarchicalStreamWriter ;
52
52
import com .thoughtworks .xstream .io .StreamException ;
53
+ import com .thoughtworks .xstream .io .naming .NameCoder ;
53
54
import com .thoughtworks .xstream .io .xml .CompactWriter ;
54
55
import com .thoughtworks .xstream .io .xml .DomReader ;
55
56
import com .thoughtworks .xstream .io .xml .DomWriter ;
@@ -160,6 +161,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
160
161
161
162
private String encoding = DEFAULT_ENCODING ;
162
163
164
+ private NameCoder nameCoder = new XmlFriendlyNameCoder ();
165
+
163
166
private Class <?>[] supportedClasses ;
164
167
165
168
private ClassLoader beanClassLoader = new CompositeClassLoader ();
@@ -356,6 +359,15 @@ protected String getDefaultEncoding() {
356
359
return this .encoding ;
357
360
}
358
361
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
+
359
371
/**
360
372
* Set the classes supported by this marshaller.
361
373
* <p>If this property is empty (the default), all classes are supported.
@@ -622,10 +634,10 @@ public boolean supports(Class<?> clazz) {
622
634
protected void marshalDomNode (Object graph , Node node ) throws XmlMappingException {
623
635
HierarchicalStreamWriter streamWriter ;
624
636
if (node instanceof Document ) {
625
- streamWriter = new DomWriter ((Document ) node );
637
+ streamWriter = new DomWriter ((Document ) node , this . nameCoder );
626
638
}
627
639
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 );
629
641
}
630
642
else {
631
643
throw new IllegalArgumentException ("DOMResult contains neither Document nor Element" );
@@ -646,7 +658,7 @@ protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) t
646
658
@ Override
647
659
protected void marshalXmlStreamWriter (Object graph , XMLStreamWriter streamWriter ) throws XmlMappingException {
648
660
try {
649
- doMarshal (graph , new StaxWriter (new QNameMap (), streamWriter ), null );
661
+ doMarshal (graph , new StaxWriter (new QNameMap (), streamWriter , this . nameCoder ), null );
650
662
}
651
663
catch (XMLStreamException ex ) {
652
664
throw convertXStreamException (ex , true );
@@ -657,7 +669,7 @@ protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter
657
669
protected void marshalSaxHandlers (Object graph , ContentHandler contentHandler , LexicalHandler lexicalHandler )
658
670
throws XmlMappingException {
659
671
660
- SaxWriter saxWriter = new SaxWriter ();
672
+ SaxWriter saxWriter = new SaxWriter (this . nameCoder );
661
673
saxWriter .setContentHandler (contentHandler );
662
674
doMarshal (graph , saxWriter , null );
663
675
}
@@ -729,10 +741,10 @@ protected Object unmarshalStreamSourceNoExternalEntitities(StreamSource streamSo
729
741
protected Object unmarshalDomNode (Node node ) throws XmlMappingException {
730
742
HierarchicalStreamReader streamReader ;
731
743
if (node instanceof Document ) {
732
- streamReader = new DomReader ((Document ) node );
744
+ streamReader = new DomReader ((Document ) node , this . nameCoder );
733
745
}
734
746
else if (node instanceof Element ) {
735
- streamReader = new DomReader ((Element ) node );
747
+ streamReader = new DomReader ((Element ) node , this . nameCoder );
736
748
}
737
749
else {
738
750
throw new IllegalArgumentException ("DOMSource contains neither Document nor Element" );
@@ -753,7 +765,7 @@ protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlM
753
765
754
766
@ Override
755
767
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 );
757
769
}
758
770
759
771
@ Override
0 commit comments