resolve INT-835

This commit is contained in:
Jonas Partner
2009-10-07 20:39:04 +00:00
parent 26a03f2c1e
commit fbac7b1c13
2 changed files with 64 additions and 51 deletions

View File

@@ -33,67 +33,72 @@ import org.xml.sax.InputSource;
/**
* Default implementation of {@link XmlPayloadConverter}.
* Supports {@link Document} and {@link String}.
*
*
* @author Jonas Partner
*/
public class DefaultXmlPayloadConverter implements XmlPayloadConverter {
private DocumentBuilderFactory documentBuilderFactory;
private DocumentBuilderFactory documentBuilderFactory;
public DefaultXmlPayloadConverter() {
this.documentBuilderFactory = DocumentBuilderFactory.newInstance();
this.documentBuilderFactory.setNamespaceAware(true);
}
public DefaultXmlPayloadConverter() {
this.documentBuilderFactory = DocumentBuilderFactory.newInstance();
this.documentBuilderFactory.setNamespaceAware(true);
}
public DefaultXmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory) {
this.documentBuilderFactory = documentBuilderFactory;
}
public DefaultXmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory) {
this.documentBuilderFactory = documentBuilderFactory;
}
public Document convertToDocument(Object object) {
if (object instanceof Document) {
return (Document) object;
}
if (object instanceof String) {
try {
return getDocumentBuilder().parse(new InputSource(new StringReader((String) object)));
}
catch (Exception e) {
throw new MessagingException("failed to parse String payload '" + object + "'", e);
}
}
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
}
public Document convertToDocument(Object object) {
if (object instanceof Document) {
return (Document) object;
}
if (object instanceof String) {
try {
return getDocumentBuilder().parse(new InputSource(new StringReader((String) object)));
}
catch (Exception e) {
throw new MessagingException("failed to parse String payload '" + object + "'", e);
}
}
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
}
public Node convertToNode(Object object) {
if (object instanceof Node){
return (Node) object;
}
return convertToDocument(object);
}
public Source convertToSource(Object object){
Source source;
if(object instanceof Source){
source = (Source)object;
} else if (object instanceof Document){
source = new DOMSource((Document)object);
} else if (object instanceof String){
source = new StringSource((String)object);
} else {
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
}
return source;
}
public Node convertToNode(Object object) {
Node n = null;
if (object instanceof Node) {
n = (Node) object;
} else if (object instanceof DOMSource) {
n = ((DOMSource) object).getNode();
} else {
n = convertToDocument(object);
}
return n;
}
protected synchronized DocumentBuilder getDocumentBuilder() {
try {
return this.documentBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
throw new MessagingException("failed to create a new DocumentBuilder", e);
}
}
public Source convertToSource(Object object) {
Source source;
if (object instanceof Source) {
source = (Source) object;
} else if (object instanceof Document) {
source = new DOMSource((Document) object);
} else if (object instanceof String) {
source = new StringSource((String) object);
} else {
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
}
return source;
}
protected synchronized DocumentBuilder getDocumentBuilder() {
try {
return this.documentBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
throw new MessagingException("failed to create a new DocumentBuilder", e);
}
}
}

View File

@@ -112,4 +112,12 @@ public class DefaultXmlPayloadConverterTests {
converter.convertToSource(12);
}
@Test
public void testGetNodePassingDOMSource(){
Node element = (Node) testDocument.getElementsByTagName("test").item(0);
Node n = converter.convertToNode(new DOMSource(element));
assertTrue("Wrong node returned", element == n);
}
}