KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > Generator


1 /*
2  * $Id: Generator.java,v 1.10 2002/05/29 04:32:42 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash;
52
53 import java.io.*;
54 import java.util.*;
55 import java.net.*;
56
57 import org.openlaszlo.iv.flash.api.*;
58 //import org.openlaszlo.iv.flash.player.*;
59
import org.openlaszlo.iv.flash.parser.*;
60 import org.openlaszlo.iv.flash.util.*;
61 import org.openlaszlo.iv.flash.commands.*;
62 import org.openlaszlo.iv.flash.context.*;
63
64 /**
65  * Command-line (offline) generator.
66  */

67 public final class Generator {
68
69     private static final int GIF_IMAGE = 0;
70     private static final int JPEG_IMAGE = 1;
71
72     private int imageType = -1;
73     private String JavaDoc outImageName;
74
75     private String JavaDoc outFileName;
76     private String JavaDoc inFileName;
77
78     public void doCommandLine( String JavaDoc[] args, boolean preview ) {
79
80         String JavaDoc dumpFileName = null;
81
82         long startTime = -1;
83         boolean logInit = false;
84
85         CommandExecutor executor = new OfflineCommandExecutor();
86         Context context = new CommandContext( executor );
87
88         GenericCommand setenv_cmd = null;
89         String JavaDoc encoding = null;
90         boolean compressOutput = false;
91
92         // parse options
93
int l = args.length-1;
94         for( int i=0; i<=l; i++ ) {
95             if( args[i].equals("-help") ) {
96                 help();
97             } else if( args[i].equals("-log") ) {
98                 err("Option -log is deprecated, please use -log4j instead");
99                 //Log.setLogToFile();
100
//logInit = true;
101
} else if( args[i].equals("-log4j") ) {
102                 logInit = true;
103             } else if( args[i].equals("-compress") ) {
104                 compressOutput = true;
105             } else if( args[i].equals("-verbose") ) {
106                 if( i == l ) err("Verbose level is not specified");
107                 String JavaDoc level = args[++i];
108                 if( level.equalsIgnoreCase("fatal") ) Log.setFatalLevel();
109                 else if( level.equalsIgnoreCase("error") ) Log.setErrorLevel();
110                 else if( level.equalsIgnoreCase("warn") ) Log.setWarnLevel();
111                 else if( level.equalsIgnoreCase("info") ) Log.setInfoLevel();
112                 else if( level.equalsIgnoreCase("debug") ) Log.setDebugLevel();
113                 else {
114                     err("Unknown verbose level "+level);
115                     Log.setInfoLevel();
116                 }
117             /*} else if( preview && args[i].equals("-logfile") ) {
118                 if( i == l ) err( "Log file is not specified" );
119                 String logFileName = args[++i];
120                 new File(logFileName).delete();
121                 Log.setLogToFile(logFileName);
122                 logInit = true;*/

123             } else if( preview && args[i].equals("-d") ) {
124                 if( i == l ) err( "Debug level is not specified" );
125                 i++; // ignore
126
} else if( args[i].equals("-swf") ) {
127                 if( i == l ) err( "Output file is not specified" );
128                 outFileName = args[++i];
129             } else if( args[i].equals("-gif") ) {
130                 if( i == l ) err( "Output GIF file is not specified" );
131                 outImageName = args[++i];
132                 imageType = GIF_IMAGE;
133             } else if( args[i].equals("-jpg") ) {
134                 if( i == l ) err( "Output JPEG file is not specified" );
135                 outImageName = args[++i];
136                 imageType = JPEG_IMAGE;
137             } else if( args[i].equals("-dump") ) {
138                 if( i == l ) err( "Dump file is not specified" );
139                 dumpFileName = args[++i];
140             } else if( args[i].equals("-time") ) {
141                 startTime = System.currentTimeMillis();
142             } else if( args[i].equals("-setenv1") ) {
143                 if( i == l ) err( "Datasource for -setenv1 is not specified" );
144                 if( setenv_cmd != null ) err( "Datasource is already specified" );
145                 setenv_cmd = new SetEnvironmentCommand();
146                 setenv_cmd.addParameter( "datasource", args[++i] );
147             } else if( args[i].equals("-setenv2") ) {
148                 if( i == l ) err( "Datasource for -setenv1 is not specified" );
149                 if( setenv_cmd != null ) err( "Datasource is already specified" );
150                 setenv_cmd = new SetEnvironment2Command();
151                 setenv_cmd.addParameter( "datasource", args[++i] );
152             } else if( args[i].equals("-encoding") ) {
153                 if( i == l ) err( "Encoding is not specified" );
154                 encoding = args[++i];
155             } else if( args[i].equals("-param") ) {
156                 if( i+2 > l ) err( "Error declaring parameter" );
157                 String JavaDoc name = args[++i];
158                 String JavaDoc value = args[++i];
159                 if( value.length() > 0 && value.charAt(0) == '"' && value.charAt( value.length()-1 ) == '"' ) {
160                     value = value.substring(1, value.length()-1);
161                 }
162                 ((StandardContext)context).setValue(name, Util.processEscapes(value) );
163             } else if( preview && args[i].equals("-t") ) {
164                 if( i == l ) err( "Input file is not specified" );
165                 inFileName = args[++i];
166             } else {
167                 inFileName = args[i];
168                 if( i != l ) err( "Too many parameters" );
169             }
170         }
171
172         if( inFileName == null ) err( "Input file is not specified" );
173         if( outFileName == null && imageType == -1 && dumpFileName == null ) {
174             if( inFileName.endsWith(".swt") ) {
175                 outFileName = inFileName.substring(0, inFileName.length()-3)+"swf";
176             } else {
177                 outFileName = inFileName+".swf";
178             }
179         }
180
181         if( !logInit ) {
182             Log.setLogToConsole();
183         }
184
185         FlashFile file = null;
186         if( encoding != null && Util.isDefault(encoding) ) {
187             encoding = null;
188         }
189
190         try {
191
192             try {
193                 file = FlashFile.parse(inFileName, imageType!=-1, encoding);
194             } catch( FileNotFoundException e ) {
195                 Log.logRB(Resource.FILENOTFOUND, new Object JavaDoc[] {inFileName}, e);
196             }
197
198             if( file != null ) {
199                 // set parsed file to command executor
200
executor.setFlashFile(file);
201
202                 if( dumpFileName != null ) dump( file, dumpFileName );
203                 else {
204                     // load environment
205
if( setenv_cmd != null ) {
206                         try {
207                             FakeContext fakeContext = new FakeContext(context);
208                             setenv_cmd.doCommand(file, fakeContext, file.getMainScript(), 0);
209                             Context myContext = fakeContext.getContext();
210                             myContext.setParent(context); // it's supposedly already done
211
context = myContext;
212                         } catch( Exception JavaDoc e ) {
213                             Log.logRB( new IVException(Resource.ERRDOCMD, new Object JavaDoc[] {
214                                         file.getFullName(), "", "0", setenv_cmd.getCommandName() }, e));
215                         }
216                     }
217
218                     file.processFile(context);
219
220                     if( outFileName != null ) {
221                         try {
222                             file.setCompressed(compressOutput);
223                             FlashOutput fob = file.generate();
224                             BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( outFileName ) );
225                             bos.write( fob.getBuf(), 0, fob.getSize() );
226                             bos.close();
227                         } catch( IOException e ) {
228                             Log.logRB(Resource.ERRWRITINGFILE, new Object JavaDoc[] {outFileName}, e);
229                         }
230                     }
231                 }
232
233             }
234
235             /*
236             if( imageType != -1 ) {
237                 Player player = new Player(file);
238                 player.play(outImageName);
239             }*/

240
241         } catch( Exception JavaDoc e ) {
242             Log.log(e);
243         } catch( Throwable JavaDoc e ) {
244             Log.logRB(Resource.UNKNOWNERROR, e);
245         }
246
247         if( startTime != -1 ) {
248             System.err.println( "Processing time is: "+(System.currentTimeMillis()-startTime)+"ms" );
249         }
250
251     }
252
253     public static void main( String JavaDoc[] args ) {
254         Util.init();
255
256         Generator gen = new Generator();
257         gen.doCommandLine(args, false);
258
259         System.exit(0);
260     }
261
262     private static void dump( FlashFile file, String JavaDoc fileName ) throws IOException {
263         FileOutputStream fout = new FileOutputStream(fileName);
264         PrintStream out = new PrintStream(fout, true);
265         file.printContent( out );
266         Enumeration defs = file.definitions();
267         out.println( "Definitions:" );
268         while( defs.hasMoreElements() ) {
269             FlashDef def = (FlashDef) defs.nextElement();
270             def.printContent( out, "" );
271         }
272     }
273
274     public static void help() {
275         System.err.println( "JGenerator Version "+Util.getVersion() );
276         System.err.println( "Copyright (c) JZox, Inc. 2000-2002. All rights reserved." );
277         System.err.println( "" );
278         System.err.println( "Usage: jgenerate [options] <filename.swt>" );
279         System.err.println( "" );
280         System.err.println( "Options:" );
281         System.err.println( " -swf <filename.swf> output as Flash movie" );
282         //System.err.println( " -jpg <filename.jpg> output as JPEG image" );
283
//System.err.println( " -gif <filename.gif> output as GIF image" );
284
System.err.println( " -param <name> <value> specifies a named parameter" );
285         System.err.println( " -setenv1 <url> specifies flash environment (Name,Value or XML)" );
286         System.err.println( " -setenv2 <url> specifies flash environment (Name1,Name2... or XML)" );
287         System.err.println( " -encoding <encoding> specifies default encoding of all datasources" );
288         System.err.println( " -dump <filename> dump template content into specified file" );
289         //System.err.println( " -log redirects output to system log file" );
290
//System.err.println( " -logfile <filename> redirects output to specified log file" );
291
System.err.println( " -log4j use log4j configuration for output" );
292         System.err.println( " -verbose <level> verbose level: fatal, error, warn, info, debug" );
293         System.err.println( " -time print processing time" );
294         System.err.println( " -compress compress output (Flash MX)" );
295         System.err.println( " -help displays usage text" );
296         System.err.println( "" );
297         System.exit(1);
298     }
299
300     public static void err( String JavaDoc msg ) {
301         System.err.println( msg );
302         help();
303     }
304
305     /**
306      * Offline command executor.
307      * <P>
308      * Adds additional commands for offline version of generator
309      */

310     public class OfflineCommandExecutor extends CommandExecutor {
311
312         /**
313          * Sets output file name
314          *
315          * @param name output file name
316          * @return output file name
317          */

318         public String JavaDoc setOutputFile( Context context, String JavaDoc name ) {
319             outFileName = name;
320             return outFileName;
321         }
322
323         /**
324          * Returns output file name
325          *
326          * @return output file name
327          */

328         public String JavaDoc getOutputFile( Context context ) {
329             return outFileName;
330         }
331
332         /**
333          * Returns input file name
334          *
335          * @return input file name
336          */

337         public String JavaDoc getInputFile( Context context ) {
338             return inFileName;
339         }
340     }
341
342 }
343
Popular Tags