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 2022/07/25 06:59:27 UTC

[velocity-engine] branch master updated: Easier #include and #parse directives subclassing - fixes VELOCITY-959

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

cbrisson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/velocity-engine.git


The following commit(s) were added to refs/heads/master by this push:
     new 28c97b43 Easier #include and #parse directives subclassing - fixes VELOCITY-959
28c97b43 is described below

commit 28c97b43c731ea42f87bcbdbd08d08fd08533a58
Author: Claude Brisson <cl...@renegat.net>
AuthorDate: Mon Jul 25 08:59:12 2022 +0200

    Easier #include and #parse directives subclassing - fixes VELOCITY-959
---
 .../apache/velocity/runtime/directive/Include.java | 20 +++++++++---
 .../apache/velocity/runtime/directive/Parse.java   | 37 +++++++++++++++-------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java
index 58fe19b8..1e1abd7b 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java
@@ -240,14 +240,14 @@ public class Include extends InputBase
         try
         {
             if (!blockinput)
-                resource = rsvc.getContent(arg, getInputEncoding(context));
+                resource = getResource(arg, getInputEncoding(context));
         }
         catch ( ResourceNotFoundException rnfe )
         {
             /*
              * the arg wasn't found.  Note it and throw
              */
-            log.error("#include(): cannot find resource '{}', called at {}",
+            log.error("#" + getName() + "(): cannot find resource '{}', called at {}",
                       arg, StringUtils.formatFileString(this));
             throw rnfe;
         }
@@ -257,13 +257,13 @@ public class Include extends InputBase
          */
         catch( RuntimeException e )
         {
-            log.error("#include(): arg = '{}', called at {}",
+            log.error("#" + getName() + "(): arg = '{}', called at {}",
                       arg, StringUtils.formatFileString(this));
             throw e;
         }
         catch (Exception e)
         {
-            String msg = "#include(): arg = '" + arg +
+            String msg = "#" + getName() + "(): arg = '" + arg +
                         "', called at " + StringUtils.formatFileString(this);
             log.error(msg, e);
             throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
@@ -304,4 +304,16 @@ public class Include extends InputBase
             writer.write(outputMsgEnd);
         }
     }
+
+    /**
+     * Find the resource to include
+     * @param path resource path
+     * @param encoding resource encoding
+     * @return found resource
+     * @throws ResourceNotFoundException if resource was not found
+     */
+    protected Resource getResource(String path, String encoding) throws ResourceNotFoundException
+    {
+        return rsvc.getContent(path, encoding);
+    }
 }
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java
index 1d4b9e7b..06dbf13b 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java
@@ -146,7 +146,7 @@ public class Parse extends InputBase
          */
         if ( node.jjtGetNumChildren() == 0 )
         {
-            throw new VelocityException("#parse(): argument missing at " +
+            throw new VelocityException("#" + getName() + "(): argument missing at " +
                 StringUtils.formatFileString(this), null, rsvc.getLogContext().getStackTrace());
         }
 
@@ -156,7 +156,7 @@ public class Parse extends InputBase
         Object value =  node.jjtGetChild(0).value( context );
         if (value == null)
         {
-            log.debug("#parse(): null argument at {}", StringUtils.formatFileString(this));
+            log.debug("#" + getName() + "(): null argument at {}", StringUtils.formatFileString(this));
         }
 
         /*
@@ -174,7 +174,7 @@ public class Parse extends InputBase
          */
         if (strictRef && value == null && arg == null)
         {
-            throw new VelocityException("The argument to #parse returned null at "
+            throw new VelocityException("The argument to #" + getName() + " returned null at "
               + StringUtils.formatFileString(this), null, rsvc.getLogContext().getStackTrace());
         }
 
@@ -217,14 +217,14 @@ public class Parse extends InputBase
 
         try
         {
-            t = rsvc.getTemplate( arg, getInputEncoding(context) );
+            t = getTemplate(arg, getInputEncoding(context));
         }
         catch ( ResourceNotFoundException rnfe )
         {
             /*
              * the arg wasn't found.  Note it and throw
              */
-            log.error("#parse(): cannot find template '{}', called at {}",
+            log.error("#" + getName() + "(): cannot find template '{}', called at {}",
                       arg, StringUtils.formatFileString(this));
             throw rnfe;
         }
@@ -234,7 +234,7 @@ public class Parse extends InputBase
              * the arg was found, but didn't parse - syntax error
              *  note it and throw
              */
-            log.error("#parse(): syntax error in #parse()-ed template '{}', called at {}",
+            log.error("#" + getName() + "(): syntax error in #" + getName() + "()-ed template '{}', called at {}",
                       arg, StringUtils.formatFileString(this));
             throw pee;
         }
@@ -243,13 +243,13 @@ public class Parse extends InputBase
          */
         catch( RuntimeException e )
         {
-            log.error("Exception rendering #parse({}) at {}",
+            log.error("Exception rendering #" + getName() + "({}) at {}",
                       arg, StringUtils.formatFileString(this));
             throw e;
         }
         catch ( Exception e )
         {
-            String msg = "Exception rendering #parse(" + arg + ") at " +
+            String msg = "Exception rendering #" + getName() + "(" + arg + ") at " +
                          StringUtils.formatFileString(this);
             log.error(msg, e);
             throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
@@ -298,13 +298,13 @@ public class Parse extends InputBase
             /*
              * Log #parse errors so the user can track which file called which.
              */
-            log.error("Exception rendering #parse({}) at {}",
+            log.error("Exception rendering #" + getName() + "({}) at {}",
                       arg, StringUtils.formatFileString(this));
             throw e;
         }
         catch ( Exception e )
         {
-            String msg = "Exception rendering #parse(" + arg + ") at " +
+            String msg = "Exception rendering #" + getName() + "(" + arg + ") at " +
                          StringUtils.formatFileString(this);
             log.error(msg, e);
             throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
@@ -332,14 +332,27 @@ public class Parse extends InputBase
     {
         if (argtypes.size() != 1)
         {
-            throw new MacroParseException("The #parse directive requires one argument",
+            throw new MacroParseException("The #" + getName() + " directive requires one argument",
                templateName, t);
         }
 
         if (argtypes.get(0) == ParserTreeConstants.JJTWORD)
         {
-            throw new MacroParseException("The argument to #parse is of the wrong type",
+            throw new MacroParseException("The argument to #" + getName() + " is of the wrong type",
                 templateName, t);
         }
     }
+
+    /**
+     * Find the template to render in the appropriate encoding
+     * @param path template path
+     * @param encoding template encoding
+     * @return found template
+     * @throws ResourceNotFoundException if template was not found
+     * @since 2.4
+     */
+    protected Template getTemplate(String path, String encoding) throws ResourceNotFoundException
+    {
+        return rsvc.getTemplate(path, encoding);
+    }
 }