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 QCCoin 
 */
public class QCCoin extends OCMtemplate
{
	public StringBuffer sb;
	public Map properties;
	public QMQJML qjml;
	public TreeMap coinMap;
	public QMCoin ed;
	public String shortClassName;
	public String packageName=null;
	public String folder="";
	public File qjmlInJava;
	public String altTemplate=null;
	public ArrayList remList;
/**
 * Method declaration<br>
 * This method contains logic for generation of directory 
 * @param param Name of the parameter
 * @exception SAXException Legal SAXException
 * @exception IOException Ligal IOException
 */
	public void cvt(Object param) throws SAXException, IOException 
	{
		OCMcontextSupport.SubContextSupport scs=(OCMcontextSupport.SubContextSupport)context;
		sb=scs.sb;
		properties=scs.properties;
		qjml=(QMQJML)scs.dataModelRoot;

		String generateOutputTo=(String)properties.get("generateOutputTo");

		coinMap=qjml.coinMap;

		ed = (QMCoin)dataModel;

		String targetClassName=ed.getClassName();
		if (targetClassName=="int" || targetClassName=="byte[]" || targetClassName=="double" || targetClassName=="float")
			return;
		if (targetClassName.startsWith("java."))
			return;
		if (targetClassName.startsWith("javax."))
			return;
		if (!ed.generate)
			return;

		if(targetClassName.indexOf('.')>-1)
		{
			folder=targetClassName.replace('.','/');

			if(!"".equals(generateOutputTo))
				folder=generateOutputTo +"/" + folder;
		}
		String javaFileName;
		int lastNdx=targetClassName.lastIndexOf('.');
		if(lastNdx==-1)
			shortClassName=targetClassName;
		else
		{		
			shortClassName=targetClassName.substring(lastNdx+1);
			packageName=targetClassName.substring(0,lastNdx);
		}
		javaFileName=shortClassName;
		if(!javaFileName.endsWith(".java"))
			javaFileName=javaFileName + ".java";
		
		if(!"".equals(generateOutputTo))
			javaFileName=generateOutputTo +"/" + javaFileName;

		if (altTemplate==null)
			super.cvt(param);
		else
			expand(altTemplate);

		if(!"".equals(folder))
		{
			File file= new File(folder);
			File folders=file.getParentFile();
			if(!folders.exists() && !folders.mkdirs())
			{
				System.out.println("unable to create the directory");
				return;
			}
		}
		if(!"".equals(folder))		
			qjmlInJava=new File(folder+".java");
		else
		{	
			if(javaFileName.indexOf('/')>-1)  
								//If javaFileName is 						appended with
							 	//the output Directory 						then create directory  
	                                                        //before writing to it.
			{
				qjmlInJava=new File(javaFileName);
				boolean success=qjmlInJava.getParentFile().mkdir();
			}
			else
				qjmlInJava=new File(javaFileName);
		
		}
		FileOutputStream fos=new FileOutputStream(qjmlInJava);
		sb=context.getStringBuffer();
		byte[] buff=sb.toString().getBytes();
		fos.write(buff);
		sb.delete(0,sb.length());
		fos.close();
			
 	}
/**
 * Method declaration for remarks and packages<br>
 * This method contains the template processing logic
 * @param templateName Name of the templates
 * @param arg The parameter name
 * @return shortClassName<br>
 * @return string It is the package name
 * @exception SAXException
 * @exception IOException
 */
	public String process(String templateName, String arg)
		throws SAXException, IOException
	{
		if("className".equals(arg))
		{
			return shortClassName;
		}
		
		if("remList".equals(arg))
		{
			remList=ed.remList;
			int size=remList.size();
			if(size==0)
				return "";	
			else
			{

				sb.append("/**");
				for (int i=0;i<size;i++)
				{
					sb.append(((QMRem)remList.get(i)).getQText());
					sb.append("\n");
					sb.append(" *");
				}
				sb.append("/\n");
			}
				
		}

		if("packageTemplate".equals(arg))
		{
			if(packageName!=null)
				return "package " + packageName + ";\n";
		}

		return "";
	}
}

