/*
 * FSExport.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.*;

/**
 * FSExport is used to export shapes and other objects so they can be used in
 * another Flash file.
 * 
 * <p>
 * Since the identifier for an object is only unique within a given Flash file,
 * each object exported must be given a name so it can referenced when it is
 * imported.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>objects</td>
 * <td>A HashTable containing key-value pairs where the key is the identifier
 * of the object being exported and the value is name assigned to the object so
 * it can be referenced when it is imported into a Flash file.</td>
 * </tr>
 * 
 * </table>
 * 
 * <h1 class="datasheet">Examples</h1>
 * 
 * <P>
 * To export an object from a Flash file:
 * </P>
 * 
 * <pre>
 *  FSDefineShape shape = new FSDefineShape(...);
 * 
 *  movie.add(shape);
 *  movie.add(new FSExport(shape.getIdentifier(), &quot;Shape&quot;));
 * </pre>
 * 
 * The object can then be imported into another file:
 * 
 * <pre>
 * movie.add(new FSImport(&quot;exportFile.swf&quot;, movie.newIdentifier(), &quot;Shape&quot;));
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSExport class represents the ExportAssets tag in the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 5.
 * </p>
 */
public class FSExport extends FSMovieObject
{
	private Hashtable objects = new Hashtable();

	FSExport()
	{
		super(Export);
	}

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

	/**
	 * Constructs an FSExport object that exports the object with the specified
	 * identifier. The exported object is assigned the specified name to allow
	 * it to be referenced in files importing the object.
	 * 
	 * @param anIdentifier
	 *            the identifier of the object to be exported.
	 * @param aString
	 *            the name of the exported object to allow it to be referenced.
	 */
	public FSExport(int anIdentifier, String aString)
	{
		super(Export);
		add(anIdentifier, aString);
	}

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

		objects = new Hashtable(obj.objects.size());

		for (Iterator i = obj.objects.keySet().iterator(); i.hasNext();)
		{
			Object key = i.next();
			objects.put(key, new String((String) objects.get(key)));
		}
	}

	/**
	 * Adds the identifier and name to the list of objects to be exported.
	 * 
	 * @param anIdentifier
	 *            the identifier of the object to be exported.
	 * @param aString
	 *            the name of the exported object to allow it to be referenced.
	 */
	public void add(int anIdentifier, String aString)
	{
		objects.put(new Integer(anIdentifier), aString);
	}

	/**
	 * Gets the table of objects to be imported.
	 * 
	 * @return the table of imported objects.
	 */
	public Hashtable getObjects()
	{
		return objects;
	}

	/**
	 * Sets the table of objects to be imported.
	 * 
	 * @param aTable
	 *            the table of objects being imported.
	 */
	public void setObjects(Hashtable aTable)
	{
		objects = aTable;
	}

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

		if (super.equals(anObject))
		{
			result = objects.equals(((FSExport) anObject).getObjects());
		}
		return result;
	}

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

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

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

		length += 2;

		for (Enumeration e = objects.keys(); e.hasMoreElements();)
		{
			String name = (String) (objects.get(e.nextElement()));

			length += 2;
			length += coder.strlen(name, true);
		}
		return length;
	}

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

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

		for (Enumeration e = objects.keys(); e.hasMoreElements();)
		{
			Integer identifier = (Integer) (e.nextElement());
			String name = (String) (objects.get(identifier));

			coder.writeWord(identifier.intValue(), 2);
			coder.writeString(name);
			coder.writeWord(0, 1);
		}
		coder.endObject(name());
	}

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

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

		for (int i = 0; i < count; i++)
		{
			int identifier = coder.readWord(2, false);
			String name = coder.readString();

			add(identifier, name);
		}
		coder.endObject(name());
	}
}
