001: /*
002: * $Id: Generator.java,v 1.10 2002/05/29 04:32:42 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash;
052:
053: import java.io.*;
054: import java.util.*;
055: import java.net.*;
056:
057: import org.openlaszlo.iv.flash.api.*; //import org.openlaszlo.iv.flash.player.*;
058: import org.openlaszlo.iv.flash.parser.*;
059: import org.openlaszlo.iv.flash.util.*;
060: import org.openlaszlo.iv.flash.commands.*;
061: import org.openlaszlo.iv.flash.context.*;
062:
063: /**
064: * Command-line (offline) generator.
065: */
066: public final class Generator {
067:
068: private static final int GIF_IMAGE = 0;
069: private static final int JPEG_IMAGE = 1;
070:
071: private int imageType = -1;
072: private String outImageName;
073:
074: private String outFileName;
075: private String inFileName;
076:
077: public void doCommandLine(String[] args, boolean preview) {
078:
079: String dumpFileName = null;
080:
081: long startTime = -1;
082: boolean logInit = false;
083:
084: CommandExecutor executor = new OfflineCommandExecutor();
085: Context context = new CommandContext(executor);
086:
087: GenericCommand setenv_cmd = null;
088: String encoding = null;
089: boolean compressOutput = false;
090:
091: // parse options
092: int l = args.length - 1;
093: for (int i = 0; i <= l; i++) {
094: if (args[i].equals("-help")) {
095: help();
096: } else if (args[i].equals("-log")) {
097: err("Option -log is deprecated, please use -log4j instead");
098: //Log.setLogToFile();
099: //logInit = true;
100: } else if (args[i].equals("-log4j")) {
101: logInit = true;
102: } else if (args[i].equals("-compress")) {
103: compressOutput = true;
104: } else if (args[i].equals("-verbose")) {
105: if (i == l)
106: err("Verbose level is not specified");
107: String level = args[++i];
108: if (level.equalsIgnoreCase("fatal"))
109: Log.setFatalLevel();
110: else if (level.equalsIgnoreCase("error"))
111: Log.setErrorLevel();
112: else if (level.equalsIgnoreCase("warn"))
113: Log.setWarnLevel();
114: else if (level.equalsIgnoreCase("info"))
115: Log.setInfoLevel();
116: else if (level.equalsIgnoreCase("debug"))
117: Log.setDebugLevel();
118: else {
119: err("Unknown verbose level " + level);
120: Log.setInfoLevel();
121: }
122: /*} else if( preview && args[i].equals("-logfile") ) {
123: if( i == l ) err( "Log file is not specified" );
124: String logFileName = args[++i];
125: new File(logFileName).delete();
126: Log.setLogToFile(logFileName);
127: logInit = true;*/
128: } else if (preview && args[i].equals("-d")) {
129: if (i == l)
130: err("Debug level is not specified");
131: i++; // ignore
132: } else if (args[i].equals("-swf")) {
133: if (i == l)
134: err("Output file is not specified");
135: outFileName = args[++i];
136: } else if (args[i].equals("-gif")) {
137: if (i == l)
138: err("Output GIF file is not specified");
139: outImageName = args[++i];
140: imageType = GIF_IMAGE;
141: } else if (args[i].equals("-jpg")) {
142: if (i == l)
143: err("Output JPEG file is not specified");
144: outImageName = args[++i];
145: imageType = JPEG_IMAGE;
146: } else if (args[i].equals("-dump")) {
147: if (i == l)
148: err("Dump file is not specified");
149: dumpFileName = args[++i];
150: } else if (args[i].equals("-time")) {
151: startTime = System.currentTimeMillis();
152: } else if (args[i].equals("-setenv1")) {
153: if (i == l)
154: err("Datasource for -setenv1 is not specified");
155: if (setenv_cmd != null)
156: err("Datasource is already specified");
157: setenv_cmd = new SetEnvironmentCommand();
158: setenv_cmd.addParameter("datasource", args[++i]);
159: } else if (args[i].equals("-setenv2")) {
160: if (i == l)
161: err("Datasource for -setenv1 is not specified");
162: if (setenv_cmd != null)
163: err("Datasource is already specified");
164: setenv_cmd = new SetEnvironment2Command();
165: setenv_cmd.addParameter("datasource", args[++i]);
166: } else if (args[i].equals("-encoding")) {
167: if (i == l)
168: err("Encoding is not specified");
169: encoding = args[++i];
170: } else if (args[i].equals("-param")) {
171: if (i + 2 > l)
172: err("Error declaring parameter");
173: String name = args[++i];
174: String value = args[++i];
175: if (value.length() > 0 && value.charAt(0) == '"'
176: && value.charAt(value.length() - 1) == '"') {
177: value = value.substring(1, value.length() - 1);
178: }
179: ((StandardContext) context).setValue(name, Util
180: .processEscapes(value));
181: } else if (preview && args[i].equals("-t")) {
182: if (i == l)
183: err("Input file is not specified");
184: inFileName = args[++i];
185: } else {
186: inFileName = args[i];
187: if (i != l)
188: err("Too many parameters");
189: }
190: }
191:
192: if (inFileName == null)
193: err("Input file is not specified");
194: if (outFileName == null && imageType == -1
195: && dumpFileName == null) {
196: if (inFileName.endsWith(".swt")) {
197: outFileName = inFileName.substring(0, inFileName
198: .length() - 3)
199: + "swf";
200: } else {
201: outFileName = inFileName + ".swf";
202: }
203: }
204:
205: if (!logInit) {
206: Log.setLogToConsole();
207: }
208:
209: FlashFile file = null;
210: if (encoding != null && Util.isDefault(encoding)) {
211: encoding = null;
212: }
213:
214: try {
215:
216: try {
217: file = FlashFile.parse(inFileName, imageType != -1,
218: encoding);
219: } catch (FileNotFoundException e) {
220: Log.logRB(Resource.FILENOTFOUND,
221: new Object[] { inFileName }, e);
222: }
223:
224: if (file != null) {
225: // set parsed file to command executor
226: executor.setFlashFile(file);
227:
228: if (dumpFileName != null)
229: dump(file, dumpFileName);
230: else {
231: // load environment
232: if (setenv_cmd != null) {
233: try {
234: FakeContext fakeContext = new FakeContext(
235: context);
236: setenv_cmd.doCommand(file, fakeContext,
237: file.getMainScript(), 0);
238: Context myContext = fakeContext
239: .getContext();
240: myContext.setParent(context); // it's supposedly already done
241: context = myContext;
242: } catch (Exception e) {
243: Log
244: .logRB(new IVException(
245: Resource.ERRDOCMD,
246: new Object[] {
247: file.getFullName(),
248: "",
249: "0",
250: setenv_cmd
251: .getCommandName() },
252: e));
253: }
254: }
255:
256: file.processFile(context);
257:
258: if (outFileName != null) {
259: try {
260: file.setCompressed(compressOutput);
261: FlashOutput fob = file.generate();
262: BufferedOutputStream bos = new BufferedOutputStream(
263: new FileOutputStream(outFileName));
264: bos.write(fob.getBuf(), 0, fob.getSize());
265: bos.close();
266: } catch (IOException e) {
267: Log.logRB(Resource.ERRWRITINGFILE,
268: new Object[] { outFileName }, e);
269: }
270: }
271: }
272:
273: }
274:
275: /*
276: if( imageType != -1 ) {
277: Player player = new Player(file);
278: player.play(outImageName);
279: }*/
280:
281: } catch (Exception e) {
282: Log.log(e);
283: } catch (Throwable e) {
284: Log.logRB(Resource.UNKNOWNERROR, e);
285: }
286:
287: if (startTime != -1) {
288: System.err.println("Processing time is: "
289: + (System.currentTimeMillis() - startTime) + "ms");
290: }
291:
292: }
293:
294: public static void main(String[] args) {
295: Util.init();
296:
297: Generator gen = new Generator();
298: gen.doCommandLine(args, false);
299:
300: System.exit(0);
301: }
302:
303: private static void dump(FlashFile file, String fileName)
304: throws IOException {
305: FileOutputStream fout = new FileOutputStream(fileName);
306: PrintStream out = new PrintStream(fout, true);
307: file.printContent(out);
308: Enumeration defs = file.definitions();
309: out.println("Definitions:");
310: while (defs.hasMoreElements()) {
311: FlashDef def = (FlashDef) defs.nextElement();
312: def.printContent(out, "");
313: }
314: }
315:
316: public static void help() {
317: System.err.println("JGenerator Version " + Util.getVersion());
318: System.err
319: .println("Copyright (c) JZox, Inc. 2000-2002. All rights reserved.");
320: System.err.println("");
321: System.err.println("Usage: jgenerate [options] <filename.swt>");
322: System.err.println("");
323: System.err.println("Options:");
324: System.err
325: .println(" -swf <filename.swf> output as Flash movie");
326: //System.err.println( " -jpg <filename.jpg> output as JPEG image" );
327: //System.err.println( " -gif <filename.gif> output as GIF image" );
328: System.err
329: .println(" -param <name> <value> specifies a named parameter");
330: System.err
331: .println(" -setenv1 <url> specifies flash environment (Name,Value or XML)");
332: System.err
333: .println(" -setenv2 <url> specifies flash environment (Name1,Name2... or XML)");
334: System.err
335: .println(" -encoding <encoding> specifies default encoding of all datasources");
336: System.err
337: .println(" -dump <filename> dump template content into specified file");
338: //System.err.println( " -log redirects output to system log file" );
339: //System.err.println( " -logfile <filename> redirects output to specified log file" );
340: System.err
341: .println(" -log4j use log4j configuration for output");
342: System.err
343: .println(" -verbose <level> verbose level: fatal, error, warn, info, debug");
344: System.err
345: .println(" -time print processing time");
346: System.err
347: .println(" -compress compress output (Flash MX)");
348: System.err
349: .println(" -help displays usage text");
350: System.err.println("");
351: System.exit(1);
352: }
353:
354: public static void err(String msg) {
355: System.err.println(msg);
356: help();
357: }
358:
359: /**
360: * Offline command executor.
361: * <P>
362: * Adds additional commands for offline version of generator
363: */
364: public class OfflineCommandExecutor extends CommandExecutor {
365:
366: /**
367: * Sets output file name
368: *
369: * @param name output file name
370: * @return output file name
371: */
372: public String setOutputFile(Context context, String name) {
373: outFileName = name;
374: return outFileName;
375: }
376:
377: /**
378: * Returns output file name
379: *
380: * @return output file name
381: */
382: public String getOutputFile(Context context) {
383: return outFileName;
384: }
385:
386: /**
387: * Returns input file name
388: *
389: * @return input file name
390: */
391: public String getInputFile(Context context) {
392: return inFileName;
393: }
394: }
395:
396: }
|