<?xml version="1.0" encoding="UTF-8"?>
<document>
  <properties>
    <title>
       JEX Overview
    </title>
    <author email="dmitri@apache.org">
       Dmitri Plotnikov
    </author>
  </properties>
  <body>


    <section name="What's JEX">
      <p>
        JEX is a universal set of APIs for expression languages.
        Individual expression languages function as plug-ins. You use
        the same set of APIs to invoke any of them.
      </p>
      <ul>
        <li><a href="#Supported Expression Languages">Supported Expression Languages</a>
        </li>
        <li><a href="#Context">Context</a>
        </li>
        <li><a href="#Types of Expressions">Types of Expressions</a>
        </li>
        <li><a href="#Multilingual Mode">Multilingual Mode</a>
        </li>
        <li><a href="#Compilers and Interpreters">Compilers and Interpreters</a>
        </li>
        <li><a href="#How to Add a New Compiler or Interpreter">How to Add a New Compiler or Interpreter</a>
        </li>
        <li><a href="#Examples">Examples</a>
          <ul>
            <li><a href="#Example 1: Interpreting an Expression">Example 1: Interpreting an Expression </a>
            </li>
            <li><a href="#Example 2: Interpreting an Expression, Multilingual Mode">Example 2: Interpreting an Expression, Multilingual Mode</a>
            </li>
            <li><a href="#Example 3: Compiling an Expression">Example 3: Compiling an Expression</a>
            </li>
            <li><a href="#Example 4: Compiling an Expression, Multilingual Mode">Example 4: Compiling an Expression, Multilingual Mode</a>
            </li>
            <li><a href="#Example 5: Using an Iteration Expression">Example 5: Using an Iteration Expression</a>
            </li>
            <li><a href="#Example 6: Using a Variable Expression">Example 6: Using a Variable Expression</a>
            </li>
            <li><a href="#Example 7: Using a Variable">Example 7: Using a Variable</a>
            </li>
            <li><a href="#Example 8: Declaring a JavaScript Variable">Example 8: Declaring a JavaScript Variable</a>
            </li>
          </ul>
        </li>
      </ul>
    </section>


    <section name="Supported Expression Languages">
      <p>
        Out of the box JEX supports the following languages:
      </p>
      <ul>
        <li><b>javascript</b>
          <br/>
           Jex uses
          <a href="http://www.mozilla.org/rhino/">Rhino Mozilla</a>
          implementation of JavaScript (aka ECMA-Script) for Java. In
          order to use Jex with JavaScript, you will need to download
          the js.jar file from
          <a href="http://www.mozilla.org/rhino/download.html">www.mozilla.org</a>.
          <br/>
          <br/>
        </li>
        <li><b>bexl</b>
          <br/>
           This is a made-up name for the expression
          language implicitly introduced by
          <a href="http://commons.apache.org/beanutils/">Apache Commons BeanUtils</a>.
          See
          <a href="http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/PropertyUtils.html">PropertyUtil</a>
          for the definition of the language.
          <br/>
          <br/>
        </li>
        <li><b>jxpath</b>
          <br/>
           An implementation of the XPath expression
          language that applies XPath expressions to graphs of Java
          Objects. See
          <a href="http://commons.apache.org/jxpath/index.html">JXPath Home Page on Apache Commons</a>.
        </li>
      </ul>
      <p>
        {Other candidates: Jexl, XPath via Jaxen, SPEL, Jpath}
      </p>
    </section>


    <section name="Context">
      <p>
        Expressions are evaluated in a Context. A context holds various
        pieces of data that are used by expression language processors:
      </p>
      <ul>
        <li><b>Variables</b>
          <br/>
           All expression languages that support the
          notion of a variable can manipulate and share this set of
          variables.
          <br/>
          <br/>
        </li>
        <li><b>Locale</b>
          <br/>
           Languages that support internationalization can
          take advantage of this piece of configuration.
          <br/>
          <br/>
        </li>
        <li><b>Expression Language</b>
          <br/>
           Expressions evaluated in this context are
          expressed in this language.
          <br/>
          <br/>
        </li>
        <li><b>Properties</b>
          <br/>
           Expression language processors can be
          configured with properties specified using this
          language-independant mechanism.
        </li>
      </ul>
      <p>
        {Other potential pieces of context: extension functions, type
        converters}
      </p>
    </section>


    <section name="Types of Expressions">
      <p>
        There are three types of expressions:
      </p>
      <ul>
        <li><b>Value expression</b>
          <br/>
           These expressions are used compute values. For
          example, <code>"xpath:2+2"</code>  and
          <code>"bexl:address[1].zipCode"</code>  are value
          expressions.
          <br/>
          <br/>
        </li>
        <li><b>Iteration expression</b>
          <br/>
           The evaluation of an iteration expression
          produces an Iterator. For example,
          <code>"xpath:/address/zipCode"</code>  produces an
          iteration over all zipCodes.
          <br/>
          <br/>
        </li>
        <li><b>Variable expression</b>
          <br/>
           Variable expressions are used to modify values.
          Think of them as the left-hand side of an assignment. For
          example, <code>"bexl:address[1].zipCode"</code>  is a
          valid variable expression. The same string can be a valid
          expression of more than one type.
        </li>
      </ul>
    </section>


    <section name="Multilingual Mode">
      <p>
        A Jex Context is by default configured to be multilingual. What
        this means is that an expression can start with a "lang:"
        prefix, where lang identifies the expression language of the
        expression. Jex will then locate and use the appropriate
        language processor.
      </p>
      <p>
        If the Context is multilingual and also has an expression
        language specified, that language is used as the default for
        the cases when the prefix is missing or does not correspond to
        a known language.
      </p>
    </section>


    <section name="Compilers and Interpreters">
      <p>
        Jex supports both interpreted and compiled languages. An
        interpreter computes the value of the expression as it parses
        it. A compiler parses the expression into an internal
        executable form first and then uses the internal representation
        for the actual computation. The internal representation can be
        cached and used for faster re-evaluation of the same
        expression.
      </p>
      <p>
        Some languages better lend themselves to compilation, some- to
        interpretation. For example, JXPath, which has relatively
        sophisticated syntax, uses a compiler, while Bexl is so simple
        that a separate compilation step would not speed it up by much
        and perhaps would even slow it down.
      </p>
      <p>
        Jex supports both types of language processors equally well.
        Better yet, if needed, Jex allows an interpreter to be invoked
        like a compiler and a compiler like an interpreter. This
        flexibility lets the user choose the mode, compilation or
        interpretation, that best suits the application's needs and not
        worry whether the actual expression languages are compiled or
        interpreted.
      </p>
    </section>


    <section name="How to Add a New Compiler or Interpreter">
      <p>
        To add a new Compiler, create a class called
        <code>org.apache.commons.jex.<i>language</i>.Compiler</code>
        that extends the
        <a href="apidocs/org/apache/commons/jex/Compiler.html"><code>org.apache.commons.jex.Compiler</code></a>
        abstract class. Jex will find it by name.
      </p>
      <p>
        Similarly, to add a new Interpreter, create a class named
        <code>org.apache.commons.jex.<i>language</i>.Interpreter</code>
        that extends the
        <a href="apidocs/org/apache/commons/jex/Interpreter.html"><code>org.apache.commons.jex.Interpreter</code></a>
        abstract class.
      </p>
    </section>


    <section name="Examples">
      <p>
        In all following examples, we will assume that we have an
        object graph constructed like this:
      </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
