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

&lt;!ELEMENT bean (<a href="QMCoin.html#remList">rem*</a>, <a href="#extendor">extends?</a>, <a href="#implementsList">implements*</a>, 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#target">(targetClass|targetFactory)</a>, <a href="#attributes">attributes?</a>, 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#elements">elements?</a>, <a href="#fieldList">field*</a>, <a href="#propertyList">property*</a>)>

&lt;!ATTLIST bean
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMCoin.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 (MIXED|BIMODAL)</a> #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#inline">inline</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMCoin.html#generate">generate</a> CDATA "True"
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>
 </pre>
 */
public class QMBean extends QMCoin
	implements QMElement
{
	/**
	 * <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 QMExtends extendor=null;

	/**
	 * A container for QMImplements 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 target, which is an interface element. 
	 * The target element (targetClass, targetEditor, or targetFactory) is optional.
	 * The target element names the Java class which this bean element describes.
	 */
	public QMTarget target=null;

	/**
	 * 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 QMAttributes 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 QMElements elements=null;

	/**
	 * A container for QMField 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 QMProperty 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 QMAbstract or QMBean object named by the extends element.
	 * This variable is set when the data model is initialized.
	 */
	public transient QMCoin extendsCoin=null;

	public Object clone()
	{
		QMBean rv=(QMBean)super.clone();
		rv.extendor=(QMExtends)clone(extendor);
		rv.implementsList=clone(implementsList);
		rv.target=(QMTarget)clone((QMCloneable)target);
		rv.attributes=(QMAttributes)clone(attributes);
		rv.elements=(QMElements)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 QMAttributes();

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

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

		s=fieldList.size();
		for (i=0;i<s;++i)
		{
			QMField f=(QMField)fieldList.get(i);
			f.buildMaps(coinMap,contentMap,this);
		}

		s=propertyList.size();
		for (i=0;i<s;++i)
		{
			QMProperty p=(QMProperty)propertyList.get(i);
			p.buildMaps(coinMap,contentMap,this);
		}
	}

	public QMAccess getAccessor(Map coinMap, String name)
		throws SAXException
	{
		QMAccess a=null;

		if (extendor!=null)
		{
			if (extendsCoin==null)
			{
				extendsCoin=(QMCoin)coinMap.get(extendor.text);
				if (extendsCoin==null)
					throw new SAXException("Undefined coin: "+extendor.text);
			}
			a=extendsCoin.getAccessor(coinMap,name);
		}
		if (a!=null)
			return a;

		int i;
		int s=implementsList.size();
		for (i=0;(a==null && i<s);++i)
		{
			String l=((QMImplements)implementsList.get(i)).text;
			QMCoin c=(QMCoin)coinMap.get(l);
			if (c==null)
				throw new SAXException("Undefined coin: "+l);
			a=c.getAccessor(coinMap,name);
		}
		if (a!=null)
			return a;
		
		a=(QMAccess)accessors.get(name);
		return a;
	}

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

		if (extendor!=null)
		{
			extendsCoin=(QMCoin)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=((QMImplements)implementsList.get(i)).text;
			c=(QMCoin)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);

		s=fieldList.size();
		for (i=0;i<s;++i)
		{
			QMField f=(QMField)fieldList.get(i);
			f.init(coinMap,contentMap,this,null);
		}

		s=propertyList.size();
		for (i=0;i<s;++i)
		{
			QMProperty p=(QMProperty)propertyList.get(i);
			p.init(coinMap,contentMap,this,null);
		}
		if ("java.lang.Object".equals(getClassName()))
			generate=false;
	}

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

	public String getClassName()
		throws SAXException
	{
		return target.getClassName();
	}
}
