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

/**
 * <p>
 * Models an abstract element in QJML.
 * An abstract element has no tag attribute, since it doesn't define a particular
 * XML element. (Similarly, an abstract Java class can't be instantiated.)
 * Abstract coins provide a means of refering to the family of beans which implement them.
 * Abstract coins also provide for inheritance of attributes, enums, and ranges by the coins
 * which extend them.
 * </p>
 <pre>
DTD:

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

&lt;!ATTLIST abstract 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMCoin.html#label">label</a> CDATA #REQUIRED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMCoin.html#generate">generate</a> CDATA "True"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#validInherited">validInherited</a> CDATA "True"
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>
 </pre>
 */
public class QMAbstract extends QMCoin
	implements QMElement, QMData
{
	/**
	 * Models the validInherited attribute on an abstract element.
	 * When false, enums and ranges are not inherited from the coin that this abstract coin extends.
	 */
	public boolean validInherited=true;

	/**
	 * Models an extends element, which is optional.
	 * An abstract coin can extend a bean coin, a text coin, or another abstract 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 abstract coin "implements".
	 * (An abstract element, like an abstract Java class, can impliment any number of interfaces.)
	 * 
	 */
	public ArrayList implementsList=new ArrayList();

	/**
	 * Models a targetClass element, which is optional.
	 * The targetClass element names the abstract Java class which this abstract element describes.
	 */
	public QMTargetClass targetClass=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 abstract Java class used
	 * to model those attributes.
	 */
	public QMAttributes attributes=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 abstract Java class which
	 * this abstract 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 abstract Java class which
	 * this abstract element describes.
	 */
	public ArrayList propertyList=new ArrayList();

	/**
	 * <p>
	 * A container for QMEnum and QMRange objects, which model enum and range elements.
	 * The enum and range elements are optional and repeating.
	 * These valid elements are used to describe some of the acceptable text content of text coins which
	 * extend this abstract coin.
	 * </p><p>
	 * The enum and range elements may not be present when the targetClass is present in the
	 * same abstract element.
	 * </p>
	 */
	public ArrayList validList=new ArrayList();

	/**
	 * The QMAbstract, QMText, or QMBean object named by the extends element.
	 * This variable is set when the data model is initialized.
	 */
	public transient QMCoin extendsCoin=null;

	/**
	 * A container for QMEnum and QMRange objects for this element, 
	 * including enum and range elements defined on abstract and text coins
	 * which this abstract coin extends.
	 * This list is populated when the data model is initialized.
	 */
	public transient ArrayList valids=null;

	public Object clone()
	{
		QMAbstract rv=(QMAbstract)super.clone();
		rv.extendor=(QMExtends)clone(extendor);
		rv.implementsList=clone(implementsList);
		rv.targetClass=(QMTargetClass)clone(targetClass);
		rv.attributes=(QMAttributes)clone(attributes);
		rv.fieldList=clone(fieldList);
		rv.propertyList=clone(propertyList);
		rv.validList=clone(validList);
		rv.extendsCoin=null;
		rv.valids=null;
		return rv;
	}

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

		if (targetClass==null)
		{
			targetClass=new QMTargetClass();
			targetClass.text="java.lang.Object";
                        generate = false;
		}

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

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

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

		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 ArrayList getValidList()
		throws SAXException
	{
		if (valids!=null)
			return valids;
		valids=new ArrayList(validList);
		if (extendsCoin==null || !validInherited)
			return valids;
		valids.addAll(((QMData)extendsCoin).getValidList());
		return valids;
	}

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