package com.jxml.quick.model.qdml;
import com.jxml.quick.ocm.*;
import com.jxml.quick.*;
import org.xml.sax.*;
import java.util.*;
import java.io.*;

/**
 * Models the item element in QDML.
 * The item element is used to define attributes and element content,
 * providing information that relates to the XML document type and
 * the Java classes that model them.
 <pre>
DTD:

&lt;!ELEMENT item (<a href="QDMContentModel.html#remList">rem*</a>, <a href="#id">id?</a>)

&lt;!ATTLIST item 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMContentModel.html#label">label</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMContentModel.html#cref">cref</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMContentModel.html#optional">optional</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMContentModel.html#repeating">repeating</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#coin">coin</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#schema">schema</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#fixed">fixed</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#value">value</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>

 </pre> 
 */
public class QDMItem extends QDMContentModel 
{
	/**
	 * <p>
	 * Models a coin attribute on the item element.
	 * The coin attribute names another coin in the QDML document.
	 * When the schema attribute is not set, the named coin must be present in the
	 * current QDML document or in an included document.
	 * But when the schema attribute is set, the named coin must be present in the
	 * QIML document referenced by the schema attribute.
	 * </p><p>
	 * The coin attribute is optional when the cref attribute is set,
	 * as its value will then be determined by the item element named by the cref.
	 * </p>
	 */
	public String coinName="";

	/**
	 * <p>
	 * Models the schema attribute on the item element.
	 * The schema attribute provides the URL of the QIML document which contains
	 * the coin referenced by the coin attribute.
	 * The schema attribute is optional.
	 * When the schema attribute is not present, the coin attribute names a coin
	 * in the current QDML document, or it names a coin in an included QDML document.
	 * </p><p>
	 * When the cref attribute is set and the schema attribute is not present, 
	 * the schema attribute on the item element named by the cref is used.
	 * </p>
	 */
	public String schema="";

	/**
	 * <p>
	 * Models the fixed attribute on the item element.
	 * When the fixed attribute is set to true,
	 * the attribute described by the item element has a pre-assigned value 
	 * that can not be changed.
	 * </p><p>
	 * The fixed attribute defaults to false.
	 * But when the fixed attribute is false and the cref attribute is set,
	 * the fixed attribute on the item element named by the cref is used.
	 * </p>
	 */
	public boolean fixed=false;

	/**
	 * <p>
	 * Models the value attribute on the item element.
	 * When the fixed attribute is false, the value attribute supplies a default
	 * value for the attribute defined by the item element.
	 * But when the fixed attribute is true, the value attribute supplies
	 * the unchangable pre-assigned value of the attribute being defined.
	 * </p><p>
	 * The value attribute is optional.
	 * But when the value attribute is not set and the cref attribute is set,
	 * the value attribute on the item element named by the cref is used.
	 * </p>
	 */
	public String value="";

	/**
	 * Models the optional id element content.
	 * When present, the attribute is of type id.
	 */
	public QDMId id=null;

	/**
	 * <p>
	 * The QDMCoin object which models the element named by the coin attribute.
	 * This variable is populated when the data model is initialized.
	 * </p>
	 */
	public transient QDMCoin coin=null;

	public Object clone()
	{
		QDMItem rv=(QDMItem)super.clone();
		rv.coin=null;
		return rv;
	}

	public void init(Map coinMap, Map contentMap)
		throws SAXException
	{
		if (initialized)
			return;
		super.init(coinMap,contentMap);

		if (content!=null && (content instanceof QDMItem))
		{
			QDMItem p=(QDMItem)content;
			if ("".equals(coinName))
				coinName=p.coinName;
			if (!fixed)
				fixed=p.fixed;
			if ("".equals(value))
				value=p.value;
		}

		if (!"".equals(coinName))
		{
			if (!"".equals(schema))
				return;
			coin=(QDMCoin)coinMap.get(coinName);
			if (coin==null)
				throw new SAXException("Undefined coin: "+coinName);
		}
	}

	/**
	 * Returns the attribute tag name.
	 */
	public String getAttributeTagName()
		throws SAXException
	{
		if (coin==null)
			if ("".equals(coinName))
				throw new SAXException("Attribute item has no coin");
			else
				throw new SAXException("href not supported for attributes: "+coinName);
		return ((QDMText)coin).getTag();
	}

	public boolean isContainedBy(String label, TreeMap coinMap)
	{
		return coin.derivesFromOther(label,coinMap);
	}
}