From 8bc229c6eb1575536b90ab546bddc5b97a30e5a0 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 23 Jul 2008 14:03:32 +0000 Subject: [PATCH] SWS-224 --- .../oxm/xstream/XStreamMarshaller.java | 50 ++++++++++++++++++- .../oxm/xstream/XStreamMarshallerTest.java | 16 ++++++ 2 files changed, 65 insertions(+), 1 deletion(-) 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 ca5a8e60..a3ef3951 100644 --- a/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -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 Class * instances (or Strings to be converted to Class 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 Class instances (or Strings to be converted to + * Class 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 * convertXStreamException. @@ -353,6 +397,10 @@ public class XStreamMarshaller extends AbstractMarshaller { } } + // + // Unmarshalling + // + private Object unmarshal(HierarchicalStreamReader streamReader) { try { return getXStream().unmarshal(streamReader); 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 0dda2fb7..e7e94146 100644 --- a/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java +++ b/oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTest.java @@ -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();