KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > Main


1 /* *****************************************************************************
2  * Main.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11 import org.openlaszlo.server.LPS;
12 import org.openlaszlo.utils.FileUtils;
13 import org.openlaszlo.sc.ScriptCompiler;
14 import java.io.*;
15 import java.util.*;
16 import org.apache.log4j.*;
17 import org.apache.log4j.spi.*;
18 import org.apache.log4j.varia.NullAppender;
19
20 public class Main {
21     private static String JavaDoc[] USAGE = {
22         "Usage: lzc [OPTION]... FILE...",
23         "",
24         "Options:",
25         "-D<name>=<value>",
26         " Set the name/var property to value (See Compiler.getProperties).",
27         "-D<name>",
28         " Short for -Dname=true.",
29         "-v",
30         " Write progress information to standard output.",
31         "--mcache on|off",
32         " Turns on/off media cache. Default is off.",
33         "--onerror [throw|warn]",
34         " Action to take on compilation errors. Defaults to warn.",
35         "--help",
36         " Prints this message.",
37         "--keepscriptcache",
38         " Doesn't flush script cache before compiling.",
39         "",
40         "Output options:",
41         "--runtime=[swf5|swf6|swf7]",
42         " Compile to swf5, swf6, or swf7.",
43         "--dir outputdir",
44         " Output directory.",
45         "-k | --krank",
46         " Add krank information to the output object.",
47         "-g | --debug",
48         " Add debugging information into the output object.",
49         "-p | --profile",
50         " Add profiling information into the output object.",
51         "",
52         "Logging options:",
53         "-l<loglevel>",
54         " Logging level (See org.apache.log4j.Level)",
55         "-l<loggerName>=<loglevel>",
56         " Logging level (See org.apache.log4j.Level)",
57         "-lp file",
58         " Log4j properties files",
59         "--log logfile",
60         " Specify logfile (output still goes to console as well)",
61         "--schema",
62         " Writes the schema to standard output.",
63         "--script",
64         " Writes JavaScript to standard output."
65     };
66
67     /** Compiles each file base.ext to the output file base.swf,
68      * writing progress information to standard output. This method
69      * is intended for testing the compiler.
70      *
71      * <p>See the usage string or execute <code>lzc --help</code>
72      * to see a list of options.
73      *
74      * @param args the command line arguments
75      */

76     public static void main(String JavaDoc args[])
77         throws IOException
78     {
79         lzc(args, null, null, null);
80     }
81
82     /** This method implements the behavior described in main
83      * but also returns an integer error code.
84      */

