//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.script.compiler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;


public class ScriptWriter
{
	public static int TAB_WIDTH = 2;
	public static String SPACES = "                              ";
	// Current indent level:
	int indent = 0;
	// The sink writer:
	PrintWriter writer;
	public ScriptWriter(PrintWriter writer)
	{
		this.writer = writer;
	}
	public void close() throws IOException
	{
		writer.close();
	}
	public void pushIndent()
	{
		if ((indent += TAB_WIDTH) > SPACES.length())
			indent = SPACES.length();
	}
	public void popIndent()
	{
		if ((indent -= TAB_WIDTH) <= 0)
			indent = 0;
	}
	/**
	 * Print a standard comment for echo outputed chunk.
	 * @param start The starting position of the RULE chunk being processed. 
	 * @param stop  The ending position of the RULE chunk being processed. 
	 */
	public void printComment(Mark start, Mark stop, char[] chars)
	{
		if (start != null && stop != null)
		{
			println("// from=" + start);
			println("//   to=" + stop);
		}
		if (chars != null)
			for (int i = 0; i < chars.length;)
			{
				indent();
				print("// ");
				while (chars[i] != '\n' && i < chars.length)
				{
					writer.print(chars[i++]);
				}
			}
	}
	/**
	 * Quote the given string to make it appear in a chunk of java code.
	 * @param s The string to quote.
	 * @return The quoted string.
	 */
	public String quoteString(String s)
	{
		// Turn null string into quoted empty strings:
		if (s == null)
		{
			return "null";
		}
		// Hard work:
		if (s.indexOf('"') < 0 && s.indexOf('\\') < 0 && s.indexOf('\n') < 0 && s.indexOf('\r') < 0)
		{
			return "\"" + s + "\"";
		}
		StringBuffer sb = new StringBuffer();
		int len = s.length();
		sb.append('"');
		for (int i = 0; i < len; i++)
		{
			char ch = s.charAt(i);
			if (ch == '\\' && i + 1 < len)
			{
				sb.append('\\');
				sb.append('\\');
				sb.append(s.charAt(++i));
			}
			else if (ch == '"')
			{
				sb.append('\\');
				sb.append('"');
			}
			else if (ch == '\n')
			{
				sb.append("\\n");
			}
			else if (ch == '\r')
			{
				sb.append("\\r");
			}
			else
			{
				sb.append(ch);
			}
		}
		sb.append('"');
		return sb.toString();
	}
	public void println(String line)
	{
		writer.println(SPACES.substring(0, indent) + line);
	}
	public void println()
	{
		writer.println("");
	}
	public void indent()
	{
		writer.print(SPACES.substring(0, indent));
	}
	public void print(String s)
	{
		writer.print(s);
	}
	public void printMultiLn(String multiline)
	{
		printMultiLn(multiline, true);
	}

	public void printMultiLn(String multiline, boolean indentEnabled)
	{
		// Try to be smart (i.e. indent properly) at generating the code:
		BufferedReader reader = new BufferedReader(new StringReader(multiline));
		try
		{
			String line = reader.readLine();
			if (line != null)
			{
				print(line);
				println();
			}
			for (;(line = reader.readLine()) != null;)
			{
				println(line);
			}
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}
