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