TL2Java Example

TL2Java is described in Program Generators with XML and Java in Chapter 12.

TL2Java.template
#declarations 
     public String version = "TL2Java Version 1.1, February 9, 2001";

/* Copyright (c) 2001, Craig Cleaveland,  All rights reserved.
	http://craigc.com

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

  Redistributions of source code must retain the above
  copyright notice, this list of conditions and the following
  disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither Craig Cleaveland nor the names of its contributors may be
  used to endorse or promote products derived from this software
  without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
#
/* Generated by #(version)# */
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
#for  "//declare[@import]"#
import #"./@import"#;
#end#

# 
String className = getClassName(context.evalString("//declare/@classname"));
String sclass = context.evalString("//declare/@extends");
String classNameDef = className
	+ (sclass.equals("") ? "" : (" extends "+sclass))
	+ (context.evalBoolean("count(//declare[@implements])>0") ? " implements": "");
#

/**
 * Program Generator created by TL2Java
 * @version #(new Date().toString())#
 */
public class #(classNameDef)#
	#for  "//declare[@implements]"# #"@implements"#
		#if "position()!=last()"#,#fi##end# {
    Properties properties = new Properties();

#for  "//declarations"#
#"."#
#end#

    /**
     * This method generates the output given a context and output stream
     */
    public boolean generate(XPathContext context, ProgramWriter out) {
        try {
# generate2(context, out); #
        } catch (Exception e) {
            System.out.println("Exception: "+e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * A program generator is typically (but not always) invoked
     * with a command line with arguments for the XML input file
     * and output file.
     */    
    public static void main(String[] args) {
        try {
            ProgramWriter out = args.length>=2
                ?new ProgramWriter(new FileOutputStream(args[1]))
                :new ProgramWriter(System.out);
            #(className)# pg = new #(className)#();
            for (int j=1; j<=args.length; ++j) {
                pg.properties.put("arg"+j, args[j-1]);
            }
            pg.generate(new XPathContext(args[0]), out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * This array provides program generator development history
     */
    public String[][] history = {
        { "#(new Date())#", // date this file was generated
             #(quoteString(properties.getProperty("arg1")))#, // input file
             #(quoteString(properties.getProperty("arg2")))# }, // output file
#. 
            for (int j=0; j<history.length; ++j) {
                out.print("        {");
                for (int k=0; k<history[j].length; ++k) {
                    out.print(quoteString(history[j][k])+", ");
                }
                out.println("}, ");
            }
#
    };
}

#declarations 
    String ind = "    ";
    int uid = 0;

    /**
     * This method generates code for the different TL constructs
     * @author Craig Cleaveland
     */
    public boolean generate2(XPathContext context, ProgramWriter out) {
        out.setIndent(out.getIndent()+ind);
        out.print(ind);
        try {
            NodeList x = context.evalNodeSet("*|text()");
            for (int j=0; j<x.getLength(); ++j) {
                Node n = (Node) x.item(j);
                boolean strip = true;
                if (j<x.getLength()-1) {
                    Node n2 = (Node) x.item(j+1);
                    int t2 = n2.getNodeType();
                    if (t2==Node.TEXT_NODE) {
                        strip = false;
                    } else if (t2==Node.ELEMENT_NODE) {
                        String ename = n2.getNodeName();
                        if (ename.equals("value") || ename.equals("expr") || ename.equals("empty")) {
                            strip = false;
                        }
                        context.push(n2, 1, 1);
                        String reverse = context.evalString("@whitespace");
                        if (reverse.equals("reverse")) {
                            strip = !strip;
                        }
                    }
                }
                context.push(n, j+1, x.getLength());
                switch (n.getNodeType()) {
                case Node.ELEMENT_NODE:
                    String ename = n.getNodeName();
                    if (ename.equals("value")) {
                        out.println("out.print(context.evalString(\""
                            +context.evalString("@path")+"\"));");
                    } else if (ename.equals("for")) {
                        ++uid;
                        String var = context.evalString("@var");
                        if (var.equals("")) {
                            var = "loopvar"+uid;
                        }
                        out.println("NodeList x"+uid+" = context.evalNodeSet(\""
                            +context.evalString("@path")+"\");");
                        out.println("for (int j"+uid+"=0; j"+uid+"<x"+uid
                            +".getLength(); ++j"+uid+") {");
                        out.println(ind+"Node "+var+" = (Node) x"+uid
                            +".item(j"+uid+");");
                        out.println(ind+"context.push("+var+", j"+uid
                            +"+1, x"+uid+".getLength());");
                        generate2(context, out);
                        out.println("context.pop();");
                        out.println("}");
                    } else if (ename.equals("if")) {
                        out.println("if (context.evalBoolean("
                            +quoteString(context.evalString("@path"))+")) {");
                        generate2(context, out);
                        out.println("\n}");
                    } else if (ename.equals("else")) {
                        out.setIndent(out.getIndent()+ind);
                        out.println("} else {");
                        generate2(context, out);
                        out.setIndent(out.getIndent().substring(ind.length()));
                    } else if (ename.equals("expr")) {
                        out.println("out.print("+
                            context.evalString(".")+");");
                    } else if (ename.equals("java")) {
                        out.println(context.evalString("."));
                    } else if (ename.equals("empty")) {
                    } else if (ename.equals("declare")) {
                    } else if (ename.equals("declarations")) {
                    } else {
                        out.println("//Unknown Element: "+n.getNodeName());
                    }
                    break;
                case Node.TEXT_NODE:
                    String t = context.evalString(".");
                    if (strip) {
                        t = strip(t);
                    }
                    if (!t.equals("")) {
                        out.println("out.print("+quoteString(t)+");");
                    }
                    break;
                }
                context.pop();
            }
        } catch (Exception e) {
            System.out.println("Exception in generate2: "+e.getMessage());
            e.printStackTrace();
            return false;
        } finally {
            out.setIndent(out.getIndent().substring(ind.length()));
        }
        return true;
    }
    
    /** Add quotes and convert new lines, tabs  quotes */
    public String quoteString(String t) {
        String r = "";
        for (int j=0; j<t.length(); ++j) {
            char c = t.charAt(j);
            if (c=='\n') {
                r += "\\n";
            } else if (c=='"') {
                r += "\\\"";
            } else if (c=='\t') {
                r += "\\t";
            } else if (c=='\\') {
                r += "\\\\";
            } else {
                r += c;
            }
        }
        return "\""+r+"\"";
    }
    
    /** Remove trailing whitespace characters. */
    public String strip(String t) {
        while (t.length()>0) {
            char c = t.charAt(t.length()-1);
            if (c==' ' || c=='\t' || c=='\n' || c=='\r') {
                t = t.substring(0, t.length()-1);
            } else {
                return t;
            }
        }
        return t;
    }
 
    /** Get the class name of the program generator from the command line. */   
    public String getClassName(String className) {
        if (className.equals("")) {
            className = properties.getProperty("arg2");
            if (className.endsWith(".java")) {
                className = className.substring(0, className.length()-5);
                int j = className.lastIndexOf(File.separatorChar);
                if (j>=0) {
                    className = className.substring(j+1);
                }
            } else {
                // unknown class name, so just make one up
                className = "MyPG";
            }
        }
        return className;
    }
#