Author: henrib Date: Wed Dec 7 18:11:57 2011 New Revision: 1211579 URL: http://svn.apache.org/viewvc?rev=1211579&view=rev Log: JEXL-123: Revamped package documentation
Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/package.html Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/package.html URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/package.html?rev=1211579&r1=1211578&r2=1211579&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/package.html (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/package.html Wed Dec 7 18:11:57 2011 @@ -27,6 +27,7 @@ <li><a href="#usage">Using JEXL</a></li> <li><a href="#configuration">Configuring JEXL</a></li> <li><a href="#customization">Customizing JEXL</a></li> + <li><a href="#extension">Extending JEXL</a></li> </ul> <h2><a name="intro">Introduction</a></h2> @@ -37,29 +38,29 @@ <h2><a name="example">A Brief Example</a></h2> <p> - When evaluating expressions, JEXL merges an - {@link org.apache.commons.jexl3.Expression} + In its simplest form, JEXL merges an + {@link org.apache.commons.jexl3.JexlExpression} with a - {@link org.apache.commons.jexl3.JexlContext}. + {@link org.apache.commons.jexl3.JexlContext} when evaluating expressions. An Expression is created using {@link org.apache.commons.jexl3.JexlEngine#createExpression(java.lang.String)}, passing a String containing valid JEXL syntax. A simple JexlContext can be created using a {@link org.apache.commons.jexl3.MapContext} instance; a map of variables that will be internally wrapped can be optionally provided through its constructor. - The following example, takes a variable named foo, and - invokes the bar() method on the property innerFoo: + The following example, takes a variable named 'car', and + invokes the checkStatus() method on the property 'engine' </p> <pre> // Create a JexlEngine (could reuse one instead) JexlEngine jexl = new JexlEngine(); - // Create an expression object - String jexlExp = "foo.innerFoo.bar()"; + // Create an expression object equivalent to 'car.getEngine().checkStatus()': + String jexlExp = "car.engine.checkStatus()"; Expression e = jexl.createExpression( jexlExp ); - + // The car we have to handle coming as an argument... + Car car = theCarThatWeHandle; // Create a context and add data JexlContext jc = new MapContext(); - jc.set("foo", new Foo() ); - + jc.set("car", car ); // Now evaluate the expression, getting the result Object o = e.evaluate(jc); </pre> @@ -74,14 +75,16 @@ </ul> <h3><a name="usage_note">Important note</a></h3> + The public API classes start with 'Jexl*' or 'Jxl*'. The only public packages you should use are: <ul> <li>org.apache.commons.jexl3</li> <li>org.apache.commons.jexl3.introspection</li> </ul> - The following packages follow a "use at your own maintenance cost" policy. + The following packages follow a "use at your own maintenance cost" policy; these are only intended to be used + for extending JEXL. Their classes and methods are not guaranteed to remain compatible in subsequent versions. - If you think you need to use some of their features, it might be a good idea to check with + If you think you need to use directly some of their features or methods, it might be a good idea to check with the community through the mailing list first. <ul> <li>org.apache.commons.jexl3.parser</li> @@ -143,9 +146,10 @@ The main methods are: </p> <ul> - <li>{@link org.apache.commons.jexl3.JexlEngine#createExpression}</li> <li>{@link org.apache.commons.jexl3.JexlEngine#createScript}</li> - <li>{@link org.apache.commons.jexl3.Expression#evaluate}</li> + <li>{@link org.apache.commons.jexl3.JexlScript#execute}</li> + <li>{@link org.apache.commons.jexl3.JexlEngine#createExpression}</li> + <li>{@link org.apache.commons.jexl3.JexlExpression#evaluate}</li> </ul> The following example illustrates their usage: <pre> @@ -154,9 +158,9 @@ JexlContext jc = new MapContext(); jc.set("quuxClass", quux.class); - Expression create = jexl.createExpression("quux = new(quuxClass, 'xuuq', 100)"); - Expression assign = jexl.createExpression("quux.froboz.value = 10"); - Expression check = jexl.createExpression("quux[\"froboz\"].value"); + JexlExpression create = jexl.createExpression("quux = new(quuxClass, 'xuuq', 100)"); + JelxExpression assign = jexl.createExpression("quux.froboz.value = 10"); + JexlExpression check = jexl.createExpression("quux[\"froboz\"].value"); Quux quux = (Quux) create.evaluate(jc); Object o = assign.evaluate(jc); assertEquals("Result is not 10", new Integer(10), o); @@ -164,87 +168,119 @@ assertEquals("Result is not 10", new Integer(10), o); </pre> - <h3><a name="usage_ujexl">UnifiedJEXL script expressions</a></h3> + <h3><a name="usage_ujexl">Unified Expressions and Templates</a></h3> <p> If you are looking for JSP-EL like and basic templating features, you can - use UnifiedJEXL. + use UnifiedExpression from a JxltEngine. </p> The main methods are: <ul> - <li>{@link org.apache.commons.jexl3.UnifiedJEXL#parse}</li> - <li>{@link org.apache.commons.jexl3.UnifiedJEXL.Expression#evaluate}</li> - <li>{@link org.apache.commons.jexl3.UnifiedJEXL.Expression#prepare}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine#createExpression}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine.UnifiedExpression#prepare}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine.UnifiedExpression#evaluate}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine#createTemplate}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine.Template#prepare}</li> + <li>{@link org.apache.commons.jexl3.JxltEngine.Template#evaluate}</li> </ul> The following example illustrates their usage: <pre> JexlEngine jexl = new JexlEngine(); - UnifiedJEXL ujexl = new UnifiedJEXL(jexl); - UnifiedJEXL.Expression expr = ujexl.parse("Hello ${user}"); - String hello = expr.evaluate(context, expr).toString(); + JxltEngine jxlt = jexl.jxlt(); + JxltEngine.UnifiedExpression expr = jxlt.createExpression("Hello ${user}"); + String hello = jxlt.evaluate(context, jxlt).toString(); </pre> - <h3>Expressions Script and UnifiedJEXL.Expression: differences</h3> - <h4>Expression</h4> + <h3>JexlExpression, JexlScript, UnifiedExpression and Template: differences</h3> + <h4>JexlExpression </h4> <p> - This only allows for a single command to be executed and the result from - that is returned. If you try to use multiple commands it ignores + These are the most basic form of JexlEngine expressions and only allows for a single command + to be executed and its result returned. If you try to use multiple commands it ignores everything after the first semi-colon and just returns the result from the first command. </p> - <h4>Script</h4> + <h4>JexlScript</h4> <p> This allows you to put multiple commands in the expression and you can - use variable assignments, loops, calculations, etc. The result from the - last command is returned from the script. + use variable assignments, loops, calculations, etc. More or less what can be achieved in Shell or + JavaScript at its basic level. The result from the last command is returned from the script. </p> - <h4>UnifiedJEXL.Expression</h4> + <h4>JxltEngine.UnifiedExpression</h4> <p> - This is ideal to produce text. To get a calculation you use the EL-like syntax + These are ideal to produce "one-liner" text, like a 'toString()' on steroids. + To get a calculation you use the EL-like syntax as in ${someVariable}. The expression that goes between the brackets - behaves like a script, not an expression. You can use semi-colons to + behaves like a JexlScript, not an expression. You can use semi-colons to execute multiple commands and the result from the last command is returned from the script. You also have the ability to use a 2-pass evaluation using the #{someScript} syntax. </p> + <h4>JxltEngine.Template</h4> + <p> + These produce text documents. Each line beginning with '$$' (as a default) is + considered JEXL code and all others considered as JxltEngine.UnifiedExpression. + Think of those as simple Velocity templates. A rewritten MudStore initial Velocity sample looks like this: + </p> + <pre><code> + <html> +<body> +Hello ${customer.name}! +<table> +$$for(var mud : mudsOnSpecial ) { +$$ if (customer.hasPurchased(mud) ) { + <tr> + <td> + ${flogger.getPromo( mud )} + </td> + </tr> +$$ } +$$} +</table> +</body> +</html> + </code></pre> <h2><a name="configuration">JEXL Configuration</a></h2> <p> The JexlEngine can be configured through a few parameters that will drive how it reacts - in case of errors. These configuration methods are best called at JEXL engine initialization time; it - is recommended to derive from JexlEngine to call those in a constructor. + in case of errors. + These configuration methods are embedded through a {@link org.apache.commons.jexl3.JexlBuilder}. + <p> + Most configuration options can be overriden during evaluation by using a {@link org.apache.commons.jexl3.JexlEvalContext} + which merges both a {@link org.apache.commons.jexl3.JexlContext} to expose variables and a +{@link org.apache.commons.jexl3.JexlEngine$Options} to carry evaluation options. </p> <p> - {@link org.apache.commons.jexl3.JexlEngine#setLenient} configures when JEXL considers 'null' as an error or not in various situations; + {@link org.apache.commons.jexl3.JexlBuilder#strict} configures when JEXL considers 'null' as an error or not in various situations; when facing an unreferenceable variable, using null as an argument to an arithmetic operator or failing to call a method or constructor. The lenient mode is close to JEXL-1.1 behavior. </p> <p> - {@link org.apache.commons.jexl3.JexlEngine#setSilent} configures how JEXL reacts to errors; if silent, the engine will not throw exceptions + {@link org.apache.commons.jexl3.JexlBuilder#silent} configures how JEXL reacts to errors; if silent, the engine will not throw exceptions but will warn through loggers and return null in case of errors. Note that when non-silent, JEXL throws JexlException which are unchecked exception. </p> <p> - {@link org.apache.commons.jexl3.JexlEngine#setDebug} makes stacktraces carried by JExlException more meaningfull; in particular, these + {@link org.apache.commons.jexl3.JexlBuilder#debug} makes stacktraces carried by JExlException more meaningfull; in particular, these traces will carry the exact caller location the Expression was created from. </p> <p> - {@link org.apache.commons.jexl3.JexlEngine#setClassLoader} indicates to a JexlEngine which class loader to use to solve a class name; this affects - how JexlEngine.newInstance and the 'new' script method operates. This is mostly usefull in cases where + {@link org.apache.commons.jexl3.JexlBuilder#loader} indicates to a JexlEngine which class loader to use to solve a class name; this affects + how JexlEngine.newInstance and the 'new' script method operates. This is mostly useful in cases where you rely on JEXL to dynamically load and call plugins for your application. </p> <p> - JexlEngine and UnifiedJEXL expression caches can be configured as well. If you intend to use JEXL + JexlEngine and JxltEngine expression caches can be configured as well. If you intend to use JEXL repeatedly in your application, these are worth configuring since expression parsing is quite heavy. Note that all caches created by JEXL are held through SoftReference; under high memory pressure, the GC will be able to reclaim those caches and JEXL will rebuild them if needed. By default, a JexlEngine does not create a cache - whilst UnifiedJEXL does. + whilst JxltEngine does. </p> - <p>Both JexlEngine and UnifiedJEXL are thread-safe; the same instance can be shared between different + <p>Both JexlEngine and JxltEngine are thread-safe, all their inner fields are final; the same instance can be shared between different threads and proper synchronization is enforced in critical areas.</p> - <p>{@link org.apache.commons.jexl3.JexlEngine#setCache} will set how many expressions can be simultaneously cached by the + <p>{@link org.apache.commons.jexl3.JexlBuilder#cache} will set how many expressions can be simultaneously cached by the JEXL engine. UnifiedJEXL allows to define the cache size through its constructor.</p> <p> - {@link org.apache.commons.jexl3.JexlEngine#setFunctions} extends JEXL scripting by registering functions in - namespaces. + {@link org.apache.commons.jexl3.JexlEngine#namespaces} extends JEXL scripting by registering your own classes as + namespaces allowing your own functions to be exposed at will. </p> This can be used as in: <pre><code> @@ -255,13 +291,12 @@ } Map<String, Object> funcs = new HashMap<String, Object>(); funcs.put("math", new MyMath()); - JexlEngine jexl = new JexlEngine(); - jexl.setFunctions(funcs); + JexlEngine jexl = new JexlBuilder().namespaces(funcs).create(); JexlContext jc = new MapContext(); jc.set("pi", Math.PI); - e = JEXL.createExpression("math:cos(pi)"); + JexlExpression e = JEXL.createExpression("math:cos(pi)"); o = e.evaluate(jc); assertEquals(Double.valueOf(-1),o); </code></pre> @@ -270,30 +305,48 @@ expression; this instance lifetime is limited to the expression evaluation. <h2><a name="customization">JEXL Customization</a></h2> - If you need to make JEXL treat some objects in a specialized manner or tweak how it - reacts to some settings, you can derive most of its inner-workings. However, using the protected methods - or internal package classes imply you might have to re-adapt your code when new JEXL versions are released. + <p> - {@link org.apache.commons.jexl3.JexlEngine} is meant to be - extended and lets you capture your own configuration defaults wrt cache sizes and various flags. - Implementing your own cache - instead of the basic LinkedHashMap based one - would be - another possible extension. + The {@link org.apache.commons.jexl3.JexlContext}, {@link org.apache.commons.jexl3.JexlBuilder} and + {@link org.apache.commons.jexl3.JexlEngine$Options} are + the most likely interfaces you'll want to implement for customization. Since they expose variables and options, + they are the primary targets. Before you do so, have a look at {@link org.apache.commons.jexl3.JexlEvalContext} + and {@link org.apache.commons.jexl3.ObjectContext} which may already cover some of your needs. + </p> + <p> + The {@link org.apache.commons.jexl3.NamespaceResolver} may also be of interest since it allows resolving namespaces (sic) + without having to store them in a map. </p> <p> {@link org.apache.commons.jexl3.JexlArithmetic} - is the class to derive if you need to change how operators behave. For example, this would + is the class to derive if you need to change how operators behave or add types upon which they + operate. For example, this would be the case if you wanted '+' to operate on arrays; you'd need to derive JexlArithmetic and implement your own version of Add. + Note however that you can not change the operator precedence. + </p> + + <h2><a name="extension">JEXL Extending</a></h2> + If you need to make JEXL treat some objects in a specialized manner or tweak how it + reacts to some settings, you can derive most of its inner-workings. The classes and methods are rarely private or + final - only when the inner contract really requires it. However, using the protected methods + and internal package classes imply you might have to re-adapt your code when new JEXL versions are released. + <p> + {@link org.apache.commons.jexl3.internal.Engine} can be + extended to let you capture your own configuration defaults wrt cache sizes and various flags. + Implementing your own cache - instead of the basic LinkedHashMap based one - would be + another possible extension. </p> <p> - {@link org.apache.commons.jexl3.Interpreter} + {@link org.apache.commons.jexl3.internal.Interpreter} is the class to derive if you need to add more features to the evaluation itself; for instance, you want pre- and post- resolvers for variables or nested scopes for - for variable contexts or add factory based support to the 'new' operator. + for variable contexts. </p> <p> {@link org.apache.commons.jexl3.internal.introspection.Uberspect} - is the class to derive if you need to add introspection or reflection capabilities for some objects. + is the class to derive if you need to add introspection or reflection capabilities for some objects, for + instance adding factory based support to the 'new' operator. The code already reflects public fields as properties on top of Java-beans conventions. </p> </body>