Browse Source

XStreamMarshaller supports stream directly

XStreamMarshaller now supports writing to OutputStreams and reading from
InputStreams directly, as opposed to wrapping streams in a
OutputStreamWriter or InputStreamReader.

Issue: SPR-9663
pull/120/head
Arjen Poutsma 14 years ago
parent
commit
7187a2435e
  1. 51
      spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java

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

@ -425,8 +425,13 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
@Override @Override
protected void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException { protected void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException {
if (this.streamDriver != null) {
marshal(graph, this.streamDriver.createWriter(outputStream));
}
else {
marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding)); marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding));
} }
}
@Override @Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
@ -483,12 +488,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
else { else {
throw new IllegalArgumentException("DOMSource contains neither Document nor Element"); throw new IllegalArgumentException("DOMSource contains neither Document nor Element");
} }
try { return unmarshal(streamReader);
return getXStream().unmarshal(streamReader);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
} }
@Override @Override
@ -504,35 +504,26 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
@Override @Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
try { return unmarshal(new StaxReader(new QNameMap(), streamReader));
HierarchicalStreamReader hierarchicalStreamReader =
new StaxReader(new QNameMap(),streamReader);
return getXStream().unmarshal(hierarchicalStreamReader);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
} }
@Override @Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
if (this.streamDriver != null) {
return unmarshal(this.streamDriver.createReader(inputStream));
}
else {
return unmarshalReader(new InputStreamReader(inputStream, this.encoding)); return unmarshalReader(new InputStreamReader(inputStream, this.encoding));
} }
}
@Override @Override
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
try {
HierarchicalStreamReader streamReader;
if (this.streamDriver != null) { if (this.streamDriver != null) {
streamReader = this.streamDriver.createReader(reader); return unmarshal(this.streamDriver.createReader(reader));
} }
else { else {
streamReader = new XppReader(reader); return unmarshal(new XppReader(reader));
}
return getXStream().unmarshal(streamReader);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
} }
} }
@ -544,6 +535,20 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
"XStreamMarshaller does not support unmarshalling using SAX XMLReaders"); "XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
} }
/**
* Unmarshals the given graph to the given XStream HierarchicalStreamWriter.
* Converts exceptions using {@link #convertXStreamException}.
*/
private Object unmarshal(HierarchicalStreamReader streamReader) {
try {
return getXStream().unmarshal(streamReader);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
}
/** /**
* Convert the given XStream exception to an appropriate exception from the * Convert the given XStream exception to an appropriate exception from the
* <code>org.springframework.oxm</code> hierarchy. * <code>org.springframework.oxm</code> hierarchy.

Loading…
Cancel
Save