package com.jxml.quick.model.qjml;
import org.xml.sax.*;
import java.util.*;

/**
 * Models the field element in QJML.
 * This element identifies a public Java variable as the accessor.
 <pre>
DTD:

&lt!ELEMENT field EMPTY>

&lt!ATTLIST field 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMAccess.html#name">name</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMAccess.html#kind">kind</a> (array|list|map) #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMAccess.html#size">size</a> CDATA "-1"
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#initializer">initializer</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMAccess.html#coin">coin</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="QMAccess.html#key">key</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;ID id #IMPLIED
>
 </pre>
 */
public class QMField extends QMAccess
{
	/**
	 * Models the initializer attribute on the field element.
	 * This attribute provides the text used to generate initialization code for the variable--
	 * useful when generating Java code from a QJML file.
	 * The initializer attribute is optional.
	 */
	public String initializer="";

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

	/**
	 * Add accessor.
	 */
	public void addAccessor(Map coinMap, Map contentMap, QMCoin myCoin)
		throws SAXException
	{
		if ("".equals(name))
			return;
		HashMap accessors=myCoin.accessors;
		String key="field_"+name;
		if (!accessors.containsKey(key))
			accessors.put(key,this);
	}

	/**
	 * Munge info from two accessors which should be identical.
	 */
	public void munge()
	{
		super.munge();
		QMField a=(QMField)ia;
		if (!initializer.equals(a.initializer))
			if ("".equals(initializer))
				initializer=a.initializer;
			else if ("".equals(a.initializer))
				a.initializer=initializer;
	}

	/**
	 * Resolve references, inheritance.
	 * This method is called during initialization of the data model.
	 */
	public void init(Map coinMap, Map contentMap, QMCoin myCoin, QMContentModel contentModel)
		throws SAXException
	{
		if (!"".equals(name))
		{
			ia=myCoin.getAccessor(coinMap,"field_"+name);
			if (ia==this)
				ia=null;
			if (ia!=null)
			{
				munge();
			}
		}
		super.init(coinMap,contentMap,myCoin,contentModel);
	}

	/**
	 * Returns true when the names are equal.
	 */
	public boolean equals(Object o)
	{
		if (o instanceof QMField)
		{
			QMField f=(QMField)o;
			return name.equals(f.name);
		}
		else
			return false;
	}
}
