diff --git a/changelog.txt b/changelog.txt
index 74eadf6f..74e81e56 100644
--- a/changelog.txt
+++ b/changelog.txt
@@ -11,6 +11,9 @@ Package org.springframework.oxm.castor
* CastorMarshaller uses a XMLClassDescriptorResolver instead of a Mapping (#SWS-54)
* CastorMarshaller marshals to DOM trees correctly (#SWS-59)
+Package org.springframework.oxm.jibx
+* added indent property to pretty print JiBX output (#SWS-72)
+
Package org.springframework.ws.context
* Refactored MessageContextFactory, SoapMessageContextFactory and implementations into WebServiceMessageFactory
diff --git a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java
index 05708a77..912abec2 100644
--- a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java
+++ b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java
@@ -82,20 +82,23 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
private TransformerFactory transfomerFactory;
- /**
- * Sets the optional binding name for this instance.
- */
+ private int indent = -1;
+
+ /** Sets the optional binding name for this instance. */
public void setBindingName(String bindingName) {
this.bindingName = bindingName;
}
- /**
- * Sets the target class for this instance. This property is required.
- */
+ /** Sets the target class for this instance. This property is required. */
public void setTargetClass(Class targetClass) {
this.targetClass = targetClass;
}
+ /** Sets the number of nesting indent spaces. Default is -1, i.e. no indentation. */
+ public void setIndent(int indent) {
+ this.indent = indent;
+ }
+
public void afterPropertiesSet() throws Exception {
Assert.notNull(targetClass, "targetClass is required");
if (logger.isInfoEnabled()) {
@@ -133,7 +136,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
* @param marshalling indicates whether the exception occurs during marshalling (true), or
* unmarshalling (false)
* @return the corresponding XmlMappingException instance
- * @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
+ * @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException,boolean)
*/
public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) {
return JibxUtils.convertJibxException(ex, marshalling);
@@ -158,7 +161,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected void marshalOutputStream(Object graph, OutputStream outputStream)
throws XmlMappingException, IOException {
try {
- IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
+ IMarshallingContext marshallingContext = createMarshallingContext();
marshallingContext.marshalDocument(graph, null, null, outputStream);
}
catch (JiBXException ex) {
@@ -185,7 +188,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
try {
- IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
+ IMarshallingContext marshallingContext = createMarshallingContext();
marshallingContext.marshalDocument(graph, null, null, writer);
}
catch (JiBXException ex) {
@@ -201,7 +204,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
try {
- MarshallingContext marshallingContext = (MarshallingContext) bindingFactory.createMarshallingContext();
+ MarshallingContext marshallingContext = (MarshallingContext) createMarshallingContext();
IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter);
marshallingContext.setXmlWriter(xmlWriter);
marshallingContext.marshalDocument(graph);
@@ -229,7 +232,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
try {
- IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
+ IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
return unmarshallingContext.unmarshalDocument(inputStream, null);
}
catch (JiBXException ex) {
@@ -239,7 +242,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
try {
- IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
+ IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
return unmarshallingContext.unmarshalDocument(reader);
}
catch (JiBXException ex) {
@@ -276,8 +279,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
try {
- UnmarshallingContext unmarshallingContext =
- (UnmarshallingContext) bindingFactory.createUnmarshallingContext();
+ UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
unmarshallingContext.setDocument(xmlReader);
return unmarshallingContext.unmarshalElement();
@@ -286,4 +288,28 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
throw convertJibxException(ex, false);
}
}
+
+ /**
+ * Creates a new IMarshallingContext, set with the correct indentation.
+ *
+ * @return the created marshalling context
+ * @throws JiBXException in case of errors
+ */
+ protected IMarshallingContext createMarshallingContext() throws JiBXException {
+ IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
+ marshallingContext.setIndent(indent);
+ return marshallingContext;
+ }
+
+ /**
+ * Creates a new IUnmarshallingContext, set with the correct indentation.
+ *
+ * @return the created unmarshalling context
+ * @throws JiBXException in case of errors
+ */
+ protected IUnmarshallingContext createUnmarshallingContext() throws JiBXException {
+ return bindingFactory.createUnmarshallingContext();
+ }
+
+
}
diff --git a/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java
index 8b8b774c..199cfdf1 100644
--- a/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java
+++ b/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java
@@ -18,6 +18,8 @@ package org.springframework.oxm.jibx;
import org.springframework.oxm.AbstractMarshallerTestCase;
import org.springframework.oxm.Marshaller;
+import org.springframework.xml.transform.StringResult;
+import org.custommonkey.xmlunit.XMLUnit;
public class JibxMarshallerTest extends AbstractMarshallerTestCase {
@@ -46,4 +48,18 @@ public class JibxMarshallerTest extends AbstractMarshallerTestCase {
}
}
+ public void testIndentation() throws Exception {
+ ((JibxMarshaller) marshaller).setIndent(4);
+ StringResult result = new StringResult();
+ marshaller.marshal(flights, result);
+ XMLUnit.setIgnoreWhitespace(false);
+ String expected = "\n" +
+ "\n" +
+ " \n" +
+ " 42\n" +
+ " \n" +
+ "";
+ assertXMLEqual(expected, result.toString());
+ }
+
}