001: /**
002: * $RCSfile: FullStringTokenizer.java,v $
003: * $Revision: 1.3 $
004: * $Date: 2006/01/07 00:21:06 $
005: *
006: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
007: *
008: * ===================================================================
009: * The Apache Software License, Version 1.1
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution,
024: * if any, must include the following acknowledgment:
025: * "This product includes software developed by
026: * CoolServlets.com (http://www.Yasna.com)."
027: * Alternately, this acknowledgment may appear in the software itself,
028: * if and wherever such third-party acknowledgments normally appear.
029: *
030: * 4. The names "Jive" and "CoolServlets.com" must not be used to
031: * endorse or promote products derived from this software without
032: * prior written permission. For written permission, please
033: * contact webmaster@Yasna.com.
034: *
035: * 5. Products derived from this software may not be called "Jive",
036: * nor may "Jive" appear in their name, without prior written
037: * permission of CoolServlets.com.
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE.
051: * ====================================================================
052: *
053: * This software consists of voluntary contributions made by many
054: * individuals on behalf of CoolServlets.com. For more information
055: * on CoolServlets.com, please see <http://www.Yasna.com>.
056: */package com.Yasna.util;
057:
058: public class FullStringTokenizer {
059:
060: private String string;
061: private String delimiter;
062:
063: private int index = 0;
064: private int length = 0;
065: private int delimiterLength;
066:
067: public FullStringTokenizer(String string, String delimiter) {
068: this .string = string;
069: this .delimiter = delimiter;
070: delimiterLength = delimiter.length();
071: length = string.length();
072: }
073:
074: /**
075: * Determine if there are more tokens available
076: */
077: public boolean hasMoreTokens() {
078: return (index < length);
079: }
080:
081: /**
082: * Get the next token
083: */
084: public String nextToken() {
085: String s = this .string;
086: int nextToken = s.indexOf(delimiter, index);
087: //Done finding tokens
088: if (nextToken < 0) {
089: // fixed to return whatever is left in the string (for the case
090: // of "[tok]data[tok]data" <- no ending token)
091: String tok = s.substring(index, length);
092: index = length;
093: return tok;
094: } else if (nextToken == index) {
095: //We found an empty token
096: index += delimiterLength;
097: return "";
098: } else {
099: String result = s.substring(index, nextToken);
100: index = nextToken + delimiterLength;
101: return result;
102: }
103: }
104:
105: /**
106: * Test program.
107: */
108: public static void main(String[] args) {
109: String testString = "||this is|| a test||||of the system||||";
110: FullStringTokenizer tokens = new FullStringTokenizer(
111: testString, "||");
112: while (tokens.hasMoreTokens()) {
113: System.out.println("> " + tokens.nextToken());
114: }
115: }
116: }
|