This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 045c165367712b8b59cfd2c489861386073068fa
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Aug 21 13:47:08 2024 +0200

    (chores) convert core/camel-xml-jaxp to use pattern matching for instanceof
---
 .../apache/camel/converter/jaxp/DomConverter.java  | 26 +++-----
 .../apache/camel/converter/jaxp/XmlConverter.java  | 76 +++++++++++-----------
 .../support/builder/xml/XMLConverterHelper.java    |  7 +-
 .../processor/validation/ValidatingProcessor.java  |  8 +--
 .../apache/camel/util/xml/StreamSourceCache.java   |  8 +--
 5 files changed, 59 insertions(+), 66 deletions(-)

diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
index 507c9bff8ee..dda5c8265dc 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
@@ -58,8 +58,7 @@ public final class DomConverter {
 
         // use XML converter at first since it preserves tag names
         boolean found = false;
-        if (nodeList instanceof Node) {
-            Node node = (Node) nodeList;
+        if (nodeList instanceof Node node) {
             String s = toString(node, exchange);
             if (org.apache.camel.util.ObjectHelper.isNotEmpty(s)) {
                 found = true;
@@ -90,15 +89,13 @@ public final class DomConverter {
     @Converter(order = 2)
     public String toString(Node node, Exchange exchange) throws 
TransformerException {
         String s;
-        if (node instanceof Text) {
-            Text textnode = (Text) node;
-
+        if (node instanceof Text textNode) {
             StringBuilder b = new StringBuilder(128);
-            b.append(textnode.getNodeValue());
-            textnode = (Text) textnode.getNextSibling();
-            while (textnode != null) {
-                b.append(textnode.getNodeValue());
-                textnode = (Text) textnode.getNextSibling();
+            b.append(textNode.getNodeValue());
+            textNode = (Text) textNode.getNextSibling();
+            while (textNode != null) {
+                b.append(textNode.getNodeValue());
+                textNode = (Text) textNode.getNextSibling();
             }
             s = b.toString();
         } else {
@@ -153,14 +150,11 @@ public final class DomConverter {
     }
 
     private static void append(StringBuilder buffer, Node node) {
-        if (node instanceof Text) {
-            Text text = (Text) node;
+        if (node instanceof Text text) {
             buffer.append(text.getTextContent());
-        } else if (node instanceof Attr) {
-            Attr attribute = (Attr) node;
+        } else if (node instanceof Attr attribute) {
             buffer.append(attribute.getTextContent());
-        } else if (node instanceof Element) {
-            Element element = (Element) node;
+        } else if (node instanceof Element element) {
             append(buffer, element.getChildNodes());
         }
     }
diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
index 79b1792b1b3..4697fd0f52b 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
@@ -238,10 +238,10 @@ public class XmlConverter {
     public String toString(Source source, Exchange exchange) throws 
TransformerException {
         if (source == null) {
             return null;
-        } else if (source instanceof StringSource) {
-            return ((StringSource) source).getText();
-        } else if (source instanceof BytesSource) {
-            return new String(((BytesSource) source).getData());
+        } else if (source instanceof StringSource stringSource) {
+            return stringSource.getText();
+        } else if (source instanceof BytesSource bytesSource) {
+            return new String(bytesSource.getData());
         } else {
             StringWriter buffer = new StringWriter();
             if (exchange != null) {
@@ -264,8 +264,8 @@ public class XmlConverter {
      */
     @Converter(order = 12)
     public byte[] toByteArray(Source source, Exchange exchange) throws 
TransformerException {
-        if (source instanceof BytesSource) {
-            return ((BytesSource) source).getData();
+        if (source instanceof BytesSource bytesSource) {
+            return bytesSource.getData();
         } else {
             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
             if (exchange != null) {
@@ -638,8 +638,8 @@ public class XmlConverter {
         ObjectHelper.notNull(node, "node");
 
         // If the node is the document, just cast it
-        if (node instanceof Document) {
-            return (Document) node;
+        if (node instanceof Document document) {
+            return document;
             // If the node is an element
         } else if (node instanceof Element elem) {
             // If this is the root element, return its owner document
@@ -687,11 +687,11 @@ public class XmlConverter {
     @Converter(order = 53)
     public Element toDOMElement(Node node) throws TransformerException {
         // If the node is an document, return the root element
-        if (node instanceof Document) {
-            return ((Document) node).getDocumentElement();
+        if (node instanceof Document document) {
+            return document.getDocumentElement();
             // If the node is an element, just cast it
-        } else if (node instanceof Element) {
-            return (Element) node;
+        } else if (node instanceof Element element) {
+            return element;
             // Other node types are not handled
         } else {
             throw new TransformerException("Unable to convert DOM node to an 
Element");
@@ -829,10 +829,10 @@ public class XmlConverter {
     public Document toDOMDocumentFromSingleNodeList(NodeList nl) throws 
ParserConfigurationException, TransformerException {
         if (nl.getLength() == 1) {
             return toDOMDocument(nl.item(0));
-        } else if (nl instanceof Node) {
+        } else if (nl instanceof Node node) {
             // as XML parsers may often have nodes that implement both Node 
and NodeList then the type converter lookup
             // may lookup either a type converter from NodeList or Node. So 
let's fallback and try with Node
-            return toDOMDocument((Node) nl);
+            return toDOMDocument(node);
         } else {
             return null;
         }
@@ -872,14 +872,14 @@ public class XmlConverter {
     @Converter(order = 69)
     public DOMSource toDOMSource(Source source, Exchange exchange)
             throws ParserConfigurationException, IOException, SAXException, 
TransformerException {
-        if (source instanceof DOMSource) {
-            return (DOMSource) source;
-        } else if (source instanceof SAXSource) {
-            return toDOMSourceFromSAX((SAXSource) source);
-        } else if (source instanceof StreamSource) {
-            return toDOMSourceFromStream((StreamSource) source, exchange);
-        } else if (source instanceof StAXSource) {
-            return toDOMSourceFromStAX((StAXSource) source);
+        if (source instanceof DOMSource domSource) {
+            return domSource;
+        } else if (source instanceof SAXSource saxSource) {
+            return toDOMSourceFromSAX(saxSource);
+        } else if (source instanceof StreamSource streamSource) {
+            return toDOMSourceFromStream(streamSource, exchange);
+        } else if (source instanceof StAXSource staxSource) {
+            return toDOMSourceFromStAX(staxSource);
         } else {
             return null;
         }
@@ -891,14 +891,14 @@ public class XmlConverter {
      */
     @Converter(order = 70)
     public SAXSource toSAXSource(Source source, Exchange exchange) throws 
SAXException, TransformerException {
-        if (source instanceof SAXSource) {
-            return (SAXSource) source;
-        } else if (source instanceof DOMSource) {
-            return toSAXSourceFromDOM((DOMSource) source, exchange);
-        } else if (source instanceof StreamSource) {
-            return toSAXSourceFromStream((StreamSource) source, exchange);
-        } else if (source instanceof StAXSource) {
-            return toSAXSourceFromStAX((StAXSource) source, exchange);
+        if (source instanceof SAXSource saxSource) {
+            return saxSource;
+        } else if (source instanceof DOMSource domSource) {
+            return toSAXSourceFromDOM(domSource, exchange);
+        } else if (source instanceof StreamSource streamSource) {
+            return toSAXSourceFromStream(streamSource, exchange);
+        } else if (source instanceof StAXSource stAXSource) {
+            return toSAXSourceFromStAX(stAXSource, exchange);
         } else {
             return null;
         }
@@ -906,14 +906,14 @@ public class XmlConverter {
 
     @Converter(order = 71)
     public StreamSource toStreamSource(Source source, Exchange exchange) 
throws TransformerException {
-        if (source instanceof StreamSource) {
-            return (StreamSource) source;
-        } else if (source instanceof DOMSource) {
-            return toStreamSourceFromDOM((DOMSource) source, exchange);
-        } else if (source instanceof SAXSource) {
-            return toStreamSourceFromSAX((SAXSource) source, exchange);
-        } else if (source instanceof StAXSource) {
-            return toStreamSourceFromStAX((StAXSource) source, exchange);
+        if (source instanceof StreamSource streamSource) {
+            return streamSource;
+        } else if (source instanceof DOMSource domSource) {
+            return toStreamSourceFromDOM(domSource, exchange);
+        } else if (source instanceof SAXSource saxSource) {
+            return toStreamSourceFromSAX(saxSource, exchange);
+        } else if (source instanceof StAXSource stAXSource) {
+            return toStreamSourceFromStAX(stAXSource, exchange);
         } else {
             return null;
         }
diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/builder/xml/XMLConverterHelper.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/builder/xml/XMLConverterHelper.java
index f78a6728d82..a473e537c91 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/builder/xml/XMLConverterHelper.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/builder/xml/XMLConverterHelper.java
@@ -88,11 +88,10 @@ public class XMLConverterHelper {
         ObjectHelper.notNull(node, "node");
 
         // If the node is the document, just cast it
-        if (node instanceof Document) {
-            return (Document) node;
+        if (node instanceof Document document) {
+            return document;
             // If the node is an element
-        } else if (node instanceof Element) {
-            Element elem = (Element) node;
+        } else if (node instanceof Element elem) {
             // If this is the root element, return its owner document
             if (elem.getOwnerDocument().getDocumentElement() == elem) {
                 return elem.getOwnerDocument();
diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/processor/validation/ValidatingProcessor.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/processor/validation/ValidatingProcessor.java
index 08b033e6050..240efaf3a88 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/processor/validation/ValidatingProcessor.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/support/processor/validation/ValidatingProcessor.java
@@ -366,12 +366,12 @@ public class ValidatingProcessor extends 
AsyncProcessorSupport {
      */
     protected Source getSource(Exchange exchange, Object content) {
         // body or header may already be a source
-        if (content instanceof Source) {
-            return (Source) content;
+        if (content instanceof Source source) {
+            return source;
         }
         Source source = null;
-        if (content instanceof InputStream) {
-            return new StreamSource((InputStream) content);
+        if (content instanceof InputStream stream) {
+            return new StreamSource(stream);
         }
         if (content != null) {
             TypeConverter tc = 
exchange.getContext().getTypeConverterRegistry().lookup(Source.class, 
content.getClass());
diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
index 542f60624d6..6b7d7364176 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
@@ -62,11 +62,11 @@ public final class StreamSourceCache extends StreamSource 
implements StreamCache
 
     public StreamSourceCache(StreamCache streamCache) {
         this.streamCache = streamCache;
-        if (streamCache instanceof InputStream) {
-            setInputStream((InputStream) streamCache);
+        if (streamCache instanceof InputStream inputStream) {
+            setInputStream(inputStream);
             this.readCache = null;
-        } else if (streamCache instanceof ReaderCache) {
-            this.readCache = (ReaderCache) streamCache;
+        } else if (streamCache instanceof ReaderCache readerCache) {
+            this.readCache = readerCache;
             setReader((java.io.Reader) streamCache);
         } else {
             this.readCache = null;

Reply via email to