01: package org.ztemplates.flex.compiler;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.File;
05:
06: import org.apache.log4j.Logger;
07: import org.ztemplates.render.impl.ZCopyUtil;
08:
09: public class ZFlexCompiler {
10: protected static Logger log = Logger.getLogger(ZFlexCompiler.class);
11:
12: private final String mxmlc;
13:
14: public ZFlexCompiler(String mxmlc) {
15: this .mxmlc = mxmlc;
16: }
17:
18: public void compile(File sourcePath) throws Exception {
19: compileDir(sourcePath, sourcePath);
20: }
21:
22: public void compileDir(File dir, File sourcePath) throws Exception {
23: File[] content = dir.listFiles();
24: for (File crt : content) {
25: if (crt.getName().endsWith(".mxml")) {
26: compileFile(crt, sourcePath);
27: } else if (crt.getName().endsWith(".as")) {
28: compileFile(crt, sourcePath);
29: } else if (crt.isDirectory()) {
30: compileDir(crt, sourcePath);
31: }
32: }
33: }
34:
35: public void compileFile(File file, File sourcePath)
36: throws Exception {
37: String destPath = file.getAbsolutePath();
38: destPath = destPath.substring(0, destPath.lastIndexOf('.'))
39: + ".swf";
40: File dest = new File(destPath);
41: if (dest.exists() && dest.lastModified() > file.lastModified()) {
42: log.info("up tp date " + file.getAbsolutePath());
43: return;
44: }
45: log.info("compiling " + file.getAbsolutePath());
46: String cmd = mxmlc + " -compiler.source-path="
47: + sourcePath.getAbsolutePath() + " "
48: + file.getAbsolutePath();
49: log.info(cmd);
50: Process proc = Runtime.getRuntime().exec(cmd, new String[0],
51: null);
52: int success = proc.waitFor();
53: ByteArrayOutputStream out = new ByteArrayOutputStream();
54: ZCopyUtil.copy(proc.getInputStream(), out);
55: log.info(new String(out.toByteArray()));
56:
57: if (success != 0) {
58: ByteArrayOutputStream errOut = new ByteArrayOutputStream();
59: ZCopyUtil.copy(proc.getErrorStream(), errOut);
60: log.error("\n" + new String(errOut.toByteArray()));
61: // throw new Exception("could not compile " + file.getAbsolutePath() +
62: // "\n" + new String(errOut.toByteArray()));
63: }
64: }
65: }
|