Skip to content

Commit 100fa17

Browse files
committed
XStreamMarshaller should convert XStream StreamException to Spring exception in case of unmarshalling an empty stream.
Issue: SPR-9536
1 parent a387d13 commit 100fa17

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -202,7 +202,7 @@ else if (value instanceof String) {
202202
* @param aliases
203203
* @throws ClassNotFoundException
204204
* @throws NoSuchFieldException
205-
* @see XStream#aliasField(String, Class, String)
205+
* @see XStream#aliasField(String, Class, String)
206206
*/
207207
public void setFieldAliases(Map<String, String> aliases) throws ClassNotFoundException, NoSuchFieldException {
208208
for (Map.Entry<String, String> entry : aliases.entrySet()) {
@@ -483,7 +483,12 @@ else if (node instanceof Element) {
483483
else {
484484
throw new IllegalArgumentException("DOMSource contains neither Document nor Element");
485485
}
486-
return unmarshal(streamReader);
486+
try {
487+
return getXStream().unmarshal(streamReader);
488+
}
489+
catch (Exception ex) {
490+
throw convertXStreamException(ex, false);
491+
}
487492
}
488493

489494
@Override
@@ -499,7 +504,14 @@ protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlM
499504

500505
@Override
501506
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
502-
return unmarshal(new StaxReader(new QNameMap(), streamReader));
507+
try {
508+
HierarchicalStreamReader hierarchicalStreamReader =
509+
new StaxReader(new QNameMap(),streamReader);
510+
return getXStream().unmarshal(hierarchicalStreamReader);
511+
}
512+
catch (Exception ex) {
513+
throw convertXStreamException(ex, false);
514+
}
503515
}
504516

505517
@Override
@@ -509,11 +521,18 @@ protected Object unmarshalInputStream(InputStream inputStream) throws XmlMapping
509521

510522
@Override
511523
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
512-
if (streamDriver != null) {
513-
return unmarshal(streamDriver.createReader(reader));
524+
try {
525+
HierarchicalStreamReader streamReader;
526+
if (this.streamDriver != null) {
527+
streamReader = this.streamDriver.createReader(reader);
528+
}
529+
else {
530+
streamReader = new XppReader(reader);
531+
}
532+
return getXStream().unmarshal(streamReader);
514533
}
515-
else {
516-
return unmarshal(new XppReader(reader));
534+
catch (Exception ex) {
535+
throw convertXStreamException(ex, false);
517536
}
518537
}
519538

@@ -525,16 +544,6 @@ protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource
525544
"XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
526545
}
527546

528-
private Object unmarshal(HierarchicalStreamReader streamReader) {
529-
try {
530-
return this.getXStream().unmarshal(streamReader);
531-
}
532-
catch (Exception ex) {
533-
throw convertXStreamException(ex, false);
534-
}
535-
}
536-
537-
538547
/**
539548
* Convert the given XStream exception to an appropriate exception from the
540549
* <code>org.springframework.oxm</code> hierarchy.

0 commit comments

Comments
 (0)