17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * ident	"%Z%%M%	%I%	%E% SMI"
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /**
327c478bd9Sstevel@tonic-gate  * This is a replacement for StringTokenizer since there
337c478bd9Sstevel@tonic-gate  * is an incompatibility between JDK 1.2 and JDK 1.3.1
347c478bd9Sstevel@tonic-gate  * and beyond which breaks slp.jar support for apps which
357c478bd9Sstevel@tonic-gate  * could use either JDK.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate package com.sun.slp;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate import java.util.Enumeration;
417c478bd9Sstevel@tonic-gate import java.util.NoSuchElementException;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate public class SLPTokenizer implements Enumeration
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate     private String str;
477c478bd9Sstevel@tonic-gate     private String delims;
487c478bd9Sstevel@tonic-gate     private boolean bRetDel;
497c478bd9Sstevel@tonic-gate     private int index;
507c478bd9Sstevel@tonic-gate 
initialize(String s, String d, boolean b)517c478bd9Sstevel@tonic-gate     private void initialize(String s, String d, boolean b)
527c478bd9Sstevel@tonic-gate     {
537c478bd9Sstevel@tonic-gate 	str = s;
547c478bd9Sstevel@tonic-gate 	delims = d;
557c478bd9Sstevel@tonic-gate 	bRetDel = b;
567c478bd9Sstevel@tonic-gate 	index = 0;
577c478bd9Sstevel@tonic-gate     }
58*55fea89dSDan Cross 
SLPTokenizer(String s)597c478bd9Sstevel@tonic-gate     public SLPTokenizer(String s)
607c478bd9Sstevel@tonic-gate     {
617c478bd9Sstevel@tonic-gate 	initialize(s, "", false);
627c478bd9Sstevel@tonic-gate     }
637c478bd9Sstevel@tonic-gate 
SLPTokenizer(String s, String delim)647c478bd9Sstevel@tonic-gate     public SLPTokenizer(String s, String delim)
657c478bd9Sstevel@tonic-gate     {
667c478bd9Sstevel@tonic-gate 	initialize(s, delim, false);
677c478bd9Sstevel@tonic-gate     }
687c478bd9Sstevel@tonic-gate 
SLPTokenizer(String s, String delim, boolean returnDelims)697c478bd9Sstevel@tonic-gate     public SLPTokenizer(String s, String delim, boolean returnDelims)
707c478bd9Sstevel@tonic-gate     {
717c478bd9Sstevel@tonic-gate 	initialize(s, delim, returnDelims);
727c478bd9Sstevel@tonic-gate     }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate     /**
75*55fea89dSDan Cross      * Calculates the number of times that this tokenizer's
76*55fea89dSDan Cross      * nextToken method can be called before it generates an
777c478bd9Sstevel@tonic-gate      * exception.
787c478bd9Sstevel@tonic-gate      */
countTokens()797c478bd9Sstevel@tonic-gate     public int countTokens()
807c478bd9Sstevel@tonic-gate     {
817c478bd9Sstevel@tonic-gate 	int i = 0;
82*55fea89dSDan Cross 
837c478bd9Sstevel@tonic-gate 	if (str.length() < 1) {
847c478bd9Sstevel@tonic-gate             return 0;
857c478bd9Sstevel@tonic-gate         }
86*55fea89dSDan Cross 
877c478bd9Sstevel@tonic-gate 	char c = str.charAt(0);
887c478bd9Sstevel@tonic-gate 	boolean inToken = false;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	// a token starts if
917c478bd9Sstevel@tonic-gate 	//  (a) next character is a non delimiter
927c478bd9Sstevel@tonic-gate 	//  (b) there are more characters
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	for (int j = 0; j < str.length(); j++)
957c478bd9Sstevel@tonic-gate 	{
967c478bd9Sstevel@tonic-gate 	    c = str.charAt(j);
977c478bd9Sstevel@tonic-gate 	    if (delims.indexOf(c) != -1) {
987c478bd9Sstevel@tonic-gate 		if (bRetDel) {
997c478bd9Sstevel@tonic-gate 		    i++;
1007c478bd9Sstevel@tonic-gate 		}
101*55fea89dSDan Cross 
1027c478bd9Sstevel@tonic-gate 		if (inToken == true) {
1037c478bd9Sstevel@tonic-gate 		    i++; // we were in a token, now completed it
1047c478bd9Sstevel@tonic-gate 		    inToken = false;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	    } else {
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		// To get here, we must be in a token.
1097c478bd9Sstevel@tonic-gate 		inToken = true;
1107c478bd9Sstevel@tonic-gate 	    }
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (inToken) {
1147c478bd9Sstevel@tonic-gate 	    i++;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return i;
1187c478bd9Sstevel@tonic-gate     }
119*55fea89dSDan Cross 
1207c478bd9Sstevel@tonic-gate     /**
1217c478bd9Sstevel@tonic-gate      * Returns the same value as the hasMoreTokens method.
1227c478bd9Sstevel@tonic-gate      */
123*55fea89dSDan Cross 
hasMoreElements()1247c478bd9Sstevel@tonic-gate     public boolean hasMoreElements()
1257c478bd9Sstevel@tonic-gate     {
1267c478bd9Sstevel@tonic-gate 	if (str.length() < 1) {
1277c478bd9Sstevel@tonic-gate             return false;
1287c478bd9Sstevel@tonic-gate         }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (index >= str.length()) {
1317c478bd9Sstevel@tonic-gate             return false;
1327c478bd9Sstevel@tonic-gate         }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (bRetDel == false) {
1357c478bd9Sstevel@tonic-gate 	    // Check to see if all there is left are delimiters.
1367c478bd9Sstevel@tonic-gate 	    // If so there are no more elements.
1377c478bd9Sstevel@tonic-gate 	    for (int i = index; i < str.length(); i++) {
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		if (delims.indexOf(str.charAt(i)) == -1) {
1407c478bd9Sstevel@tonic-gate 		    return true;  // A non-delim char found!
1417c478bd9Sstevel@tonic-gate                 }
1427c478bd9Sstevel@tonic-gate 	    }
1437c478bd9Sstevel@tonic-gate 	    return false; // No non-delim chars remain!
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return true;  // Something remains.
1477c478bd9Sstevel@tonic-gate     }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate     /**
1507c478bd9Sstevel@tonic-gate      * Tests if there are more tokens available from this
1517c478bd9Sstevel@tonic-gate      * tokenizer's string.
1527c478bd9Sstevel@tonic-gate      */
hasMoreTokens()1537c478bd9Sstevel@tonic-gate     public boolean hasMoreTokens()
1547c478bd9Sstevel@tonic-gate     {
1557c478bd9Sstevel@tonic-gate 	return hasMoreElements();
1567c478bd9Sstevel@tonic-gate     }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate     /**
1597c478bd9Sstevel@tonic-gate      * Returns the same value as the nextToken method,
1607c478bd9Sstevel@tonic-gate      * except that its declared return value is Object
1617c478bd9Sstevel@tonic-gate      * rather than String.
1627c478bd9Sstevel@tonic-gate      */
nextElement()1637c478bd9Sstevel@tonic-gate     public Object nextElement()
1647c478bd9Sstevel@tonic-gate 	throws NoSuchElementException
1657c478bd9Sstevel@tonic-gate     {
1667c478bd9Sstevel@tonic-gate 	return (Object) nextToken();
1677c478bd9Sstevel@tonic-gate     }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate     /**
1707c478bd9Sstevel@tonic-gate      * Returns the next token from this string tokenizer.
1717c478bd9Sstevel@tonic-gate      *
1727c478bd9Sstevel@tonic-gate      */
nextToken()1737c478bd9Sstevel@tonic-gate     public String nextToken()
1747c478bd9Sstevel@tonic-gate 	throws NoSuchElementException
1757c478bd9Sstevel@tonic-gate     {
1767c478bd9Sstevel@tonic-gate 	if (index >= str.length()) throw new NoSuchElementException();
177*55fea89dSDan Cross 
1787c478bd9Sstevel@tonic-gate 	StringBuffer sb = new StringBuffer();
1797c478bd9Sstevel@tonic-gate         char c = str.charAt(index);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (bRetDel == true)
1827c478bd9Sstevel@tonic-gate         {
183*55fea89dSDan Cross 
1847c478bd9Sstevel@tonic-gate 	    if (delims.indexOf(c) != -1) {
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		// We begin at a delimiter.  Return it & advance over.
1877c478bd9Sstevel@tonic-gate 		sb.append(str.charAt(index));
1887c478bd9Sstevel@tonic-gate 		index++;
1897c478bd9Sstevel@tonic-gate 		return sb.toString();
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	    } else {
1927c478bd9Sstevel@tonic-gate 		// Advance to next delimiter and stop.  Return string.
1937c478bd9Sstevel@tonic-gate 		while (index < str.length()) {
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		    c = str.charAt(index);
1967c478bd9Sstevel@tonic-gate 		    if (delims.indexOf(c) != -1) {
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 			return sb.toString();
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		    } else {
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 			sb.append(c);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		    }
2057c478bd9Sstevel@tonic-gate 		    index++;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		// We get here only if this is the last token.
2087c478bd9Sstevel@tonic-gate 		return sb.toString();
2097c478bd9Sstevel@tonic-gate 	    }
2107c478bd9Sstevel@tonic-gate 	} else {
2117c478bd9Sstevel@tonic-gate 	    // 3 cases
2127c478bd9Sstevel@tonic-gate 	    //   token till the end
2137c478bd9Sstevel@tonic-gate             //   token till a delimiter
2147c478bd9Sstevel@tonic-gate 	    //   only delimiters till the end (exception!)
2157c478bd9Sstevel@tonic-gate 	    while (index < str.length()) {
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		c = str.charAt(index);
2187c478bd9Sstevel@tonic-gate 		if (delims.indexOf(c) != -1) {
2197c478bd9Sstevel@tonic-gate 		    if (sb.length() != 0) {
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 			index++; // Skip past the delimiter.
2227c478bd9Sstevel@tonic-gate 			return sb.toString();
2237c478bd9Sstevel@tonic-gate 		    }
2247c478bd9Sstevel@tonic-gate 		    index++; // Do not include delimiters if no content yet.
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		} else { // Not the delimiter yet.
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		    sb.append(c);
2297c478bd9Sstevel@tonic-gate 		    index++;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 	    }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	    if (sb.length() == 0) {
2347c478bd9Sstevel@tonic-gate                 throw new NoSuchElementException();
2357c478bd9Sstevel@tonic-gate             }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	    return sb.toString();
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate     }
240*55fea89dSDan Cross 
2417c478bd9Sstevel@tonic-gate     /**
2427c478bd9Sstevel@tonic-gate      * Returns the next token in this string tokenizer's string.
2437c478bd9Sstevel@tonic-gate      */
nextToken(String delim)2447c478bd9Sstevel@tonic-gate     public String nextToken(String delim)
2457c478bd9Sstevel@tonic-gate 	throws NoSuchElementException
2467c478bd9Sstevel@tonic-gate     {
2477c478bd9Sstevel@tonic-gate 	String saveDelims = delims;
2487c478bd9Sstevel@tonic-gate 	delims = delim;
2497c478bd9Sstevel@tonic-gate 	try
2507c478bd9Sstevel@tonic-gate 	{
2517c478bd9Sstevel@tonic-gate 	    // This is not thread safe, but it will String.
2527c478bd9Sstevel@tonic-gate 	    // There are no guarantees StringTokenizer is
2537c478bd9Sstevel@tonic-gate 	    // thread safe either.
2547c478bd9Sstevel@tonic-gate 	    String ret = nextToken();
2557c478bd9Sstevel@tonic-gate 	    delims = saveDelims;
2567c478bd9Sstevel@tonic-gate 	    return ret;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	catch (NoSuchElementException nsee)
2597c478bd9Sstevel@tonic-gate 	{
2607c478bd9Sstevel@tonic-gate 	    delims = saveDelims;
2617c478bd9Sstevel@tonic-gate 	    throw nsee;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate     }
2647c478bd9Sstevel@tonic-gate }
265