📄 example.java
字号:
/*
* Example.java
* Examples
*
* Created by Stuart MacKay on Fri Jul 25 2003.
* Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
*
* This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
*/
package com.flagstone.transform.examples;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import com.flagstone.transform.*;
/*
* The Example class is used as a parent class for each of the examples that
* illustrate how different Flash features are generated using the Transform SWF
* and Transform Utilities frameworks.
*
* The Example class supports a defaulkt option --resultDir <directory> which specifies
* the directory in which the Flash file generated by the example will be written.
* If the resultDir option is not specified then the file is written to the directory
* from where the example was executed.
*/
public abstract class Example
{
/*
* movie - an instance of the FSMovie class that is used to hold the objects
* that will be encoded to generate a Flash movie.
*/
protected FSMovie movie = null;
/*
* options - a table of command line options passed to the example class. The
* name of the option is used as the key. Each entry in the table is an array
* containing zero or more strings which contains the values associated with
* the option.
*/
protected HashMap options = null;
/*
* resultDir - the path to the destination directory where the Flash file
* generated by the example will be written.
*/
protected String resultDir = null;
public Example(String[] args)
{
options = getOptions(args);
/*
* If the resultDir has not been specified on the command line
* then it defaults to the directory from where the example is
* executed.
*/
resultDir = getOption("resultDir", ".");
/*
* Add a trailing slash if it is missing from command line value
*/
if (resultDir.endsWith(File.separator) == false)
resultDir = resultDir + File.separator;
/*
* Create the movie and add the Example class as a listener to record
* any validation messages that are generated when the movie is encoded.
*/
movie = new FSMovie();
}
/*
* Parse the command line arguments. The name of an option is prefixed by '--'.
* Zero or more values may follow an argument name until the next argument name
* is detected. The values for an argument are placed in an array and are stored
* in a map with the option name used as the key.
*
* @param args an array of strings containing the command-line arguments.
* @return a HashMap used to store the options and associated values.
*/
public HashMap getOptions(String[] args)
{
HashMap opts = new HashMap();
for (int i=0; i<args.length; i++)
{
String anOption = args[i];
if (anOption.length() >= 2 && anOption.substring(0, 2).equals("--"))
{
String optionName = anOption.substring(2);
ArrayList arguments = new ArrayList();
for (int j=i+1; j<args.length; j++)
{
String str = args[j];
if (str.length() >= 2 && str.substring(0, 2).equals("--"))
break;
arguments.add(str);
}
if (opts.containsKey(optionName))
{
ArrayList values = (ArrayList) opts.get(optionName);
values.addAll(arguments);
}
else
{
opts.put(optionName, arguments);
}
}
}
return opts;
}
/*
* getOption returns a string value associated with an option. If the
* option has not been specified on the command line then the default
* value passed to the method is returned.
*
* If more than one value is associated with the option only the first
* value is returned.
*
* @param name the name of the option to retrieve the value for.
*
* @param defaultValue the default value returned if the table does not
* contain the option.
*
* @return a string containing the value associated with the option or
* the default value if the option was not specified on the command line.
*/
public String getOption(String name, String defaultValue)
{
String value = defaultValue;
if (options.containsKey(name))
{
ArrayList values = (ArrayList) options.get(name);
value = (String)values.get(0);
}
return value;
}
/*
* getOption returns a single integer value associated with an option.
* If the option has not been specified on the command line then the
* default value passed to the method is returned.
*
* If more than one value is associated with the option only the first
* value is returned.
*
* @param name the name of the option to retrieve the value for.
*
* @param defaultValue the default value returned if the table does not
* contain the option.
*
* @return the int associated with the option or the default value if
* the option was not specified on the command line.
*/
public int getOption(String name, int defaultValue)
{
int value = defaultValue;
if (options.containsKey(name))
{
ArrayList values = (ArrayList) options.get(name);
try {
value = new Integer((String)values.get(0)).intValue();
}
catch (NumberFormatException e) {
}
}
return value;
}
/*
* writeFile is used to encode the objects and write them to the specified Flash file.
*
* To create a Flash movie objects representing each of the Flash data structures are added
* to the movie object. The encoded binary Flash file is then generated by calling the
* FSMovie object's encodeToFile() method.
*/
public void writeFile(String fileName)
{
try
{
movie.encodeToFile(resultDir + fileName);
}
catch (FileNotFoundException e)
{
System.err.println("Cannot open file: " + e.getMessage());
}
catch (IOException e)
{
System.err.println("Cannot write to file: " + e.getMessage());
}
}
/*
* readFile is used to parse a Flash file and initialize the movie with a series of
* of objects - one for each of the different Flash data structures read from the
* file.
*/
public void readFile(String fileName)
{
try
{
movie.decodeFromFile(fileName);
}
catch (FileNotFoundException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
catch (DataFormatException e)
{
System.err.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -