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

/**
 * FSImport is used to import shapes and other objects from another Flash file.
 * 
 * <p>
 * Since the identifier for an object is only unique within a given Flash file,
 * imported objects are referenced by a name assigned when the object is
 * exported.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left">Attribute</th>
 * <th align="left">Description</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>url</td>
 * <td>The URL of the file containing the object to be imported. The URL must
 * be in the same sub-domain and relative to the URL of the Flash file
 * containing the file importing the object.</td>
 * </tr>
 * 
 * <tr>
 * <td>objects</td>
 * <td>A HashTable containing key-value pairs where the key is the identifier
 * of the object being imported and the value is name assigned to the object
 * when it was exported into a Flash file.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * To provide a degree of security the Flash Player will only import files that
 * originate from the same domain as the file that it is currently playing. For
 * example if the Flash file being shown was loaded from
 * www.mydomain.com/flash.swf then the file contains the exported objects must
 * reside somewhere at www.mydomain.com. This prevents a malicious Flash file
 * from loading files from an unknown third party.
 * </p>
 * 
 * <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>
 * 
 * <p>
 * The object can then be imported:
 * </p>
 * 
 * <pre>
 * movie.add(new FSImport(&quot;exportFile.swf&quot;, movie.newIdentifier(), &quot;Shape&quot;));
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSImport class represents the ImportAssets data structure in the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 5.
 * </p>
 */
public class FSImport extends FSMovieObject
{
	private String url = null;
	private Hashtable objects = new Hashtable();

	FSImport()
	{
		super(Import);
	}

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

	/**
	 * Constructs an FSImport object that imports an object from the specified
	 * file. The exported object is referenced by a name assigned to it when it
	 * was exported. The newly imported object must be assigned an identifier
	 * that is unique within the movie the object is imported into. Limited
	 * security is provided by requiring that the URL must be in the same domain
	 * or sub-domain as the URL of the movie which contains this object.
	 * 
	 * @param aUrl
	 *            the URL referencing the file to be imported.
	 * @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 FSImport(String aUrl, int anIdentifier, String aString)
	{
		super(Import);
		setUrl(aUrl);
		add(anIdentifier, aString);
	}

	/**
	 * Constructs an FSImport object by copying values from an existing object.
	 * 
	 * @param obj
	 *            an FSImport object.
	 */
	public FSImport(FSImport obj)
	{
		super(obj);
		url = new String(obj.url);

		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 imported.
	 * 
	 * @param anIdentifier
	 *            the identifier of the object to be imported.
	 * @param aString
	 *            the name of the imported object to allow it to be referenced.
	 */
	public void add(int anIdentifier, String aString)
	{
		objects.put(new Integer(anIdentifier), aString);
	}

	/**
	 * Gets the URL of the file containing the object to be imported. Limited
	 * security is provided by requiring that the URL must be in the same domain
	 * or sub-domain as the URL of the movie which contains this object.
	 * 
	 * @return the URL of the file containing the object to be imported.
	 */
	public String getUrl()
	{
		return url;
	}

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

	/**
	 * Sets the URL of the file containing the imported objects. The URL must be
	 * in the same sub-domain and relative to the URL of the file containing the
	 * FSImport object.
	 * 
	 * @param aString
	 *            a URL relative to the URL of the file containing the FSImport
	 *            object.
	 */
	public void setUrl(String aString)
	{
		url = aString;
	}

	/**
	 * 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))
		{
			if (url != null)
				result = url.equals(((FSImport) anObject).getUrl());
			else
				result = ((FSImport) anObject).getUrl() == null;

			result = result
							&& objects.equals(((FSImport) anObject)
											.getObjects());
		}
		return result;
	}

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

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

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

		length += coder.strlen(url, true);
		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.writeString(url);
		coder.writeWord(0, 1);

		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);

		url = coder.readString();

		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());
	}
}
