/*
 * <copyright>
 *  Copyright (c) 2001 by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        AbstractResourceReader.js
 *  Created:     2001-01-14
 *  $Id: AbstractResourceReader.js,v 1.7 2003/07/03 08:05:57 mmair Exp $
 * </file>
 */

initPackage ( "com.hyperwave.res" );
loadClass ( "com.hyperwave.res.ResourceException" );

//----------------------------------------------------------------------
/**
 * An <code>AbstractResourceReader</code> defines a model for reading
 * and accessing resources and templates from the file system or the server. 
 * These readers provide a simple map structure which can be accessed 
 * directly using keys. Furthermore it can be stepped through using the 
 * built-in iteration mechanism.
 */
// <JSClass Name="com.hyperwave.res.AbstractResourceReader">

  //--------------------------------------------------------------------
  /**
   * Constructs an <code>AbstractResrouceReader</code> which only
   * prepares the intrinsic arrays and the iteration mechanism.
   */
  com.hyperwave.res.AbstractResourceReader = function ( aParam )
  {
    if ( aParam == "__proto__" )
      return;

    this.keyArray_ = [];
    this.resArray_ = {};
    this.iteratorIndex_ = 0;

    this.error_ = null;
  }
  class$ = doInherit ( com.hyperwave.res.AbstractResourceReader );

  //--------------------------------------------------------------------
  /**
   * Sets (or overrides) a key - value pair of the internal structure.
   * @param aKey: String: the key that gets an associated value
   * @param aRes: Object: the associated object (or resource)
   */
  class$.setRes = function ( aKey, aRes )
  {
    this.keyArray_ [this.keyArray_.length] = aKey;
    this.resArray_ [aKey] = aRes;
  }

  //--------------------------------------------------------------------
  /**
   * Get a resource by key.
   * @param aKey: string: the key by which the resource is referenced
   * @return Object: the object referenced by this key or
   *   null if the key is not present.
   */
  class$.getRes = function ( aKey )
  {
    return this.resArray_ [aKey];
  }

  //--------------------------------------------------------------------
  //                       Iterator Interface
  //--------------------------------------------------------------------

  //--------------------------------------------------------------------
  /**
   * Initialize the iteration process.
   */
  class$.initIteration = function ()
  {
    this.iteratorIndex_ = 0;
  }

  //--------------------------------------------------------------------
  /**
   * Check, whether or not there exists another key for the iteration.
   * @return Boolean: true if another key is there for iteration.
   */
  class$.hasNextKey = function ()
  {
    return ( this.iteratorIndex_ < this.keyArray_.length );
  }

  //--------------------------------------------------------------------
  /**
   * Gets the next key if there is one. This function will return
   * null if no key is present for iteration.
   * @return String: the next key or <code>null</code> if no key
   *   is left for iteration.
   */
  class$.getNextKey = function ()
  {
    var curr_index = this.iteratorIndex_;
    if ( curr_index == this.keyArray_.length )
      return null;

    ++this.iteratorIndex_;
    return this.keyArray_ [curr_index];
  }

  //--------------------------------------------------------------------
  /**
   * Shows a string representation of the reader.
   * @return String: the string representation
   */
  class$.toString = function ()
  {
    var out_array = [ "@com.hyperwave.res.AbstractResourceReader:\n"];
    
    this.initIteration ();
    while ( this.hasNextKey () )
    {
      var key = this.getNextKey ();
      out_array [out_array.length] = "  " + key + ":='" + 
                                     this.getRes ( key ) + "'\n";
    }
    return out_array.join ( "" );
  }

  //--------------------------------------------------------------------
  /**
   * Returns valid JavaScript code of a fully functional client side 
   * resource reader, <code>CSResourceReader</code>. Can be used to
   * transport a ResourceReader (read in on server side) to the client.
   * @return String: a CSResourceReader in its serialized form
   * @see com.hyperwave.res.CSResourceReader
   */
  class$.toSource = function ()
  {
    // start instantiating a CSResourceReader
    var ret_arr = [ "new com.hyperwave.res.CSResourceReader ( {" ];
    res_out = [];
    // step through all defined resources
    for ( var i = 0; i < this.keyArray_.length; ++i )
    {
      var key = this.keyArray_ [i];
      var res = this.resArray_ [key];
      
      // if resource is a template, create JS code for a template
      if ( res.isTemplate_ )
      {
        res_out.push ( "'" + key + 
          "': new com.hyperwave.res.Template ( { templateString_: decodeURIComponent('" + encodeURIComponent(res.getTemplateString()) + "') } ) " );
      }
      // otherwise simply encode and decode
      else
      {
        res_out.push ( "'" + key + "': decodeURIComponent ( '" + encodeURIComponent(res) + "' ) " );
      }
    }
    ret_arr.push ( res_out.join ( ", " ) );
    ret_arr.push ( "} );\n" );  
    
    return ( ret_arr.join ( "" ) );
  }

// </JSClass>
//----------------------------------------------------------------------

/* End of "com.hyperwave.res.AbstractResourceReader.js" */

