/*
 * <copyright>
 *  Copyright (c) 2001 by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        IterateCmd.js
 *  Created:     2001-03-29
 *  $Id: WithCmd.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 with command.
 */
// <JSClass Name="com.hyperwave.res.WithCmd">

  //--------------------------------------------------------------------
  /**
   * Construct the for command node.
   * @see com.hyperwave.res.AbstractCmd
   */
  com.hyperwave.res.WithCmd = function ( aParam )
  {
    if ( aParam == "__proto__" )
      return;

    this.base$ = com.hyperwave.res.AbstractCmd;
    this.base$ ( aParam );
    delete this.base$;

    /**
     * Holds the parsed placeholder or null in case of invalid placeholder 
     */
    this.parsedPHolder_ =  
      com.hyperwave.res.Template.static_.parsePH ( this.expression_ );

    /**
     * Error flag is set when supplied placeholder is invalid
     */
    if ( this.parsedPHolder_ == null ) 
      this.error_ = new com.hyperwave.res.ResourceException (
            "com.hyperwave.res.WithCmd: Invalid placeholder '" +
            this.expression_ + "'." );
  }   
  class$ = doInherit ( com.hyperwave.res.WithCmd, 
                       com.hyperwave.res.AbstractCmd );

  //--------------------------------------------------------------------
  /**
   * Parses the rest of the <code>with</code> command. This is done
   * by finding the next <code>endwith</code>.
   * @see com.hyperwave.res.AbstractCmd#doParse
   */
  class$.doParse = function ( theTemplateTail )
  {
    this.withBodyTemplate_ = 
      new com.hyperwave.res.Template 
        ( { templateString_: theTemplateTail } );
    var state = 
      this.withBodyTemplate_.continueParse ( theTemplateTail, { endwith:true } );

    if ( state.error_ )
      return state;

    if ( state.parsedEndCmd_ == null )
    {
      state.textTail_ = theTemplateTail;
      state.error_ = new com.hyperwave.res.ResourceException ( 
        "com.hyperwave.res.WithCmd: Could not find *endwith* statement." );
      return state;
    }

    return state;
  }

  //--------------------------------------------------------------------
  /**
   * Executes the command in the given environment. This means 
   * that the data placeholder is evaluated and the scope is extended
   * thus the properties can be accessed via placeholders
   *
   * @see com.hyperwave.res.AbstractCmd#execCmd
   */
  class$.execCmd = function ( theEnv )
  {
    var ScopedMap = com.hyperwave.res.ScopedMap;
    var obj_data = theEnv.get ( this.parsedPHolder_ );

    if ( obj_data == null )
    {
      return "($UNKNOWN VALUE OF:" + this.parsedPHolder_ + ")";
    }
    
    var with_env = new ScopedMap ( theEnv, obj_data );

    return this.withBodyTemplate_.fill ( with_env );
  }

// </JSClass>
//----------------------------------------------------------------------

/* End of "com.hyperwave.res.WithCmd.js" */

