1 9 package javolution.text; 10 11 import j2me.lang.CharSequence; 12 import j2me.text.ParsePosition; 13 import javolution.context.ObjectFactory; 14 15 import java.io.IOException ; 16 17 54 public abstract class TextFormat{ 55 56 59 protected TextFormat() { 60 } 61 62 70 public abstract Appendable format(Object obj, Appendable dest) 71 throws IOException ; 72 73 85 public abstract Object parse(CharSequence csq, Cursor cursor); 86 87 94 public final Text format(Object obj) { 95 try { 96 TextBuilder tb = TextBuilder.newInstance(); 97 format(obj, tb); 98 return tb.toText(); 99 } catch (IOException e) { 100 throw new Error (); } 102 } 103 104 113 public final Object parse(CharSequence csq) { 114 Cursor cursor = Cursor.newInstance(0, csq.length()); 115 Object obj = parse(csq, cursor); 116 if (cursor.hasNext()) 117 throw new IllegalArgumentException ("Incomplete Parsing"); 118 Cursor.recycle(cursor); 119 return obj; 120 } 121 122 128 public static class Cursor extends ParsePosition { 129 130 133 private static final ObjectFactory FACTORY = new ObjectFactory() { 134 public Object create() { 135 return new Cursor(); 136 } 137 }; 138 139 142 private int _index; 143 144 147 private int _start; 148 149 152 private int _end; 153 154 157 private Cursor() { 158 super(0); 159 } 160 161 170 public static Cursor newInstance(int start, int end) { 171 Cursor cursor = (Cursor) FACTORY.object(); 172 cursor._start = cursor._index = start; 173 cursor._end = end; 174 cursor.setErrorIndex(-1); 175 return cursor; 176 } 177 178 185 public static void recycle(Cursor instance) { 186 FACTORY.recycle(instance); 187 } 188 189 194 public final int getIndex() { 195 return _index; 196 } 197 198 203 public final int getStartIndex() { 204 return _start; 205 } 206 207 212 public final int getEndIndex() { 213 return _end; 214 } 215 216 223 public final int getErrorIndex() { 224 int errorIndex = this.getErrorIndex(); 225 return errorIndex >= 0 ? errorIndex : _index; 226 } 227 228 235 public final void setIndex(int i) { 236 if ((i < _start) || (i > _end)) 237 throw new IllegalArgumentException (); 238 _index = i; 239 } 240 241 246 public final void setStartIndex(int start) { 247 _start = start; 248 } 249 250 255 public final void setEndIndex(int end) { 256 _end = end; 257 } 258 259 264 public final void setErrorIndex(int errorIndex) { 265 super.setErrorIndex(errorIndex); 266 } 267 268 273 public final boolean hasNext() { 274 return _index < _end; 275 } 276 277 291 public final char next(CharSequence csq) { 292 return (_index < _end) ? csq.charAt(_index++) : 0; 293 } 294 295 304 public final boolean at(char c, CharSequence csq) { 305 return (_index < _end) && (csq.charAt(_index) == c); 306 } 307 308 317 public final boolean at(CharSet charSet, CharSequence csq) { 318 return (_index < _end) && (charSet.contains(csq.charAt(_index))); 319 } 320 321 330 public final boolean at(String pattern, CharSequence csq) { 331 return (_index < _end) && (csq.charAt(_index) == pattern.charAt(0)) ? match( 332 pattern, csq) 333 : false; 334 } 335 336 private final boolean match(String pattern, CharSequence csq) { 337 for (int i = 1, j = _index + 1, n = pattern.length(), m = _end; i < n;) { 338 if ((j >= m) || (csq.charAt(j++) != pattern.charAt(i++))) 339 return false; 340 } 341 return true; 342 } 343 344 354 public final boolean skip(char c, CharSequence csq) { 355 while ((_index < _end) && (csq.charAt(_index) == c)) { 356 _index++; 357 } 358 return _index < _end; 359 } 360 361 377 public final boolean skip(CharSet charSet, CharSequence csq) { 378 while ((_index < _end) && (charSet.contains(csq.charAt(_index)))) { 379 _index++; 380 } 381 return _index < _end; 382 } 383 384 389 public final Cursor increment() { 390 _index++; 391 return this; 392 } 393 394 400 public final Cursor increment(int i) { 401 _index += i; 402 return this; 403 } 404 405 410 public String toString() { 411 return String.valueOf(_index); 412 } 413 414 420 public boolean equals(Object obj) { 421 if (obj == null) return false; 422 if (!(obj instanceof Cursor)) 423 return false; 424 return _index == ((Cursor)obj)._index; 425 } 426 427 432 public int hashCode() { 433 return _index; 434 } 435 436 } 437 } | Popular Tags |