jvanzyl     00/10/01 20:48:01

  Modified:    build    build-velocity.sh
  Added:       build/lib JavaClass.jar
               src/java/org/apache/velocity/runtime/compiler Compiler.java
               testbed  compile.sh
  Log:
  - using the JavaClass package to create a template compiler. there
    is a trivial compiler that generates a valid class file. it only
    _says_ it wants to be a template compiler :-) if you run the
    resultant class that is.
  
  Revision  Changes    Path
  1.3       +10 -3     jakarta-velocity/build/build-velocity.sh
  
  Index: build-velocity.sh
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/build/build-velocity.sh,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- build-velocity.sh 2000/10/02 00:43:40     1.2
  +++ build-velocity.sh 2000/10/02 03:47:57     1.3
  @@ -8,14 +8,21 @@
   #-------------------------------------------------------------------
   
   LIB=lib
  +
  +# Libs needed for build.
   ANT=${LIB}/ant.jar
   ANTXML=${LIB}/xml.jar
   XERCES=${LIB}/xerces-1.1.3.jar
  -XALAN=${LIB}/xalan_1_1_D01.jar
  -SB=${LIB}/stylebook-1.0-b2.jar
  +
  +# Libs needed for runtime.
   LOG=${LIB}/log.jar
  +JAVACLASS=${LIB}/JavaClass.jar
  +
  +# Libs needed for docs.
  +XALAN=${LIB}/xalan_1_1_D01.jar
   FOP=${LIB}/fop.jar
   W3C=${LIB}/w3c.jar
  +SB=${LIB}/stylebook-1.0-b2.jar
   
   #--------------------------------------------
   # No need to edit anything past here
  @@ -31,7 +38,7 @@
       CLASSPATH="${CLASSPATH}:${JAVA_HOME}/lib/tools.jar"
   fi
   
  
-CP=${CLASSPATH}:${ANT}:${ANTXML}:${ANTLRALL}:${XERCES}:${XALAN}:${SB}:${LOG}:${FOP}:${LIB}:${W3C}
  
+CP=${CLASSPATH}:${ANT}:${ANTXML}:${ANTLRALL}:${XERCES}:${XALAN}:${SB}:${LOG}:${FOP}:${LIB}:${W3C}:${JAVACLASS}
   
   echo "Now building ${TARGET}..."
   
  
  
  
  1.1                  jakarta-velocity/build/lib/JavaClass.jar
  
        <<Binary file>>
  
  
  1.1                  
