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 attributes element in QJML.
 * The attributes element is used to describe the attributes of an element.
 <pre>
DTD:

&lt;!ELEMENT attributes <a href="#itemList">(item)*</a>>

&lt;!ATTLIST attributes
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#inherited">inherited</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>
 </pre> 
 */
public class QMAttributes extends QMCloneable
{
	/**
	 * Models the inherited attribute on the attributes element.
	 * The inherited attribute defaults to true.
	 * When set to false, inheritance of attributes is inhibited.
	 */
	public boolean inherited=true;

	/**
	 * A container for the QMItem objects, which model item elements.
	 * The item elements are optional and repeating within the content of the attributes element.
	 * Each item describes an attribute on the element being defined by
	 * the abstract or bean element which contains the attributes element.
	 */
	public ArrayList itemList=new ArrayList();

	/**
	 * A table of all attribute items, including inherited attributes,
	 * keyed and ordered by attribute name.
	 * This table is populated when the data model is initialized.
	 */
	public transient TreeMap atts=null;

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

	private transient Map coinMap=null;
	private transient Map contentMap=null;
	private transient QMCoin myCoin=null;

	/**
	 * Returns a deep copy of this object, but in an uninitialized form.
	 */
	public Object clone()
	{
		QMAttributes rv=(QMAttributes)super.clone();
		rv.itemList=clone(itemList);
		rv.atts=null;
		rv.extendsCoin=null;
		coinMap=null;
		contentMap=null;
		myCoin=null;
		return rv;
	}

	/**
	 * Builds the maps.
	 * This method is called during initialization of the data model.
	 */
	public void buildMaps(Map coinMap, Map contentMap, QMCoin coin)
		throws SAXException
	{
		int s;
		int i;

		s=itemList.size();
		for (i=0;i<s;++i)
		{
			QMItem p=(QMItem)itemList.get(i);
			p.buildMaps(coinMap,contentMap,coin);
		}
	}

	/**
	 * Resolve references, inheritance.
	 * This method is called during initialization of the data model.
	 */
	public void init(Map coinMap, Map contentMap, QMCoin myCoin, QMCoin extendsCoin)
		throws SAXException
	{
		int s;
		int i;

		s=itemList.size();
		for (i=0;i<s;++i)
		{
			QMItem p=(QMItem)itemList.get(i);
			p.init(coinMap,contentMap);
		}

		this.extendsCoin=extendsCoin;
		this.coinMap=coinMap;
		this.contentMap=contentMap;
		this.myCoin=myCoin;
	}

	/**
	 * Returns a table of QMItem objects, keyed by attribute name.
	 * Inherited attribute items are included.
	 */
	public Map getAtts()
		throws SAXException
	{
		if (atts!=null)
			return atts;
		int i;
		int s;
		atts=new TreeMap();
		if (extendsCoin!=null && inherited)
		{
			Map ias=((QMElement)extendsCoin).getAtts();
			Collection col=ias.values();
			Iterator it=col.iterator();
			while (it.hasNext())
			{
				QMItem p=(QMItem)it.next();
				QMItem c=(QMItem)p.clone();
				c.init(coinMap,contentMap);
				c.label="";
				String tag=c.getAttributeTagName();
				atts.put(tag,c);
			}
		}
		s=itemList.size();
		for (i=0;i<s;++i)
		{
			QMItem p=(QMItem)itemList.get(i);
			String tag=p.getAttributeTagName();
			atts.put(tag,p);
		}
		return atts;
	}
}
