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 912abec2..dfe449a5 100644 --- a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -84,6 +84,10 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe private int indent = -1; + private String encoding; + + private Boolean standalone; + /** Sets the optional binding name for this instance. */ public void setBindingName(String bindingName) { this.bindingName = bindingName; @@ -99,6 +103,16 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe this.indent = indent; } + /** Sets the document encoding using for marshalling. Default is UTF-8. */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + /** Sets the document standalone flag for marshalling. By default, this flag is not present. */ + public void setStandalone(Boolean standalone) { + this.standalone = standalone; + } + public void afterPropertiesSet() throws Exception { Assert.notNull(targetClass, "targetClass is required"); if (logger.isInfoEnabled()) { @@ -142,8 +156,50 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe return JibxUtils.convertJibxException(ex, marshalling); } + // + // Supported Marshalling + // + + protected void marshalOutputStream(Object graph, OutputStream outputStream) + throws XmlMappingException, IOException { + try { + IMarshallingContext marshallingContext = createMarshallingContext(); + marshallingContext.marshalDocument(graph, encoding, standalone, outputStream); + } + catch (JiBXException ex) { + throw convertJibxException(ex, true); + } + } + + protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { + try { + IMarshallingContext marshallingContext = createMarshallingContext(); + marshallingContext.marshalDocument(graph, encoding, standalone, writer); + } + catch (JiBXException ex) { + throw convertJibxException(ex, true); + } + } + + protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { + try { + MarshallingContext marshallingContext = (MarshallingContext) createMarshallingContext(); + IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter); + marshallingContext.setXmlWriter(xmlWriter); + marshallingContext.marshalDocument(graph); + } + catch (JiBXException ex) { + throw convertJibxException(ex, false); + } + } + + // + // Unsupported Marshalling + // + protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { try { + // JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node ByteArrayOutputStream os = new ByteArrayOutputStream(); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); @@ -158,25 +214,17 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } - protected void marshalOutputStream(Object graph, OutputStream outputStream) - throws XmlMappingException, IOException { - try { - IMarshallingContext marshallingContext = createMarshallingContext(); - marshallingContext.marshalDocument(graph, null, null, outputStream); - } - catch (JiBXException ex) { - throw convertJibxException(ex, true); - } - } - protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException { try { + // JiBX does not support SAX natively, so we write to a buffer first, and transform that to the handlers ByteArrayOutputStream os = new ByteArrayOutputStream(); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = transfomerFactory.newTransformer(); - transformer.transform(new StreamSource(is), new SAXResult(contentHandler)); + SAXResult saxResult = new SAXResult(contentHandler); + saxResult.setLexicalHandler(lexicalHandler); + transformer.transform(new StreamSource(is), saxResult); } catch (IOException ex) { throw new JibxSystemException(ex); @@ -186,33 +234,14 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } - protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { - try { - IMarshallingContext marshallingContext = createMarshallingContext(); - marshallingContext.marshalDocument(graph, null, null, writer); - } - catch (JiBXException ex) { - throw convertJibxException(ex, true); - } - } - protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { - ContentHandler contentHandler = new StaxEventContentHandler(eventWriter); marshalSaxHandlers(graph, contentHandler, null); } - protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { - try { - MarshallingContext marshallingContext = (MarshallingContext) createMarshallingContext(); - IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter); - marshallingContext.setXmlWriter(xmlWriter); - marshallingContext.marshalDocument(graph); - } - catch (JiBXException ex) { - throw convertJibxException(ex, false); - } - } + // + // Unmarshalling + // protected Object unmarshalDomNode(Node node) throws XmlMappingException { try { 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 199cfdf1..98398380 100644 --- a/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java +++ b/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java @@ -16,10 +16,10 @@ package org.springframework.oxm.jibx; +import org.custommonkey.xmlunit.XMLUnit; 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 { @@ -54,12 +54,18 @@ public class JibxMarshallerTest extends AbstractMarshallerTestCase { marshaller.marshal(flights, result); XMLUnit.setIgnoreWhitespace(false); String expected = "\n" + - "\n" + - " \n" + - " 42\n" + - " \n" + - ""; + "\n" + " \n" + + " 42\n" + " \n" + ""; assertXMLEqual(expected, result.toString()); } + public void testEncodingAndStandalone() throws Exception { + ((JibxMarshaller) marshaller).setEncoding("ISO-8859-1"); + ((JibxMarshaller) marshaller).setStandalone(Boolean.TRUE); + StringResult result = new StringResult(); + marshaller.marshal(flights, result); + assertTrue("Encoding and standalone not set", + result.toString().startsWith("")); + } + }