OPEN - issue INT-310: XML Document Builders Not Namespace Aware
set default on document builders to be namespace aware
This commit is contained in:
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
@@ -52,12 +50,7 @@ public class ResultFactoryResultTypeHelper {
|
||||
builder.addPropertyReference("resultFactory", resultFactory);
|
||||
}
|
||||
else if (resultType.equals(DOM_RESULT) || !StringUtils.hasText(resultType)) {
|
||||
try {
|
||||
builder.addPropertyValue("resultFactory", new DomResultFactory());
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
throw new org.springframework.integration.ConfigurationException("Exception creating DomResultFactory");
|
||||
}
|
||||
builder.addPropertyValue("resultFactory", new DomResultFactory());
|
||||
}
|
||||
else if (resultType.equals(STRING_RESULT)) {
|
||||
builder.addPropertyValue("resultFactory", new StringResultFactory());
|
||||
|
||||
@@ -22,23 +22,39 @@ import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class DomResultFactory implements ResultFactory {
|
||||
|
||||
private final DocumentBuilder documentBuilder;
|
||||
|
||||
public DomResultFactory(DocumentBuilder documentBuilder){
|
||||
this.documentBuilder = documentBuilder;
|
||||
private final DocumentBuilderFactory docBuilderFactory;
|
||||
|
||||
public DomResultFactory(DocumentBuilderFactory docBuilderFactory) {
|
||||
this.docBuilderFactory = docBuilderFactory;
|
||||
}
|
||||
|
||||
public DomResultFactory() throws ParserConfigurationException{
|
||||
this(DocumentBuilderFactory.newInstance().newDocumentBuilder());
|
||||
}
|
||||
|
||||
|
||||
public DomResultFactory() {
|
||||
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
docBuilderFactory.setNamespaceAware(true);
|
||||
}
|
||||
|
||||
public synchronized Result createResult(Object payload) {
|
||||
return new DOMResult(documentBuilder.newDocument());
|
||||
try {
|
||||
return new DOMResult(getNewDocumentBuilder().newDocument());
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
throw new MessagingException("Failed to create Result for payload type [" + payload.getClass().getName()
|
||||
+ "]");
|
||||
}
|
||||
}
|
||||
|
||||
protected DocumentBuilder getNewDocumentBuilder() throws ParserConfigurationException {
|
||||
synchronized (docBuilderFactory) {
|
||||
return docBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,26 +41,26 @@ public class DomSourceFactory implements SourceFactory {
|
||||
|
||||
public DomSourceFactory() {
|
||||
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
this.docBuilderFactory.setNamespaceAware(true);
|
||||
}
|
||||
|
||||
public DomSourceFactory(DocumentBuilderFactory docBuilderFactory) {
|
||||
this.docBuilderFactory = docBuilderFactory;
|
||||
}
|
||||
|
||||
|
||||
public Source createSource(Object payload) {
|
||||
Source source = null;
|
||||
if (payload instanceof Document) {
|
||||
source = createDomSourceForDocument((Document) payload);
|
||||
source = createDomSourceForDocument((Document) payload);
|
||||
}
|
||||
else if (payload instanceof String) {
|
||||
source = createDomSourceForString((String) payload);
|
||||
}
|
||||
|
||||
if(source == null){
|
||||
throw new MessagingException("Failed to create Source for payload type ["
|
||||
+ payload.getClass().getName() + "]");
|
||||
}
|
||||
|
||||
if (source == null) {
|
||||
throw new MessagingException("Failed to create Source for payload type [" + payload.getClass().getName()
|
||||
+ "]");
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -74,15 +74,16 @@ public class DomSourceFactory implements SourceFactory {
|
||||
Document doc = getNewDocumentBuilder().parse(new InputSource(new StringReader(s)));
|
||||
DOMSource source = new DOMSource(doc.getDocumentElement());
|
||||
return source;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException("Exception creating DOMSource", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected DocumentBuilder getNewDocumentBuilder() throws ParserConfigurationException{
|
||||
protected DocumentBuilder getNewDocumentBuilder() throws ParserConfigurationException {
|
||||
synchronized (docBuilderFactory) {
|
||||
return docBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,25 +40,26 @@ public class ResultToDocumentTransformer implements ResultTransformer {
|
||||
// Not guaranteed to be thread safe
|
||||
private final DocumentBuilderFactory documentBuilderFactory;
|
||||
|
||||
public ResultToDocumentTransformer(
|
||||
DocumentBuilderFactory documentBuilderFactory) {
|
||||
public ResultToDocumentTransformer(DocumentBuilderFactory documentBuilderFactory) {
|
||||
this.documentBuilderFactory = documentBuilderFactory;
|
||||
}
|
||||
|
||||
public ResultToDocumentTransformer() {
|
||||
this(DocumentBuilderFactory.newInstance());
|
||||
this.documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
this.documentBuilderFactory.setNamespaceAware(true);
|
||||
}
|
||||
|
||||
public Object transformResult(Result res) {
|
||||
Document doc = null;
|
||||
if (DOMResult.class.isAssignableFrom(res.getClass())) {
|
||||
doc = createDocumentFromDomResult((DOMResult) res);
|
||||
} else if (StringResult.class.isAssignableFrom(res.getClass())) {
|
||||
}
|
||||
else if (StringResult.class.isAssignableFrom(res.getClass())) {
|
||||
doc = createDocumentFromStringResult((StringResult) res);
|
||||
} else {
|
||||
throw new MessagingException(
|
||||
"Failed to create document from payload type ["
|
||||
+ res.getClass().getName() + "]");
|
||||
}
|
||||
else {
|
||||
throw new MessagingException("Failed to create document from payload type [" + res.getClass().getName()
|
||||
+ "]");
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
@@ -69,20 +70,19 @@ public class ResultToDocumentTransformer implements ResultTransformer {
|
||||
|
||||
protected Document createDocumentFromStringResult(StringResult stringResult) {
|
||||
try {
|
||||
return getDocumentBuilder().parse(
|
||||
new InputSource(new StringReader(stringResult.toString())));
|
||||
} catch (Exception e) {
|
||||
throw new MessagingException(
|
||||
"Failed to create Document from StringResult payload", e);
|
||||
return getDocumentBuilder().parse(new InputSource(new StringReader(stringResult.toString())));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException("Failed to create Document from StringResult payload", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected synchronized DocumentBuilder getDocumentBuilder() {
|
||||
try {
|
||||
return this.documentBuilderFactory.newDocumentBuilder();
|
||||
} catch (ParserConfigurationException e) {
|
||||
throw new MessagingException(
|
||||
"Failed to create a new DocumentBuilder", e);
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
throw new MessagingException("Failed to create a new DocumentBuilder", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ public class ResultToStringTransformer implements ResultTransformer {
|
||||
|
||||
public ResultToStringTransformer() {
|
||||
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
this.docBuilderFactory.setNamespaceAware(true);
|
||||
this.transformerFactory = TransformerFactory.newInstance();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user