home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / StringTokenizer.java < prev    next >
Text File  |  1997-05-20  |  9KB  |  272 lines

  1. /*
  2.  * @(#)StringTokenizer.java    1.15 97/01/28
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util;
  24.  
  25. import java.lang.*;
  26.  
  27. /**
  28.  * The string tokenizer class allows an application to break a 
  29.  * string into tokens. The tokenization method is much simpler than 
  30.  * the one used by the <code>StreamTokenizer</code> class. The 
  31.  * <code>StringTokenizer</code> methods do not distinguish among 
  32.  * identifiers, numbers, and quoted strings, nor do they recognize 
  33.  * and skip comments. 
  34.  * <p>
  35.  * The set of delimiters (the characters that separate tokens) may 
  36.  * be specified either at creation time or on a per-token basis. 
  37.  * <p>
  38.  * An instance of <code>StringTokenizer</code> behaves in one of two 
  39.  * ways, depending on whether it was created with the 
  40.  * <code>returnTokens</code> flag having the value <code>true</code> 
  41.  * or <code>false</code>: 
  42.  * <ul>
  43.  * <li>If the flag is <code>false</code>, delimiter characters serve to 
  44.  *     separate tokens. A token is a maximal sequence of consecutive 
  45.  *     characters that are not delimiters. 
  46.  * <li>If the flag is <code>true</code>, delimiter characters are considered to
  47.  *     be tokens. A token is either one delimiter character, or a maximal
  48.  *     sequence of consecutive characters that are not delimiters.
  49.  * </ul>
  50.  * <p>
  51.  * The following is one example of the use of the tokenizer. The code:
  52.  * <blockquote><pre>
  53.  *     StringTokenizer st = new StringTokenizer("this is a test");
  54.  *     while (st.hasMoreTokens()) {
  55.  *         println(st.nextToken());
  56.  *     }
  57.  * </pre></blockquote>
  58.  * <p>
  59.  * prints the following output:
  60.  * <blockquote><pre>
  61.  *     this
  62.  *     is
  63.  *     a
  64.  *     test
  65.  * </pre></blockquote>
  66.  *
  67.  * @author  unascribed
  68.  * @version 1.15, 01/28/97
  69.  * @see     java.io.StreamTokenizer
  70.  * @since   JDK1.0
  71.  */
  72. public
  73. class StringTokenizer implements Enumeration {
  74.     private int currentPosition;
  75.     private int maxPosition;
  76.     private String str;
  77.     private String delimiters;
  78.     private boolean retTokens;
  79.  
  80.     /**
  81.      * Constructs a string tokenizer for the specified string. The 
  82.      * characters in the <code>delim</code> argument are the delimiters 
  83.      * for separating tokens. 
  84.      * <p>
  85.      * If the <code>returnTokens</code> flag is <code>true</code>, then 
  86.      * the delimiter characters are also returned as tokens. Each 
  87.      * delimiter is returned as a string of length one. If the flag is 
  88.      * <code>false</code>, the delimiter characters are skipped and only 
  89.      * serve as separators between tokens. 
  90.      *
  91.      * @param   str            a string to be parsed.
  92.      * @param   delim          the delimiters.
  93.      * @param   returnTokens   flag indicating whether to return the delimiters
  94.      *                         as tokens.
  95.      * @since   JDK1.0
  96.      */
  97.     public StringTokenizer(String str, String delim, boolean returnTokens) {
  98.     currentPosition = 0;
  99.     this.str = str;
  100.     maxPosition = str.length();
  101.     delimiters = delim;
  102.     retTokens = returnTokens;
  103.     }
  104.  
  105.     /**
  106.      * Constructs a string tokenizer for the specified string. The 
  107.      * characters in the <code>delim</code> argument are the delimiters 
  108.      * for separating tokens. 
  109.      *
  110.      * @param   str     a string to be parsed.
  111.      * @param   delim   the delimiters.
  112.      * @since   JDK1.0
  113.      */
  114.     public StringTokenizer(String str, String delim) {
  115.     this(str, delim, false);
  116.     }
  117.  
  118.     /**
  119.      * Constructs a string tokenizer for the specified string. The 
  120.      * tokenizer uses the default delimiter set, which is 
  121.      * <code>"\t\n\r"</code>: the space character, the tab
  122.      * character, the newline character, and the carriage-return character. 
  123.      *
  124.      * @param   str   a string to be parsed.
  125.      * @since   JDK1.0
  126.      */
  127.     public StringTokenizer(String str) {
  128.     this(str, " \t\n\r", false);
  129.     }
  130.  
  131.     /**
  132.      * Skips delimiters.
  133.      */
  134.     private void skipDelimiters() {
  135.     while (!retTokens &&
  136.            (currentPosition < maxPosition) &&
  137.            (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  138.         currentPosition++;
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Tests if there are more tokens available from this tokenizer's string.
  144.      *
  145.      * @return  <code>true</code> if there are more tokens available from this
  146.      *          tokenizer's string; <code>false</code> otherwise.
  147.      * @since   JDK1.0
  148.      */
  149.     public boolean hasMoreTokens() {
  150.     skipDelimiters();
  151.     return (currentPosition < maxPosition);
  152.     }
  153.  
  154.     /**
  155.      * Returns the next token from this string tokenizer.
  156.      *
  157.      * @return     the next token from this string tokenizer.
  158.      * @exception  NoSuchElementException  if there are no more tokens in this
  159.      *               tokenizer's string.
  160.      * @since      JDK1.0
  161.      */
  162.     public String nextToken() {
  163.     skipDelimiters();
  164.  
  165.     if (currentPosition >= maxPosition) {
  166.         throw new NoSuchElementException();
  167.     }
  168.  
  169.     int start = currentPosition;
  170.     while ((currentPosition < maxPosition) && 
  171.            (delimiters.indexOf(str.charAt(currentPosition)) < 0)) {
  172.         currentPosition++;
  173.     }
  174.     if (retTokens && (start == currentPosition) &&
  175.         (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  176.         currentPosition++;
  177.     }
  178.     return str.substring(start, currentPosition);
  179.     }
  180.  
  181.     /**
  182.      * Returns the next token in this string tokenizer's string. The new 
  183.      * delimiter set remains the default after this call. 
  184.      *
  185.      * @param      delim   the new delimiters.
  186.      * @return     the next token, after switching to the new delimiter set.
  187.      * @exception  NoSuchElementException  if there are no more tokens in this
  188.      *               tokenizer's string.
  189.      * @since   JDK1.0
  190.      */
  191.     public String nextToken(String delim) {
  192.     delimiters = delim;
  193.     return nextToken();
  194.     }
  195.  
  196.     /**
  197.      * Returns the same value as the <code>hasMoreTokens</code>
  198.      * method. It exists so that this class can implement the
  199.      * <code>Enumeration</code> interface. 
  200.      *
  201.      * @return  <code>true</code> if there are more tokens;
  202.      *          <code>false</code> otherwise.
  203.      * @see     java.util.Enumeration
  204.      * @see     java.util.StringTokenizer#hasMoreTokens()
  205.      * @since   JDK1.0
  206.      */
  207.     public boolean hasMoreElements() {
  208.     return hasMoreTokens();
  209.     }
  210.  
  211.     /**
  212.      * Returns the same value as the <code>nextToken</code> method,
  213.      * except that its declared return value is <code>Object</code> rather than
  214.      * <code>String</code>. It exists so that this class can implement the
  215.      * <code>Enumeration</code> interface. 
  216.      *
  217.      * @return     the next token in the string.
  218.      * @exception  NoSuchElementException  if there are no more tokens in this
  219.      *               tokenizer's string.
  220.      * @see        java.util.Enumeration
  221.      * @see        java.util.StringTokenizer#nextToken()
  222.      * @since      JDK1.0
  223.      */
  224.     public Object nextElement() {
  225.     return nextToken();
  226.     }
  227.  
  228.     /**
  229.      * Calculates the number of times that this tokenizer's 
  230.      * <code>nextToken</code> method can be called before it generates an 
  231.      * exception. 
  232.      *
  233.      * @return  the number of tokens remaining in the string using the current
  234.      *          delimiter set.
  235.      * @see     java.util.StringTokenizer#nextToken()
  236.      * @since   JDK1.0
  237.      */
  238.     public int countTokens() {
  239.     int count = 0;
  240.     int currpos = currentPosition;
  241.  
  242.     while (currpos < maxPosition) {
  243.         /*
  244.          * This is just skipDelimiters(); but it does not affect
  245.          * currentPosition.
  246.          */
  247.         while (!retTokens &&
  248.            (currpos < maxPosition) &&
  249.            (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  250.         currpos++;
  251.         }
  252.  
  253.         if (currpos >= maxPosition) {
  254.         break;
  255.         }
  256.  
  257.         int start = currpos;
  258.         while ((currpos < maxPosition) && 
  259.            (delimiters.indexOf(str.charAt(currpos)) < 0)) {
  260.         currpos++;
  261.         }
  262.         if (retTokens && (start == currpos) &&
  263.         (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  264.         currpos++;
  265.         }
  266.         count++;
  267.  
  268.     }
  269.     return count;
  270.     }
  271. }
  272.