You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2016/06/09 20:25:15 UTC

svn commit: r1747593 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl3/JxltEngine.java main/java/org/apache/commons/jexl3/internal/TemplateScript.java test/java/org/apache/commons/jexl3/PragmaTest.java

Author: henrib
Date: Thu Jun  9 20:25:15 2016
New Revision: 1747593

URL: http://svn.apache.org/viewvc?rev=1747593&view=rev
Log:
JEXL-198: 
added the submitted code & test (thanks Terefang) to trunk 

Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JxltEngine.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateScript.java
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PragmaTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JxltEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JxltEngine.java?rev=1747593&r1=1747592&r2=1747593&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JxltEngine.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JxltEngine.java Thu Jun  9 20:25:15 2016
@@ -21,20 +21,21 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.io.Writer;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
  * A simple "JeXL Template" engine.
- * 
+ *
  * <p>At the base is an evaluator similar to the Unified EL evaluator used in JSP/JSF based on JEXL.
  * At the top is a template engine inspired by Velocity that uses JEXL (instead of OGNL/VTL) as the scripting
  * language.</p>
- * 
+ *
  * <p>The evaluator is intended to be used in configuration modules, XML based frameworks or JSP taglibs
  * and facilitate the implementation of expression evaluation.</p>
- * 
+ *
  * <p>The template engine is intended to output any form of text; html, XML, CSV...</p>
