Author: henrib Date: Thu Jun 11 11:41:43 2015 New Revision: 1684857 URL: http://svn.apache.org/r1684857 Log: JEXL: Added method to get template parameters
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/TemplateEngine.java commons/proper/jexl/trunk/src/site/xdoc/changes.xml commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JXLTTest.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=1684857&r1=1684856&r2=1684857&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 11 11:41:43 2015 @@ -297,13 +297,18 @@ public abstract class JxltEngine { Template prepare(JexlContext context); /** - * Gets the list of variables accessed by this expression. + * 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 */ Set<List<String>> getVariables(); + + /** + * Gets the list of parameters expected by this template. + */ + String[] getParameters(); } /** Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java?rev=1684857&r1=1684856&r2=1684857&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java Thu Jun 11 11:41:43 2015 @@ -1058,13 +1058,6 @@ public final class TemplateEngine extend interpreter.interpret(script); } - /** - * 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 - */ @Override public Set<List<String>> getVariables() { VarCollector collector = new VarCollector(); @@ -1073,6 +1066,11 @@ public final class TemplateEngine extend } return collector.collected(); } + + @Override + public String[] getParameters() { + return script.getParameters(); + } } /** Modified: commons/proper/jexl/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/changes.xml?rev=1684857&r1=1684856&r2=1684857&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Thu Jun 11 11:41:43 2015 @@ -26,6 +26,12 @@ </properties> <body> <release version="3.0" date="unreleased"> + <action dev="henrib" type="add" > + Added a method to get parameters from a template + </action> + <action dev="henrib" type="fix" > + Identifiers beginning with 0s (like 00200) shoud not be considered integers + </action> <action dev="henrib" type="add" issue="JEXL-152" due-to="Dmitri Blinov"> Extend Range literal to support Long values </action> Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java?rev=1684857&r1=1684856&r2=1684857&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java Thu Jun 11 11:41:43 2015 @@ -30,6 +30,7 @@ import org.junit.Before; * * @since 2.0 */ +@SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"}) public class ArrayAccessTest extends JexlTestCase { private Asserter asserter; Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JXLTTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JXLTTest.java?rev=1684857&r1=1684856&r2=1684857&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JXLTTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JXLTTest.java Thu Jun 11 11:41:43 2015 @@ -100,7 +100,8 @@ public class JXLTTest extends JexlTestCa super("JXLTTest"); } - @Test public void testStatement() throws Exception { + @Test + public void testStatement() throws Exception { Froboz froboz = new Froboz(32); context.set("froboz", froboz); JxltEngine.Expression check = JXLT.createExpression("${ froboz.plus10() }"); @@ -111,7 +112,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(1, evars.size()); } - @Test public void testAssign() throws Exception { + @Test + public void testAssign() throws Exception { JxltEngine.Expression assign = JXLT.createExpression("${froboz.value = 10}"); JxltEngine.Expression check = JXLT.createExpression("${froboz.value}"); Object o = assign.evaluate(context); @@ -120,7 +122,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("Result is not 10", new Integer(10), o); } - @Test public void testComposite() throws Exception { + @Test + public void testComposite() throws Exception { String source = "Dear ${p} ${name};"; JxltEngine.Expression expr = JXLT.createExpression(source); context.set("p", "Mr"); @@ -144,7 +147,8 @@ public class JXLTTest extends JexlTestCa return false; } - @Test public void testPrepareEvaluate() throws Exception { + @Test + public void testPrepareEvaluate() throws Exception { final String source = "Dear #{p} ${name};"; JxltEngine.Expression expr = JXLT.createExpression("Dear #{p} ${name};"); Assert.assertTrue("expression should be deferred", expr.isDeferred()); @@ -170,7 +174,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testNested() throws Exception { + @Test + public void testNested() throws Exception { final String source = "#{${hi}+'.world'}"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -187,7 +192,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testImmediate() throws Exception { + @Test + public void testImmediate() throws Exception { JexlContext none = null; final String source = "${'Hello ' + 'World!'}"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -200,7 +206,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testConstant() throws Exception { + @Test + public void testConstant() throws Exception { JexlContext none = null; final String source = "Hello World!"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -212,7 +219,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testConstant2() throws Exception { + @Test + public void testConstant2() throws Exception { JexlContext none = null; final String source = "${size({'map':123,'map2':456})}"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -224,7 +232,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testConstant3() throws Exception { + @Test + public void testConstant3() throws Exception { JexlContext none = null; final String source = "#{size({'map':123,'map2':456})}"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -236,7 +245,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testConstant4() throws Exception { + @Test + public void testConstant4() throws Exception { JexlContext none = null; final String source = "#{ ${size({'1':2,'2': 3})} }"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -248,7 +258,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testDeferred() throws Exception { + @Test + public void testDeferred() throws Exception { JexlContext none = null; final String source = "#{'world'}"; JxltEngine.Expression expr = JXLT.createExpression(source); @@ -261,7 +272,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(source, getSource(expr.toString())); } - @Test public void testEscape() throws Exception { + @Test + public void testEscape() throws Exception { JexlContext none = null; JxltEngine.Expression expr; Object o; @@ -274,21 +286,24 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("${'world'}", o); } - @Test public void testEscapeString() throws Exception { + @Test + public void testEscapeString() throws Exception { JxltEngine.Expression expr = JXLT.createExpression("\\\"${'world\\'s finest'}\\\""); JexlContext none = null; Object o = expr.evaluate(none); Assert.assertEquals("\"world's finest\"", o); } - @Test public void testNonEscapeString() throws Exception { + @Test + public void testNonEscapeString() throws Exception { JxltEngine.Expression expr = JXLT.createExpression("c:\\some\\windows\\path"); JexlContext none = null; Object o = expr.evaluate(none); Assert.assertEquals("c:\\some\\windows\\path", o); } - @Test public void testMalformed() throws Exception { + @Test + public void testMalformed() throws Exception { try { JxltEngine.Expression expr = JXLT.createExpression("${'world'"); JexlContext none = null; @@ -301,7 +316,8 @@ public class JXLTTest extends JexlTestCa } } - @Test public void testMalformedNested() throws Exception { + @Test + public void testMalformedNested() throws Exception { try { JxltEngine.Expression expr = JXLT.createExpression("#{${hi} world}"); JexlContext none = null; @@ -314,7 +330,8 @@ public class JXLTTest extends JexlTestCa } } - @Test public void testMalformedNested2() throws Exception { + @Test + public void testMalformedNested2() throws Exception { try { JxltEngine.Expression expr = JXLT.createExpression("#{${hi} world}"); JexlContext ctxt = new MapContext(); @@ -328,8 +345,8 @@ public class JXLTTest extends JexlTestCa } } - - @Test public void testBadContextNested() throws Exception { + @Test + public void testBadContextNested() throws Exception { try { JxltEngine.Expression expr = JXLT.createExpression("#{${hi}+'.world'}"); JexlContext none = null; @@ -342,7 +359,8 @@ public class JXLTTest extends JexlTestCa } } - @Test public void testCharAtBug() throws Exception { + @Test + public void testCharAtBug() throws Exception { context.set("foo", "abcdef"); JxltEngine.Expression expr = JXLT.createExpression("${foo.substring(2,4)/*comment*/}"); Object o = expr.evaluate(context); @@ -361,7 +379,8 @@ public class JXLTTest extends JexlTestCa } - @Test public void testTemplate0() throws Exception { + @Test + public void testTemplate0() throws Exception { String source = " $$ if(x) {\nx is ${x}\n $$ } else {\n${'no x'}\n$$ }\n"; StringWriter strw; String output; @@ -383,22 +402,28 @@ public class JXLTTest extends JexlTestCa String dstr = t.toString(); Assert.assertNotNull(dstr); } - @Test public void testTemplate10() throws Exception { + + @Test + public void testTemplate10() throws Exception { String source = "$$(x)->{ if(x) {\nx is ${x}\n$$ } else {\n${'no x'}\n$$ } }\n"; StringWriter strw; String output; - JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(source), (String[])null); + JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(source), (String[]) null); String dstr = t.asString(); Assert.assertNotNull(dstr); + String[] ps = t.getParameters(); + Assert.assertTrue(Arrays.asList(ps).contains("x")); + strw = new StringWriter(); t.evaluate(context, strw, 42); output = strw.toString(); Assert.assertEquals("x is 42\n", output); } - @Test public void testTemplate1() throws Exception { + @Test + public void testTemplate1() throws Exception { String source = "$$ if(x) {\nx is ${x}\n$$ } else {\n${'no x'}\n$$ }\n"; StringWriter strw; String output; @@ -418,9 +443,26 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("no x\n", output); } - @Test public void testPrepareTemplate() throws Exception { - String source = - "$$ for(var x : list) {\n" + @Test + public void testTemplate2() throws Exception { + String source = "The answer: ${x}"; + StringWriter strw; + String output; + + JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(source), "x"); + String dstr = t.asString(); + Assert.assertNotNull(dstr); + + strw = new StringWriter(); + t.evaluate(context, strw, 42); + output = strw.toString(); + Assert.assertEquals("The answer: 42", output); + } + + @Test + public void testPrepareTemplate() throws Exception { + String source + = "$$ for(var x : list) {\n" + "${l10n}=#{x}\n" + "$$ }\n"; int[] args = {42}; @@ -448,9 +490,10 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("value=42\n", outEN); } - @Test public void test42() throws Exception { - String test42 = - "$$ for(var x : list) {\n" + @Test + public void test42() throws Exception { + String test42 + = "$$ for(var x : list) {\n" + "$$ if (x == 42) {\n" + "Life, the universe, and everything\n" + "$$ } else if (x > 42) {\n" @@ -464,8 +507,8 @@ public class JXLTTest extends JexlTestCa int[] list = {1, 3, 5, 42, 169}; t.evaluate(context, strw, list); String output = strw.toString(); - String out42 = - "The value 1 is under fourty-two\n" + String out42 + = "The value 1 is under fourty-two\n" + "The value 3 is under fourty-two\n" + "The value 5 is under fourty-two\n" + "Life, the universe, and everything\n" @@ -493,7 +536,8 @@ public class JXLTTest extends JexlTestCa } } - @Test public void testWriter() throws Exception { + @Test + public void testWriter() throws Exception { Froboz froboz = new Froboz(42); Writer writer = new FrobozWriter(new StringWriter()); JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader("$$$jexl.print(froboz)"), "froboz"); @@ -501,9 +545,10 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("froboz{42}", writer.toString()); } - @Test public void testReport() throws Exception { - String rpt = - "<report>\n" + @Test + public void testReport() throws Exception { + String rpt + = "<report>\n" + "\n" + "\n$$ var x = 2;" + "\n" @@ -519,9 +564,10 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(ctl, output); } - @Test public void testReport1() throws Exception { - String rpt = - "<report>\n" + @Test + public void testReport1() throws Exception { + String rpt + = "<report>\n" + "this is ${x}\n" + "${x + 1}\n" + "${x + 2}\n" @@ -533,7 +579,7 @@ public class JXLTTest extends JexlTestCa t.evaluate(context, strw, 42); String output = strw.toString(); int count = 0; - for(int i = 0; i < output.length(); ++i) { + for (int i = 0; i < output.length(); ++i) { char c = output.charAt(i); if ('\n' == c) { count += 1; @@ -542,8 +588,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals(6, count); } - - @Test public void testOneLiner() throws Exception { + @Test + public void testOneLiner() throws Exception { JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader("fourty-two")); StringWriter strw = new StringWriter(); t.evaluate(context, strw); @@ -551,7 +597,8 @@ public class JXLTTest extends JexlTestCa Assert.assertEquals("fourty-two", output); } - @Test public void testOneLinerVar() throws Exception { + @Test + public void testOneLinerVar() throws Exception { JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader("fourty-${x}")); StringWriter strw = new StringWriter(); context.set("x", "two");