001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: /**
021: * String tokenizer is used to break a string apart into tokens.
022: *
023: * If returnDelimiters is false, successive calls to nextToken() return maximal
024: * blocks of characters that do not contain a delimiter.
025: *
026: * If returnDelimiters is true, delimiters are considered to be tokens, and
027: * successive calls to nextToken() return either a one character delimiter, or a
028: * maximal block of text between delimiters.
029: */
030: public class StringTokenizer implements Enumeration<Object> {
031:
032: private String string;
033:
034: private String delimiters;
035:
036: private boolean returnDelimiters;
037:
038: private int position;
039:
040: /**
041: * Constructs a new StringTokenizer for string using whitespace as the
042: * delimiter, returnDelimiters is false.
043: *
044: * @param string
045: * the string to be tokenized
046: */
047: public StringTokenizer(String string) {
048: this (string, " \t\n\r\f", false); //$NON-NLS-1$
049: }
050:
051: /**
052: * Constructs a new StringTokenizer for string using the specified
053: * delimiters, returnDelimiters is false.
054: *
055: * @param string
056: * the string to be tokenized
057: * @param delimiters
058: * the delimiters to use
059: */
060: public StringTokenizer(String string, String delimiters) {
061: this (string, delimiters, false);
062: }
063:
064: /**
065: * Constructs a new StringTokenizer for string using the specified
066: * delimiters and returning delimiters as tokens when specified.
067: *
068: * @param string
069: * the string to be tokenized
070: * @param delimiters
071: * the delimiters to use
072: * @param returnDelimiters
073: * true to return each delimiter as a token
074: */
075: public StringTokenizer(String string, String delimiters,
076: boolean returnDelimiters) {
077: if (string != null) {
078: this .string = string;
079: this .delimiters = delimiters;
080: this .returnDelimiters = returnDelimiters;
081: this .position = 0;
082: } else
083: throw new NullPointerException();
084: }
085:
086: /**
087: * Returns the number of unprocessed tokens remaining in the string.
088: *
089: * @return number of tokens that can be retreived before an exception will
090: * result
091: */
092: public int countTokens() {
093: int count = 0;
094: boolean inToken = false;
095: for (int i = position, length = string.length(); i < length; i++) {
096: if (delimiters.indexOf(string.charAt(i), 0) >= 0) {
097: if (returnDelimiters)
098: count++;
099: if (inToken) {
100: count++;
101: inToken = false;
102: }
103: } else {
104: inToken = true;
105: }
106: }
107: if (inToken)
108: count++;
109: return count;
110: }
111:
112: /**
113: * Returns true if unprocessed tokens remain.
114: *
115: * @return true if unprocessed tokens remain
116: */
117: public boolean hasMoreElements() {
118: return hasMoreTokens();
119: }
120:
121: /**
122: * Returns true if unprocessed tokens remain.
123: *
124: * @return true if unprocessed tokens remain
125: */
126: public boolean hasMoreTokens() {
127: int length = string.length();
128: if (position < length) {
129: if (returnDelimiters)
130: return true; // there is at least one character and even if
131: // it is a delimiter it is a token
132:
133: // otherwise find a character which is not a delimiter
134: for (int i = position; i < length; i++)
135: if (delimiters.indexOf(string.charAt(i), 0) == -1)
136: return true;
137: }
138: return false;
139: }
140:
141: /**
142: * Returns the next token in the string as an Object.
143: *
144: * @return next token in the string as an Object
145: * @exception NoSuchElementException
146: * if no tokens remain
147: */
148: public Object nextElement() {
149: return nextToken();
150: }
151:
152: /**
153: * Returns the next token in the string as a String.
154: *
155: * @return next token in the string as a String
156: * @exception NoSuchElementException
157: * if no tokens remain
158: */
159: public String nextToken() {
160: int i = position;
161: int length = string.length();
162:
163: if (i < length) {
164: if (returnDelimiters) {
165: if (delimiters.indexOf(string.charAt(position), 0) >= 0)
166: return String.valueOf(string.charAt(position++));
167: for (position++; position < length; position++)
168: if (delimiters.indexOf(string.charAt(position), 0) >= 0)
169: return string.substring(i, position);
170: return string.substring(i);
171: }
172:
173: while (i < length
174: && delimiters.indexOf(string.charAt(i), 0) >= 0)
175: i++;
176: position = i;
177: if (i < length) {
178: for (position++; position < length; position++)
179: if (delimiters.indexOf(string.charAt(position), 0) >= 0)
180: return string.substring(i, position);
181: return string.substring(i);
182: }
183: }
184: throw new NoSuchElementException();
185: }
186:
187: /**
188: * Returns the next token in the string as a String. The delimiters used are
189: * changed to the specified delimiters.
190: *
191: * @param delims
192: * the new delimiters to use
193: * @return next token in the string as a String
194: * @exception NoSuchElementException
195: * if no tokens remain
196: */
197: public String nextToken(String delims) {
198: this.delimiters = delims;
199: return nextToken();
200: }
201: }
|