|
35 | 35 | import com.thoughtworks.xstream.converters.Converter;
|
36 | 36 | import com.thoughtworks.xstream.converters.ConverterMatcher;
|
37 | 37 | import com.thoughtworks.xstream.converters.SingleValueConverter;
|
| 38 | +import com.thoughtworks.xstream.io.HierarchicalStreamDriver; |
38 | 39 | import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
39 | 40 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
40 | 41 | import com.thoughtworks.xstream.io.xml.CompactWriter;
|
|
45 | 46 | import com.thoughtworks.xstream.io.xml.StaxReader;
|
46 | 47 | import com.thoughtworks.xstream.io.xml.StaxWriter;
|
47 | 48 | import com.thoughtworks.xstream.io.xml.XppReader;
|
48 |
| -import org.springframework.beans.propertyeditors.ClassEditor; |
49 |
| -import org.springframework.oxm.AbstractMarshaller; |
50 |
| -import org.springframework.oxm.XmlMappingException; |
51 |
| -import org.springframework.util.ObjectUtils; |
52 |
| -import org.springframework.xml.stream.StaxEventContentHandler; |
53 |
| -import org.springframework.xml.stream.XmlEventStreamReader; |
54 | 49 | import org.w3c.dom.Document;
|
55 | 50 | import org.w3c.dom.Element;
|
56 | 51 | import org.w3c.dom.Node;
|
|
59 | 54 | import org.xml.sax.XMLReader;
|
60 | 55 | import org.xml.sax.ext.LexicalHandler;
|
61 | 56 |
|
| 57 | +import org.springframework.beans.propertyeditors.ClassEditor; |
| 58 | +import org.springframework.oxm.AbstractMarshaller; |
| 59 | +import org.springframework.oxm.XmlMappingException; |
| 60 | +import org.springframework.util.ObjectUtils; |
| 61 | +import org.springframework.xml.stream.StaxEventContentHandler; |
| 62 | +import org.springframework.xml.stream.XmlEventStreamReader; |
| 63 | + |
62 | 64 | /**
|
63 | 65 | * Implementation of the <code>Marshaller</code> interface for XStream. By default, XStream does not require any further
|
64 | 66 | * configuration, though class aliases can be used to have more control over the behavior of XStream.
|
@@ -88,6 +90,9 @@ public class XStreamMarshaller extends AbstractMarshaller {
|
88 | 90 |
|
89 | 91 | private Class[] supportedClasses;
|
90 | 92 |
|
| 93 | + /** Specialized driver to be used with stream readers and writers */ |
| 94 | + private HierarchicalStreamDriver streamDriver; |
| 95 | + |
91 | 96 | /**
|
92 | 97 | * Returns the encoding to be used for stream access. If this property is not set, the default encoding is used.
|
93 | 98 | *
|
@@ -153,6 +158,11 @@ else if (converters[i] instanceof SingleValueConverter) {
|
153 | 158 | }
|
154 | 159 | }
|
155 | 160 |
|
| 161 | + /** Sets the XStream hierarchical stream driver to be used with stream readers and writers */ |
| 162 | + public void setStreamDriver(HierarchicalStreamDriver streamDriver) { |
| 163 | + this.streamDriver = streamDriver; |
| 164 | + } |
| 165 | + |
156 | 166 | /**
|
157 | 167 | * Set a alias/type map, consisting of string aliases mapped to <code>Class</code> instances (or Strings to be
|
158 | 168 | * converted to <code>Class</code> instances).
|
@@ -302,7 +312,12 @@ protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, L
|
302 | 312 | }
|
303 | 313 |
|
304 | 314 | protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
|
305 |
| - marshal(graph, new CompactWriter(writer)); |
| 315 | + if (streamDriver != null) { |
| 316 | + marshal(graph, streamDriver.createWriter(writer)); |
| 317 | + } |
| 318 | + else { |
| 319 | + marshal(graph, new CompactWriter(writer)); |
| 320 | + } |
306 | 321 | }
|
307 | 322 |
|
308 | 323 | private Object unmarshal(HierarchicalStreamReader streamReader) {
|
@@ -347,12 +362,19 @@ protected Object unmarshalInputStream(InputStream inputStream) throws XmlMapping
|
347 | 362 | }
|
348 | 363 |
|
349 | 364 | protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
|
350 |
| - return unmarshal(new XppReader(reader)); |
| 365 | + if (streamDriver != null) { |
| 366 | + return unmarshal(streamDriver.createReader(reader)); |
| 367 | + } |
| 368 | + else { |
| 369 | + return unmarshal(new XppReader(reader)); |
| 370 | + } |
351 | 371 | }
|
352 | 372 |
|
353 | 373 | protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
354 | 374 | throws XmlMappingException, IOException {
|
355 | 375 | throw new UnsupportedOperationException(
|
356 | 376 | "XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
|
357 | 377 | }
|
| 378 | + |
| 379 | + |
358 | 380 | }
|
0 commit comments