1 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.parser.*; 60 import org.openlaszlo.iv.flash.util.*; 61 import org.openlaszlo.iv.flash.commands.*; 62 import org.openlaszlo.iv.flash.context.*; 63 64 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 outImageName; 74 75 private String outFileName; 76 private String inFileName; 77 78 public void doCommandLine( String [] args, boolean preview ) { 79 80 String 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 encoding = null; 90 boolean compressOutput = false; 91 92 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 } 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 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 123 } else if( preview && args[i].equals("-d") ) { 124 if( i == l ) err( "Debug level is not specified" ); 125 i++; } 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 name = args[++i]; 158 String 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 [] {inFileName}, e); 196 } 197 198 if( file != null ) { 199 executor.setFlashFile(file); 201 202 if( dumpFileName != null ) dump( file, dumpFileName ); 203 else { 204 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); context = myContext; 212 } catch( Exception e ) { 213 Log.logRB( new IVException(Resource.ERRDOCMD, new Object [] { 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 [] {outFileName}, e); 229 } 230 } 231 } 232 233 } 234 235 240 241 } catch( Exception e ) { 242 Log.log(e); 243 } catch( Throwable 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 [] 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 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( " -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( " -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 msg ) { 301 System.err.println( msg ); 302 help(); 303 } 304 305 310 public class OfflineCommandExecutor extends CommandExecutor { 311 312 318 public String setOutputFile( Context context, String name ) { 319 outFileName = name; 320 return outFileName; 321 } 322 323 328 public String getOutputFile( Context context ) { 329 return outFileName; 330 } 331 332 337 public String getInputFile( Context context ) { 338 return inFileName; 339 } 340 } 341 342 } 343 | Popular Tags |