polishing
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
/**
|
||||
*
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@@ -12,18 +26,20 @@ import java.util.List;
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class AggregatedXmlMessageValidationException extends RuntimeException {
|
||||
|
||||
|
||||
private final List<Throwable> exceptions;
|
||||
|
||||
public AggregatedXmlMessageValidationException(List<Throwable> exceptions){
|
||||
this.exceptions = exceptions;
|
||||
|
||||
public AggregatedXmlMessageValidationException(List<Throwable> exceptions) {
|
||||
this.exceptions = (exceptions != null) ? exceptions : Collections.<Throwable>emptyList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Will return iterator of exceptions aggregated by this Class.
|
||||
*
|
||||
* @return
|
||||
* Returns an Iterator for the aggregated Exceptions.
|
||||
*/
|
||||
public Iterator<Throwable> exceptionIterator(){
|
||||
public Iterator<Throwable> exceptionIterator() {
|
||||
return exceptions.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,89 +25,95 @@ import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link XmlPayloadConverter}.
|
||||
* Supports {@link Document} and {@link String}.
|
||||
*
|
||||
* Default implementation of {@link XmlPayloadConverter}. Supports
|
||||
* {@link Document}, {@link File} and {@link String} payloads.
|
||||
*
|
||||
* @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 File) {
|
||||
try {
|
||||
return getDocumentBuilder().parse((File) object);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException("failed to parse File payload '" + object + "'", e);
|
||||
}
|
||||
}
|
||||
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 File) {
|
||||
try {
|
||||
return getDocumentBuilder().parse((File) object);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException("failed to parse File payload '" + object + "'", e);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
public Node convertToNode(Object object) {
|
||||
Node node = null;
|
||||
if (object instanceof Node) {
|
||||
node = (Node) object;
|
||||
}
|
||||
else if (object instanceof DOMSource) {
|
||||
node = ((DOMSource) object).getNode();
|
||||
}
|
||||
else {
|
||||
node = convertToDocument(object);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
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 Source convertToSource(Object object) {
|
||||
Source source = null;
|
||||
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);
|
||||
}
|
||||
}
|
||||
protected synchronized DocumentBuilder getDocumentBuilder() {
|
||||
try {
|
||||
return this.documentBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
throw new MessagingException("failed to create a new DocumentBuilder", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Converter for creating XML {@link Document} instances
|
||||
* Converter for creating XML {@link Document}, {@link Node} or {@link Source}
|
||||
* instances from other types (e.g. String).
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
@@ -31,7 +32,7 @@ public interface XmlPayloadConverter {
|
||||
public Document convertToDocument(Object object);
|
||||
|
||||
public Node convertToNode(Object object);
|
||||
|
||||
|
||||
public Source convertToSource(Object object);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user