| 1 package rero.util; 2 3 import java.util.*; 4 import rero.util.hidden.MyTokenizer; 5 6 public class TokenizedString 7 { 8 protected String text; 9 10 protected String tokenDelimeter; 11 protected String stringToken[]; 12 protected String tokenUpCache[]; 13 protected String tokenDownCache[]; 14 15 protected int totalTokens; 16 17 public String getString() 18 { 19 return text; 20 } 21 22 public TokenizedString(String str) 23 { 24 text = str; 25 } 26 27 public TokenizedString(String str, String delim) 28 { 29 text = str; 30 tokenize(delim); 31 } 32 33 public int tokenize(String delim) 34 { 35 MyTokenizer erect = new MyTokenizer(text, delim); 36 stringToken = new String [erect.countTokens()]; 37 tokenUpCache = new String [erect.countTokens()]; 38 tokenDownCache = new String [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 52 public String getTokenRange(int a, int b) 53 { 54 if (a == 0) { 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 startOfRange = getTokenTo(a); 70 String endOfRange = getTokenTo(b + 1); 73 int lenStart, lenEnd, lenDelim, lenText; 74 lenStart = startOfRange.length(); 75 lenEnd = endOfRange.length(); 76 lenDelim = tokenDelimeter.length(); 77 lenText = text.length(); 78 79 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 91 public String getTokenTo(int x) 92 { 93 if (x >= tokenDownCache.length) { 95 return text; 96 } 97 98 99 if (tokenDownCache[x] == null) 100 { 101 tokenDownCache[x] = gettokdn(text, x, tokenDelimeter); 102 } 103 return tokenDownCache[x]; 104 } 105 106 107 public String getTokenFrom(int x) 108 { 109 if (x >= tokenDownCache.length) { 111 return ""; 112 } 113 114 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 text, String 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 137 public String getToken(int x) 138 { 139 140 if (x < totalTokens) 141 { 142 return stringToken[x]; 143 } 144 return null; 145 } 146 147 public String toString() 148 { 149 String 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 gettokup(String text, int occurence, String delim) 164 { 165 if (occurence == 0) { 167 return text; 168 } 169 170 String 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 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 gettokdn(String text, int occurence, String delim) 195 { 196 if (occurence == 0) { 198 return ""; 199 } 200 201 int occ = 0; 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 token) 228 { 229 if (findToken(token) >= 0) 230 { 231 return true; 232 } 233 return false; 234 } 235 236 public int findToken(String 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 |