You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/08/13 01:52:42 UTC

svn commit: r685382 - in /velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node: ASTStringLiteral.java NodeUtils.java SetPropertyExecutor.java

Author: nbubna
Date: Tue Aug 12 16:52:42 2008
New Revision: 685382

URL: http://svn.apache.org/viewvc?rev=685382&view=rev
Log:
use StrBuilder for better performance (thx to Jarkko Viinamaki in patch for VELOCITY-607)

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SetPropertyExecutor.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java?rev=685382&r1=685381&r2=685382&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java Tue Aug 12 16:52:42 2008
@@ -21,6 +21,7 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 
+import org.apache.commons.lang.text.StrBuilder;
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
@@ -184,7 +185,7 @@
         int u = string.indexOf("\\u");
         if (u < 0) return string;
 
-        StringBuffer result = new StringBuffer();
+        StrBuilder result = new StrBuilder();
         
         int lastCopied = 0;
 

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java?rev=685382&r1=685381&r2=685382&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java Tue Aug 12 16:52:42 2008
@@ -19,6 +19,7 @@
  * under the License.    
  */
 
+import org.apache.commons.lang.text.StrBuilder;
 import org.apache.velocity.context.Context;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.runtime.parser.ParserConstants;
@@ -46,13 +47,13 @@
      */
     public static String specialText(Token t)
     {
-        StringBuffer specialText = new StringBuffer();
-
         if (t.specialToken == null || t.specialToken.image.startsWith("##") )
         {
             return "";
         }
 
+        StrBuilder specialText = new StrBuilder();
+
         Token tmp_t = t.specialToken;
 
         while (tmp_t.specialToken != null)
@@ -64,9 +65,9 @@
         {
             String st = tmp_t.image;
 
-            StringBuffer sb = new StringBuffer();
+            StrBuilder sb = new StrBuilder();
 
-            for(int i = 0; i < st.length(); i++)
+            for(int i = 0, is = st.length(); i < is; i++)
             {
                 char c = st.charAt(i);
 
@@ -87,7 +88,7 @@
                     boolean term = false;
 
                     int j = i;
-                    for( ok = true; ok && j < st.length(); j++)
+                    for( ok = true; ok && j < is; j++)
                     {
                         char cc = st.charAt( j );
 
@@ -124,16 +125,7 @@
                 }
             }
 
-            // This is a potential JDK 1.3/JDK 1.4 gotcha. If we remove
-            // the toString() method call, then when compiling under JDK 1.4,
-            // this will be mapped to StringBuffer.append(StringBuffer) and
-            // under JDK 1.3, it will be mapped to StringBuffer.append(Object).
-            // So the JDK 1.4 compiled jar will bomb out under JDK 1.3 with a
-            // MethodNotFound error.
-            //
-            // @todo Once we are JDK 1.4+ only, remove the toString(), make this
-            // loop perform a little bit better.
-            specialText.append(sb.toString());
+            specialText.append(sb);
 
             tmp_t = tmp_t.next;
         }
@@ -171,6 +163,9 @@
      * And the string literal argument will
      * be transformed into "candy.jpg" before
      * the method is executed.
+     * 
+     * @deprecated this method isn't called by any class
+     * 
      * @param argStr
      * @param vars
      * @return Interpoliation result.
@@ -178,43 +173,46 @@
      */
     public static String interpolate(String argStr, Context vars) throws MethodInvocationException
     {
-        StringBuffer argBuf = new StringBuffer();
+        // if there's nothing to replace, skip this (saves buffer allocation)
+        if( argStr.indexOf('$') == -1 )
+            return argStr;
+        
+        StrBuilder argBuf = new StrBuilder();
 
-        for (int cIdx = 0 ; cIdx < argStr.length();)
+        for (int cIdx = 0, is = argStr.length(); cIdx < is;)
         {
             char ch = argStr.charAt(cIdx);
-
-            switch (ch)
+            
+            if( ch == '$' )
             {
-                case '$':
-                    StringBuffer nameBuf = new StringBuffer();
-                    for (++cIdx ; cIdx < argStr.length(); ++cIdx)
-                    {
-                        ch = argStr.charAt(cIdx);
-                        if (ch == '_' || ch == '-'
-                            || Character.isLetterOrDigit(ch))
-                            nameBuf.append(ch);
-                        else if (ch == '{' || ch == '}')
-                            continue;
-                        else
-                            break;
-                    }
-
-                    if (nameBuf.length() > 0)
-                    {
-                        Object value = vars.get(nameBuf.toString());
+                StrBuilder nameBuf = new StrBuilder();
+                for (++cIdx ; cIdx < is; ++cIdx)
+                {
+                    ch = argStr.charAt(cIdx);
+                    if (ch == '_' || ch == '-'
+                        || Character.isLetterOrDigit(ch))
+                        nameBuf.append(ch);
+                    else if (ch == '{' || ch == '}')
+                        continue;
+                    else
+                        break;
+                }
 
-                        if (value == null)
-                            argBuf.append("$").append(nameBuf.toString());
-                        else
-                            argBuf.append(value.toString());
-                    }
-                    break;
+                if (nameBuf.length() > 0)
+                {
+                    Object value = vars.get(nameBuf.toString());
 
-                default:
-                    argBuf.append(ch);
-                    ++cIdx;
-                    break;
+                    if (value == null)
+                        argBuf.append("$").append(nameBuf.toString());
+                    else
+                        argBuf.append(value.toString());
+                }
+                
+            }
+            else
+            {
+                argBuf.append(ch);
+                ++cIdx;
             }
         }
 

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SetPropertyExecutor.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SetPropertyExecutor.java?rev=685382&r1=685381&r2=685382&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SetPropertyExecutor.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SetPropertyExecutor.java Tue Aug 12 16:52:42 2008
@@ -22,6 +22,7 @@
 import java.lang.reflect.InvocationTargetException;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.text.StrBuilder;
 import org.apache.velocity.runtime.log.Log;
 import org.apache.velocity.util.introspection.Introspector;
 
@@ -78,7 +79,7 @@
 
         try
         {
-            StringBuffer sb = new StringBuffer("set");
+            StrBuilder sb = new StrBuilder("set");
             sb.append(property);
 
             setMethod(introspector.getMethod(clazz, sb.toString(), params));