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

kusal pushed a commit to branch WW-5334-velocity-manager
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 114b6e4f27480ed8f391772ca908ac5498e0b3b5
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Thu Aug 17 18:58:36 2023 +1000

    WW-5334 Clean up VelocityManager#applyDefaultConfiguration
---
 .../struts2/views/velocity/VelocityManager.java    | 56 +++-------------------
 1 file changed, 6 insertions(+), 50 deletions(-)

diff --git 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java
 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java
index 9ea9d7a7f..939f23bec 100644
--- 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java
+++ 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java
@@ -467,11 +467,11 @@ public class VelocityManager {
             // remove strutsfile from resource loader property
             String prop = properties.getProperty(Velocity.RESOURCE_LOADER);
             if (prop.contains("strutsfile,")) {
-                prop = replace(prop, "strutsfile,", "");
+                prop = prop.replace("strutsfile,", "");
             } else if (prop.contains(", strutsfile")) {
-                prop = replace(prop, ", strutsfile", "");
+                prop = prop.replace(", strutsfile", "");
             } else if (prop.contains("strutsfile")) {
-                prop = replace(prop, "strutsfile", "");
+                prop = prop.replace("strutsfile", "");
             }
 
             properties.setProperty(Velocity.RESOURCE_LOADER, prop);
@@ -488,62 +488,18 @@ public class VelocityManager {
         properties.setProperty("strutsclass.resource.loader.cache", "true");
 
         // components
-        StringBuilder sb = new StringBuilder();
-
-        for (TagLibraryDirectiveProvider tagLibrary : tagLibraries) {
-            List<Class<?>> directives = tagLibrary.getDirectiveClasses();
-            for (Class<?> directive : directives) {
-                addDirective(sb, directive);
-            }
-        }
-
-        String directives = sb.toString();
+        String directives = 
tagLibraries.stream().map(TagLibraryDirectiveProvider::getDirectiveClasses).flatMap(
+                Collection::stream).map(directive -> directive.getName() + 
",").collect(joining());
 
         String userdirective = properties.getProperty("userdirective");
-        if ((userdirective == null) || userdirective.trim().equals("")) {
+        if (userdirective == null || userdirective.trim().isEmpty()) {
             userdirective = directives;
         } else {
             userdirective = userdirective.trim() + "," + directives;
         }
-
         properties.setProperty("userdirective", userdirective);
     }
 
-    private void addDirective(StringBuilder sb, Class<?> clazz) {
-        sb.append(clazz.getName()).append(",");
-    }
-
-    private static String replace(String string, String oldString, String 
newString) {
-        if (string == null) {
-            return null;
-        }
-        // If the newString is null, just return the string since there's 
nothing to replace.
-        if (newString == null) {
-            return string;
-        }
-        int i = 0;
-        // Make sure that oldString appears at least once before doing any 
processing.
-        if ((i = string.indexOf(oldString, i)) >= 0) {
-            // Use char []'s, as they are more efficient to deal with.
-            char[] string2 = string.toCharArray();
-            char[] newString2 = newString.toCharArray();
-            int oLength = oldString.length();
-            StringBuilder buf = new StringBuilder(string2.length);
-            buf.append(string2, 0, i).append(newString2);
-            i += oLength;
-            int j = i;
-            // Replace all remaining instances of oldString with newString.
-            while ((i = string.indexOf(oldString, i)) > 0) {
-                buf.append(string2, j, i - j).append(newString2);
-                i += oLength;
-                j = i;
-            }
-            buf.append(string2, j, string2.length - j);
-            return buf.toString();
-        }
-        return string;
-    }
-
     /**
      * @return the velocityProperties
      */

Reply via email to