org.safs
Class StringMBTokenizer

java.lang.Object
  extended by org.safs.StringMBTokenizer
All Implemented Interfaces:
java.util.Enumeration

public class StringMBTokenizer
extends java.lang.Object
implements java.util.Enumeration


All Implemented Interfaces: Enumeration

--------------------------------------------------------------------------------

The String Multi-Byte Tokenizer allows an application to break a String into tokens. The tokenization method is such that the provided delimiter string, typically a string of 2 or more characters, is used as a single multi-byte delimiter. This differs from the java.util.StringTokenizer which uses each separate character in the delimiter string as a possible token delimiter.

The StringMBTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments. Both the String to tokenize and the delimiter String are treated simply as literal text strings.

The multi-byte delimiter (the String that separates tokens) may be specified either at creation time, or on a per-token basis.

StringMBTokenizer will only return tokens. You cannot optionally request to also return delimiters. There is little need for that feature since there is only one active multi-byte delimiter at any given time.

A token is returned by taking a substring of the string that was used to create the StringMBTokenizer object.

Two multi-byte delimiters back-to-back delimit an inner empty string token.
A leading multi-byte delimiter delimits a leading empty string token.
A trailing multi-byte delimiter only terminates the preceding token. There is no trailing empty string token following a trailing delimiter.

The following is one example of the use of the tokenizer. The code:

StringMBTokenizer st = new StringMBTokenizer("File->Menu->Menuitem","->"); while (st.hasMoreTokens()) { println(st.nextToken()); }

prints the following output:


File
Menu
Menuitem


Constructor Summary
StringMBTokenizer(java.lang.String source, java.lang.String delimiter)
          
Purpose: Constructs a string tokenizer for the specified string.
 
Method Summary
 int countTokens()
          
Purpose: Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.
 int countTokens(java.lang.String delimiter)
          
Purpose: Calculates the number of times that this tokenizer's nextToken(delimiter) method can be called before it generates an exception.
 boolean hasMoreElements()
          
Purpose: Returns the same value as the hasMoreTokens method.
 boolean hasMoreTokens()
          
Purpose: Tests if there are more tokens available from this tokenizer's string.
static void main(java.lang.String[] args)
           
 java.lang.Object nextElement()
          
Purpose: Returns the same value as the nextToken method, except that its declared return value is Object rather than String.
 java.lang.String nextToken()
          
Purpose: Returns the next token from this string tokenizer.
 java.lang.String nextToken(java.lang.String delimiter)
          
Purpose: Returns the next token in this string tokenizer's string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringMBTokenizer

public StringMBTokenizer(java.lang.String source,
                         java.lang.String delimiter)

Purpose: Constructs a string tokenizer for the specified string. The delimiter argument is the mult-byte String separating tokens.

Parameters:
source - string to be parsed
delimiter - string separating tokens
Method Detail

countTokens

public int countTokens()

Purpose: Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. This assumes the continued use of the delimiter provided to the constructor. If a different delimiter is used with nextToken then this count may be inaccurate.

Returns:
int

countTokens

public int countTokens(java.lang.String delimiter)

Purpose: Calculates the number of times that this tokenizer's nextToken(delimiter) method can be called before it generates an exception. This assumes the continued use of the delimiter parameter provided this routine. If a different delimiter is used with nextToken then this count may be inaccurate.

Parameters:
delimiter, - String alternate delimiter
Returns:
int

hasMoreElements

public boolean hasMoreElements()

Purpose: Returns the same value as the hasMoreTokens method.

Specified by:
hasMoreElements in interface java.util.Enumeration
Returns:
boolean, true if more, false if not

nextElement

public java.lang.Object nextElement()
                             throws java.util.NoSuchElementException

Purpose: Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

Specified by:
nextElement in interface java.util.Enumeration
Returns:
Object
Throws:
java.util.NoSuchElementException - - if there are no more tokens in this tokenizer's string.

hasMoreTokens

public boolean hasMoreTokens()

Purpose: Tests if there are more tokens available from this tokenizer's string.

Returns:
boolean, true if more, false if not

nextToken

public java.lang.String nextToken()
                           throws java.util.NoSuchElementException

Purpose: Returns the next token from this string tokenizer. except that its declared return value is String rather than Object.

Returns:
Object
Throws:
java.util.NoSuchElementException - - if there are no more tokens in this tokenizer's string.

nextToken

public java.lang.String nextToken(java.lang.String delimiter)
                           throws java.util.NoSuchElementException

Purpose: Returns the next token in this string tokenizer's string.

Parameters:
delimiter, - String alternate delimiter
Returns:
the next token in this string tokenizer's string.
Throws:
java.util.NoSuchElementException - - if there are no more tokens in this tokenizer's string.

main

public static void main(java.lang.String[] args)