Avoid throws Exception where possible - Phase III
This commit is contained in:
committed by
Artem Bilan
parent
b138ab80f8
commit
78199dca9b
@@ -86,7 +86,7 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
|
||||
|
||||
private Properties outputProperties;
|
||||
|
||||
private boolean iterator = true;
|
||||
private boolean returnIterator = true;
|
||||
|
||||
public XPathMessageSplitter(String expression) {
|
||||
this(expression, new HashMap<>());
|
||||
@@ -182,7 +182,7 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
|
||||
* @since 4.2
|
||||
*/
|
||||
public void setIterator(boolean iterator) {
|
||||
this.iterator = iterator;
|
||||
this.returnIterator = iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,9 +200,9 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
|
||||
@Override
|
||||
protected void doInit() {
|
||||
super.doInit();
|
||||
if (this.iterator && this.jaxpExpression == null) {
|
||||
if (this.returnIterator && this.jaxpExpression == null) {
|
||||
logger.info("The 'iterator' option isn't available for an external XPathExpression. Will be ignored");
|
||||
this.iterator = false;
|
||||
this.returnIterator = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object splitDocument(Document document) throws Exception {
|
||||
private Object splitDocument(Document document) throws ParserConfigurationException, TransformerException {
|
||||
Object nodes = splitNode(document);
|
||||
final Transformer transformer;
|
||||
synchronized (this.transformerFactory) {
|
||||
@@ -282,7 +282,7 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
|
||||
}
|
||||
|
||||
private Object splitNode(Node node) throws ParserConfigurationException {
|
||||
if (this.iterator) {
|
||||
if (this.returnIterator) {
|
||||
try {
|
||||
NodeList nodeList = (NodeList) this.jaxpExpression.evaluate(node, XPathConstants.NODESET);
|
||||
return new NodeListIterator(nodeList);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -112,7 +112,7 @@ public class XPathTransformer extends AbstractTransformer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doTransform(Message<?> message) throws Exception {
|
||||
protected Object doTransform(Message<?> message) {
|
||||
Node node = this.converter.convertToNode(message.getPayload());
|
||||
Object result = null;
|
||||
if (this.nodeMapper != null) {
|
||||
|
||||
@@ -250,7 +250,7 @@ public class XsltPayloadTransformer extends AbstractXmlTransformer implements Be
|
||||
try {
|
||||
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
catch (@SuppressWarnings("unused") IllegalArgumentException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("The '" + XMLConstants.ACCESS_EXTERNAL_STYLESHEET + "' property is not supported by "
|
||||
+ transformerFactory.getClass().getCanonicalName());
|
||||
@@ -260,33 +260,38 @@ public class XsltPayloadTransformer extends AbstractXmlTransformer implements Be
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doTransform(Message<?> message) throws Exception {
|
||||
Transformer transformer = buildTransformer(message);
|
||||
Object payload;
|
||||
if (this.alwaysUseSourceFactory) {
|
||||
payload = this.sourceFactory.createSource(message.getPayload());
|
||||
protected Object doTransform(Message<?> message) {
|
||||
try {
|
||||
Transformer transformer = buildTransformer(message);
|
||||
Object payload;
|
||||
if (this.alwaysUseSourceFactory) {
|
||||
payload = this.sourceFactory.createSource(message.getPayload());
|
||||
}
|
||||
else {
|
||||
payload = message.getPayload();
|
||||
}
|
||||
Object transformedPayload = null;
|
||||
if (this.alwaysUseResultFactory) {
|
||||
transformedPayload = transformUsingResultFactory(payload, transformer);
|
||||
}
|
||||
else if (payload instanceof String) {
|
||||
transformedPayload = transformString((String) payload, transformer);
|
||||
}
|
||||
else if (payload instanceof Document) {
|
||||
transformedPayload = transformDocument((Document) payload, transformer);
|
||||
}
|
||||
else if (payload instanceof Source) {
|
||||
transformedPayload = transformSource((Source) payload, payload, transformer);
|
||||
}
|
||||
else {
|
||||
// fall back to trying factories
|
||||
transformedPayload = transformUsingResultFactory(payload, transformer);
|
||||
}
|
||||
return transformedPayload;
|
||||
}
|
||||
else {
|
||||
payload = message.getPayload();
|
||||
catch (TransformerException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
Object transformedPayload = null;
|
||||
if (this.alwaysUseResultFactory) {
|
||||
transformedPayload = transformUsingResultFactory(payload, transformer);
|
||||
}
|
||||
else if (payload instanceof String) {
|
||||
transformedPayload = transformString((String) payload, transformer);
|
||||
}
|
||||
else if (payload instanceof Document) {
|
||||
transformedPayload = transformDocument((Document) payload, transformer);
|
||||
}
|
||||
else if (payload instanceof Source) {
|
||||
transformedPayload = transformSource((Source) payload, payload, transformer);
|
||||
}
|
||||
else {
|
||||
// fall back to trying factories
|
||||
transformedPayload = transformUsingResultFactory(payload, transformer);
|
||||
}
|
||||
return transformedPayload;
|
||||
}
|
||||
|
||||
private Object transformUsingResultFactory(Object payload, Transformer transformer) throws TransformerException {
|
||||
|
||||
@@ -61,7 +61,7 @@ public class XPathSplitterParserTests {
|
||||
public void testXpathSplitterConfig() {
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter, "createDocuments", Boolean.class)).isTrue();
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter, "applySequence", Boolean.class)).isFalse();
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter, "iterator", Boolean.class)).isFalse();
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter, "returnIterator", Boolean.class)).isFalse();
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter, "outputProperties")).isSameAs(this.outputProperties);
|
||||
assertThat(TestUtils.getPropertyValue(this.xpathSplitter,
|
||||
"xpathExpression.xpathExpression.xpath.m_patternString",
|
||||
|
||||
@@ -60,7 +60,7 @@ import org.springframework.xml.transform.StringSource;
|
||||
*/
|
||||
public class XsltPayloadTransformerTests {
|
||||
|
||||
private XsltPayloadTransformer transformer;
|
||||
private XsltPayloadTransformer testTransformer;
|
||||
|
||||
private final String docAsString =
|
||||
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
@@ -72,16 +72,16 @@ public class XsltPayloadTransformerTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.transformer = new XsltPayloadTransformer(getXslTemplates());
|
||||
this.transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
this.transformer.setAlwaysUseResultFactory(false);
|
||||
this.transformer.afterPropertiesSet();
|
||||
this.testTransformer = new XsltPayloadTransformer(getXslTemplates());
|
||||
this.testTransformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
this.testTransformer.setAlwaysUseResultFactory(false);
|
||||
this.testTransformer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDocumentAsPayload() throws Exception {
|
||||
Message<?> message = new GenericMessage<>(XmlTestUtil.getDocumentForString(this.docAsString));
|
||||
Object transformed = this.transformer.doTransform(message);
|
||||
Object transformed = this.testTransformer.doTransform(message);
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
.isInstanceOf(Document.class);
|
||||
@@ -92,7 +92,7 @@ public class XsltPayloadTransformerTests {
|
||||
@Test
|
||||
public void testSourceAsPayload() throws Exception {
|
||||
GenericMessage<?> message = new GenericMessage<>(new StringSource(this.docAsString));
|
||||
Object transformed = transformer.doTransform(message);
|
||||
Object transformed = testTransformer.doTransform(message);
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
@@ -105,8 +105,8 @@ public class XsltPayloadTransformerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayload() throws Exception {
|
||||
Object transformed = this.transformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
public void testStringAsPayload() {
|
||||
Object transformed = this.testTransformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
@@ -120,8 +120,8 @@ public class XsltPayloadTransformerTests {
|
||||
|
||||
@Test
|
||||
public void testStringAsPayloadUseResultFactoryTrue() throws Exception {
|
||||
this.transformer.setAlwaysUseResultFactory(true);
|
||||
Object transformed = transformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
this.testTransformer.setAlwaysUseResultFactory(true);
|
||||
Object transformed = testTransformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for useFactories true")
|
||||
@@ -171,18 +171,19 @@ public class XsltPayloadTransformerTests {
|
||||
|
||||
@Test
|
||||
public void testNonXmlString() {
|
||||
assertThatExceptionOfType(TransformerException.class)
|
||||
.isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test")));
|
||||
assertThatExceptionOfType(IllegalStateException.class)
|
||||
.isThrownBy(() -> this.testTransformer.doTransform(new GenericMessage<>("test")))
|
||||
.withCauseInstanceOf(TransformerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnsupportedPayloadType() {
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12)));
|
||||
.isThrownBy(() -> this.testTransformer.doTransform(new GenericMessage<>(12)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXsltWithImports() throws Exception {
|
||||
public void testXsltWithImports() {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl", getClass());
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
@@ -271,6 +272,7 @@ public class XsltPayloadTransformerTests {
|
||||
this.objectToReturn = objectToReturn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object transformResult(Result result) {
|
||||
return objectToReturn;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user