jakarta-velocity/src/java/org/apache/velocity/runtime/compiler/Compiler.java
  
  Index: Compiler.java
  ===================================================================
  package org.apache.velocity.runtime.compiler;
  
  import de.fub.bytecode.generic.*;
  import de.fub.bytecode.Constants;
  
  /**
   * The start of a velocity template compiler.
   */
  public class Compiler implements InstructionConstants
  {
      public static void main(String[] args)
      {
          String template = args[0].substring(0, args[0].indexOf("."));
          ClassGen cg =
                  new ClassGen(template, "java.lang.Object", "<generated>",
                  Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
          
          ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
          InstructionList il = new InstructionList();
          MethodGen mg = new MethodGen(Constants.ACC_STATIC |
                  Constants.ACC_PUBLIC, // access flags
                  Type.VOID, // return type
                  new Type[]{ // argument types
                  new ArrayType(Type.STRING, 1)},
                  new String[]{ "argv" }, // arg names
                  "main", template, // method, class
                  il, cp);
  
          //Add often needed constants to constant pool.
  
          int br_index = cp.addClass("java.io.BufferedReader");
          int ir_index = cp.addClass("java.io.InputStreamReader");
          int system_out = cp.addFieldref("java.lang.System", "out", // System.out
                  "Ljava/io/PrintStream;");
          int system_in = cp.addFieldref("java.lang.System", "in", // System.in
                  "Ljava/io/InputStream;");
  
          // Create BufferedReader object and store it in local variable `in'.
  
          il.append(new NEW(br_index));
          il.append(DUP);
          il.append(new NEW(ir_index));
          il.append(DUP);
          il.append(new GETSTATIC(system_in));
  
          // Call constructors, i.e. BufferedReader(InputStreamReader())
  
          il.append( new INVOKESPECIAL(
                  cp.addMethodref("java.io.InputStreamReader", "<init>",
                  "(Ljava/io/InputStream;)V")));
          il.append( new INVOKESPECIAL(
                  cp.addMethodref("java.io.BufferedReader", "<init>", 
"(Ljava/io/Reader;)V")));
          
          // Create local variable `in'
  
          LocalVariableGen lg = mg.addLocalVariable("in",
                  new ObjectType("java.io.BufferedReader"), null, null);
          int in = lg.getIndex();
          lg.setStart(il.append(new ASTORE(in))); // `i' valid from here
  
          // Create local variable `name'
  
          lg = mg.addLocalVariable("name", Type.STRING, null, null);
          int name = lg.getIndex();
          il.append(ACONST_NULL);
          lg.setStart(il.append(new ASTORE(name))); // `name' valid from here
  
          InstructionHandle try_start = il.append(new GETSTATIC(system_out));
          il.append(new PUSH(cp, "I will be a template compiler!"));
          il.append( new INVOKEVIRTUAL(
                  cp.addMethodref("java.io.PrintStream", "println", 
"(Ljava/lang/String;)V")));
          
          // Upon normal execution we jump behind exception handler,
          // the target address is not known yet.
  
          GOTO g = new GOTO(null);
          InstructionHandle try_end = il.append(g);
  
          InstructionHandle handler = il.append(RETURN);
          mg.addExceptionHandler(try_start, try_end, handler,
                  new ObjectType("java.io.IOException"));
  
          // Normal code continues, now we can set the branch target of the GOTO.
  
          InstructionHandle ih = il.append(new GETSTATIC(system_out));
          g.setTarget(ih);
  
          // String concatenation compiles to StringBuffer operations.
          
          il.append(new NEW(cp.addClass("java.lang.StringBuffer")));
          il.append(DUP);
          il.append(new PUSH(cp, " "));
          il.append( new INVOKESPECIAL(
                  cp.addMethodref("java.lang.StringBuffer", "<init>", 
"(Ljava/lang/String;)V")));
          
          il.append(new ALOAD(name));
  
          // One can also abstract from using the ugly signature syntax by using
          // the getMethodSignature() method. For example:
  
          String sig = Type.getMethodSignature(Type.STRINGBUFFER,
                  new Type[]{ Type.STRING });
          il.append( new INVOKEVIRTUAL(
                  cp.addMethodref("java.lang.StringBuffer", "append", sig)));
  
          il.append( new INVOKEVIRTUAL(
                  cp.addMethodref("java.lang.StringBuffer", "toString", 
"()Ljava/lang/String;")));
  
          il.append(RETURN);
          
          mg.setMaxStack(5); // Needed stack size
          cg.addMethod(mg.getMethod());
  
          // Add public <init> method, i.e. empty constructor
          cg.addEmptyConstructor(Constants.ACC_PUBLIC);
  
          // Get JavaClass object and dump it to file.
          try
          {
              cg.getJavaClass().dump(template + ".class");
          }
          catch (java.io.IOException e)
          {
              System.err.println(e);
          }
      }
  }
  
  
  
  1.1                  jakarta-velocity/testbed/compile.sh
  
  Index: compile.sh
  ===================================================================
  #!/bin/sh
  
  CLASSPATH=.:../bin/classes
  
  for jar in ../build/lib/*.jar
  do
      CLASSPATH=${CLASSPATH}:${jar}
  done
  
  java -cp ${CLASSPATH} org.apache.velocity.runtime.compiler.Compiler $1
  
  
  

Reply via email to