//----------------------------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.RuleSetBase;

public class ServiceRuleSet 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 ServiceRuleSet()
	{

		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 ServiceRuleSet(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.addObjectCreate(prefix +"Service", "org.huihoo.willow.core.StandardService", "className");
		digester.addRule(prefix +"Service", new setAllProperitesExcludingClassNameRule());
		digester.addRule(
			prefix + "Service",
			new LifecycleListenerRule(digester, "org.huihoo.willow.startup.ServiceConfig", "serviceConfigClass"));
		digester.addSetNext(prefix +"Service", "addService", "org.huihoo.willow.Service");
		
		digester.addRuleSet(new ContextRuleSet(prefix +" Service/Default"));
		
		digester.addObjectCreate(prefix +"Service/Listener", null, // MUST be specified in the element
		"className");
		digester.addRule(prefix +"Service/Listener", new setAllProperitesExcludingClassNameRule());
		digester.addSetNext(prefix +"Service/Listener", "addLifecycleListener", "org.huihoo.willow.LifecycleListener");

		digester.addObjectCreate(prefix + "Service/RealmDatabase", null, "className");
		digester.addSetNext(prefix + "Service/RealmDatabase", "setRealmDatabase", "org.huihoo.willow.RealmDatabase");

		digester.addObjectCreate(prefix + "Service/RealmDatabase/Store", "com.zosatapo.commons.store.Store");
		digester.addSetProperties(prefix + "Service/RealmDatabase/Store");
		digester.addRule(prefix + "Service/RealmDatabase/Store", new setAllProperitesExcludingClassNameRule());
		digester.addSetNext(prefix + "Service/RealmDatabase/Store", "setStore", "com.zosatapo.commons.store.Store");

		digester.addObjectCreate(prefix + "Service/Logger", null, "className");
		digester.addSetProperties(prefix + "Service/Logger");
		digester.addRule(prefix + "Service/Logger", new setAllProperitesExcludingClassNameRule());
		digester.addSetNext(prefix + "Service/Logger", "setLogger", "org.huihoo.willow.Logger");

		digester.addObjectCreate(prefix +"Service/Connector", null, "className");
		digester.addRule(prefix +"Service/Connector", new setAllProperitesExcludingClassNameRule());
		digester.addSetNext(prefix +"Service/Connector", "addConnector", "org.huihoo.willow.Connector");


		// Add RuleSets for nested elements		
		digester.addRuleSet(new ContextRuleSet(prefix +"Service/"));	
	}

}