summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/javalib/com/sun/slp/SLPTokenizer.java
blob: 82d2f9da25cab8b68588c8a66f3907be598fbff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * ident	"%Z%%M%	%I%	%E% SMI"
 *
 * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 */


/**
 * This is a replacement for StringTokenizer since there
 * is an incompatibility between JDK 1.2 and JDK 1.3.1
 * and beyond which breaks slp.jar support for apps which
 * could use either JDK.
 */

package com.sun.slp;

import java.util.Enumeration;
import java.util.NoSuchElementException;

public class SLPTokenizer implements Enumeration
{

    private String str;
    private String delims;
    private boolean bRetDel;
    private int index;

    private void initialize(String s, String d, boolean b)
    {
	str = s;
	delims = d;
	bRetDel = b;
	index = 0;
    }
    
    public SLPTokenizer(String s)
    {
	initialize(s, "", false);
    }

    public SLPTokenizer(String s, String delim)
    {
	initialize(s, delim, false);
    }

    public SLPTokenizer(String s, String delim, boolean returnDelims)
    {
	initialize(s, delim, returnDelims);
    }

    /**
     * Calculates the number of times that this tokenizer's 
     * nextToken method can be called before it generates an 
     * exception.
     */
    public int countTokens()
    {
	int i = 0;
	
	if (str.length() < 1) {
            return 0;
        }
	
	char c = str.charAt(0);
	boolean inToken = false;

	// a token starts if
	//  (a) next character is a non delimiter
	//  (b) there are more characters

	for (int j = 0; j < str.length(); j++)
	{
	    c = str.charAt(j);
	    if (delims.indexOf(c) != -1) {
		if (bRetDel) {
		    i++;
		}
		
		if (inToken == true) {
		    i++; // we were in a token, now completed it
		    inToken = false;
		}
	    } else {

		// To get here, we must be in a token.
		inToken = true;
	    }
	}

	if (inToken) {
	    i++;
	}

	return i;
    }
          
    /**
     * Returns the same value as the hasMoreTokens method.
     */
    
    public boolean hasMoreElements()
    {
	if (str.length() < 1) {
            return false;
        }

	if (index >= str.length()) {
            return false;
        }

	if (bRetDel == false) {
	    // Check to see if all there is left are delimiters.
	    // If so there are no more elements.
	    for (int i = index; i < str.length(); i++) {

		if (delims.indexOf(str.charAt(i)) == -1) {
		    return true;  // A non-delim char found!
                }
	    }
	    return false; // No non-delim chars remain!
	}

	return true;  // Something remains.
    }

    /**
     * Tests if there are more tokens available from this
     * tokenizer's string.
     */
    public boolean hasMoreTokens()
    {
	return hasMoreElements();
    }

    /**
     * Returns the same value as the nextToken method,
     * except that its declared return value is Object
     * rather than String.
     */
    public Object nextElement()
	throws NoSuchElementException
    {
	return (Object) nextToken();
    }

    /**
     * Returns the next token from this string tokenizer.
     *
     */
    public String nextToken()
	throws NoSuchElementException
    {
	if (index >= str.length()) throw new NoSuchElementException();
	
	StringBuffer sb = new StringBuffer();
        char c = str.charAt(index);

	if (bRetDel == true)
        {
	    
	    if (delims.indexOf(c) != -1) {

		// We begin at a delimiter.  Return it & advance over.
		sb.append(str.charAt(index));
		index++;
		return sb.toString();

	    } else {
		// Advance to next delimiter and stop.  Return string.
		while (index < str.length()) {

		    c = str.charAt(index);
		    if (delims.indexOf(c) != -1) {

			return sb.toString();

		    } else {

			sb.append(c);

		    }
		    index++;
		}
		// We get here only if this is the last token.
		return sb.toString();
	    }
	} else {
	    // 3 cases
	    //   token till the end
            //   token till a delimiter
	    //   only delimiters till the end (exception!)
	    while (index < str.length()) {

		c = str.charAt(index);
		if (delims.indexOf(c) != -1) {
		    if (sb.length() != 0) {

			index++; // Skip past the delimiter.
			return sb.toString();
		    }
		    index++; // Do not include delimiters if no content yet.

		} else { // Not the delimiter yet.

		    sb.append(c);
		    index++;
		}
	    }

	    if (sb.length() == 0) {
                throw new NoSuchElementException();
            }

	    return sb.toString();
	}
    }
    
    /**
     * Returns the next token in this string tokenizer's string.
     */
    public String nextToken(String delim)
	throws NoSuchElementException
    {
	String saveDelims = delims;
	delims = delim;
	try
	{
	    // This is not thread safe, but it will String.
	    // There are no guarantees StringTokenizer is
	    // thread safe either.
	    String ret = nextToken();
	    delims = saveDelims;
	    return ret;
	}
	catch (NoSuchElementException nsee)
	{
	    delims = saveDelims;
	    throw nsee;
	}
    }
}