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

michaelo pushed a commit to branch VELOCITY-972
in repository https://gitbox.apache.org/repos/asf/velocity-engine.git

commit 5b094c606a0e4fe2aa1ccf09aa20864a6dbc45a8
Author: Michael Osipov <[email protected]>
AuthorDate: Sat Feb 3 21:18:31 2024 +0100

    [VELOCITY-972] Remove Commons IO
---
 velocity-custom-parser-example/pom.xml             |  5 ---
 .../runtime/parser/CustomParserTestCase.java       |  7 ++--
 velocity-engine-core/pom.xml                       | 38 ----------------------
 .../resource/loader/FileResourceLoader.java        | 26 ++-------------
 .../runtime/resource/loader/JarResourceLoader.java | 24 +++-----------
 .../velocity/test/ClassloaderChangeTestCase.java   | 11 ++++---
 .../velocity/test/OldPropertiesTestCase.java       |  6 ++--
 7 files changed, 23 insertions(+), 94 deletions(-)

diff --git a/velocity-custom-parser-example/pom.xml 
b/velocity-custom-parser-example/pom.xml
index 4844488d..f18aa533 100644
--- a/velocity-custom-parser-example/pom.xml
+++ b/velocity-custom-parser-example/pom.xml
@@ -80,11 +80,6 @@
             <version>${slf4j.version}</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>commons-io</groupId>
-            <artifactId>commons-io</artifactId>
-            <version>2.8.0</version>
-        </dependency>
     </dependencies>
 
     <build>
diff --git 
a/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
 
b/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
index 9a27ff89..f44e849a 100644
--- 
a/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
+++ 
b/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
@@ -1,6 +1,5 @@
 package org.apache.velocity.runtime.parser;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -11,6 +10,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 
 import static org.junit.Assert.*;
 
@@ -49,8 +50,8 @@ public class CustomParserTestCase
         writer.flush();
         writer.close();
 
-        String result = IOUtils.toString(new FileInputStream(resultFile), 
StandardCharsets.UTF_8);
-        String reference = IOUtils.toString(new 
FileInputStream(referenceFile), StandardCharsets.UTF_8);
+        String result = new String(Files.readAllBytes(Paths.get(resultFile)), 
StandardCharsets.UTF_8)
+        String reference = new 
String(Files.readAllBytes(Paths.get(referenceFile)), StandardCharsets.UTF_8)
         assertEquals(reference, result);
     }
 }
diff --git a/velocity-engine-core/pom.xml b/velocity-engine-core/pom.xml
index 43114183..a3bddc8f 100644
--- a/velocity-engine-core/pom.xml
+++ b/velocity-engine-core/pom.xml
@@ -104,38 +104,6 @@
                 </executions>
             </plugin>
 
-            <!-- shading of commons-io -->
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-shade-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>shade</id>
-                        <phase>package</phase>
-                        <goals>
-                            <goal>shade</goal>
-                        </goals>
-                        <configuration>
-                            <artifactSet>
-                                <includes>
-                                    <include>commons-io:commons-io</include>
-                                </includes>
-                                <excludes>
-                                    <exclude>org.slf4j:slf4j-api</exclude>
-                                </excludes>
-                            </artifactSet>
-                            <relocations>
-                                <relocation>
-                                    <pattern>org.apache.commons.io</pattern>
-                                    
<shadedPattern>org.apache.velocity.shaded.commons.io</shadedPattern>
-                                </relocation>
-                            </relocations>
-                            <minimizeJar>true</minimizeJar>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
             <!-- parser -->
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
@@ -220,7 +188,6 @@
                             org.apache.velocity.*
                         </Export-Package>
                         <Import-Package>
-                            !org.apache.commons.io,
                             *
                         </Import-Package>
                     </instructions>
@@ -312,11 +279,6 @@
             <scope>test</scope>
             <classifier>${test.jdbc.driver.classifier}</classifier>
         </dependency>
-        <dependency>
-            <groupId>commons-io</groupId>
-            <artifactId>commons-io</artifactId>
-            <version>2.8.0</version>
-        </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-simple</artifactId>
diff --git 
a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
 
b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
index ba9e5f98..8993b7c7 100644
--- 
a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
+++ 
b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
@@ -25,7 +25,6 @@ import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.resource.Resource;
 import org.apache.velocity.util.ExtProperties;
 
-import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.File;
@@ -114,19 +113,6 @@ public class FileResourceLoader extends ResourceLoader
                     "Need to specify a file name or file path!");
         }
 
