/*
 * FSTable.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

import java.util.*;

/**
 * FSTable is used to create a table of string literals that can be referenced
 * by an index rather than using the literal value when executing a sequence of
 * actions.
 * 
 * <p>
 * Variables and built-in functions are specified by their name and the FSTable
 * class contains a table of the respective strings. References to a variable or
 * function can then use its index in the table rather than the name resulting
 * in a more compact representation when the actions are encoded into binary
 * form.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the action when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>values</td>
 * <td>An array of up to 65536 strings.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The use of the FSTable class is illustrated in the following simple
 * expression:
 * </p>
 * 
 * <pre>
 * var1 = (var1 + var2) / (var1 - var3);
 * </pre>
 * 
 * <p>
 * This can be translated into the following sequence of actions:
 * </p>
 * 
 * <pre>
 * FSTable values = new FSTable();
 * 
 * values.add(&quot;var1&quot;);
 * values.add(&quot;var2&quot;);
 * values.add(&quot;var3&quot;);
 * 
 * actions.add(values);
 * 
 * actions.add(new FSPush(FSTableIndex(0))); // var1
 * actions.add(FSAction.GetVariable());
 * actions.add(new FSPush(FSTableIndex(0))); // var1
 * actions.add(FSAction.GetVariable());
 * actions.add(new FSPush(FSTableIndex(1))); // var2
 * actions.add(FSAction.GetVariable());
 * actions.add(new FSAction(FSAction.Add));
 * actions.add(new FSPush(FSTableIndex(0))); // var1
 * actions.add(FSAction.GetVariable());
 * actions.add(new FSPush(FSTableIndex(2))); // var3
 * actions.add(FSAction.Subtract());
 * actions.add(FSAction.Divide());
 * actions.add(FSAction.SetVariable());
 * </pre>
 * 
 * <p>
 * The table in the FSTable class can support up to 65536 different variables.
 * As a result using the FSVariable class to reference the variables in the
 * example above uses one byte rather than the five required to represent the
 * name directly (including the null character terminating the string).
 * </p>
 * 
 * <p>
 * The FSTable stores the strings in an array. No checking for duplicate values
 * is performed.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSTable class represents the ActionConstantPool in the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 5. It was
 * extended in Flash 6 to support tables of up to 65536 strings.
 * </p>
 */
public class FSTable extends FSActionObject
{
	private ArrayList values = null;

	/**
	 * Construct an FSTable object, initialising it with values decoded from an
	 * encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSTable(FSCoder coder)
	{
		super(Table);
		decode(coder);
	}

	/** Constructs an empty table. */
	public FSTable()
	{
		super(Table);
	}

	/**
	 * Constructs an FSTable object using the array of strings.
	 * 
	 * @param anArray
	 *            of Strings that will be added to the table.
	 */
	public FSTable(ArrayList anArray)
	{
		super(Table);
		setValues(anArray);
	}

	/**
	 * Constructs an FSTable object by copying values from an existing object.
	 * 
	 * @param obj
	 *            an FSGotoFrame object.
	 */
	public FSTable(FSTable obj)
	{
		super(obj);

		values = new ArrayList(obj.values.size());

		for (Iterator i = obj.values.iterator(); i.hasNext();)
			values.add(new String((String) i.next()));
	}

	/**
	 * Adds a String to the variable table.
	 * 
	 * @param aString
	 *            a String that will be added to the end of the table.
	 */
	public void add(String aString)
	{
		values.add(aString);
	}

	/**
	 * Gets the array of Strings stored in the variable table.
	 * 
	 * @return the array of string literals.
	 */
	public ArrayList getValues()
	{
		return values;
	}

	/**
	 * Sets the array of Strings stored in the literal table.
	 * 
	 * @param anArray
	 *            of Strings that will replaces the existing literal table.
	 */
	public void setValues(ArrayList anArray)
	{
		values = anArray;
	}

	public Object clone()
	{
		FSTable anObject = (FSTable) super.clone();

		anObject.values = new ArrayList();

		for (Iterator i = values.iterator(); i.hasNext();)
			anObject.values.add(new String((String) i.next()));

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
			result = values.equals(((FSTable) anObject).getValues());

		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "values", values, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += 2;

		for (Iterator i = values.iterator(); i.hasNext();)
			length += coder.strlen((String) i.next(), true);

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeWord(values.size(), 2);

		if (values.size() > 0)
		{
			for (Iterator i = values.iterator(); i.hasNext();)
			{
				coder.writeString((String) i.next());
				coder.writeWord(0, 1);
			}
		}
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		int attributeCount = coder.readWord(2, false);

		if (attributeCount > 0)
		{
			values = new ArrayList();

			for (int i = 0; i < attributeCount; i++)
				values.add(coder.readString());
		} else
		{
			/*
			 * Reset the length as Macromedia is using the length of a table to
			 * hide coded following an empty table.
			 */
			length = 2;
			values = new ArrayList();
		}
		coder.endObject(name());
	}
}
