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

/**
 * Models the item element in QJML.
 * 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="QMContentModel.html#remList">rem*</a>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMContentModel.html#aref">(aref</a>|<a href="QMContentModel.html#access">id|identity|ignore|field|property|accessClass)?</a>)>

&lt;!ATTLIST item 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMContentModel.html#label">label</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMContentModel.html#cref">cref</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMContentModel.html#optional">optional</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMContentModel.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 QMItem extends QMContentModel 
{
	/**
	 * <p>
	 * Models a coin attribute on the item element.
	 * The coin attribute names another coin in the QJML document.
	 * When the schema attribute is not set, the named coin must be present in the
	 * current QJML 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 QJML document, or it names a coin in an included QJML 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="";

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

	public Object clone()
	{
		QMItem rv=(QMItem)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 QMItem))
		{
			QMItem p=(QMItem)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=(QMCoin)coinMap.get(coinName);
			if (coin==null)
				throw new SAXException("Undefined coin: "+coinName);
		}

		// Now perform the delayed access initialization.
		if (access!=null)
			access.init(coinMap,contentMap,enclosingCoin,this);
	}

	/**
	 * Initialize access--delayed until QMItem completes its initialization.
	 * 
	 */
	public void accessInit(Map coinMap, Map contentMap, QMCoin coin)
		throws SAXException
	{
	}

	/**
	 * 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);
		if (coin instanceof QMBean)
			return ((QMBean)coin).getTag();
		if (coin instanceof QMText)
			return ((QMText)coin).getTag();
		else
			return coin.getLabel();
	}
}