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

/**
 * The FSTransformObject object defines the basic set of method that an object
 * must implement if it can used to represent the data structures that make up
 * the Flash File Format specification.
 * 
 * Sub-classes can be used to represent either one of the tagged data structures
 * such as PlaceObject and added directly to an FSMovie or one of the data-types
 * that the objects contain e.g. a colour.
 * 
 * In addition to the methods directly relating to the coding of the object to
 * and from the binary data representation that comprises the Flash File Format
 * specification the class also includes a common set of methods used when
 * validating or reporting the state of individual objects.
 */
public abstract class FSTransformObject extends Object implements Cloneable
{
	/**
	 * Return the name of the class without the package prefix. This method is
	 * used when logging events to identify the object being encoded or decoded.
	 * 
	 * @return the name of the class, e.g. FSDefineShape.
	 */
	public String name()
	{
		String className = getClass().getName();
		int index = className.lastIndexOf(".") + 1;

		return className.substring(index, className.length());
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		return getClass().isInstance(anObject);
	}

	/**
	 * Creates a deep copy of the entire object.
	 * 
	 * @return a copy of the object.
	 */
	public Object clone()
	{
		Object anObject = null;

		try
		{
			anObject = super.clone();
		} catch (CloneNotSupportedException e)
		{
			throw new InternalError();
		}
		return anObject;
	}

	/**
	 * Generates a string representation of the event object.
	 * 
	 * This method calls appendDescription with the level set to 2 which will
	 * displays the contents of the first level of nested objects (level 1 will
	 * only show the type of any nested objects).
	 * 
	 * @return a string reporting the values in the attributes.
	 */
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();

		appendDescription(buffer, 2);

		return buffer.toString();
	}

	/**
	 * AppendDescription is used to present a string description of the object
	 * including all nested objects up to a specified depth. This method provide
	 * a more controlled way of creating a string representation of an object
	 * since large objects such as font or shape definitions can contain dozens
	 * of nested objects.
	 * 
	 * The representation of the object is appended to the StringBuffer, showing
	 * the name of the class and values of the attributes it contains. If the
	 * object contains any attributes that are objects then the object graph
	 * will be traversed up to the specified depth. If objects are nested at a
	 * level less than specified depth then the full string representation of
	 * the object is displayed. For objects at the specified depth only the name
	 * of the class is displayed. Any objects below this depth are not
	 * displayed.
	 * 
	 * @param buffer
	 *            a StringBuffer to which the description of each object is
	 *            appended.
	 * @param depth
	 *            the maximum level of nesting up to which objects will be
	 *            displayed.
	 */
	public abstract void appendDescription(StringBuffer buffer, int depth);

	abstract public int length(FSCoder coder);

	abstract public void encode(FSCoder coder);

	abstract public void decode(FSCoder coder);
}