Skip to content

Commit f72c431

Browse files
committed
SPR-7257 - AbstractMarshaller incorrectly expects DOMResult to already have a node
1 parent 0dc29cb commit f72c431

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

org.springframework.oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -189,7 +189,21 @@ protected XMLReader createXmlReader() throws SAXException {
189189
* @see #marshalDomNode(Object, org.w3c.dom.Node)
190190
*/
191191
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException {
192-
Assert.notNull(domResult.getNode(), "DOMResult does not contain Node");
192+
if (domResult.getNode() == null) {
193+
try {
194+
synchronized (this.documentBuilderFactoryMonitor) {
195+
if (this.documentBuilderFactory == null) {
196+
this.documentBuilderFactory = createDocumentBuilderFactory();
197+
}
198+
}
199+
DocumentBuilder documentBuilder = createDocumentBuilder(this.documentBuilderFactory);
200+
domResult.setNode(documentBuilder.newDocument());
201+
}
202+
catch (ParserConfigurationException ex) {
203+
throw new UnmarshallingFailureException(
204+
"Could not create document placeholder for DOMResult: " + ex.getMessage(), ex);
205+
}
206+
}
193207
marshalDomNode(graph, domResult.getNode());
194208
}
195209

org.springframework.oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,8 +28,9 @@
2828
import javax.xml.transform.stax.StAXResult;
2929
import javax.xml.transform.stream.StreamResult;
3030

31-
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
31+
import static org.custommonkey.xmlunit.XMLAssert.*;
3232
import org.custommonkey.xmlunit.XMLUnit;
33+
import static org.junit.Assert.assertTrue;
3334
import org.junit.Before;
3435
import org.junit.Test;
3536
import org.w3c.dom.Attr;
@@ -86,6 +87,30 @@ public void marshalDOMResult() throws Exception {
8687
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
8788
}
8889

90+
@Test
91+
public void marshalEmptyDOMResult() throws Exception {
92+
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
93+
documentBuilderFactory.setNamespaceAware(true);
94+
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
95+
DOMResult domResult = new DOMResult();
96+
marshaller.marshal(flights, domResult);
97+
assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document);
98+
Document result = (Document) domResult.getNode();
99+
Document expected = builder.newDocument();
100+
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
101+
Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
102+
namespace.setNodeValue("http://samples.springframework.org/flight");
103+
flightsElement.setAttributeNode(namespace);
104+
expected.appendChild(flightsElement);
105+
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
106+
flightsElement.appendChild(flightElement);
107+
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
108+
flightElement.appendChild(numberElement);
109+
Text text = expected.createTextNode("42");
110+
numberElement.appendChild(text);
111+
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
112+
}
113+
89114
@Test
90115
public void marshalStreamResultWriter() throws Exception {
91116
StringWriter writer = new StringWriter();

0 commit comments

Comments
 (0)