001: /**
002: *
003: * © Copyright International Business Machines Corporation 2006.
004: * All rights reserved.
005: *
006: * The 'TemplateCompiler' class compiles templates into Java classes.
007: *
008: * File : TemplateCompiler.java
009: * Created : 2006/03/02
010: *
011: * @author Rene Pawlitzek (rpa@zurich.ibm.com)
012: * @version 1.00, 2006/03/02
013: * @since JDK 1.3
014: *
015: * History : 2006/03/02, rpa, new file
016: * 2006/03/14, rpa, code review
017: *
018: */package com.ibm.hamlet.compiler;
019:
020: import java.io.*;
021: import com.ibm.hamlet.*;
022: import com.sun.tools.javac.*;
023:
024: public class TemplateCompiler {
025:
026: private boolean debug = false, verbose = false;
027: private String fileName, dstDir = ".";
028:
029: public void setFileName(String fileName) {
030: this .fileName = fileName;
031: } // setFileName
032:
033: public void setDstDir(String dstDir) {
034: this .dstDir = dstDir;
035: } // setDstDir
036:
037: public void setDebug(boolean debug) {
038: this .debug = debug;
039: } // setDebug
040:
041: public void setVerbose(boolean verbose) {
042: this .verbose = verbose;
043: } // setVerbose
044:
045: public SourceGenerator getSourceGenerator() {
046: return new DefaultGenerator();
047: } // getSourceGenerator
048:
049: public void perform() throws Exception {
050: // create template.java
051: File fin = new File(fileName);
052: String templateFileName = fin.getName();
053: String className = RuntimeUtilities
054: .getClassName(templateFileName);
055: String classFileName = dstDir + File.separator + className
056: + ".class";
057: File f = new File(classFileName);
058: if ((f != null) && (fin.lastModified() <= f.lastModified())) {
059: if (verbose)
060: System.out.println(classFileName + " is up-to-date.");
061: return;
062: } // if
063: String sourceFileName = dstDir + File.separator + className
064: + ".java";
065: File fout = new File(sourceFileName);
066: InputStream in = new FileInputStream(fin);
067: OutputStream out = new FileOutputStream(fout);
068: SourceGenerator generator = getSourceGenerator();
069: if (verbose)
070: System.out.println("Creating " + sourceFileName + " ...");
071: generator.perform(in, out, className);
072: out.close();
073: in.close();
074: // compile template.java
075: String[] args = { sourceFileName };
076: Main compiler = new Main();
077: if (verbose)
078: System.out.println("Compiling " + sourceFileName + " ...");
079: compiler.compile(args);
080: // delete template.java
081: if (!debug) {
082: if (verbose)
083: System.out.println("Deleting " + sourceFileName
084: + " ...");
085: fout.delete();
086: } // if
087: if (verbose)
088: System.out.println("Finished.");
089: } // perform
090:
091: public static void main(String args[]) {
092: try {
093: if (args.length == 0) {
094: System.out
095: .println("Usage: java -cp <classpath> com.ibm.hamlet.compiler.TemplateCompiler "
096: + "[-debug] [-verbose] [-dstDir <directory>] <Template.html>");
097: Runtime.getRuntime().exit(1);
098: } // if
099: TemplateCompiler tc = new TemplateCompiler();
100: for (int n = 0; n < args.length; n++) {
101: if ("-debug".equals(args[n]))
102: tc.setDebug(true);
103: else if ("-verbose".equals(args[n]))
104: tc.setVerbose(true);
105: else if ("-dstDir".equals(args[n]))
106: tc.setDstDir(args[++n]);
107: else
108: tc.setFileName(args[n]);
109: } // for
110: tc.perform();
111: } catch (Exception e) {
112: e.printStackTrace();
113: Runtime.getRuntime().exit(1);
114: } // try
115: } // main
116:
117: } // TemplateCompiler
118:
119: /* ----- End of File ----- */
|