- * 
+ *
  * @since 3.0
  */
 public abstract class JxltEngine {
@@ -49,7 +50,7 @@ public abstract class JxltEngine {
 
         /**
          * Creates an Exception.
-         * 
+         *
          * @param info the contextual information
          * @param msg the exception message
          * @param cause the exception cause
@@ -67,57 +68,57 @@ public abstract class JxltEngine {
      *   <li>The "nested" syntax is of the form <code>"...#{...${jexl-expr0}...}..."</code></li>
      *   <li>The "composite" syntax is of the form <code>"...${jexl-expr0}... #{jexl-expr1}..."</code></li>
      * </ul>
-     * 
+     *
      * <p>Deferred and immediate expression carry different intentions:</p>
-     * 
+     *
      * <ul>
      *   <li>An immediate expression indicate that evaluation is intended to be performed close to
      *       the definition/parsing point.</li>
      *   <li>A deferred expression indicate that evaluation is intended to occur at a later stage.</li>
      * </ul>
-     * 
+     *
      * <p>For instance: <code>"Hello ${name}, now is #{time}"</code> is a composite "deferred" expression since one
      * of its subexpressions is deferred. Furthermore, this (composite) expression intent is
      * to perform two evaluations; one close to its definition and another one in a later
      * phase.</p>
-     * 
+     *
      * <p>The API reflects this feature in 2 methods, prepare and evaluate. The prepare method
      * will evaluate the immediate subexpression and return an expression that contains only
      * the deferred subexpressions (and constants), a prepared expression. Such a prepared expression
      * is suitable for a later phase evaluation that may occur with a different JexlContext.
      * Note that it is valid to call evaluate without prepare in which case the same JexlContext
      * is used for the 2 evaluation phases.</p>
-     * 
+     *
      * <p>In the most common use-case where deferred expressions are to be kept around as properties of objects,
      * one should createExpression and prepare an expression before storing it and evaluate it each time
      * the property storing it is accessed.</p>
-     * 
+     *
      * <p>Note that nested expression use the JEXL syntax as in:</p>
-     * 
+     *
      * <blockquote><code>"#{${bar}+'.charAt(2)'}"</code></blockquote>
-     * 
+     *
      * <p>The most common mistake leading to an invalid expression being the following:</p>
-     * 
+     *
      * <blockquote><code>"#{${bar}charAt(2)}"</code></blockquote>
-     *  
+     *
      * <p>Also note that methods that createExpression evaluate expressions may throw <em>unchecked</em> exceptions;
      * The {@link JxltEngine.Exception} are thrown when the engine instance is in "non-silent" mode
      * but since these are RuntimeException, user-code <em>should</em> catch them where appropriate.</p>
-     * 
+     *
      * @since 2.0
      */
     public interface Expression {
 
         /**
          * Generates this expression's string representation.
-         * 
+         *
          * @return the string representation
          */
         String asString();
 
         /**
          * Adds this expression's string representation to a StringBuilder.
-         * 
+         *
          * @param strb the builder to fill
          * @return the builder argument
          */
@@ -125,9 +126,9 @@ public abstract class JxltEngine {
 
         /**
          * Evaluates this expression.
-         * 
+         *
          * <p>If the underlying JEXL engine is silent, errors will be logged through its logger as warning.</p>
-         * 
+         *
          * @param context the variable context
          * @return the result of this expression evaluation or null if an error occurs and the {@link JexlEngine} is
          * running in silent mode
@@ -142,7 +143,7 @@ public abstract class JxltEngine {
          * If this expression was prepared, this allows to retrieve the
          * original expression that lead to it.</p>
          * <p>Other expressions return themselves.</p>
-         * 
+         *
          * @return the source expression
          */
         Expression getSource();
@@ -151,7 +152,7 @@ public abstract class JxltEngine {
          * Gets the list of variables accessed by this expression.
          * <p>This method will visit all nodes of the sub-expressions and extract all variables whether they
          * are written in 'dot' or 'bracketed' notation. (a.b is equivalent to a['b']).</p>
-         * 
+         *
          * @return the set of variables, each as a list of strings (ant-ish variables use more than 1 string)
          * or the empty set if no variables are used
          */
@@ -159,32 +160,32 @@ public abstract class JxltEngine {
 
         /**
          * Checks whether this expression is deferred.
-         * 
+         *
          * @return true if deferred, false otherwise
          */
         boolean isDeferred();
 
         /**
          * Checks whether this expression is immediate.
-         * 
+         *
          * @return true if immediate, false otherwise
          */
         boolean isImmediate();
 
         /**
          * Evaluates the immediate sub-expressions.
-         * 
+         *
          * <p>When the expression is dependant upon immediate and deferred sub-expressions,
          * evaluates the immediate sub-expressions with the context passed as parameter
          * and returns this expression deferred form.</p>
-         * 
+         *
          * <p>In effect, this binds the result of the immediate sub-expressions evaluation in the
          * context, allowing to differ evaluation of the remaining (deferred) expression within another context.
          * This only has an effect to nested and composite expressions that contain differed and
          * immediate sub-expressions.</p>
-         * 
+         *
          * <p>If the underlying JEXL engine is silent, errors will be logged through its logger as warning.* </p>
-         * 
+         *
          * @param context the context to use for immediate expression evaluations
          * @return an {@link Expression} or null if an error occurs and the {@link JexlEngine} is running
          * in silent mode
@@ -195,7 +196,7 @@ public abstract class JxltEngine {
         /**
          * Formats this expression, adding its source string representation in
          * comments if available: 'expression /*= source *\/'' .
-         * 
+         *
          * @return the formatted expression string
          */
         @Override
@@ -205,9 +206,9 @@ public abstract class JxltEngine {
     /**
      * Creates a a {@link Expression} from an expression string.
      * Uses and fills up the expression cache if any.
-     * 
+     *
      * <p>If the underlying JEXL engine is silent, errors will be logged through its logger as warnings.</p>
-     * 
+     *
      * @param expression the {@link Template} string expression
      * @return the {@link Expression}, null if silent and an error occurred
      * @throws Exception if an error occurs and the {@link JexlEngine} is not silent
@@ -219,9 +220,9 @@ public abstract class JxltEngine {
     /**
      * Creates a a {@link Expression} from an expression string.
      * Uses and fills up the expression cache if any.
-     * 
+     *
      * <p>If the underlying JEXL engine is silent, errors will be logged through its logger as warnings.</p>
-     * 
+     *
      * @param info the {@link JexlInfo} source information
      * @param expression the {@link Template} string expression
      * @return the {@link Expression}, null if silent and an error occured
@@ -249,9 +250,9 @@ public abstract class JxltEngine {
      * $$   }
      * $$ }
      * </pre></blockquote>
-     * 
+     *
      * <p>Will evaluate as:</p>
-     * 
+     *
      * <blockquote><pre>
      * The value 1 is under fourty-two
      * The value 3 is under fourty-two
@@ -259,34 +260,34 @@ public abstract class JxltEngine {
      * Life, the universe, and everything
      * The value 169 is over fourty-two
      * </pre></blockquote>
-     * 
+     *
      * <p>During evaluation, the template context exposes its writer as '$jexl' which is safe to use in this case.
      * This allows writing directly through the writer without adding new-lines as in:</p>
-     * 
+     *
      * <blockquote><pre>
      * $$ for(var cell : cells) { $jexl.print(cell); $jexl.print(';') }
      * </pre></blockquote>
-     * 
+     *
      * <p>A template is expanded as one JEXL script and a list of template expressions; each template expression is
      * being replaced in the script by a call to jexl:print(expr) (the expr is in fact the expr number in the template).
      * This integration uses a specialized JexlContext (TemplateContext) that serves as a namespace (for jexl:)
      * and stores the template expression array and the writer (java.io.Writer) that the 'jexl:print(...)'
      * delegates the output generation to.</p>
-     * 
+     *
      * @since 3.0
      */
     public interface Template {
 
         /**
          * Recreate the template source from its inner components.
-         * 
+         *
          * @return the template source rewritten
          */
         String asString();
 
         /**
          * Evaluates this template.
-         * 
+         *
          * @param context the context to use during evaluation
          * @param writer the writer to use for output
          */
@@ -294,7 +295,7 @@ public abstract class JxltEngine {
 
         /**
          * Evaluates this template.
-         * 
+         *
          * @param context the context to use during evaluation
          * @param writer the writer to use for output
          * @param args the arguments
@@ -303,7 +304,7 @@ public abstract class JxltEngine {
 
         /**
          * Prepares this template by expanding any contained deferred TemplateExpression.
-         * 
+         *
          * @param context the context to prepare against
          * @return the prepared version of the template
          */
@@ -313,7 +314,7 @@ public abstract class JxltEngine {
          * Gets the list of variables accessed by this template.
          * <p>This method will visit all nodes of the sub-expressions and extract all variables whether they
          * are written in 'dot' or 'bracketed' notation. (a.b is equivalent to a['b']).</p>
-         * 
+         *
          * @return the set of variables, each as a list of strings (ant-ish variables use more than 1 string)
          * or the empty set if no variables are used
          */
@@ -321,15 +322,22 @@ public abstract class JxltEngine {
 
         /**
          * Gets the list of parameters expected by this template.
-         * 
+         *
          * @return the parameter names array
          */
         String[] getParameters();
+
+        /**
+         * Gets this script pragmas.
+         *
+         * @return the (non null, may be empty) pragmas map
+         */
+        Map<String, Object> getPragmas();
     }
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param info the jexl info (file, line, column)
      * @param prefix the directive prefix
      * @param source the source
@@ -340,7 +348,7 @@ public abstract class JxltEngine {
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param info the source info
      * @param parms the parameter names
      * @param source the source
@@ -352,7 +360,7 @@ public abstract class JxltEngine {
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param info the source info
      * @param source the source
      * @return the template
@@ -363,7 +371,7 @@ public abstract class JxltEngine {
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param prefix the directive prefix
      * @param source the source
      * @param parms the parameter names
@@ -375,7 +383,7 @@ public abstract class JxltEngine {
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param source the source
      * @param parms the parameter names
      * @return the template
@@ -386,7 +394,7 @@ public abstract class JxltEngine {
 
     /**
      * Creates a new template.
-     * 
+     *
      * @param source the source
      * @return the template
      */
@@ -396,7 +404,7 @@ public abstract class JxltEngine {
 
     /**
      * Gets the {@link JexlEngine} underlying this template engine.
-     * 
+     *
      * @return the JexlEngine
      */
     public abstract JexlEngine getEngine();

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateScript.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateScript.java?rev=1747593&r1=1747592&r2=1747593&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateScript.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateScript.java Thu Jun  9 20:25:15 2016
@@ -27,6 +27,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -207,4 +208,8 @@ public final class TemplateScript implem
         return script.getParameters();
     }
 
+    @Override
+    public Map<String, Object> getPragmas() {
+        return script.getPragmas();
+    }
 }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PragmaTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PragmaTest.java?rev=1747593&r1=1747592&r2=1747593&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PragmaTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PragmaTest.java Thu Jun  9 20:25:15 2016
@@ -39,16 +39,31 @@ public class PragmaTest extends JexlTest
     public void testPragmas() throws Exception {
         JexlContext jc = new MapContext();
         try {
-        JexlScript script = JEXL.createScript("#pragma one 1\n#pragma the.very.hard 'truth'\n2;");
-        Assert.assertTrue(script != null);
-        Map<String, Object> pragmas = script.getPragmas();
-        Assert.assertEquals(2, pragmas.size());
-        Assert.assertEquals(1, pragmas.get("one"));
-        Assert.assertEquals("truth", pragmas.get("the.very.hard"));
-        } catch(JexlException xjexl) {
+            JexlScript script = JEXL.createScript("#pragma one 1\n#pragma the.very.hard 'truth'\n2;");
+            Assert.assertTrue(script != null);
+            Map<String, Object> pragmas = script.getPragmas();
+            Assert.assertEquals(2, pragmas.size());
+            Assert.assertEquals(1, pragmas.get("one"));
+            Assert.assertEquals("truth", pragmas.get("the.very.hard"));
+        } catch (JexlException xjexl) {
             String s = xjexl.toString();
         }
     }
 
+    @Test
+    public void testJxltPragmas() throws Exception {
+        JexlContext jc = new MapContext();
+        try {
+            JxltEngine engine = new JexlBuilder().create().createJxltEngine();
+            JxltEngine.Template tscript = engine.createTemplate("$$ #pragma one 1\n$$ #pragma the.very.hard 'truth'\n2;");
+            Assert.assertTrue(tscript != null);
+            Map<String, Object> pragmas = tscript.getPragmas();
+            Assert.assertEquals(2, pragmas.size());
+            Assert.assertEquals(1, pragmas.get("one"));
+            Assert.assertEquals("truth", pragmas.get("the.very.hard"));
+        } catch (JexlException xjexl) {
+            String s = xjexl.toString();
+        }
+    }
 
 }