KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > ProtoStringTokenizer


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.util.*;
54
55 /**
56  * A string tokenizer. Behaves like <tt>java.util.StringTokenizer</tt>
57  * extept that two delimiters next to eachother are recognized as
58  * two separate delimiters. For instance, splitting "<tt>foo||bar</tt>"
59  * using "<tt>|</tt>" as the delimiter will return three tokens
60  * ("<tt>foo</tt>", "" (empty string), and "<tt>bar</tt>").
61  */

62 public class ProtoStringTokenizer
63 {
64   private String JavaDoc string;
65   private int index = 0;
66   private int length = 0;
67   private Dictionary tokens = new Hashtable();
68
69   /**
70    * Create a new ProtoStringTokenizer with the given string
71    * to tokenize and the given list of tokens.
72    */

73   public ProtoStringTokenizer(String JavaDoc string, String JavaDoc tokens)
74   {
75     this.string = string;
76     this.length = string.length();
77     String JavaDoc value = "";
78     for (int i=0; i<tokens.length(); i++)
79       this.tokens.put(new Character JavaDoc(tokens.charAt(i)), value);
80   }
81
82   /**
83    * Determine if there are more tokens available
84    */

85   public boolean hasMoreTokens()
86   {
87     return (index < length);
88   }
89
90   /**
91    * Get the next token
92    */

93   public String JavaDoc nextToken()
94   {
95     String JavaDoc s = this.string;
96     for (int i=index; i<length; i++)
97     {
98       if (tokens.get(new Character JavaDoc(s.charAt(i))) != null)
99       {
100         String JavaDoc ret = s.substring(index, i);
101         this.index = i +1;
102         return ret;
103       }
104     }
105     String JavaDoc ret = s.substring(index, length);
106     this.index = length;
107     return ret;
108   }
109
110   /**
111    * A test program.
112    */

113   public static void main(String JavaDoc args[])
114   {
115     if (args.length != 2)
116     {
117       System.out.println("Usage: ProtoStringTokenizer string delim");
118       System.exit(0);
119     }
120     ProtoStringTokenizer st = new ProtoStringTokenizer(args[0], args[1]);
121     System.out.println("String = '" + args[0] + "'");
122     System.out.println("Delim = '" + args[1] + "'");
123     while (st.hasMoreTokens())
124     {
125       System.out.println("Token = '" + st.nextToken() + "'");
126     }
127   }
128 }
129
Popular Tags