2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * ident "%Z%%M% %I% %E% SMI"
2N/A *
2N/A * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A */
2N/A
2N/A
2N/A/**
2N/A * This is a replacement for StringTokenizer since there
2N/A * is an incompatibility between JDK 1.2 and JDK 1.3.1
2N/A * and beyond which breaks slp.jar support for apps which
2N/A * could use either JDK.
2N/A */
2N/A
2N/Apackage com.sun.slp;
2N/A
2N/Aimport java.util.Enumeration;
2N/Aimport java.util.NoSuchElementException;
2N/A
2N/Apublic class SLPTokenizer implements Enumeration
2N/A{
2N/A
2N/A private String str;
2N/A private String delims;
2N/A private boolean bRetDel;
2N/A private int index;
2N/A
2N/A private void initialize(String s, String d, boolean b)
2N/A {
2N/A str = s;
2N/A delims = d;
2N/A bRetDel = b;
2N/A index = 0;
2N/A }
2N/A
2N/A public SLPTokenizer(String s)
2N/A {
2N/A initialize(s, "", false);
2N/A }
2N/A
2N/A public SLPTokenizer(String s, String delim)
2N/A {
2N/A initialize(s, delim, false);
2N/A }
2N/A
2N/A public SLPTokenizer(String s, String delim, boolean returnDelims)
2N/A {
2N/A initialize(s, delim, returnDelims);
2N/A }
2N/A
2N/A /**
2N/A * Calculates the number of times that this tokenizer's
2N/A * nextToken method can be called before it generates an
2N/A * exception.
2N/A */
2N/A public int countTokens()
2N/A {
2N/A int i = 0;
2N/A
2N/A if (str.length() < 1) {
2N/A return 0;
2N/A }
2N/A
2N/A char c = str.charAt(0);
2N/A boolean inToken = false;
2N/A
2N/A // a token starts if
2N/A // (a) next character is a non delimiter
2N/A // (b) there are more characters
2N/A
2N/A for (int j = 0; j < str.length(); j++)
2N/A {
2N/A c = str.charAt(j);
2N/A if (delims.indexOf(c) != -1) {
2N/A if (bRetDel) {
2N/A i++;
2N/A }
2N/A
2N/A if (inToken == true) {
2N/A i++; // we were in a token, now completed it
2N/A inToken = false;
2N/A }
2N/A } else {
2N/A
2N/A // To get here, we must be in a token.
2N/A inToken = true;
2N/A }
2N/A }
2N/A
2N/A if (inToken) {
2N/A i++;
2N/A }
2N/A
2N/A return i;
2N/A }
2N/A
2N/A /**
2N/A * Returns the same value as the hasMoreTokens method.
2N/A */
2N/A
2N/A public boolean hasMoreElements()
2N/A {
2N/A if (str.length() < 1) {
2N/A return false;
2N/A }
2N/A
2N/A if (index >= str.length()) {
2N/A return false;
2N/A }
2N/A
2N/A if (bRetDel == false) {
2N/A // Check to see if all there is left are delimiters.
2N/A // If so there are no more elements.
2N/A for (int i = index; i < str.length(); i++) {
2N/A
2N/A if (delims.indexOf(str.charAt(i)) == -1) {
2N/A return true; // A non-delim char found!
2N/A }
2N/A }
2N/A return false; // No non-delim chars remain!
2N/A }
2N/A
2N/A return true; // Something remains.
2N/A }
2N/A
2N/A /**
2N/A * Tests if there are more tokens available from this
2N/A * tokenizer's string.
2N/A */
2N/A public boolean hasMoreTokens()
2N/A {
2N/A return hasMoreElements();
2N/A }
2N/A
2N/A /**
2N/A * Returns the same value as the nextToken method,
2N/A * except that its declared return value is Object
2N/A * rather than String.
2N/A */
2N/A public Object nextElement()
2N/A throws NoSuchElementException
2N/A {
2N/A return (Object) nextToken();
2N/A }
2N/A
2N/A /**
2N/A * Returns the next token from this string tokenizer.
2N/A *
2N/A */
2N/A public String nextToken()
2N/A throws NoSuchElementException
2N/A {
2N/A if (index >= str.length()) throw new NoSuchElementException();
2N/A
2N/A StringBuffer sb = new StringBuffer();
2N/A char c = str.charAt(index);
2N/A
2N/A if (bRetDel == true)
2N/A {
2N/A
2N/A if (delims.indexOf(c) != -1) {
2N/A
2N/A // We begin at a delimiter. Return it & advance over.
2N/A sb.append(str.charAt(index));
2N/A index++;
2N/A return sb.toString();
2N/A
2N/A } else {
2N/A // Advance to next delimiter and stop. Return string.
2N/A while (index < str.length()) {
2N/A
2N/A c = str.charAt(index);
2N/A if (delims.indexOf(c) != -1) {
2N/A
2N/A return sb.toString();
2N/A
2N/A } else {
2N/A
2N/A sb.append(c);
2N/A
2N/A }
2N/A index++;
2N/A }
2N/A // We get here only if this is the last token.
2N/A return sb.toString();
2N/A }
2N/A } else {
2N/A // 3 cases
2N/A // token till the end
2N/A // token till a delimiter
2N/A // only delimiters till the end (exception!)
2N/A while (index < str.length()) {
2N/A
2N/A c = str.charAt(index);
2N/A if (delims.indexOf(c) != -1) {
2N/A if (sb.length() != 0) {
2N/A
2N/A index++; // Skip past the delimiter.
2N/A return sb.toString();
2N/A }
2N/A index++; // Do not include delimiters if no content yet.
2N/A
2N/A } else { // Not the delimiter yet.
2N/A
2N/A sb.append(c);
2N/A index++;
2N/A }
2N/A }
2N/A
2N/A if (sb.length() == 0) {
2N/A throw new NoSuchElementException();
2N/A }
2N/A
2N/A return sb.toString();
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Returns the next token in this string tokenizer's string.
2N/A */
2N/A public String nextToken(String delim)
2N/A throws NoSuchElementException
2N/A {
2N/A String saveDelims = delims;
2N/A delims = delim;
2N/A try
2N/A {
2N/A // This is not thread safe, but it will String.
2N/A // There are no guarantees StringTokenizer is
2N/A // thread safe either.
2N/A String ret = nextToken();
2N/A delims = saveDelims;
2N/A return ret;
2N/A }
2N/A catch (NoSuchElementException nsee)
2N/A {
2N/A delims = saveDelims;
2N/A throw nsee;
2N/A }
2N/A }
2N/A}