1 63 64 65 package org.jahia.services.applications; 66 67 68 79 80 public final class StringParser { 81 82 83 85 86 89 public StringParser () { 90 91 this (null); 92 93 } 94 95 96 102 public StringParser (String string) { 103 104 super (); 105 setString (string); 106 107 } 108 109 110 112 113 118 private char chars[] = null; 119 120 121 129 private int index = 0; 130 131 132 136 private int length = 0; 137 138 139 140 private String string = null; 141 142 143 145 146 150 public int getIndex () { 151 152 return (this.index); 153 154 } 155 156 157 160 public int getLength () { 161 162 return (this.length); 163 164 } 165 166 167 170 public String getString () { 171 172 return (this.string); 173 174 } 175 176 177 183 public void setString (String string) { 184 185 this.string = string; 186 if (string != null) { 187 this.length = string.length (); 188 chars = this.string.toCharArray (); 189 } else { 190 this.length = 0; 191 chars = new char[0]; 192 } 193 reset (); 194 195 } 196 197 198 200 201 205 public void advance () { 206 207 if (index < length) 208 index++; 209 210 } 211 212 213 220 public String extract (int start) { 221 222 if ((start < 0) || (start >= length)) 223 return (""); 224 else 225 return (string.substring (start)); 226 227 } 228 229 230 238 public String extract (int start, int end) { 239 240 if ((start < 0) || (start >= end) || (end > length)) 241 return (""); 242 else 243 return (string.substring (start, end)); 244 245 } 246 247 248 256 public int findChar (char ch) { 257 258 while ((index < length) && (ch != chars[index])) 259 index++; 260 return (index); 261 262 } 263 264 265 271 public int findText () { 272 273 while ((index < length) && isWhite (chars[index])) 274 index++; 275 return (index); 276 277 } 278 279 280 286 public int findWhite () { 287 288 while ((index < length) && !isWhite (chars[index])) 289 index++; 290 return (index); 291 292 } 293 294 295 299 public void reset () { 300 301 index = 0; 302 303 } 304 305 306 313 public int skipChar (char ch) { 314 315 while ((index < length) && (ch == chars[index])) 316 index++; 317 return (index); 318 319 } 320 321 322 327 public int skipText () { 328 329 while ((index < length) && !isWhite (chars[index])) 330 index++; 331 return (index); 332 333 } 334 335 336 341 public int skipWhite () { 342 343 while ((index < length) && isWhite (chars[index])) 344 index++; 345 return (index); 346 347 } 348 349 350 352 353 358 protected boolean isWhite (char ch) { 359 360 if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n')) 361 return (true); 362 else 363 return (false); 364 365 } 366 367 368 } 369 | Popular Tags |