//----------------------------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.willow.xmlrules;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.RuleSetBase;
import org.huihoo.willow.Context;
import org.huihoo.willow.Wrapper;
import org.xml.sax.Attributes;
public class WorkflowRuleSet extends RuleSetBase
{

	// ----------------------------------------------------- Instance Variables

	/**
	 * The matching pattern prefix to use for recognizing our elements.
	 */
	protected String prefix = null;

	// ------------------------------------------------------------ Constructor

	/**
	 * Construct an instance of this <code>RuleSet</code> with the default
	 * matching pattern prefix.
	 */
	public WorkflowRuleSet()
	{

		this("");

	}

	/**
	 * Construct an instance of this <code>RuleSet</code> with the specified
	 * matching pattern prefix.
	 *
	 * @param prefix Prefix for matching pattern rules (including the
	 *  trailing slash character)
	 */
	public WorkflowRuleSet(String prefix)
	{

		super();
		this.namespaceURI = null;
		this.prefix = prefix;

	}

	// --------------------------------------------------------- Public Methods

	/**
	 * <p>Add the set of Rule instances defined in this RuleSet to the
	 * specified <code>Digester</code> instance, associating them with
	 * our namespace URI (if any).  This method should only be called
	 * by a Digester instance.</p>
	 *
	 * @param digester Digester instance to which the new Rule instances
	 *  should be added.
	 */
	public void addRuleInstances(Digester digester)
	{
		digester.addCallMethod(prefix + "workflow-app/display-name", "setDisplayName", 0);

		digester.addCallMethod(prefix + "workflow-app/autoDeploy", "setAutoDeploy", 1, new Class[] { Boolean.TYPE });
		digester.addCallParam(prefix + "workflow-app/autoDeploy", 0);

		digester.addCallMethod(prefix + "workflow-app/reloadable", "setReloadable", 1, new Class[] { Boolean.TYPE });
		digester.addCallParam(prefix + "workflow-app/reloadable", 0);

		digester.addCallMethod(
			prefix + "workflow-app/backgroundProcessorDelay",
			"setBackgroundProcessorDelay",
			1,
			new Class[] { Integer.TYPE });
		digester.addCallParam(prefix + "workflow-app/backgroundProcessorDelay", 0);

		//------------------------------------------------------------------------
		//
		//------------------------------------------------------------------------
		digester.addCallMethod(prefix + "workflow-app/context-param", "addInitParameter", 2);
		digester.addCallParam(prefix + "workflow-app/context-param/param-name", 0);
		digester.addCallParam(prefix + "workflow-app/context-param/param-value", 1);

		//------------------------------------------------------------------------
		//
		//------------------------------------------------------------------------
		digester.addRule(prefix + "workflow-app/selector", new WrapperCreateRule(digester));
		digester.addSetNext(prefix + "workflow-app/selector", "addWrapper", "org.huihoo.willow.Wrapper");

		digester.addCallMethod(prefix + "workflow-app/selector/init-param", "addInitParameter", 2);
		digester.addCallParam(prefix + "workflow-app/selector/init-param/param-name", 0);
		digester.addCallParam(prefix + "workflow-app/selector/init-param/param-value", 1);

		digester.addCallMethod(prefix + "workflow-app/selector/load-on-startup", "setLoadOnStartupString", 0);

		digester.addCallMethod(prefix + "workflow-app/selector/selector-class", "setSelectorClass", 0);
		digester.addCallMethod(prefix + "workflow-app/selector/selector-name", "setName", 0);

		digester.addCallMethod(prefix + "workflow-app/selector-mapping", "addSelectorMapping", 2);
		digester.addCallParam(prefix + "workflow-app/selector-mapping/selector-name", 1);
		digester.addCallParam(prefix + "workflow-app/selector-mapping/url-pattern", 0);
	}

	public void recycle()
	{
	}
}

/**
 * A Rule that calls the factory method on the specified Context to
 * create the object that is to be added to the stack.
 */

final class WrapperCreateRule extends Rule
{

	public WrapperCreateRule(Digester digester)
	{
		super(digester);
	}

	public void begin(Attributes attributes) throws Exception
	{
		Context context = (Context) digester.peek(digester.getCount() - 1);
		Wrapper wrapper = context.createWrapper();
		digester.push(wrapper);
		if (digester.getDebug() > 0)
			digester.log("new " + wrapper.getClass().getName());
	}

	public void end() throws Exception
	{
		Wrapper wrapper = (Wrapper) digester.pop();
		if (digester.getDebug() > 0)
			digester.log("pop " + wrapper.getClass().getName());
	}

}
