package com.jxml.quick.util.qdmlExpand;
import com.jxml.quick.ocm.*;
import com.jxml.quick.*;
import java.util.*;

/**
 * Track the ID assignments.
<pre>
DTD:

&lt;!ELEMENT qdmlExpand (<a href="../../ocm/OCMsinkSupport.html#source">(source|sourceLink)</a>,(<a href="../../ocm/OCMsinkSupport.html#validator">(validate|validateLink)</a>?,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMcontextSupport.html#factory">(factory|factoryLink)</a>)>

&lt;!ATTLIST qdmlExpand
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMsinkSupport.html#prefix">docNamePrefix </a>CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMsinkSupport.html#expressDefaults">expressDefaults</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMtranscribe.html#schemaName">outputSchema</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;ID CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#IdPrefix">Prefix </a>CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#setIDs">setIDs</a> CDATA "False"
>
</pre>
 */
public class Counter extends OCMcontextSupport
{
	/**
	 * Generate IDs only when true.
	 */
	public boolean setIDs=false;

	/**
	 * Prefix added to the generated ID.
	 */
	public String IdPrefix="ID";

	// implements an OCMbase method
	public OCMbase.SubBase spawn(HashMap prior)
	{
		OCMbase.SubBase rv=(OCMbase.SubBase)prior.get(this);
		if (rv!=null)
			return rv;
		OCMbase.SubBase base=new SubCounter(prior);
		prior.put(this,base);
		return base;
	}

	public class SubCounter extends SubContextSupport
	{
		/**
		 * The count value used in previously generated ID.
	         */
		public int count=0;

		public SubCounter(HashMap prior)
		{
			super(prior);
		}

		/**
		 * Generate an ID.
		 */
		public String genID()
		{
			return IdPrefix+(++count);
		}

		/**
		 * Add and ID, but only if setIDs is true.
		 */
		public void addIDAttribute(QConverterSupport cs)
		{
			if (setIDs)
			cs.addStringAttribute("ID",genID());
		}
	}
}