85     public static int lzc(String JavaDoc args[], String JavaDoc logFile,
86             String JavaDoc outFileName, String JavaDoc outDir)
87         throws IOException
88     {
89         Logger logger = Logger.getRootLogger();
90         List files = new Vector();
91         boolean millis = false;
92         
93
94         // Preprocess args
95
for (int i = 0; i < args.length; i++) {
96             if (args[i].equals("m")) {
97                 millis = true;
98             }
99         }
100         // Configure logging
101
logger.setLevel(Level.ERROR);
102         PatternLayout layout;
103         if (millis) {
104             layout = new PatternLayout("%r %m%n");
105         } else {
106             layout = new PatternLayout("%r %m%n");
107         }
108         logger.removeAllAppenders();
109         if (logFile == null) {
110             logger.addAppender(new ConsoleAppender(layout));
111         } else {
112             logger.addAppender(new FileAppender(layout, logFile, false));
113         }
114     
115         Compiler JavaDoc compiler = new Compiler JavaDoc();
116         CompilerMediaCache cache;
117         String JavaDoc cacheDir = LPS.getWorkDirectory() + File.separator + "cache" + File.separator + "cmcache";
118         cache = new CompilerMediaCache(new File(cacheDir), new Properties());
119
120         // Make sure we always retranscode media
121
//cache.setProperty("forcetranscode", "true");
122
compiler.setMediaCache(cache);
123
124         String JavaDoc scacheDir = LPS.getWorkDirectory() + File.separator + "scache";
125         ScriptCompiler.initScriptCompilerCache(new File(scacheDir), new Properties());
126         boolean flushScriptCache = true;
127
128         for (int i = 0; i < args.length; i++) {
129             String JavaDoc arg = args[i].intern();
130             if (arg.startsWith("-")) {
131                 if (arg == "-v") {
132                     logger.setLevel(Level.ALL);
133                 } else if (arg == "-lp") {
134                     PropertyConfigurator.configure(args[++i]);
135                     String JavaDoc lhome = System.getProperty("LPS_HOME");
136                     if (lhome == null || lhome.equals("")) {
137                         lhome = System.getenv("LPS_HOME");
138                     }
139                     LPS.setHome(lhome);
140                 } else if (arg == "--schema") {
141                     compiler.SchemaLogger.setLevel(Level.ALL);
142                 } else if (arg == "--keepscriptcache") {
143                     flushScriptCache = false;
144                 } else if (arg == "--onerror") {
145                     String JavaDoc value = args[++i];
146                     CompilationError.ThrowCompilationErrors =
147                         "throw".equals(value);
148                 } else if (arg.startsWith("--runtime=")) {
149                     String JavaDoc value = arg.substring("--runtime=".length());
150                     if (value.equals("swf5") || value.equals("swf6") || value.equals("swf7")) {
151                       compiler.setProperty(CompilationEnvironment.SWFVERSION_PROPERTY, value);
152                     } else {
153                       System.err.println("Invalid value for --runtime");
154                       return 1;
155                     }
156                 } else if (arg == "--script") {
157                     Logger.getLogger(org.openlaszlo.sc.ScriptCompiler.class)
158                         .setLevel(Level.ALL);
159                 } else if (arg == "-mcache" || arg == "--mcache") {
160                     String JavaDoc value = args[++i];
161                     if (value.equals("on")) {
162                         cache.getProperties().setProperty("forcetranscode", "false");
163                     } else if (value.equals("off")) {
164                         cache.getProperties().setProperty("forcetranscode", "true");
165                     }
166                 } else if (arg == "-log" || arg == "--log") {
167                     String JavaDoc log = args[++i];
168                     logger.removeAllAppenders();
169                     logger.addAppender(new FileAppender(layout, log, false));
170                 } else if (arg == "-dir" || arg == "--dir") {
171                     outDir = args[++i];
172                 } else if (arg.startsWith("-D")) {
173                     String JavaDoc key = arg.substring(2);
174                     String JavaDoc value = "true";
175                     int offset = key.indexOf('=');
176                     if (offset >= 0) {
177                         value = key.substring(offset + 1).intern();
178                         key = key.substring(0, offset);
179                     }
180                     compiler.setProperty(key, value);
181                     LPS.setProperty(key, value);
182                 } else if (arg.startsWith("-l")) {
183                     Logger thisLogger = logger;
184                     String JavaDoc level = arg.substring(2);
185                     if (level.indexOf('=') > -1) {
186                         String JavaDoc key = level.substring(0, level.indexOf('='));
187                         level = level.substring(level.indexOf('=')+1);
188                         thisLogger = Logger.getLogger(key);
189                     }
190                     if (level != "" && level != null) {
191                         thisLogger.setLevel(Level.toLevel(level));
192                     }
193                 } else if (arg == "-k" || arg == "--krank") {
194                     compiler.setProperty(CompilationEnvironment.KRANK_PROPERTY, "true");
195                 } else if (arg == "-g" || arg == "--debug") {
196                     compiler.setProperty(CompilationEnvironment.DEBUG_PROPERTY, "true");
197                 } else if (arg == "-p" || arg == "--profile") {
198                     compiler.setProperty(CompilationEnvironment.PROFILE_PROPERTY, "true");
199                 } else if (arg == "--help") {
200                     for (int j = 0; j < USAGE.length; j++) {
201                         System.err.println(USAGE[j]);
202                     }
203                     return 0;
204                 } else {
205                     System.err.println("Usage: lzc [OPTION]... file...");
206                     System.err.println("Try `lzc --help' for more information.");
207                     return 1;
208                 }
209                 continue;
210             }
211             String JavaDoc sourceName = args[i];
212             files.add(sourceName);
213         }
214
215         if (flushScriptCache) {
216             ScriptCompiler.clearCacheStatic();
217         }
218
219         LPS.initialize();
220
221         for (Iterator iter = files.iterator(); iter.hasNext(); ) {
222             String JavaDoc sourceName = (String JavaDoc) iter.next();
223             if (files.size() > 1)
224                 System.err.println("Compiling " + sourceName);
225             compile(compiler, logger, sourceName, outFileName, outDir);
226         }
227         return 0;
228     }
229
230     static private void compile(Compiler JavaDoc compiler, Logger logger,
231                  String JavaDoc sourceName, String JavaDoc outFileName, String JavaDoc outDir)
232     {
233         File sourceFile = new File(sourceName);
234         if (outFileName == null && outDir == null) {
235             outFileName = FileUtils.getBase(sourceName) + ".swf";
236         } else if (outFileName == null) {
237             outFileName = outDir + File.separator +
238                 FileUtils.getBase(sourceFile.getName()) + ".swf";
239         }
240         File objectFile = new File(outFileName);
241         try {
242             compiler.compile(sourceFile, objectFile, new Properties());
243         } catch (CompilationError e) {
244             logger.error("Compilation errors occurred:");
245             logger.error(e.getMessage());
246         } catch (IOException e) {
247             logger.error("IO exception: " + e.getMessage());
248             e.printStackTrace();
249         }
250     }
251 }
252
Popular Tags