package tags.TagLibraryValidator;

import java.util.Map;
import javax.servlet.jsp.tagext.TagLibraryValidator;
import javax.servlet.jsp.tagext.PageData;

/**
 * Validates that the init-params are the correct ones, (as specified in 
 * /WEB-INF/tlds/tld_uri.tld).
 */
public class Validator extends TagLibraryValidator
{
   /**
    * The map that contains the init-parameters.  This is stored so as to make
    * it accessible from getInitParameters().
    */
   private Map m_map;
   
   
   /**
    * Called by the container to set the init-parameters specified in the TLD.
    */
   public void setInitParameters(Map map)
   {
      m_map = map;
   }

   
   /**
    * Returns an error message if either of the init-params is incorrect.
    */
   public String validate(String prefix, String uri, PageData page)
   {
      String error = null;
      if( m_map.get( "usa" ) != null && !m_map.get( "usa" ).equals( "Washington" ) )
      {
         error = "Expecting 'Washington' as the value for init-param 'usa'.";
      }
      if( m_map.get( "india" ) != null && !m_map.get( "india" ).equals( "New Delhi" ) )
      {
         error = "Expecting 'New Delhi' as the value for init-param 'india'.";
      }
      return error;
   }


   /**
    * Sets the map equal to null, allowing it to be garbage collected.
    */
   public void release()
   {
      m_map = null;
   }
   

   /**
    * Returns the init parameters.
    */
   public Map getInitParameters()
   {
      return m_map;
   }
}
