KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > util > TokenizedString


1 package rero.util;
2
3 import java.util.*;
4 import rero.util.hidden.MyTokenizer;
5
6 public class TokenizedString
7 {
8     protected String JavaDoc text;
9
10     protected String JavaDoc tokenDelimeter;
11     protected String JavaDoc stringToken[];
12     protected String JavaDoc tokenUpCache[];
13     protected String JavaDoc tokenDownCache[];
14
15     protected int totalTokens;
16
17     public String JavaDoc getString()
18     {
19        return text;
20     }
21
22     public TokenizedString(String JavaDoc str)
23     {
24        text = str;
25     }
26
27     public TokenizedString(String JavaDoc str, String JavaDoc delim)
28     {
29        text = str;
30        tokenize(delim);
31     }
32
33     public int tokenize(String JavaDoc delim)
34     { /* return number of tokens */
35        MyTokenizer erect = new MyTokenizer(text, delim);
36        stringToken = new String JavaDoc[erect.countTokens()];
37        tokenUpCache = new String JavaDoc[erect.countTokens()];
38        tokenDownCache = new String JavaDoc[erect.countTokens()];
39
40        totalTokens = stringToken.length;
41        tokenDelimeter = delim;
42
43        for (int x = 0; x < totalTokens; x++)
44        {
45           stringToken[x] = erect.nextToken();
46        }
47
48        return totalTokens;
49     }
50
51     /** returns tokens from and including a up to and including b */
52     public String JavaDoc getTokenRange(int a, int b)
53     {
54         if (a == 0) // special case.
55
{
56            return getTokenTo(b);
57         }
58
59         if (a == b)
60         {
61            return "";
62         }
63
64         if (a > b)
65         {
66            return "";
67         }
68
69         String JavaDoc startOfRange = getTokenTo(a);
70         String JavaDoc endOfRange = getTokenTo(b + 1); // we want the token range to include both the beginning and
71
// end token in the range. This is why we have the +1 hack.
72

73         int lenStart, lenEnd, lenDelim, lenText;
74         lenStart = startOfRange.length();
75         lenEnd = endOfRange.length();
76         lenDelim = tokenDelimeter.length();
77         lenText = text.length();
78
79         //
80
// strip out the pesky deliminator...
81
//
82
while ((lenStart + lenDelim) < lenText && text.substring(lenStart, lenStart + lenDelim).equals(tokenDelimeter))
83         {
84            lenStart += lenDelim;
85         }
86
87         return text.substring(lenStart, lenEnd);
88     }
89
90     /** returns tokens up to and including x (starting at a 0 count) */
91     public String JavaDoc getTokenTo(int x)
92     {
93         if (x >= tokenDownCache.length) // special case.
94
{
95            return text;
96         }
97
98         /* get token x on down (tokens preserved) */
99         if (tokenDownCache[x] == null)
100         {
101            tokenDownCache[x] = gettokdn(text, x, tokenDelimeter);
102         }
103         return tokenDownCache[x];
104     }
105
106     /** returns tokens from and including x (starting at a 0 count) */
107     public String JavaDoc getTokenFrom(int x)
108     {
109         if (x >= tokenDownCache.length) // special case.
110
{
111            return "";
112         }
113
114         /* get token x on up (tokens preserved) */
115         if (tokenUpCache[x] == null)
116         {
117            tokenUpCache[x] = gettokup(text, x, tokenDelimeter);
118         }
119         return tokenUpCache[x];
120     }
121
122     public boolean isTokenized(String JavaDoc text, String JavaDoc delim)
123     {
124        if ((text.equals(this.text)) && (delim.equals(tokenDelimeter)))
125        {
126           return true;
127        }
128        return false;
129     }
130
131     public int getTotalTokens()
132     {
133        return totalTokens;
134     }
135
136     /** returns token x */
137     public String JavaDoc getToken(int x)
138     {
139        /* return token x */
140        if (x < totalTokens)
141        {
142           return stringToken[x];
143        }
144        return null;
145     }
146
147     public String JavaDoc toString()
148     {
149        String JavaDoc t;
150        t = "--- jString:Report ---\n";
151        t = t+"Original String: "+text+"\n";
152        t = t+"Token Delimeter: \""+tokenDelimeter+"\"\n";
153        t = t+"Total Tokens : "+totalTokens+"\n";
154        t = t+"--- Token:Breakdown ---\n";
155        for (int x = 0; x < totalTokens; x++)
156        {
157           t = t+x+") "+getToken(x)+"\n";
158        }
159        t = t+"--- END:REPORT ---\n";
160        return t;
161     }
162
163     protected static String JavaDoc gettokup(String JavaDoc text, int occurence, String JavaDoc delim)
164     {
165          if (occurence == 0) // special case.. if the token we want up to is 0 then this is the whole string
166
{
167             return text;
168          }
169
170          String JavaDoc tokensPrior = gettokdn(text, occurence, delim);
171          
172          int lenPrior, lenDelim, lenText;
173
174          lenPrior = tokensPrior.length();
175          lenDelim = delim.length();
176          lenText = text.length();
177
178          if (lenPrior < lenText)
179          {
180             //
181
// strip out the pesky deliminator...
182
//
183
while ((lenPrior + lenDelim) < lenText && text.substring(lenPrior, lenPrior + lenDelim).equals(delim))
184             {
185                lenPrior += lenDelim;
186             }
187
188             return text.substring(lenPrior, lenText);
189          }
190
191          return "";
192      }
193
194      protected static String JavaDoc gettokdn(String JavaDoc text, int occurence, String JavaDoc delim)
195      {
196         if (occurence == 0) // deal with the special case, from token 0 on down is an empty string.
197
{
198            return "";
199         }
200
201         int occ = 0; // occurence;
202

203         int dlen = delim.length();
204         int tlen = text.length();
205
206         for (int x = 0; x < tlen; x++)
207         {
208            if ((x + dlen) < tlen && text.substring(x, x + dlen).equals(delim))
209            {
210               occ++;
211
212               if (occ == occurence)
213               {
214                   return text.substring(0, x);
215               }
216
217               while ((x + dlen) < tlen && text.substring(x, x + dlen).equals(delim))
218               {
219                  x += dlen;
220               }
221            }
222         }
223
224         return text;
225      }
226
227      public boolean isToken(String JavaDoc token)
228      {
229         if (findToken(token) >= 0)
230         {
231            return true;
232         }
233         return false;
234      }
235
236      public int findToken(String JavaDoc token)
237      {
238         for (int x = 0; x < getTotalTokens(); x++)
239         {
240            if (getToken(x).toUpperCase().equals(token.toUpperCase()))
241            {
242               return x;
243            }
244         }
245         return -1;
246      }
247 }
248
Popular Tags