You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2019/06/17 14:11:18 UTC

svn commit: r1861511 - in /velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime: ./ directive/ parser/node/

Author: cbrisson
Date: Mon Jun 17 14:11:18 2019
New Revision: 1861511

URL: http://svn.apache.org/viewvc?rev=1861511&view=rev
Log:
[engine][VELOCITY-917] RuntimeServices shouldn't directly expose configured parser chars

Added:
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java
Modified:
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java

Added: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java?rev=1861511&view=auto
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java (added)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java Mon Jun 17 14:11:18 2019
@@ -0,0 +1,116 @@
+package org.apache.velocity.runtime;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Class gathering configured replacement characters for a specific parser class.
+ * @since 2.2
+ */
+
+public class ParserConfiguration
+{
+    /**
+     * Configured replacement character for '$'
+     */
+    private char dollar = '$';
+
+    /**
+     * Configured replacement character for '#'
+     */
+    private char hash = '#';
+
+    /**
+     * Configured replacement character for '@'
+     */
+    private char at = '@';
+
+    /**
+     * Configured replacement character for '*'
+     */
+    private char asterisk = '*';
+
+    /**
+     * Getter for '$' configured replacement character
+     * @return configured replacement character for '$'
+     */
+    public char getDollarChar()
+    {
+        return dollar;
+    }
+
+    /**
+     * Setter for '$' configured replacement character
+     */
+    void setDollarChar(char dollar)
+    {
+        this.dollar = dollar;
+    }
+
+    /**
+     * Getter for '#' configured replacement character
+     * @return configured replacement character for '#'
+     */
+    public char getHashChar()
+    {
+        return hash;
+    }
+
+    /**
+     * Setter for '#' configured replacement character
+     */
+    void setHashChar(char hash)
+    {
+        this.hash = hash;
+    }
+
+    /**
+     * Getter for '@' configured replacement character
+     * @return configured replacement character for '@'
+     */
+    public char getAtChar()
+    {
+        return at;
+    }
+
+    /**
+     * Setter for '@' configured replacement character
+     */
+    void setAtChar(char at)
+    {
+        this.at = at;
+    }
+
+    /**
+     * Getter for '*' configured replacement character
+     * @return configured replacement character for '*'
+     */
+    public char getAsteriskChar()
+    {
+        return asterisk;
+    }
+
+    /**
+     * Setter for '*' configured replacement character
+     */
+    void setAsteriskChar(char asterisk)
+    {
+        this.asterisk = asterisk;
+    }
+}

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java Mon Jun 17 14:11:18 2019
@@ -40,7 +40,6 @@ import org.apache.velocity.runtime.direc
 import org.apache.velocity.runtime.parser.LogContext;
 import org.apache.velocity.runtime.parser.ParseException;
 import org.apache.velocity.runtime.parser.Parser;
-import org.apache.velocity.runtime.parser.StandardParser;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.node.SimpleNode;
 import org.apache.velocity.runtime.resource.ContentResource;
@@ -236,29 +235,10 @@ public class RuntimeInstance implements
     private Constructor parserConstructor;
 
     /**
-     * Configured '$' character
+     * Configured replacement characters in parser grammar
      * @since 2.2
      */
-    private char dollar = '$';
-
-    /**
-     * Configured '#' character
-     * @since 2.2
-     */
-    private char hash = '$';
-
-    /**
-     * Configured '@' character
-     * @since 2.2
-     */
-    private char at = '@';
-
-    /**
-     * Configured '*' character
-     * @since 2.2
-     */
-    private char asterisk = '*';
-
+    private ParserConfiguration parserConfiguration;
 
     /**
      * Creates a new RuntimeInstance object.
@@ -355,10 +335,7 @@ public class RuntimeInstance implements
         this.runtimeDirectivesShared = null;
         this.uberSpect = null;
         this.stringInterning = false;
-        this.dollar = '$';
-        this.hash = '#';
-        this.at = '@';
-        this.asterisk = '*';
+        this.parserConfiguration = new ParserConfiguration();
 
         /*
          *  create a VM factory, introspector, and application attributes
@@ -1271,10 +1248,11 @@ public class RuntimeInstance implements
              * test parser creation and use generated parser to fill up customized characters
              */
             Parser parser = parserPool.get();
-            dollar = parser.dollar();
-            hash = parser.hash();
-            at = parser.at();
-            asterisk = parser.asterisk();
+            parserConfiguration = new ParserConfiguration();
+            parserConfiguration.setDollarChar(parser.dollar());
+            parserConfiguration.setHashChar(parser.hash());
+            parserConfiguration.setAtChar(parser.at());
+            parserConfiguration.setAsteriskChar(parser.asterisk());
             parserPool.put(parser);
         }
         else