-        String template = FilenameUtils.normalize( templateName, true );
-        if ( template == null || template.length() == 0 )
-        {
-            String msg = "File resource error: argument " + template +
-                    " contains .. and may be trying to access " +
-                    "content outside of template root.  Rejected.";
-
-            log.error("FileResourceLoader: {}", msg);
-
-            throw new ResourceNotFoundException ( msg );
-        }
-
-        int size = paths.size();
         for (String path : paths)
         {
             InputStream rawStream = null;
@@ -134,7 +120,7 @@ public class FileResourceLoader extends ResourceLoader
 
             try
             {
-                rawStream = findTemplate(path, template);
+                rawStream = findTemplate(path, templateName);
                 if (rawStream != null)
                 {
                     reader = buildReader(rawStream, encoding);
@@ -143,7 +129,7 @@ public class FileResourceLoader extends ResourceLoader
             catch (IOException ioe)
             {
                 closeQuiet(rawStream);
-                String msg = "Exception while loading Template " + template;
+                String msg = "Exception while loading Template " + 
templateName;
                 log.error(msg, ioe);
                 throw new VelocityException(msg, ioe, 
rsvc.getLogContext().getStackTrace());
             }
@@ -164,7 +150,7 @@ public class FileResourceLoader extends ResourceLoader
          * templates and we didn't find anything so
          * throw an exception.
          */
-        throw new ResourceNotFoundException("FileResourceLoader: cannot find " 
+ template);
+        throw new ResourceNotFoundException("FileResourceLoader: cannot find " 
+ templateName);
     }
 
     /**
@@ -174,17 +160,11 @@ public class FileResourceLoader extends ResourceLoader
     @Override
     public boolean resourceExists(String name)
     {
-        if (name == null)
-        {
-            return false;
-        }
-        name =  FilenameUtils.normalize(name);
         if (name == null || name.length() == 0)
         {
             return false;
         }
 
-        int size = paths.size();
         for (String path : paths)
         {
             try
diff --git 
a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
 
b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
index 23db3743..4f56bf50 100644
--- 
a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
+++ 
b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
@@ -25,7 +25,6 @@ import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.resource.Resource;
 import org.apache.velocity.util.ExtProperties;
 
-import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.IOException;
@@ -186,35 +185,22 @@ public class JarResourceLoader extends ResourceLoader
             throw new ResourceNotFoundException("Need to have a resource!");
         }
 
-        String normalizedPath = FilenameUtils.normalize( source, true );
-
-        if ( normalizedPath == null || normalizedPath.length() == 0 )
-        {
-            String msg = "JAR resource error: argument " + normalizedPath +
-                    " contains .. and may be trying to access " +
-                    "content outside of template root.  Rejected.";
-
-            log.error( "JarResourceLoader: {}", msg );
-
-            throw new ResourceNotFoundException ( msg );
-        }
-
         /*
          *  if a / leads off, then just nip that :)
          */
-        if ( normalizedPath.startsWith("/") )
+        if ( source.startsWith("/") )
         {
-            normalizedPath = normalizedPath.substring(1);
+            source = source.substring(1);
         }
 
-        if ( entryDirectory.containsKey( normalizedPath ) )
+        if ( entryDirectory.containsKey( source ) )
         {
-            String jarurl  = entryDirectory.get( normalizedPath );
+            String jarurl  = entryDirectory.get( source );
 
             if ( jarfiles.containsKey( jarurl ) )
             {
                 JarHolder holder = (JarHolder)jarfiles.get( jarurl );
-                InputStream rawStream = holder.getResource( normalizedPath );
+                InputStream rawStream = holder.getResource( source );
                 try
                 {
                     return buildReader(rawStream, encoding);
diff --git 
a/velocity-engine-core/src/test/java/org/apache/velocity/test/ClassloaderChangeTestCase.java
 
b/velocity-engine-core/src/test/java/org/apache/velocity/test/ClassloaderChangeTestCase.java
index 096dbe7b..73f7b08d 100644
--- 
a/velocity-engine-core/src/test/java/org/apache/velocity/test/ClassloaderChangeTestCase.java
+++ 
b/velocity-engine-core/src/test/java/org/apache/velocity/test/ClassloaderChangeTestCase.java
@@ -22,7 +22,6 @@ package org.apache.velocity.test;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
-import org.apache.commons.io.IOUtils;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.test.misc.TestLogger;
@@ -149,9 +148,13 @@ public class ClassloaderChangeTestCase extends TestCase
                 throws Exception
         {
             ByteArrayOutputStream os = new ByteArrayOutputStream();
-            InputStream fis = getClass().getResourceAsStream("/" + testclass);
-            IOUtils.copy(fis, os);
-            fis.close();
+            InputStream is = getClass().getResourceAsStream("/" + testclass);
+            byte[] buf = new byte[8192];
+            int length;
+            while ((length = is.read(buf)) != -1) {
+                os.write(buf, 0, length);
+            }
+            is.close();
             os.close();
 
             byte[] barr = os.toByteArray();
diff --git 
a/velocity-engine-core/src/test/java/org/apache/velocity/test/OldPropertiesTestCase.java
 
b/velocity-engine-core/src/test/java/org/apache/velocity/test/OldPropertiesTestCase.java
index 46c3b06d..b0dad04e 100644
--- 
a/velocity-engine-core/src/test/java/org/apache/velocity/test/OldPropertiesTestCase.java
+++ 
b/velocity-engine-core/src/test/java/org/apache/velocity/test/OldPropertiesTestCase.java
@@ -22,7 +22,6 @@ package org.apache.velocity.test;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.test.misc.TestLogger;
@@ -30,6 +29,9 @@ import org.apache.velocity.util.DeprecationAwareExtProperties;
 
 import java.io.File;
 import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -98,7 +100,7 @@ public class OldPropertiesTestCase extends TestCase 
implements TemplateTestBase
         Translator translator = new Translator();
 
         // check getting old/new values
-        List<String> oldPropSettings = FileUtils.readLines(new 
File(oldProperties));
+        List<String> oldPropSettings = 
Files.readAllLines(Paths.get(oldProperties), StandardCharsets.ISO_8859_1);
         Set<String> oldKeys = new HashSet<>();
         for (String oldProp : oldPropSettings)
         {

Reply via email to