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>
 * A model for the qjml element, which is the root element of the QJML binding schema.
 * Every QJML document contains exactly one qjml element, which is the top-level (or outer-most)
 * element of the document.
 * </p>
 <pre>
DTD:

&lt;!ELEMENT qjml (<A href="#includes">include*</A>, <a href="#excludeses">excludes*</a>, <A href="#coins">(interface|abstract|text|bean|link)*</A>)&gt;

&lt;!ATTLIST qjml
    <a href="#root">root</a> CDATA #REQUIRED
    <a href="#SYSTEM">SYSTEM</a> CDATA #IMPLIED
    <a href="#PUBLIC">PUBLIC</a> CDATA #IMPLIED
    <a href="#JAR">JAR</a> CDATA #IMPLIED
    ID id #IMPLIED
&gt;
 </pre>

 */
public class QMQJML extends QMCloneable implements QName
{
	/** 
	 * XML tag name for the qjml element.
	 * The value is almost always "qjml".
	 */
	public String tagName="";
	
	/**
	 * <p>
	 * Models the attribute root, a required attribute on the qjml element.
	 * The root attribute must uniquely name the coin which specifies the top-level element
	 * (or elements) of the document type being defined by this QJML document.
	 * </p><p>
	 * Note that any coin which implements or extends the coin named by root can
	 * be used as a root element.
	 * </p>
	 */
	public String root="";

	/**
	 * <p>
	 * Models the attribute SYSTEM, an optional attribute on the qjml element.
	 * The SYSTEM attribute is a fully qualified URL, typically the URL of the DTD
	 * for the type of document which this QJML document implements.
	 * </p>	 */
	public String SYSTEM="";

	/**
	 * <p>
	 * Models the attribute PUBLIC, an optional attribute on the qjml element.
	 * The PUBLIC attribute is a URI naming the document type being defined.
	 * </p>
	 */
	public String PUBLIC="";

	/**
	 * <p>
	 * Models the attribute JAR, an optional attribute on the qjml element.
	 * The JAR attribute names the jar file which holds the QJML document together
	 * with the classes referenced by the QJML document.
	 * this name should include a version number, allowing for the simultanious use
	 * of multiple versions.
	 * </p>
	 */
	public String JAR="";


	/**
	 * <p>
	 * A container for the Strings which model include elements, which are optional and repeating.
	 * During initialization of the model, the coins found in the QJML documents named by these Strings
	 * are included in the effective content of this QJML document.
	 * When coins with the same (otherwise) unique label or tag are found in more than one document,
	 * the last coin included is used. And when an included coin has the same name as a coin in this document,
	 * the coin in this document is chosen.
	 * </p><p>
	 * Inclusion is a recursive process, allowing for indirect inclusion to any depth.
	 * </p>
	 */
	public ArrayList includes=new ArrayList();

	/**
	 * <p>
	 * A container for the objects which model excludes elements, which are optional and repeating.
	 * During initialization of the model, any coins that have been included which are named by an excludes
	 * element are dropped.
	 * </p><p>
	 * The coins in a QJML document are not effected by the excludes elements in that, or any included, document.
	 * </p>
	 */
	public ArrayList excludeses=new ArrayList();

	/**
	 * <p>
	 * A container for the objects which model the coins elements, which are optional and repeating.
	 * (Coin elements are interface, abstract, text, bean, and link.)
	 * </p><p>
	 * This container is uneffected by the initialization process--it contains only coins defined in the
	 * current document.
	 * </p>
	 */
	public ArrayList coins=new ArrayList();

	/**
	 * <p>
	 * A table of all the coins in the document, along with the coins of all included documents.
	 * Entries are ordered/keyed by each coin's unique label or tag name.
	 * This table is populated when the data model is initialized.
	 * </p>
	 */
	public transient TreeMap coinMap=null;

	/**
	 * <p>
	 * A table of all the contentModels.
	 * Entries are ordered/keyed by each contentModel's unique label.
	 * (ContentModel elements are item, sequence, and selection.)
	 * This table is populated when the data model is initialized.
	 * </p>
	 */
	public transient TreeMap contentMap=null;

	/**
	 * Returns a deep copy of this object, but in an uninitialized form.
	 */
	public Object clone()
	{
		QMQJML rv=(QMQJML)super.clone();
		rv.includes=clone(includes);
		rv.excludeses=clone(excludeses);
		rv.coins=clone(coins);
		rv.coinMap=null;
		rv.contentMap=null;
		return rv;
	}

	/**
	 * Returns the tag name.
	 */
        public String getQName()
	{
		return tagName;
	}

	/**
	 * Sets the tag name.
	*/
        public void setQName(String name)
	{
		tagName=name;	
	}

	/**
	 * <p>
	 * Initializes the data model.
	 * The init method on the QMQJML object must be called explicitly by the
	 * application which uses this data model. The init and buildMaps on
	 * the subordinate objects are called in turn.
	 * </p><p>
	 * This is a thread-safe method, allowing multiple applications to share the
	 * same data model. This is also a recursive method, calling init on all included
	 * QJML documents in turn.
	 * </p>
	 */
	public synchronized void init()
		throws SAXException, IOException
	{
		if (coinMap==null)
		{
			coinMap=new TreeMap();
			contentMap=new TreeMap();

			buildMaps(coinMap,contentMap);

			Collection col=coinMap.values();
			Iterator it=col.iterator();
			while(it.hasNext())
			{
				QMCoin c=(QMCoin)it.next();
				c.init(coinMap,contentMap);
			}
		}
	}

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

		s=includes.size();
		for (i=0;i<s;++i)
		{
			QMQJML incl=(QMQJML)includes.get(i);
			QMQJML deepClone=(QMQJML)incl.clone();
			deepClone.buildMaps(coinMap,contentMap);
		}

		s=excludeses.size();
		for (i=0;i<s;++i)
		{
			QMExcludes excl=(QMExcludes)excludeses.get(i);
			excl.buildMaps(coinMap,contentMap);
		}

		s=coins.size();
		for (i=0;i<s;++i)
		{
			QMCoin c=(QMCoin)coins.get(i);
			c.buildMaps(coinMap,contentMap);
		}
	}
}