@@ -1622,7 +1600,7 @@ public class RuntimeInstance implements
         }
 
         /* now just create the VM call, and use evaluate */
-        StringBuilder template = new StringBuilder(String.valueOf(hash));
+        StringBuilder template = new StringBuilder(String.valueOf(parserConfiguration.getHashChar()));
         template.append(vmName);
         template.append("(");
          for (String param : params)
@@ -1998,26 +1976,8 @@ public class RuntimeInstance implements
     }
 
     @Override
-    public char dollar()
-    {
-        return dollar;
-    }
-
-    @Override
-    public char hash()
-    {
-        return hash;
-    }
-
-    @Override
-    public char at()
-    {
-        return at;
-    }
-
-    @Override
-    public char asterisk()
+    public ParserConfiguration getParserConfiguration()
     {
-        return asterisk;
+        return parserConfiguration;
     }
 }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java Mon Jun 17 14:11:18 2019
@@ -491,30 +491,9 @@ public interface RuntimeServices
    boolean isScopeControlEnabled(String scopeName);
 
     /**
-     * Get the character configured for '$'
-     * @return configured character for '$', or '$'
+     * Get the replacement characters configured for this runtime service's parser
+     * @return configured replacement characters
      * @since 2.2
      */
-    char dollar();
-
-    /**
-     * Get the character configured for '#'
-     * @return configured character for '#', or '#'
-     * @since 2.2
-     */
-    char hash();
-
-    /**
-     * Get the character configured for '@'
-     * @return configured character for '@', or '@'
-     * @since 2.2
-     */
-    char at();
-
-    /**
-     * Get the character configured for '*'
-     * @return configured character for '*', or '*'
-     * @since 2.2
-     */
-    char asterisk();
+    ParserConfiguration getParserConfiguration();
 }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java Mon Jun 17 14:11:18 2019
@@ -220,7 +220,7 @@ public class Macro extends Directive
 
             // trim off the leading $ for the args after the macro name.
             // saves everyone else from having to do it
-            if (i > 0 && macroArg.name.startsWith(String.valueOf(rsvc.dollar())))
+            if (i > 0 && macroArg.name.startsWith(String.valueOf(rsvc.getParserConfiguration().getDollarChar())))
             {
                 macroArg.name = macroArg.name.substring(1);
             }
@@ -273,10 +273,10 @@ public class Macro extends Directive
     {
         StringBuilder ret = (buf == null) ? new StringBuilder() : buf;
 
-        ret.append(rsvc.hash()).append(macroArgs.get(0).name).append("( ");
+        ret.append(rsvc.getParserConfiguration().getHashChar()).append(macroArgs.get(0).name).append("( ");
         for (MacroArg marg : macroArgs)
         {
-            ret.append(rsvc.dollar()).append(marg.name);
+            ret.append(rsvc.getParserConfiguration().getDollarChar()).append(marg.name);
             if (marg.defaultVal != null)
             {
               ret.append("=").append(marg.defaultVal);

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java Mon Jun 17 14:11:18 2019
@@ -141,7 +141,7 @@ public class RuntimeMacro extends Direct
          */
         // Tokens can be used here since we are in init() and Tokens have not been dropped yet
         Token t = node.getLastToken();
-        if (t.image.startsWith(")") || t.image.startsWith(rsvc.hash() + "end"))
+        if (t.image.startsWith(")") || t.image.startsWith(rsvc.getParserConfiguration().getHashChar() + "end"))
         {
             strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
         }
@@ -195,7 +195,7 @@ public class RuntimeMacro extends Direct
             int pos = -1;
             while (t != null && t != node.getLastToken())
             {
-                if (pos == -1) pos = t.image.lastIndexOf(rsvc.hash());
+                if (pos == -1) pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar());
                 if (pos != -1)
                 {
                     buffer.append(t.image.substring(pos));
@@ -210,7 +210,7 @@ public class RuntimeMacro extends Direct
 
             if (t != null)
             {
-                if (pos == -1) pos = t.image.lastIndexOf(rsvc.hash());
+                if (pos == -1) pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar());
                 if (pos != -1)
                 {
                     buffer.append(t.image.substring(pos));

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java Mon Jun 17 14:11:18 2019
@@ -112,7 +112,7 @@ public class ASTDirective extends Simple
              */
             t = getFirstToken();
             int pos = -1;
-            while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1)
+            while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1)
             {
                 t = t.next;
             }
@@ -147,7 +147,7 @@ public class ASTDirective extends Simple
                 directive.setLocation(t.beginLine, t.beginColumn, getTemplate());
                 directive.init(rsvc, context, this);
             }
-            else if( directiveName.startsWith(String.valueOf(rsvc.at())) )
+            else if( directiveName.startsWith(String.valueOf(rsvc.getParserConfiguration().getAtChar())) )
             {
                 if( this.jjtGetNumChildren() > 0 )
                 {
@@ -312,7 +312,7 @@ public class ASTDirective extends Simple
         {
             writer.write(prefix);
             writer.write(morePrefix);
-            writer.write(rsvc.hash());
+            writer.write(rsvc.getParserConfiguration().getHashChar());
             writer.write(directiveName);
             writer.write(postfix);
         }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java Mon Jun 17 14:11:18 2019
@@ -93,7 +93,7 @@ public class ASTIfStatement extends Simp
          */
         Token t = getFirstToken();
         int pos = -1;
-        while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1)
+        while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1)
         {
             t = t.next;
         }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java Mon Jun 17 14:11:18 2019
@@ -322,7 +322,7 @@ public class ASTReference extends Simple
                             (!onlyTestingReference || warnInvalidTestedReferences)))
                 {
                     result = EventHandlerUtil.invalidGetMethod(rsvc, context,
-                            rsvc.dollar() + rootString, null, null, uberInfo);
+                            rsvc.getParserConfiguration().getDollarChar() + rootString, null, null, uberInfo);
                 }
 
                 return result;
