001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.perseus.util;
028:
029: /**
030: * This simple tokenizer is able to break down a String into tokens,
031: * given a single delimiter character.
032: *
033: * @version $Id: SimpleTokenizer.java,v 1.3 2006/04/21 06:35:58 st125089 Exp $
034: */
035: public final class SimpleTokenizer {
036: /**
037: * The data to parse.
038: */
039: protected String data;
040:
041: /**
042: * The length of the string to parse.
043: */
044: protected int length;
045:
046: /**
047: * The array of delimiters.
048: */
049: protected char[] del;
050:
051: /**
052: * The current parse index.
053: */
054: protected int cur = 0;
055:
056: /**
057: * @param data the string to tokenizer. Should not be null.
058: * @param delimiters each character in the string is considered to be a
059: * delimiter. Should not be null.
060: * @return an array of tokens.
061: */
062: public SimpleTokenizer(String data, String delimiters) {
063: if (data == null || delimiters == null) {
064: throw new IllegalArgumentException();
065: }
066:
067: this .data = data;
068: this .length = data.length();
069: del = delimiters.toCharArray();
070:
071: // Initialize by skipping delimiters.
072: skipDelimiters();
073: }
074:
075: /**
076: * Moves the current position to the first next character which is
077: * not a delimiter.
078: */
079: void skipToken() {
080: while (cur < length && !curIsDelimiter()) {
081: cur++;
082: }
083: }
084:
085: /**
086: * Moves the current position to the first next character which is
087: * a delimiter.
088: */
089: void skipDelimiters() {
090: while (cur < length && curIsDelimiter()) {
091: cur++;
092: }
093: }
094:
095: /**
096: * @return true if the current character is a delimiter.
097: */
098: boolean curIsDelimiter() {
099: char c = data.charAt(cur);
100: for (int i = 0; i < del.length; i++) {
101: if (c == del[i]) {
102: return true;
103: }
104: }
105: return false;
106: }
107:
108: /**
109: * @returns the nextToken
110: */
111: public String nextToken() {
112: if (!hasMoreTokens()) {
113: return null;
114: }
115:
116: // Now, build the new token.
117: int s = cur;
118: cur++;
119: skipToken();
120: int e = cur;
121:
122: // Skip all characters, starting at the current position, which
123: // match one of the delimiters.
124: skipDelimiters();
125:
126: return data.substring(s, e);
127: }
128:
129: /**
130: * @return true if there are more tokens available.
131: */
132: public boolean hasMoreTokens() {
133: return cur < length;
134: }
135:
136: /**
137: * @return the number of tokens
138: */
139: public int countTokens() {
140: int n = 0;
141: int tmpCur = cur;
142:
143: cur = 0;
144: while (cur < length) {
145: skipDelimiters();
146: if (cur < length) {
147: n++;
148: }
149: skipToken();
150: }
151:
152: cur = tmpCur;
153:
154: return n;
155: }
156: }
|