package com.jxml.quick.util.qjml2html;
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.*;


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 qjmlInHtml;
	public String targetClassName;
	public String className;
	public String htmlFileName;
	public ArrayList remList=new ArrayList();

	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;
		targetClassName=ed.getClassName();
		if (targetClassName.startsWith("java."))
			return;
		if(targetClassName.startsWith("javax."))
			return;

		if(ed instanceof  QMBean)
			 htmlFileName=((QMBean)param).tag +".html";
		else if(ed instanceof QMText)
                         htmlFileName=((QMText)param).tag +".html";
		else if(ed instanceof QMAbstract)
                         htmlFileName=((QMAbstract)param).getLabel() +".html";

		if(!"".equals(generateOutputTo))
			htmlFileName=generateOutputTo +"/" + htmlFileName;


		if(!htmlFileName.endsWith(".html"))
			htmlFileName=htmlFileName + ".html";
		properties.put("className",targetClassName.replace('.','/'));

		super.cvt(param);
		qjmlInHtml=new File(htmlFileName);
		qjmlInHtml.createNewFile();
		FileOutputStream fos=new FileOutputStream(qjmlInHtml);
		sb=context.getStringBuffer();
		byte[] buff=sb.toString().getBytes();
		fos.write(buff);
		sb.delete(0,sb.length());
		fos.close();
 	}

	public String process(String templateName, String arg)
		throws SAXException, IOException
	{
		if("className".equals(arg))
		{
			return targetClassName;
		}

		if("remarks".equals(arg))
		{
			remList=ed.remList;
			for(int i=0;i<remList.size();i++)
			{
				sb.append(((QMRem)remList.get(i)).getQText());	
				sb.append("\n ");
			}

		}
		return "";
	}
}

