You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/05/24 13:40:55 UTC

[tomcat] branch main updated: Reduce code duplication

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

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


The following commit(s) were added to refs/heads/main by this push:
     new bc12accbb9 Reduce code duplication
bc12accbb9 is described below

commit bc12accbb9ee6caf2d6159dc484f74d33ab929bb
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed May 24 14:40:48 2023 +0100

    Reduce code duplication
---
 java/org/apache/catalina/manager/JspHelper.java | 56 ++++++-------------------
 1 file changed, 13 insertions(+), 43 deletions(-)

diff --git a/java/org/apache/catalina/manager/JspHelper.java b/java/org/apache/catalina/manager/JspHelper.java
index cb6db56572..bc0945c08f 100644
--- a/java/org/apache/catalina/manager/JspHelper.java
+++ b/java/org/apache/catalina/manager/JspHelper.java
@@ -24,6 +24,7 @@ import java.util.Locale;
 
 import org.apache.catalina.Session;
 import org.apache.catalina.manager.util.SessionUtils;
+import org.apache.tomcat.util.security.Escape;
 
 
 /**
@@ -191,57 +192,26 @@ public class JspHelper {
     }
 
     /**
-     * Performs the following substring replacements
-     * (to facilitate output to XML/HTML pages):
+     * Performs the following substring replacements (to facilitate output to XML/HTML pages):
+     * <ul>
+     *   <li>&amp; -&gt; &amp;amp;</li>
+     *   <li>&lt; -&gt; &amp;lt;</li>
+     *   <li>&gt; -&gt; &amp;gt;</li>
+     *   <li>" -&gt; &amp;#034;</li>
+     *   <li>' -&gt; &amp;#039;</li>
+     * </ul>
      *
-     *    &amp; -&gt; &amp;amp;
-     *    &lt; -&gt; &amp;lt;
-     *    &gt; -&gt; &amp;gt;
-     *    " -&gt; &amp;#034;
-     *    ' -&gt; &amp;#039;
-     *
-     * See also OutSupport.writeEscapedXml().
      * @param buffer The XML to escape
+     *
      * @return the escaped XML
      */
-    @SuppressWarnings("null") // escapedBuffer cannot be null
     public static String escapeXml(String buffer) {
+
         if (buffer == null) {
             return "";
         }
-        int start = 0;
-        int length = buffer.length();
-        char[] arrayBuffer = buffer.toCharArray();
-        StringBuilder escapedBuffer = null;
-
-        for (int i = 0; i < length; i++) {
-            char c = arrayBuffer[i];
-            if (c <= HIGHEST_SPECIAL) {
-                char[] escaped = specialCharactersRepresentation[c];
-                if (escaped != null) {
-                    // create StringBuilder to hold escaped xml string
-                    if (start == 0) {
-                        escapedBuffer = new StringBuilder(length + 5);
-                    }
-                    // add unescaped portion
-                    if (start < i) {
-                        escapedBuffer.append(arrayBuffer,start,i-start);
-                    }
-                    start = i + 1;
-                    // add escaped xml
-                    escapedBuffer.append(escaped);
-                }
-            }
-        }
-        // no xml escaping was necessary
-        if (start == 0) {
-            return buffer;
-        }
-        // add rest of unescaped portion
-        if (start < length) {
-            escapedBuffer.append(arrayBuffer,start,length-start);
-        }
-        return escapedBuffer.toString();
+
+        return Escape.xml(buffer);
     }
 
     public static String formatNumber(long number) {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org