Fixed #SWS-72: Pretty print JIBX output
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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 <code>-1</code>, 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 (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code> 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 <code>IMarshallingContext</code>, 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 <code>IUnmarshallingContext</code>, set with the correct indentation.
|
||||
*
|
||||
* @return the created unmarshalling context
|
||||
* @throws JiBXException in case of errors
|
||||
*/
|
||||
protected IUnmarshallingContext createUnmarshallingContext() throws JiBXException {
|
||||
return bindingFactory.createUnmarshallingContext();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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 = "<?xml version=\"1.0\"?>\n" +
|
||||
"<flights xmlns=\"http://samples.springframework.org/flight\">\n" +
|
||||
" <flight>\n" +
|
||||
" <number>42</number>\n" +
|
||||
" </flight>\n" +
|
||||
"</flights>";
|
||||
assertXMLEqual(expected, result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user