/*
 * <copyright>
 *  Copyright (c) 2001 by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        IfCmd.js
 *  Created:     2001-01-14
 *  $Id: IfCmd.js,v 1.4 2003/07/03 08:05:58 mmair Exp $
 * </file>
 */

initPackage ( "com.hyperwave.res" );
loadClass ( "com.hyperwave.res.ResourceException" );
loadClass ( "com.hyperwave.res.ScopedMap" );

//----------------------------------------------------------------------
/**
 * Defines the behaviour of a template's for command.
 */
// <JSClass Name="com.hyperwave.res.IfCmd">

  //--------------------------------------------------------------------
  /**
   * Construct the for command node.
   * @see com.hyperwave.res.AbstractCmd
   */
  com.hyperwave.res.IfCmd = function ( aParam )
  {
    if ( aParam == "__proto__" )
      return;

    this.base$ = com.hyperwave.res.AbstractCmd;
    this.base$ ( aParam );
    delete this.base$;
 
    /**
     * A function evaluating the parsed expression.
     */
    this.funPHolder_ = 
      com.hyperwave.res.Template.static_.parseExpr ( this.expression_ );

    /**
     * Error flag is set when supplied placeholder is invalid.
     */
    if ( this.funPHolder_ == null ) 
      this.error_ = new com.hyperwave.res.ResourceException (
            "com.hyperwave.res.IfCmd: Invalid placeholder expression '" +
            this.expression_ + "'." );

  }
  class$ = doInherit ( com.hyperwave.res.IfCmd, 
                       com.hyperwave.res.AbstractCmd );
                       

  //--------------------------------------------------------------------
  /**
   * Parses the rest of the <code>if</code> command. This is done
   * by finding the next <code>endif</code> or <code>else</code>.
   * @see com.hyperwave.res.AbstractCmd#doParse
   */
  class$.doParse = function ( theTemplateTail )
  {
    this.ifTemplate_ = 
      new com.hyperwave.res.Template 
        ( { templateString_: theTemplateTail } );
    var state = 
      this.ifTemplate_.continueParse ( theTemplateTail, { endif:true, "else":true, "elseif":true } );

    if ( state.error_ )
      return state;

    if ( ( state.parsedEndCmd_ == "else" ) || ( state.parsedEndCmd_ == "elseif" ) )
    {
      this.elseTemplate_ =
        new com.hyperwave.res.Template 
          ( { templateString_: state.textTail_ } );
      state = 
        this.elseTemplate_.continueParse ( state.textTail_, { endif:true } );
    }

    if ( state.error_ )
      return state;

    if ( state.parsedEndCmd_ == null )
    {
      state.textTail_ = theTemplateTail;
      state.error_ = new com.hyperwave.res.ResourceException ( 
        "com.hyperwave.res.IfCmd: Could not find *endif* statement." );
      return state;
    }

    return state;
  }

 //--------------------------------------------------------------------
  /**
   * Executes the command in the given environment. This means 
   * that the data placeholder is evaluated and iterated in this
   * case.
   * @see com.hyperwave.res.AbstractCmd#execCmd
   */
  class$.execCmd = function ( theEnv )
  {
    var condition = this.funPHolder_ ( theEnv )

    if ( condition )
      return ( this.ifTemplate_.fill ( theEnv ) );

    if ( this.elseTemplate_ )
      return ( this.elseTemplate_.fill ( theEnv ) );

    return "";
  }

// </JSClass>
//----------------------------------------------------------------------

/* End of "com.hyperwave.res.IfCmd.js" */