public class AddressBook {
   private Vector addresses = new Vector();
   public Vector getAddresses(){
       return addresses;
   }
}


public class Address {
    private String zipCode;
    public String getZipCode(){
        return zipCode;
    }
    public void setZipCode(){
        this.zipCode = zipCode;
    }
}


// Create an address book with two entries

AddressBook book = new AddressBook();
Address addr1 = new Address();
add1.setZipCode("90210");
Address addr2 = new Address();
add2.setZipCode("20191");
book.getAddresses().add(addr1);
book.getAddresses().add(addr2);

// Create a Jex context

Context context = new Context();
</source>
<!--============================ - SOURCE - ============================-->

      <p/>


      <subsection name="Example 1: Interpreting an Expression">
        <p>
          In the following example, we invoke an interpreter to
          process a Bexl expression:
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
context.setExpressionLanguage("bexl");

String zipCode = (String)context.
    evaluate("addresses[1].zipCode", new Object[]{book});
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 2: Interpreting an Expression, Multilingual Mode">
        <p>
          Here we are using the multilingual capabilities of Jex:
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
// Use Bexl
String zipCode = (String)context.
    evaluate("bexl:addresses[1].zipCode", new Object[]{book});

// Same with JXPath
zipCode = (String)context.
    evaluate("jxpath:addresses[2]/zipCode", new Object[]{book});

</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 3: Compiling an Expression">
        <p>
          Here we use the compiler and cache the compiled expression:
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
context.setExpressionLanguage("bexl");

Expression expr = context.getCompiler().
     compileExpression("addresses[1].zipCode");

...
// Later, when we need to compute the expression,
// use the compiled form

String zipCode = (String)expr.evaluate(context, new Object[]{bean});
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 4: Compiling an Expression, Multilingual Mode">
        <p>
          In this example, we compile an expression in the
          multilingual mode:
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
Expression expr = context.getCompiler().
     compileExpression("bexl:addresses[1].zipCode");

...

String zipCode = (String)expr.evaluate(context, new Object[]{bean});
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 5: Using an Iteration Expression">
        <p>
          An iteration expression produces an Iterator.
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
context.setExpressionLanguage("jxpath");

Iterator iter = context.iterate("addresses", new Object[]{book});
while (iter.hasNext()){
    Address address = (Address)iter.next();
    ...
}
</source>
<!--============================ - SOURCE - ============================-->

        <p>
          An iteration expression can also be used in both
          interpreted and compiled modes.
        </p>
      </subsection>


      <subsection name="Example 6: Using a Variable Expression">
        <p>
          A variable expression is used to modify a value:
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
context.assign("jxpath:addresses[1].zipCode",
               "22190", new Object[]{book});
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 7: Using a Variable">
        <p>
          The notion of a variable is common for many expression
          languages. With Jex they all share the same pool of
          variables.
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
// Set a variable first
context.getVariables().declareVariable("i", new Integer(2));

// Now we can use the variable
String zipCode = (String)context.
    evaluate("jxpath:addresses[$i]/zipCode", new Object[]{book});

// Same with JavaScript
zipCode = (String)context.
    evaluate("javascript:addresses[i].zipCode", new Object[]{book});
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>


      <subsection name="Example 8: Declaring a JavaScript Variable">
        <p>
          By default Jex requires that all variables be declared
          explicitly. However, if you are using JavaScript, you can
          configure Jex to allow automatic declaration of variables.
        </p>

<!--============================ + SOURCE + ============================-->
<source>
                                                                       <br/>
// Allow automatic declaration of variables
context.setProperty("javascript.autodeclare", "true");

// Let's declare a new variable
context.assign("javascript:company", "PLOTNIX", null);
</source>
<!--============================ - SOURCE - ============================-->

      </subsection>
    </section>
  </body>
</document>