package com.jxml.quick.ocm;
import com.jxml.quick.*;
//import com.jxml.quick.util.*;
import java.util.*;
import org.xml.sax.*;
import java.io.*;
import com.jxml.quick.config.*;

public abstract class OCMtemplate extends OCMrootPeerSupportLite
{
	public void cvt(Object param)
		throws SAXException, IOException
	{
		expand("template");
	}

	public void expand(String templateName)
		throws SAXException, IOException
	{
		String template=paramMap.get(templateName).toString();
		expandString(template,templateName);
	}


	public void expandString(String template, String templateName)
		throws SAXException, IOException
	{
	
			
		StringBuffer sb=context.getStringBuffer();
		int i=template.indexOf("*{{");
		int j=0;
		while(true)
		{
			if (i==-1)
			{
				sb.append(template.substring(j));
				return;
			}
			sb.append(template.substring(j,i));
			i+=3;
			j=template.indexOf("}}*",i);
			if (j==-1)
			{
				throw new RuntimeException("template "+templateName+" is missing }}*");
			}
			String arg=template.substring(i,j);
			sb.append(process(templateName,arg));
			j+=3;
			i=template.indexOf("*{{",j);
		}		
	}

	public abstract String process(String templateName, String arg)
		throws SAXException, IOException;

	public String express(boolean expressDefaults)
		throws SAXException, IOException
	{
		StringBuffer sb=new StringBuffer();
		context.setStringBuffer(sb);
		cvt(null);
		return sb.toString();
	}

	public void express(PrintWriter pw, boolean expressDefaults)
		throws SAXException, IOException
	{
		pw.print(express(expressDefaults));
	}
}
