diff --git a/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java
index b6f6b23b..f3508cf4 100644
--- a/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java
+++ b/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java
@@ -103,6 +103,11 @@ public class XStreamMarshaller extends AbstractMarshaller {
this.encoding = encoding;
}
+ /** Returns the XStream instance used by this marshaller. */
+ public XStream getXStream() {
+ return xstream;
+ }
+
/**
* Sets the XStream mode.
*
@@ -111,7 +116,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
* @see XStream#NO_REFERENCES
*/
public void setMode(int mode) {
- xstream.setMode(mode);
+ getXStream().setMode(mode);
}
/**
@@ -124,10 +129,10 @@ public class XStreamMarshaller extends AbstractMarshaller {
public void setConverters(ConverterMatcher[] converters) {
for (int i = 0; i < converters.length; i++) {
if (converters[i] instanceof Converter) {
- xstream.registerConverter((Converter) converters[i], i);
+ getXStream().registerConverter((Converter) converters[i], i);
}
else if (converters[i] instanceof SingleValueConverter) {
- xstream.registerConverter((SingleValueConverter) converters[i], i);
+ getXStream().registerConverter((SingleValueConverter) converters[i], i);
}
else {
throw new IllegalArgumentException("Invalid ConverterMatcher [" + converters[i] + "]");
@@ -145,7 +150,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
// Check whether we need to convert from String to Class.
- Class type = null;
+ Class type;
if (entry.getValue() instanceof Class) {
type = (Class) entry.getValue();
}
@@ -165,7 +170,39 @@ public class XStreamMarshaller extends AbstractMarshaller {
* @param type the type to be aliased
*/
public void addAlias(String name, Class type) {
- xstream.alias(name, type);
+ getXStream().alias(name, type);
+ }
+
+ /**
+ * Sets types to use XML attributes for.
+ *
+ * @see XStream#useAttributeFor(Class)
+ */
+ public void setUseAttributeForTypes(Class[] types) {
+ for (int i = 0; i < types.length; i++) {
+ getXStream().useAttributeFor(types[i]);
+ }
+ }
+
+ /**
+ * Sets the types to use XML attributes for. The given map can contain either <String, Class>
+ * pairs, in which case {@link XStream#useAttributeFor(String,Class)} is called, or <Class,
+ * String> pairs, which results in {@link XStream#useAttributeFor(Class,String)}.
+ */
+ public void setUseAttributeFor(Map attributes) {
+ for (Iterator iterator = attributes.entrySet().iterator(); iterator.hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ if (entry.getKey() instanceof String && entry.getValue() instanceof Class) {
+ getXStream().useAttributeFor((String) entry.getKey(), (Class) entry.getValue());
+ }
+ else if (entry.getKey() instanceof Class && entry.getValue() instanceof String) {
+ getXStream().useAttributeFor((Class) entry.getKey(), (String) entry.getValue());
+ }
+ else {
+ throw new IllegalArgumentException("Invalid attribute key and value pair. " +
+ "'useAttributesFor' property takes either a map or a map");
+ }
+ }
}
public boolean supports(Class clazz) {
@@ -194,7 +231,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
*/
private void marshal(Object graph, HierarchicalStreamWriter streamWriter) {
try {
- xstream.marshal(graph, streamWriter);
+ getXStream().marshal(graph, streamWriter);
}
catch (Exception ex) {
throw convertXStreamException(ex, true);
@@ -202,7 +239,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
}
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
- HierarchicalStreamWriter streamWriter = null;
+ HierarchicalStreamWriter streamWriter;
if (node instanceof Document) {
streamWriter = new DomWriter((Document) node);
}
@@ -247,7 +284,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
private Object unmarshal(HierarchicalStreamReader streamReader) {
try {
- return xstream.unmarshal(streamReader);
+ return getXStream().unmarshal(streamReader);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
@@ -255,7 +292,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
}
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
- HierarchicalStreamReader streamReader = null;
+ HierarchicalStreamReader streamReader;
if (node instanceof Document) {
streamReader = new DomReader((Document) node);
}
diff --git a/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java
index 5b678af8..e42b86ee 100644
--- a/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java
+++ b/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java
@@ -19,8 +19,8 @@ package org.springframework.oxm.xstream;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Properties;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventWriter;
@@ -34,15 +34,14 @@ import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import org.custommonkey.xmlunit.XMLTestCase;
import org.easymock.MockControl;
+import org.springframework.xml.transform.StaxResult;
+import org.springframework.xml.transform.StringResult;
+import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.ContentHandler;
-import org.springframework.xml.transform.StaxResult;
-import org.springframework.xml.transform.StringResult;
-import org.springframework.xml.transform.StringSource;
-
public class XStreamMarshallerTest extends XMLTestCase {
private static final String EXPECTED_STRING = "42";
@@ -138,5 +137,29 @@ public class XStreamMarshallerTest extends XMLTestCase {
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
}
+ public void testUseAttributesFor() throws Exception {
+ marshaller.setUseAttributeForTypes(new Class[]{Long.TYPE});
+ StringResult result = new StringResult();
+ marshaller.marshal(flight, result);
+ String expected = "";
+ assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
+ }
+
+ public void testUseAttributesForStringClassMap() throws Exception {
+ marshaller.setUseAttributeFor(Collections.singletonMap("flightNumber", Long.TYPE));
+ StringResult result = new StringResult();
+ marshaller.marshal(flight, result);
+ String expected = "";
+ assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
+ }
+
+ public void testUseAttributesForClassStringMap() throws Exception {
+ marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, "flightNumber"));
+ StringResult result = new StringResult();
+ marshaller.marshal(flight, result);
+ String expected = "";
+ assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
+ }
+
}
diff --git a/pom.xml b/pom.xml
index 4399f8df..c125256a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -542,7 +542,7 @@
com.thoughtworks.xstream
xstream
- 1.2.1
+ 1.2.2