Author: markt
Date: Sun Oct 28 21:57:54 2012
New Revision: 1403104

URL: http://svn.apache.org/viewvc?rev=1403104&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=53867
Improve performance of XML escaping
Based on a patch by Sheldon Shao

Added:
    
tomcat/tc7.0.x/trunk/test/org/apache/jasper/runtime/TesterPageContextImpl.java
      - copied unchanged from r1403099, 
tomcat/trunk/test/org/apache/jasper/runtime/TesterPageContextImpl.java
Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/jasper/runtime/PageContextImpl.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1403099

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/jasper/runtime/PageContextImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/jasper/runtime/PageContextImpl.java?rev=1403104&r1=1403103&r2=1403104&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/jasper/runtime/PageContextImpl.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/jasper/runtime/PageContextImpl.java 
Sun Oct 28 21:57:54 2012
@@ -913,27 +913,67 @@ public class PageContextImpl extends Pag
         }
     }
 
-    private static String XmlEscape(String s) {
-        if (s == null)
+    protected static String XmlEscape(String s) {
+        if (s == null) {
             return null;
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < s.length(); i++) {
+        }
+        int len = s.length();
+
+        /*
+         * Look for any "bad" characters, Escape "bad" character was found
+         */
+        // ASCII " 34 & 38 ' 39 < 60 > 62
+        for (int i = 0; i < len; i++) {
             char c = s.charAt(i);
-            if (c == '<') {
-                sb.append("&lt;");
-            } else if (c == '>') {
-                sb.append("&gt;");
-            } else if (c == '\'') {
-                sb.append("&#039;"); // &apos;
-            } else if (c == '&') {
-                sb.append("&amp;");
-            } else if (c == '"') {
-                sb.append("&#034;"); // &quot;
-            } else {
-                sb.append(c);
+            if (c >= '\"' && c <= '>' &&
+                    (c == '<' || c == '>' || c == '\'' || c == '&' || c == 
'"')) {
+                // need to escape them and then quote the whole string
+                StringBuilder sb = new StringBuilder((int) (len * 1.2));
+                sb.append(s, 0, i);
+                int pos = i + 1;
+                for (int j = i; j < len; j++) {
+                    c = s.charAt(j);
+                    if (c >= '\"' && c <= '>') {
+                        if (c == '<') {
+                            if (j > pos) {
+                                sb.append(s, pos, j);
+                            }
+                            sb.append("&lt;");
+                            pos = j + 1;
+                        } else if (c == '>') {
+                            if (j > pos) {
+                                sb.append(s, pos, j);
+                            }
+                            sb.append("&gt;");
+                            pos = j + 1;
+                        } else if (c == '\'') {
+                            if (j > pos) {
+                                sb.append(s, pos, j);
+                            }
+                            sb.append("&#039;"); // &apos;
+                            pos = j + 1;
+                        } else if (c == '&') {
+                            if (j > pos) {
+                                sb.append(s, pos, j);
+                            }
+                            sb.append("&amp;");
+                            pos = j + 1;
+                        } else if (c == '"') {
+                            if (j > pos) {
+                                sb.append(s, pos, j);
+                            }
+                            sb.append("&#034;"); // &quot;
+                            pos = j + 1;
+                        }
+                    }
+                }
+                if (pos < len) {
+                    sb.append(s, pos, len);
+                }
+                return sb.toString();
             }
         }
-        return sb.toString();
+        return s;
     }
 
     /**

Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1403104&r1=1403103&r2=1403104&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Sun Oct 28 21:57:54 2012
@@ -99,6 +99,10 @@
   <subsection name="Jasper">
     <changelog>
       <scode>
+        <bug>53867</bug>: Optimise the XML escaping provided by the PageContext
+        implementation. Based on a patch by Sheldon Shao. (markt)
+      </scode>
+      <scode>
         <bug>53896</bug>: Use an optimised CompositeELResolver for Jasper that
         skips resolvers that are known to be unable to resolve the value. Patch
         by Jarek Gawor. (markt)



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

Reply via email to