Skip to content

Commit

Permalink
Change default driver in XStreamMarshaller from XppDriver to DomDriver
Browse files Browse the repository at this point in the history
As explained in commit a247b83, the
XppDriver from XStream relies on the XPP3 library which publishes
javax.xml.namespace.QName as part of its JAR. The QName type is also
published by the java.xml system module in modular JREs (i.e., Java 9
or higher).

This results in a split package between the unnamed module and the
java.xml system module, which the Java Language Specification defines
as illegal (see §6.5.5.2 and §7.4.3).

Most Java compilers do not currently enforce this rule; however, the
Eclipse compiler does. This makes it impossible to use spring-oxm out
of the box in the Eclipse IDE. In addition, if bug JDK-8215739 is fixed
in future versions of other JDK implementations, this rule will affect
any users using spring-oxm with those JDKs.

This commit therefore switches the default driver in XStreamMarshaller
from XppDriver to DomDriver. Users can naturally switch back to the
XppDriver if they wish, since the driver is configurable.

Closes gh-27464
  • Loading branch information
sbrannen committed Sep 25, 2021
1 parent 50f2016 commit 30efa4d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.DomReader;
import com.thoughtworks.xstream.io.xml.DomWriter;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.SaxWriter;
import com.thoughtworks.xstream.io.xml.StaxReader;
import com.thoughtworks.xstream.io.xml.StaxWriter;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;
Expand Down Expand Up @@ -112,6 +112,11 @@
* Note that {@link XStream} construction has been reworked in 4.0, with the
* stream driver and the class loader getting passed into XStream itself now.
*
* <p>As of Spring Framework 6.0, the default {@link HierarchicalStreamDriver} is
* a {@link DomDriver} that uses the configured {@linkplain #setEncoding(String)
* encoding} and {@link #setNameCoder(NameCoder) NameCoder}. The driver can be
* changed via {@link #setStreamDriver(HierarchicalStreamDriver)}.
*
* @author Peter Meijer
* @author Arjen Poutsma
* @author Juergen Hoeller
Expand Down Expand Up @@ -205,7 +210,7 @@ public void setReflectionProvider(ReflectionProvider reflectionProvider) {
}

/**
* Set a XStream {@link HierarchicalStreamDriver} to be used for readers and writers.
* Set an XStream {@link HierarchicalStreamDriver} to be used for readers and writers.
* <p>As of Spring 4.0, this stream driver will also be passed to the {@link XStream}
* constructor and therefore used by streaming-related native API methods themselves.
*/
Expand All @@ -216,7 +221,7 @@ public void setStreamDriver(HierarchicalStreamDriver streamDriver) {

private HierarchicalStreamDriver getDefaultDriver() {
if (this.defaultDriver == null) {
this.defaultDriver = new XppDriver();
this.defaultDriver = new DomDriver(this.encoding, this.nameCoder);
}
return this.defaultDriver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.security.AnyTypePermission;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -86,7 +84,6 @@ void createMarshaller() {
marshaller = new XStreamMarshaller();
marshaller.setTypePermissions(AnyTypePermission.ANY);
marshaller.setAliases(Collections.singletonMap("flight", Flight.class.getName()));
marshaller.setStreamDriver(new DomDriver("UTF-8", new XmlFriendlyNameCoder()));
flight.setFlightNumber(42L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;

import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.security.AnyTypePermission;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -57,7 +55,6 @@ public class XStreamUnmarshallerTests {
public void createUnmarshaller() {
unmarshaller = new XStreamMarshaller();
unmarshaller.setTypePermissions(AnyTypePermission.ANY);
unmarshaller.setStreamDriver(new DomDriver("UTF-8", new XmlFriendlyNameCoder()));
Map<String, Class<?>> aliases = new HashMap<>();
aliases.put("flight", Flight.class);
unmarshaller.setAliases(aliases);
Expand Down

0 comments on commit 30efa4d

Please sign in to comment.