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 a bean element in QDML.
 * A bean element is used to describe elements.
 <pre>
DTD:

&lt;!ELEMENT bean (<a href="QDMCoin.html#remList">rem*</a>, <a href="#extendor">extends?</a>, <a href="#implementsList">implements*</a>, <a href="#attributes">attributes?</a>, <a href="#elements">elements?</a>)> 

&lt;!ATTLIST bean
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMCoin.html#label">label</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#tag">tag</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#wild">wild</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#type">type</a> (MIXED|BIMODAL)#IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#inline">inline</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QDMCoin.html#generate">generate</a> CDATA "True"
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>
 </pre>
 */
public class QDMBean extends QDMCoin
	implements QDMElement
{
	/**
	 * <p>
	 * Models the tag attribute on the bean element.
	 * The tag attribute holds the XML tag name for the element
	 * being defined by the bean element.
	 * </p><p>
	 * When the label attribute is not present, the tag attribute is used to name the bean coin.
	 * </p>
	 */
	public String tag="";

	/**
	 * <p>
	 * Models the wild attribute on the bean coin.
	 * The wild attribute defaults to false.
	 * When the wild attribute is true,
	 * the bean element can be used to represent an element with any tag name.
	 * </p><p>
	 * When the wild attribute is false, the tag attribute is required, but
	 * when the wild attribute is true, the tag attribute must not be present.
	 * </p><p>
	 * When the wild attribute is false, the label attribute is optional,
	 * but when the wild attribute is true, the label attribute is required.
	 * </p>
	 */
	public boolean wild=false;

	/**
	 * <p>
	 * Models the type attribute on the bean coin.
	 * The type attribute is optional.
	 * </p><p>
	 * The following values are allowed:
	 * <ul>
	 * </ul>
	 * <li>
	 * MIXED -- The element can contain both text and other elements.
	 * </li>
	 * <li>
	 * BIMODAL -- Any given element can contain either text or other elements.
	 * </li>
	 * </p>
	 * <p>
	 * When the type attribute is not specified, the element being defined can
	 * contain only other elements.
	 * </p>
	 */
	public String type="";

	/**
	 * Models the inline attribute on the bean element.
	 * The inline attribute is optional, with a default value of false.
	 * When creating an XML document, the element is begun on a new line
	 * unless inline is set to true.
	 */
	public boolean inline=false;

	/**
	 * Models an extends element, which is optional.
	 * A bean coin can extend an abstract coin or another bean coin,
	 * which is named by the extends element.
	 */
	public QDMExtends extendor=null;

	/**
	 * A container for QDMImplements objects, which model implements elements.
	 * The implements elements are optional and repeating.
	 * These implements elements name the interface coins which this bean coin "implements".
	 * (A bean element can impliment any number of interfaces.)
	 */
	public ArrayList implementsList=new ArrayList();

	/**
	 * Models an attributes element, which is optional.
	 * The attributes element defines the attributes associated with this coin,
	 * as well as various related variables and property methods on the Java class used
	 * to model those attributes.
	 */
	public QDMAttributes attributes=null;

	/**
	 * Models an elements element, which is optional.
	 * The elements element specifies the elements which can be contained by the element being defined.
	 */
	public QDMElements elements=null;

	/**
	 * A container for QDMField objects, which model field elements.
	 * The field elements are optional and repeating.
	 * These field elements describe the variables of the Java class which
	 * this bean element describes.
	 */
	public ArrayList fieldList=new ArrayList();

	/**
	 * A container for QDMProperty objects, which model property elements.
	 * The property elements are optional and repeating.
	 * These property elements describe the bean property methods of the Java class which
	 * this bean element describes.
	 */
	public ArrayList propertyList=new ArrayList();

	/**
	 * The QDMAbstract or QDMBean object named by the extends element.
	 * This variable is set when the data model is initialized.
	 */
	public transient QDMCoin extendsCoin=null;

	public Object clone()
	{
		QDMBean rv=(QDMBean)super.clone();
		rv.extendor=(QDMExtends)clone(extendor);
		rv.implementsList=clone(implementsList);
		rv.attributes=(QDMAttributes)clone(attributes);
		rv.elements=(QDMElements)clone(elements);
		rv.fieldList=clone(fieldList);
		rv.propertyList=clone(propertyList);
		rv.extendsCoin=null;
		return rv;
	}

	public String getLabel()
	{
		if (!"".equals(label))
			return label;
		return tag;
	}

	/**
	 * Returns the XML tag name assigned to the bean element.
	 * When wild is true, the value of the label attribute is returned.
	 */
	public String getTag()
	{
		if (wild)
			return label;
		return tag;
	}

	public void buildMaps(Map coinMap, Map contentMap)
		throws SAXException
	{
		int i;
		int s;

		if (attributes==null)
			attributes=new QDMAttributes();

		if (elements==null)
			elements=new QDMElements();

		String l=getLabel();
		if ("".equals(l))
			throw new SAXException(
				"Struct coin has no label or element tag.");
		if (coinMap.containsKey(l))
			throw new SAXException("Duplicate label: "+l);
		coinMap.put(l,this);

		attributes.buildMaps(coinMap,contentMap,this);

		elements.buildMaps(coinMap,contentMap,this);
	}

	public void init(Map coinMap, Map contentMap)
		throws SAXException
	{
		int i;
		int s;
		QDMCoin c;

		if (extendor!=null)
		{
			extendsCoin=(QDMCoin)coinMap.get(extendor.text);
			if (extendsCoin==null)
				throw new SAXException("Undefined coin: "+extendor.text);
			extendsCoin.extensions.add(this);
		}


		s=implementsList.size();
		for (i=0;i<s;++i)
		{
			String l=((QDMImplements)implementsList.get(i)).text;
			c=(QDMCoin)coinMap.get(l);
			if (c==null)
				throw new SAXException("Undefined coin: "+l);
			c.implementations.add(this);
		}

		attributes.init(coinMap,contentMap,this,extendsCoin);

		elements.init(coinMap,contentMap,this);
	}

	public Map getAtts()
		throws SAXException
	{
		return attributes.getAtts();
	}

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