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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jxpath.git

commit c38e94e40b4efdb5b2984aec397963868cae74ec
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Thu Mar 20 10:44:18 2025 -0400

    Use try-with-resources
    
    - JXPathContextFactory.findFactory(String, String) now allows
    IOExceptions on closing of stream to interrupt processing
    - Better local variable name
---
 .../commons/jxpath/JXPathContextFactory.java       | 50 ++++++----------------
 1 file changed, 14 insertions(+), 36 deletions(-)

diff --git a/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java 
b/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java
index ccb234f..a0461ba 100644
--- a/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java
+++ b/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java
@@ -81,22 +81,13 @@ public abstract class JXPathContextFactory {
         }
         // try to read from $java.home/lib/xml.properties
         try {
-            final String javah = System.getProperty("java.home");
-            final String configFile = javah + File.separator + "lib" + 
File.separator + "jxpath.properties";
+            final String javaHome = System.getProperty("java.home");
+            final String configFile = javaHome + File.separator + "lib" + 
File.separator + "jxpath.properties";
             final File f = new File(configFile);
             if (f.exists()) {
                 final Properties props = new Properties();
-                final FileInputStream fis = new FileInputStream(f);
-                try {
+                try (FileInputStream fis = new FileInputStream(f)) {
                     props.load(fis);
-                } finally {
-                    if (fis != null) {
-                        try {
-                            fis.close();
-                        } catch (final IOException ignore) { // NOPMD
-                            // swallow
-                        }
-                    }
                 }
                 final String factory = props.getProperty(property);
                 if (factory != null) {
@@ -115,32 +106,19 @@ public abstract class JXPathContextFactory {
         // try to find services in CLASSPATH
         try {
             final ClassLoader cl = JXPathContextFactory.class.getClassLoader();
-            InputStream is;
-            if (cl == null) {
-                is = ClassLoader.getSystemResourceAsStream(serviceId);
-            } else {
-                is = cl.getResourceAsStream(serviceId);
-            }
-            if (is != null) {
-                if (debug) {
-                    System.err.println("JXPath: found  " + serviceId);
-                }
-                final BufferedReader rd = new BufferedReader(new 
InputStreamReader(is, "UTF-8"));
-                String factory = null;
-                try {
-                    factory = rd.readLine();
-                } finally {
-                    try {
-                        rd.close();
-                    } catch (final IOException ignore) { // NOPMD
-                        // Ignore
-                    }
-                }
-                if (factory != null && !"".equals(factory)) {
+            try (InputStream is = cl == null ? 
ClassLoader.getSystemResourceAsStream(serviceId) : 
cl.getResourceAsStream(serviceId)) {
+                if (is != null) {
                     if (debug) {
-                        System.err.println("JXPath: loaded from services: " + 
factory);
+                        System.err.println("JXPath: found  " + serviceId);
+                    }
+                    final BufferedReader rd = new BufferedReader(new 
InputStreamReader(is, "UTF-8"));
+                    final String factory = rd.readLine();
+                    if (factory != null && !"".equals(factory)) {
+                        if (debug) {
+                            System.err.println("JXPath: loaded from services: 
" + factory);
+                        }
+                        return factory;
                     }
-                    return factory;
                 }
             }
         } catch (final Exception ex) {

Reply via email to