1 17 18 19 package org.apache.catalina.util; 20 21 22 33 34 public final class StringParser { 35 36 37 39 40 43 public StringParser() { 44 45 this(null); 46 47 } 48 49 50 56 public StringParser(String string) { 57 58 super(); 59 setString(string); 60 61 } 62 63 64 66 67 72 private char chars[] = null; 73 74 75 83 private int index = 0; 84 85 86 90 private int length = 0; 91 92 93 96 private String string = null; 97 98 99 101 102 106 public int getIndex() { 107 108 return (this.index); 109 110 } 111 112 113 116 public int getLength() { 117 118 return (this.length); 119 120 } 121 122 123 126 public String getString() { 127 128 return (this.string); 129 130 } 131 132 133 139 public void setString(String string) { 140 141 this.string = string; 142 if (string != null) { 143 this.length = string.length(); 144 chars = this.string.toCharArray(); 145 } else { 146 this.length = 0; 147 chars = new char[0]; 148 } 149 reset(); 150 151 } 152 153 154 156 157 161 public void advance() { 162 163 if (index < length) 164 index++; 165 166 } 167 168 169 176 public String extract(int start) { 177 178 if ((start < 0) || (start >= length)) 179 return (""); 180 else 181 return (string.substring(start)); 182 183 } 184 185 186 194 public String extract(int start, int end) { 195 196 if ((start < 0) || (start >= end) || (end > length)) 197 return (""); 198 else 199 return (string.substring(start, end)); 200 201 } 202 203 204 212 public int findChar(char ch) { 213 214 while ((index < length) && (ch != chars[index])) 215 index++; 216 return (index); 217 218 } 219 220 221 227 public int findText() { 228 229 while ((index < length) && isWhite(chars[index])) 230 index++; 231 return (index); 232 233 } 234 235 236 242 public int findWhite() { 243 244 while ((index < length) && !isWhite(chars[index])) 245 index++; 246 return (index); 247 248 } 249 250 251 255 public void reset() { 256 257 index = 0; 258 259 } 260 261 262 269 public int skipChar(char ch) { 270 271 while ((index < length) && (ch == chars[index])) 272 index++; 273 return (index); 274 275 } 276 277 278 283 public int skipText() { 284 285 while ((index < length) && !isWhite(chars[index])) 286 index++; 287 return (index); 288 289 } 290 291 292 297 public int skipWhite() { 298 299 while ((index < length) && isWhite(chars[index])) 300 index++; 301 return (index); 302 303 } 304 305 306 308 309 314 protected boolean isWhite(char ch) { 315 316 if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n')) 317 return (true); 318 else 319 return (false); 320 321 } 322 323 324 } 325 | Popular Tags |