package com.jxml.quick.util.qjml2java2;
import com.jxml.quick.model.qjml.*;
import com.jxml.quick.*;
import org.xml.sax.*;
import java.util.*;
import com.jxml.quick.ocm.*;
import java.io.*;
/**
 * Controller Class for Properties<br> 
 * @see <a href="http://quickutil.sourceforge.net/view/Main/QjmlProperty">More details here.</a><br>
 */
public class QCProperty extends OCMtemplate
{	
/**
 * It is veriable type
 */
	public String vt="";

/**
 * It is property name
 */
	public String pName="";
/**
 * Method declaration<br>
 * @param param Name of the parameter
 * @return string The name of the package
 * @return string The veriable type
 * @exception SAXException
 * @exception IOException 
 */
	public void cvt(Object param) throws SAXException, IOException
	{
		OCMcontextSupport.SubContextSupport 		scs=(OCMcontextSupport.SubContextSupport)context;
		StringBuffer sb=scs.sb;
		QMQJML qjml=(QMQJML)scs.dataModelRoot;
		
		QMProperty ed = (QMProperty)dataModel;
		String initializer=ed.initializer;
	       	String variableType=ed.getClassName();
		pName=ed.name;
		int i=variableType.lastIndexOf('.');
		if (i==-1)
			vt=variableType;
		else
			vt=variableType.substring(i+1);
		if (param instanceof ArrayList)
		{
			if (i==-1)
				return;
			ArrayList imports=(ArrayList)param;
	

			int j,s;
			s=imports.size();
			String pkg=variableType.substring(0,i);
			for (j=0;j<s;++j)
			{
				if (pkg.equals(imports.get(j)))
					return;
			}

			imports.add(pkg);
			return;
		}

		if (param==null)
		{

			if (ed.ia!=null
			 && !((QMProperty)ed.ia).abst)
				return;

			if (ed.myCoin instanceof QMBean
			 || ed.myCoin instanceof QMText
			 || (ed.ia==null && !ed.abst))
			{
				sb.append("\tpublic ");
				sb.append(vt);
				sb.append(" _");
				sb.append(ed.name);

				if (!"".equals(initializer))
				{
					sb.append("=");
					sb.append(initializer);
				}
				else if ("list".equals(ed.kind)
				      || "map".equals(ed.kind))
				{
					sb.append("=new ");
					sb.append(vt);
					sb.append("()");
				}

				sb.append(";");
				sb.append("\n ");
			}
		}

		if ("".equals(param))
		{
			if (ed.ia!=null
			 && !((QMProperty)ed.ia).abst)
				return;

			if (ed.myCoin instanceof QMBean
			 || ed.myCoin instanceof QMText)
			{
				super.cvt(param);
				return;
			}

			if (ed.ia!=null)
				return;
			if (ed.abst)
				expand("interface");
			else
				super.cvt(param);
			return;
		}
	}

/**
 * Method declaration<br>
 * @param templateName The name of the Template<br>
 * @param arg The parameter name,ie. method name, property type and property name <br>
 * @return string The string values to be plugged inside the templates
 * @exception SAXException  
 * @exception IOException 
 */
	public String process(String templateName, String arg)
		throws SAXException, IOException
	{

		if("type".equals(arg))
		{
			return vt;
		}

		if("pName".equals(arg))
		{

			return pName;
		}

		if("mName".equals(arg))
		{

			return pName.substring(0,1).toUpperCase()+pName.substring(1);

		}
		
		return "";
	}


}