xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/SLPTokenizer.java (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  */
29 
30 
31 /**
32  * This is a replacement for StringTokenizer since there
33  * is an incompatibility between JDK 1.2 and JDK 1.3.1
34  * and beyond which breaks slp.jar support for apps which
35  * could use either JDK.
36  */
37 
38 package com.sun.slp;
39 
40 import java.util.Enumeration;
41 import java.util.NoSuchElementException;
42 
43 public class SLPTokenizer implements Enumeration
44 {
45 
46     private String str;
47     private String delims;
48     private boolean bRetDel;
49     private int index;
50 
51     private void initialize(String s, String d, boolean b)
52     {
53 	str = s;
54 	delims = d;
55 	bRetDel = b;
56 	index = 0;
57     }
58 
59     public SLPTokenizer(String s)
60     {
61 	initialize(s, "", false);
62     }
63 
64     public SLPTokenizer(String s, String delim)
65     {
66 	initialize(s, delim, false);
67     }
68 
69     public SLPTokenizer(String s, String delim, boolean returnDelims)
70     {
71 	initialize(s, delim, returnDelims);
72     }
73 
74     /**
75      * Calculates the number of times that this tokenizer's
76      * nextToken method can be called before it generates an
77      * exception.
78      */
79     public int countTokens()
80     {
81 	int i = 0;
82 
83 	if (str.length() < 1) {
84             return 0;
85         }
86 
87 	char c = str.charAt(0);
88 	boolean inToken = false;
89 
90 	// a token starts if
91 	//  (a) next character is a non delimiter
92 	//  (b) there are more characters
93 
94 	for (int j = 0; j < str.length(); j++)
95 	{
96 	    c = str.charAt(j);
97 	    if (delims.indexOf(c) != -1) {
98 		if (bRetDel) {
99 		    i++;
100 		}
101 
102 		if (inToken == true) {
103 		    i++; // we were in a token, now completed it
104 		    inToken = false;
105 		}
106 	    } else {
107 
108 		// To get here, we must be in a token.
109 		inToken = true;
110 	    }
111 	}
112 
113 	if (inToken) {
114 	    i++;
115 	}
116 
117 	return i;
118     }
119 
120     /**
121      * Returns the same value as the hasMoreTokens method.
122      */
123 
124     public boolean hasMoreElements()
125     {
126 	if (str.length() < 1) {
127             return false;
128         }
129 
130 	if (index >= str.length()) {
131             return false;
132         }
133 
134 	if (bRetDel == false) {
135 	    // Check to see if all there is left are delimiters.
136 	    // If so there are no more elements.
137 	    for (int i = index; i < str.length(); i++) {
138 
139 		if (delims.indexOf(str.charAt(i)) == -1) {
140 		    return true;  // A non-delim char found!
141                 }
142 	    }
143 	    return false; // No non-delim chars remain!
144 	}
145 
146 	return true;  // Something remains.
147     }
148 
149     /**
150      * Tests if there are more tokens available from this
151      * tokenizer's string.
152      */
153     public boolean hasMoreTokens()
154     {
155 	return hasMoreElements();
156     }
157 
158     /**
159      * Returns the same value as the nextToken method,
160      * except that its declared return value is Object
161      * rather than String.
162      */
163     public Object nextElement()
164 	throws NoSuchElementException
165     {
166 	return (Object) nextToken();
167     }
168 
169     /**
170      * Returns the next token from this string tokenizer.
171      *
172      */
173     public String nextToken()
174 	throws NoSuchElementException
175     {
176 	if (index >= str.length()) throw new NoSuchElementException();
177 
178 	StringBuffer sb = new StringBuffer();
179         char c = str.charAt(index);
180 
181 	if (bRetDel == true)
182         {
183 
184 	    if (delims.indexOf(c) != -1) {
185 
186 		// We begin at a delimiter.  Return it & advance over.
187 		sb.append(str.charAt(index));
188 		index++;
189 		return sb.toString();
190 
191 	    } else {
192 		// Advance to next delimiter and stop.  Return string.
193 		while (index < str.length()) {
194 
195 		    c = str.charAt(index);
196 		    if (delims.indexOf(c) != -1) {
197 
198 			return sb.toString();
199 
200 		    } else {
201 
202 			sb.append(c);
203 
204 		    }
205 		    index++;
206 		}
207 		// We get here only if this is the last token.
208 		return sb.toString();
209 	    }
210 	} else {
211 	    // 3 cases
212 	    //   token till the end
213             //   token till a delimiter
214 	    //   only delimiters till the end (exception!)
215 	    while (index < str.length()) {
216 
217 		c = str.charAt(index);
218 		if (delims.indexOf(c) != -1) {
219 		    if (sb.length() != 0) {
220 
221 			index++; // Skip past the delimiter.
222 			return sb.toString();
223 		    }
224 		    index++; // Do not include delimiters if no content yet.
225 
226 		} else { // Not the delimiter yet.
227 
228 		    sb.append(c);
229 		    index++;
230 		}
231 	    }
232 
233 	    if (sb.length() == 0) {
234                 throw new NoSuchElementException();
235             }
236 
237 	    return sb.toString();
238 	}
239     }
240 
241     /**
242      * Returns the next token in this string tokenizer's string.
243      */
244     public String nextToken(String delim)
245 	throws NoSuchElementException
246     {
247 	String saveDelims = delims;
248 	delims = delim;
249 	try
250 	{
251 	    // This is not thread safe, but it will String.
252 	    // There are no guarantees StringTokenizer is
253 	    // thread safe either.
254 	    String ret = nextToken();
255 	    delims = saveDelims;
256 	    return ret;
257 	}
258 	catch (NoSuchElementException nsee)
259 	{
260 	    delims = saveDelims;
261 	    throw nsee;
262 	}
263     }
264 }
265