

/*
   Client side JavaScript object for tokenization of a string.
   Best used for something as simple as a comma separated record of values.

   Sample usage:

   <script type="text/javascript" language="javascript" src="../lib/stringTokenizer.js"></script>
   <script type="text/javascript" language="javascript">

   	var separator = ",";
   	var names = "one,two,three";

   	var tokenizer = new StringTokenizer (names, separator);

   	while (tokenizer.hasMoreTokens())
   	{
   		document.write("<p>Name " + tokenizer.nextToken() + "</p>");
   	}  // end while

   </script>

Edited 27/09/2004 11:26AM
   Added a trim function and fixed a few "this"
   references that were not there and should have
   been.


*/



/*
   Constructor.
   Split up a material string based upong the separator.

   Param    -  material, the String to be split up.
   Param    -  separator, the String to look for within material. Should be
               something like "," or ".", not a regular expression.

*/
function StringTokenizer (material, separator)
{
   // Attributes.
   this.material = material;
   this.separator = separator;

   // Initialisation code.
   this.buildTokens();
   this.tokensReturned = 0;

}  // end constructor


var stProt = StringTokenizer.prototype;

/*
   Go through material, putting each token into a new array.

   Return      - the array with all the tokens in it.
*/
stProt.buildTokens=function()
{
   // Create array of tokens.
   this.tokens = new Array();
   var nextToken;

   // If no separators found, single token is the material string itself.
    if (this.material.indexOf (this.separator) < 0)
    {
        this.tokens[0] = this.material;
        return;
    }  // end if

    // Establish initial start and end positions of the first token.
    var start = 0;
    var end = this.material.indexOf (this.separator, start);

    // Counter for how many tokens were found.
    var counter = 0;

    // Go through material, token at a time.
    while (this.material.length - start > 1)
    {
        nextToken = this.material.substring (start, end);
	start = end + 1;
	if (this.material.indexOf (this.separator, start + 1) < 0)
	{
	    end = this.material.length;
	}  // end if
	else
	{
	    end = this.material.indexOf (this.separator, start + 1);
	}  // end else

        this.tokens[counter] = this.trim (nextToken);

	counter++;
    }   // end while

    // Return the initialised array.
    return;


}  // end buildTokens function


/*
   Return a count of the number of tokens in the material.

   Return      - int number of tokens in material.
*/
stProt.countTokens=function()
{
  return this.tokens.length;
}  // end countTokens function



/*
   Get next token in material.

   Return      - next token in material.
*/
stProt.nextToken=function()
{

   if (this.tokensReturned >= this.tokens.length)
   {
      return null;
   }  // end if
   else
   {
      var returnToken = this.tokens [this.tokensReturned];
      this.tokensReturned++;
      return returnToken;
   }  // end else

}  // end nextToken function



/*
   Tests if there are more tokens available from this tokenizer's string. If
   this method returns true, then a subsequent call to nextToken
   will successfully return a token.

   Return      true if more tokens, false otherwise.
*/
stProt.hasMoreTokens=function()
{
   if (this.tokensReturned < this.tokens.length)
   {
      return true;
   }  // end if
   else
   {
      return false;
   }  // end else
}  // end hasMoreTokens function

stProt.tokensReturned=function()
{
   return this.tokensReturned;
}  // end tokensReturned function


stProt.trim=function(strToTrim)
{
   return(strToTrim.replace(/^\s+|\s+$/g, ''));
}  // end trim function



