/* * Example.java * Examples * * Created by Stuart MacKay on Fri Jul 25 2003. * Copyright (c) 2001-2004 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 com.flagstone.transform.*; import java.util.*; import java.util.zip.*; import java.io.*; /* * 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 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 options = new HashMap(); for (int i=0; i= 2 && anOption.substring(0, 2).equals("--")) { String optionName = anOption.substring(2); ArrayList arguments = new ArrayList(); for (int j=i+1; j= 2 && str.substring(0, 2).equals("--")) break; arguments.add(str); } if (options.containsKey(optionName)) { ArrayList values = (ArrayList) options.get(optionName); values.addAll(arguments); } else { options.put(optionName, arguments); } } } return options; } /* * 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); Integer optionValue; 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); } } }