My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
JXTPipeline  
Introduction to the JXT Pipeline
Updated Nov 15, 2010 by manico.james@gmail.com

Introduction

While not necessary for its use, understanding the basics of the JXT pipeline can be helpful in effectively using JXT.

Details

Phase 1. Parse JXT as XML

The JXT pipeline starts with a JXT file. The engine uses an XML parser read the file into memory, utilizing "tag factories" to construct an abstract syntax tree (AST) of the source file.

Because JXT files are parsed as XML, they must be valid XML to pass the first phase of the pipeline. This means that JXT occasionally forces the authors to add escape. For example, the expression "${a < b}" must be written as "${a &lt; b}".

However, since the files are XML, JXT authors may also make use of XML functionality to make things easier. For example, CDATA sections make escaping easier, XInclude may be used to include external files and entity declarations may be used to create "macros" documents.

Phase 2. Translate AST to Java Source

After a JXT file has been parsed into an AST, the next step is to translate the AST into Java Source. This is a fairly straight-forward process in which the AST nodes are asked to (recursively) generate snippets of Java source. For example, the AST node for an "if", as in <jxt:if test="${a == b}">content</jxt:if>, would generate the following Java Source:

    if (a == b) {
        // code generated by AST of "content"
    }

Notice that the "jxt:if" node is only responsible for creating the "if (condition) {" and closing "}". It delegates the generation of the content to the nested AST. Internally the code to generate that block would look roughly like this:

class JxtIfNode extends ASTNode {
    ASTNode condition;
    ASTNode content;
    public void translate(TranslateContext ctx) {
        ctx.print("if (");
        condition.translate(ctx);
        ctx.print(") {");
        content.translate(ctx);
        ctx.print("}");
    }
}

The real implementation is slightly more complex, since it must handle complex content and "proper" indentation of the output Java code (for easier debugging). But it is not that much more complicated--browse the source tab to see the real implementation.

Phase 3. Compile

The next step, after generating the Java source, is to compile it to a Java class file. Fortunately there are a number of compilers available freely available to do this step for the JXT engine. We use the ECJ compiler from the Eclipse project by default. However it is also possible to use any JSR199 compliant compiler on a 1.6 (or later) JVM. And finally as a backup, command-line javac can be configured if nothing else works.

Phase 4. Class Load

Once the class file has been compiled, it is then loadable into the JVM for immediate use. This process requires creating and using a custom ClassLoader. The only complexity in this step is that we must create and track a new ClassLoader to allow for modifying and reloading JXT files. Since a ClassLoader may only load one class of the same name, we simply discard the ClassLoader and create a new one, every time a page is modified and reloaded.

Phase 5. Instantiate and Execute

Once the class is loaded, it is instantiated (Class.newInstance()) and passed on to the Servlet lifecycle. From this point on, the JXT code is executing on the JVM and processing and handling requests at the speed of any other Java code.


Sign in to add a comment
Powered by Google Project Hosting