Skip to content

Commit 7616af7

Browse files
committed
SWS-235
1 parent 4ffe9c6 commit 7616af7

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

oxm/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,11 @@
180180
<version>1.2_Java1.3</version>
181181
<scope>test</scope>
182182
</dependency>
183+
<dependency>
184+
<groupId>org.codehaus.jettison</groupId>
185+
<artifactId>jettison</artifactId>
186+
<version>1.0-RC2</version>
187+
<scope>test</scope>
188+
</dependency>
183189
</dependencies>
184190
</project>

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.thoughtworks.xstream.converters.Converter;
3636
import com.thoughtworks.xstream.converters.ConverterMatcher;
3737
import com.thoughtworks.xstream.converters.SingleValueConverter;
38+
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
3839
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
3940
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
4041
import com.thoughtworks.xstream.io.xml.CompactWriter;
@@ -45,12 +46,6 @@
4546
import com.thoughtworks.xstream.io.xml.StaxReader;
4647
import com.thoughtworks.xstream.io.xml.StaxWriter;
4748
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;
5449
import org.w3c.dom.Document;
5550
import org.w3c.dom.Element;
5651
import org.w3c.dom.Node;
@@ -59,6 +54,13 @@
5954
import org.xml.sax.XMLReader;
6055
import org.xml.sax.ext.LexicalHandler;
6156

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+
6264
/**
6365
* Implementation of the <code>Marshaller</code> interface for XStream. By default, XStream does not require any further
6466
* 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 {
8890

8991
private Class[] supportedClasses;
9092

93+
/** Specialized driver to be used with stream readers and writers */
94+
private HierarchicalStreamDriver streamDriver;
95+
9196
/**
9297
* Returns the encoding to be used for stream access. If this property is not set, the default encoding is used.
9398
*
@@ -153,6 +158,11 @@ else if (converters[i] instanceof SingleValueConverter) {
153158
}
154159
}
155160

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+
156166
/**
157167
* Set a alias/type map, consisting of string aliases mapped to <code>Class</code> instances (or Strings to be
158168
* converted to <code>Class</code> instances).
@@ -302,7 +312,12 @@ protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, L
302312
}
303313

304314
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+
}
306321
}
307322

308323
private Object unmarshal(HierarchicalStreamReader streamReader) {
@@ -347,12 +362,19 @@ protected Object unmarshalInputStream(InputStream inputStream) throws XmlMapping
347362
}
348363

349364
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+
}
351371
}
352372

353373
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
354374
throws XmlMappingException, IOException {
355375
throw new UnsupportedOperationException(
356376
"XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
357377
}
378+
379+
358380
}

oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@
3232

3333
import com.thoughtworks.xstream.converters.Converter;
3434
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
35+
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
3536
import org.custommonkey.xmlunit.XMLTestCase;
3637
import org.easymock.MockControl;
37-
import org.springframework.xml.transform.StaxResult;
38-
import org.springframework.xml.transform.StringResult;
39-
import org.springframework.xml.transform.StringSource;
4038
import org.w3c.dom.Document;
4139
import org.w3c.dom.Element;
4240
import org.w3c.dom.Text;
4341
import org.xml.sax.ContentHandler;
4442

43+
import org.springframework.xml.transform.StaxResult;
44+
import org.springframework.xml.transform.StringResult;
45+
import org.springframework.xml.transform.StringSource;
46+
4547
public class XStreamMarshallerTest extends XMLTestCase {
4648

4749
private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
@@ -161,5 +163,16 @@ public void testUseAttributesForClassStringMap() throws Exception {
161163
assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
162164
}
163165

166+
public void testDriver() throws Exception {
167+
marshaller.setStreamDriver(new JettisonMappedXmlDriver());
168+
StringResult result = new StringResult();
169+
marshaller.marshal(flight, result);
170+
assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":\"42\"}}", result.toString());
171+
Object o = marshaller.unmarshal(new StringSource(result.toString()));
172+
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
173+
Flight unflight = (Flight) o;
174+
assertNotNull("Flight is null", unflight);
175+
assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
176+
}
164177

165178
}

0 commit comments

Comments
 (0)