SWS-224
This commit is contained in:
@@ -59,6 +59,7 @@ import org.springframework.beans.propertyeditors.ClassEditor;
|
||||
import org.springframework.oxm.AbstractMarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.stream.StaxEventContentHandler;
|
||||
import org.springframework.xml.stream.XmlEventStreamReader;
|
||||
|
||||
@@ -242,7 +243,7 @@ public class XStreamMarshaller extends AbstractMarshaller {
|
||||
* Set a implicit colletion/type map, consisting of string implicit collection mapped to <code>Class</code>
|
||||
* instances (or Strings to be converted to <code>Class</code> instances).
|
||||
*
|
||||
* @see org.springframework.beans.propertyeditors.ClassEditor
|
||||
* @see XStream#addImplicitCollection(Class, String)
|
||||
*/
|
||||
public void setImplicitCollection(Map implicitCollection) {
|
||||
for (Iterator iterator = implicitCollection.entrySet().iterator(); iterator.hasNext();) {
|
||||
@@ -261,6 +262,45 @@ public class XStreamMarshaller extends AbstractMarshaller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an omitted field for the given type.
|
||||
*
|
||||
* @param type the type to be containing the field
|
||||
* @param fieldName field to omitt
|
||||
* @see XStream#omitField(Class, String)
|
||||
*/
|
||||
public void addOmittedField(Class type, String fieldName) {
|
||||
xstream.omitField(type, fieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a ommited field map, consisting of <code>Class</code> instances (or Strings to be converted to
|
||||
* <code>Class</code> instances) mapped to comma separated field names.
|
||||
*
|
||||
* @see XStream#omitField(Class, String)
|
||||
*/
|
||||
public void setOmittedFields(Map omittedFields) {
|
||||
for (Iterator iterator = omittedFields.entrySet().iterator(); iterator.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) iterator.next();
|
||||
// Check whether we need to convert from String to Class.
|
||||
Class type;
|
||||
if (entry.getKey() instanceof Class) {
|
||||
type = (Class) entry.getKey();
|
||||
}
|
||||
else {
|
||||
ClassEditor editor = new ClassEditor();
|
||||
editor.setAsText(String.valueOf(entry.getKey()));
|
||||
type = (Class) editor.getValue();
|
||||
}
|
||||
// add each omitted field for the current type
|
||||
String fieldsString = (String) entry.getValue();
|
||||
String[] fields = StringUtils.commaDelimitedListToStringArray(fieldsString);
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
addOmittedField(type, fields[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
if (ObjectUtils.isEmpty(supportedClasses)) {
|
||||
return true;
|
||||
@@ -291,6 +331,10 @@ public class XStreamMarshaller extends AbstractMarshaller {
|
||||
return XStreamUtils.convertXStreamException(ex, marshalling);
|
||||
}
|
||||
|
||||
//
|
||||
// Marshalling
|
||||
//
|
||||
|
||||
/**
|
||||
* Marshals the given graph to the given XStream HierarchicalStreamWriter. Converts exceptions using
|
||||
* <code>convertXStreamException</code>.
|
||||
@@ -353,6 +397,10 @@ public class XStreamMarshaller extends AbstractMarshaller {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Unmarshalling
|
||||
//
|
||||
|
||||
private Object unmarshal(HierarchicalStreamReader streamReader) {
|
||||
try {
|
||||
return getXStream().unmarshal(streamReader);
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -191,6 +192,21 @@ public class XStreamMarshallerTest extends XMLTestCase {
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
|
||||
}
|
||||
|
||||
public void testOmitField() throws Exception {
|
||||
marshaller.addOmittedField(Flight.class, "flightNumber");
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
assertXpathNotExists("/flight/flightNumber", result.toString());
|
||||
}
|
||||
|
||||
public void testOmitFields() throws Exception {
|
||||
Map omittedFieldsMap = Collections.singletonMap(Flight.class, "flightNumber");
|
||||
marshaller.setOmittedFields(omittedFieldsMap);
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
assertXpathNotExists("/flight/flightNumber", result.toString());
|
||||
}
|
||||
|
||||
public void testDriver() throws Exception {
|
||||
marshaller.setStreamDriver(new JettisonMappedXmlDriver());
|
||||
StringResult result = new StringResult();
|
||||
|
||||
Reference in New Issue
Block a user