//----------------------------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.coreImpl.xpdl.parser.xmlrules;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.digester.Rule;
import org.xml.sax.Attributes;


public class SetNextRuleInBeginning extends Rule
{
	//SetNextRule在元素处理结束时候才建立父子元素之间的层次关系,
	//Activity ,Transition,Process之间存在复杂关系, 在元素处理结束
	//时候建立父子关系已经不能满足需求.
	//
	//Activity,Transition元素开始处理时候就建立父子元素层次关系,
	//满足此种父子处理逻辑需要
	//
	// @see SetTransitionActivityRule

	// ----------------------------------------------------------- Constructors
	/**
	 * Construct a "set next" rule with the specified method name.  The
	 * method's argument type is assumed to be the class of the
	 * child object.
	 *
	 * @param methodName Method name of the parent method to call
	 */
	public SetNextRuleInBeginning(String methodName)
	{
		this(methodName, null);
	}
	/**
	 * Construct a "set next" rule with the specified method name.
	 *
	 * @param methodName Method name of the parent method to call
	 * @param paramType Java class of the parent method's argument
	 *  (if you wish to use a primitive type, specify the corresonding
	 *  Java wrapper class instead, such as <code>java.lang.Boolean</code>
	 *  for a <code>boolean</code> parameter)
	 */
	public SetNextRuleInBeginning(String methodName, String paramType)
	{
		this.methodName = methodName;
		this.paramType = paramType;
	}
	// ----------------------------------------------------- Instance Variables
	/**
	 * The method name to call on the parent object.
	 */
	protected String methodName = null;
	/**
	 * The Java class name of the parameter type expected by the method.
	 */
	protected String paramType = null;
	/**
	 * Should we use exact matching. Default is no.
	 */
	protected boolean useExactMatch = false;
	// --------------------------------------------------------- Public Methods
	/**
	 * <p>Is exact matching being used.</p>
	 *
	 * <p>This rule uses <code>org.apache.commons.beanutils.MethodUtils</code> 
	 * to introspect the relevent objects so that the right method can be called.
	 * Originally, <code>MethodUtils.invokeExactMethod</code> was used.
	 * This matches methods very strictly 
	 * and so may not find a matching method when one exists.
	 * This is still the behaviour when exact matching is enabled.</p>
	 *
	 * <p>When exact matching is disabled, <code>MethodUtils.invokeMethod</code> is used.
	 * This method finds more methods but is less precise when there are several methods 
	 * with correct signatures.
	 * So, if you want to choose an exact signature you might need to enable this property.</p>
	 *
	 * <p>The default setting is to disable exact matches.</p>
	 *
	 * @return true iff exact matching is enabled
	 * @since Digester Release 1.1.1
	 */
	public boolean isExactMatch()
	{
		return useExactMatch;
	}
	/**
	 * <p>Set whether exact matching is enabled.</p>
	 *
	 * <p>See {@link #isExactMatch()}.</p>
	 *
	 * @param useExactMatch should this rule use exact method matching
	 * @since Digester Release 1.1.1
	 */
	public void setExactMatch(boolean useExactMatch)
	{
		this.useExactMatch = useExactMatch;
	}

	/**
	 * This method is called when the beginning of a matching XML element
	 * is encountered. The default implementation delegates to the deprecated
	 * method {@link #begin(Attributes) begin} without the 
	 * <code>namespace</code> and <code>name</code> parameters, to retain 
	 * backwards compatibility.
	 *
	 * @param namespace the namespace URI of the matching element, or an 
	 *   empty string if the parser is not namespace aware or the element has
	 *   no namespace
	 * @param eleName the local name if the parser is namespace aware, or just 
	 *   the element name otherwise
	 * @param attributes The attribute list of this element
	 * @since Digester 1.4
	 */
	public void begin(String namespace, String eleNname, Attributes attributes) throws Exception
	{
		// Identify the objects to be used
		Object child = digester.peek(0);
		Object parent = digester.peek(1);
		if (digester.getLogger().isDebugEnabled())
		{
			if (parent == null)
			{
				digester.getLogger().debug(
					"[SetNextRuleInBeginning]{"
						+ digester.getMatch()
						+ "} WorkflowCall [NULL PARENT]."
						+ methodName
						+ "("
						+ child
						+ ")");
			}
			else
			{
				digester.getLogger().debug(
					"[SetNextRuleInBeginning]{"
						+ digester.getMatch()
						+ "} WorkflowCall "
						+ parent.getClass().getName()
						+ "."
						+ methodName
						+ "("
						+ child
						+ ")");
			}
		}
		// WorkflowCall the specified method
		Class paramTypes[] = new Class[1];
		if (paramType != null)
		{
			paramTypes[0] = digester.getClassLoader().loadClass(paramType);
		}
		else
		{
			paramTypes[0] = child.getClass();
		}
		if (useExactMatch)
		{
			MethodUtils.invokeExactMethod(parent, methodName, new Object[] { child }, paramTypes);
		}
		else
		{
			MethodUtils.invokeMethod(parent, methodName, new Object[] { child }, paramTypes);
		}
	}
	/**
	 * Render a printable version of this Rule.
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer("SetNextRuleInBeginning[");
		sb.append("methodName=");
		sb.append(methodName);
		sb.append(", paramType=");
		sb.append(paramType);
		sb.append("]");
		return (sb.toString());
	}
}