@@ -388,7 +388,7 @@ public class ASTReference extends Simple
                             (!onlyTestingReference || warnInvalidTestedReferences || numChildren > 0))
                         {
                             result = EventHandlerUtil.invalidGetMethod(rsvc, context,
-                                    rsvc.dollar() + rootString, previousResult, null, uberInfo);
+                                    rsvc.getParserConfiguration().getDollarChar() + rootString, previousResult, null, uberInfo);
                         }
                     }
                     else
@@ -402,7 +402,7 @@ public class ASTReference extends Simple
                             (referenceType != QUIET_REFERENCE || warnInvalidQuietReferences) &&
                             (!onlyTestingReference || warnInvalidTestedReferences || failedChild < numChildren - 1))
                         {
-                            StringBuilder name = new StringBuilder(String.valueOf(rsvc.dollar())).append(rootString);
+                            StringBuilder name = new StringBuilder(String.valueOf(rsvc.getParserConfiguration().getDollarChar())).append(rootString);
                             for (int i = 0; i <= failedChild; i++)
                             {
                                 Node node = jjtGetChild(i);
@@ -953,7 +953,7 @@ public class ASTReference extends Simple
             int i = 0;
             int len = t.image.length();
 
-            i = t.image.indexOf(rsvc.dollar());
+            i = t.image.indexOf(rsvc.getParserConfiguration().getDollarChar());
 
             if (i == -1)
             {
@@ -1037,7 +1037,7 @@ public class ASTReference extends Simple
          *  last $
          */
 
-        int loc1 = t.image.lastIndexOf(rsvc.dollar());
+        int loc1 = t.image.lastIndexOf(rsvc.getParserConfiguration().getDollarChar());
 
         /*
          *  if we have extra stuff, loc > 0

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java Mon Jun 17 14:11:18 2019
@@ -115,7 +115,7 @@ public class ASTSetDirective extends Sim
              */
             Token t = getFirstToken();
             int pos = -1;
-            while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1)
+            while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1)
             {
                 t = t.next;
             }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java?rev=1861511&r1=1861510&r2=1861511&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java Mon Jun 17 14:11:18 2019
@@ -96,8 +96,8 @@ public class ASTStringLiteral extends Si
         interpolate = rsvc.getBoolean(
                 RuntimeConstants.INTERPOLATE_STRINGLITERALS, true)
                 && getFirstToken().image.startsWith("\"")
-                && ((getFirstToken().image.indexOf(rsvc.dollar()) != -1) || (getFirstToken().image
-                        .indexOf(rsvc.hash()) != -1));
+                && ((getFirstToken().image.indexOf(rsvc.getParserConfiguration().getDollarChar()) != -1) || (getFirstToken().image
+                        .indexOf(rsvc.getParserConfiguration().getHashChar()) != -1));
 
         /*
          * get the contents of the string, minus the '/" at each end