//----------------------------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.CharArrayWriter;
import java.util.Enumeration;
import java.util.Hashtable;

import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.script.Constants;
import org.huihoo.workflow.util.StringManager;

public class ScriptUtilities
{
	private static StringManager sm = StringManager.getManager(Constants.RESOURCE_BUNDLE_BASE);
	private static final String OPEN_EXPR = "<%=";
	private static final String CLOSE_EXPR = "%>";
	public static char[] removeQuotes(char[] chars)
	{
		CharArrayWriter caw = new CharArrayWriter();
		for (int i = 0; i < chars.length; i++)
		{
			if (chars[i] == '%' && chars[i + 1] == '\\' && chars[i + 2] == '\\' && chars[i + 3] == '>')
			{
				caw.write('%');
				caw.write('>');
				i = i + 3;
			}
			else
				caw.write(chars[i]);
		}
		return caw.toCharArray();
	}
	// Checks if the token is a runtime expression.
	public static boolean isExpression(String token)
	{
		if (token.startsWith(OPEN_EXPR) && token.endsWith(CLOSE_EXPR))
		{
			return true;
		}
		return false;
	}
	// Returns the "expression" part -- takin <%= and %> out.
	public static String getExpr(String expression)
	{
		String returnString;
		int length = expression.length();
		if (expression.startsWith(OPEN_EXPR) && expression.endsWith(CLOSE_EXPR))
		{
			returnString = expression.substring(OPEN_EXPR.length(), length - CLOSE_EXPR.length());
		}
		else
		{
			returnString = "";
		}
		return returnString;
	}

	public static void checkAttributes(String typeOfTag, Hashtable attrs, ValidAttribute[] validAttributes, Mark start)
		throws WorkflowException
	{
		boolean valid = true;
		Hashtable temp = (Hashtable) attrs.clone();
		/**
		 * First check to see if all the mandatory attributes are present.
		 * If so only then proceed to see if the other attributes are valid
		 * for the particular tag.
		 */
		String missingAttribute = null;
		for (int i = 0; i < validAttributes.length; i++)
		{
			if (validAttributes[i].mandatory)
			{
				if (temp.get(validAttributes[i].name) != null)
				{
					temp.remove(validAttributes[i].name);
					valid = true;
				}
				else
				{
					valid = false;
					missingAttribute = validAttributes[i].name;
					break;
				}
			}
		}
		/**
		 * If mandatory attribute is missing then the exception is thrown.
		 */
		if (!valid)
			throw new WorkflowException(
				start + " " + sm.getString("script.error.mandatory.attribute", new Object[] { typeOfTag, missingAttribute }));
		/**
		 * Check to see if there are any more attributes for the specified
		 * tag.
		 */
		if (temp.size() == 0)
			return;
		/**
		 * Now check to see if the rest of the attributes are valid too.
		 */
		Enumeration enum = temp.keys();
		String attribute = null;
		while (enum.hasMoreElements())
		{
			valid = false;
			attribute = (String) enum.nextElement();
			for (int i = 0; i < validAttributes.length; i++)
			{
				if (attribute.equals(validAttributes[i].name))
				{
					valid = true;
					break;
				}
			}
			if (!valid)
				throw new WorkflowException(
					start + " " + sm.getString("script.error.invalid.attribute", new Object[] { typeOfTag, attribute }));
		}
	}
	public static String escapeQueryString(String unescString)
	{
		if (unescString == null)
			return null;
		String escString = "";
		String shellSpChars = "\\\"";
		for (int index = 0; index < unescString.length(); index++)
		{
			char nextChar = unescString.charAt(index);
			if (shellSpChars.indexOf(nextChar) != -1)
				escString += "\\";
			escString += nextChar;
		}
		return escString;
	}
	/**
	 *  Escape the 5 entities defined by XML.
	 */
	public static String escapeXml(String s)
	{
		if (s == null)
			return null;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if (c == '<')
			{
				sb.append("&lt;");
			}
			else if (c == '>')
			{
				sb.append("&gt;");
			}
			else if (c == '\'')
			{
				sb.append("&apos;");
			}
			else if (c == '&')
			{
				sb.append("&amp;");
			}
			else if (c == '"')
			{
				sb.append("&quote;");
			}
			else
			{
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	public static class ValidAttribute
	{
		String name;
		boolean mandatory;
		public ValidAttribute(String name, boolean mandatory)
		{
			this.name = name;
			this.mandatory = mandatory;
		}
		public ValidAttribute(String name)
		{
			this(name, false);
		}
	}
}
