1 27 package org.htmlparser.lexer; 28 29 import java.io.IOException ; 30 import org.htmlparser.util.ParserException; 31 32 35 public class StringSource 36 extends 37 Source 38 { 39 42 protected String mString; 43 44 47 protected int mOffset; 48 49 53 protected String mEncoding; 54 55 58 protected int mMark; 59 60 65 public StringSource (String string) 66 { 67 this (string, "ISO-8859-1"); 68 } 69 70 76 public StringSource (String string, String character_set) 77 { 78 mString = (null == string) ? "" : string; 79 mOffset = 0; 80 mEncoding = character_set; 81 mMark = -1; 82 } 83 84 88 public String getEncoding () 89 { 90 return (mEncoding); 91 } 92 93 99 public void setEncoding (String character_set) 100 throws 101 ParserException 102 { 103 mEncoding = character_set; 104 } 105 106 110 115 public void close () throws IOException 116 { 117 } 118 119 125 public int read () throws IOException 126 { 127 int ret; 128 129 if (null == mString) 130 throw new IOException ("source is closed"); 131 else if (mOffset >= mString.length ()) 132 ret = EOF; 133 else 134 { 135 ret = mString.charAt (mOffset); 136 mOffset++; 137 } 138 139 return (ret); 140 } 141 142 151 public int read (char[] cbuf, int off, int len) throws IOException 152 { 153 int length; 154 int ret; 155 156 if (null == mString) 157 throw new IOException ("source is closed"); 158 else 159 { 160 length = mString.length (); 161 if (mOffset >= length) 162 ret = EOF; 163 else 164 { 165 if (len > length - mOffset) 166 len = length - mOffset; 167 mString.getChars (mOffset, mOffset + len, cbuf, off); 168 mOffset += len; 169 ret = len; 170 } 171 } 172 173 return (ret); 174 } 175 176 183 184 public int read (char[] cbuf) throws IOException 185 { 186 return (read (cbuf, 0, cbuf.length)); 187 } 188 189 195 public boolean ready () throws IOException 196 { 197 if (null == mString) 198 throw new IOException ("source is closed"); 199 return (mOffset < mString.length ()); 200 } 201 202 207 public void reset () 208 { 209 if (null == mString) 210 throw new IllegalStateException ("source is closed"); 211 else 212 if (-1 != mMark) 213 mOffset = mMark; 214 else 215 mOffset = 0; 216 } 217 218 222 public boolean markSupported () 223 { 224 return (true); 225 } 226 227 235 public void mark (int readAheadLimit) throws IOException 236 { 237 if (null == mString) 238 throw new IOException ("source is closed"); 239 mMark = mOffset; 240 } 241 242 250 public long skip (long n) throws IOException 251 { 252 int length; 253 long ret; 254 255 if (null == mString) 256 throw new IOException ("source is closed"); 257 if (n < 0) 258 throw new IllegalArgumentException ("cannot skip backwards"); 259 else 260 { 261 length = mString.length (); 262 if (mOffset >= length) 263 n = 0L; 264 else if (n > length - mOffset) 265 n = length - mOffset; 266 mOffset += n; 267 ret = n; 268 } 269 270 return (ret); 271 } 272 273 277 281 public void unread () throws IOException 282 { 283 if (null == mString) 284 throw new IOException ("source is closed"); 285 else if (mOffset <= 0) 286 throw new IOException ("can't unread no characters"); 287 else 288 mOffset--; 289 } 290 291 298 public char getCharacter (int offset) throws IOException 299 { 300 char ret; 301 302 if (null == mString) 303 throw new IOException ("source is closed"); 304 else if (offset >= mOffset) 305 throw new IOException ("read beyond current offset"); 306 else 307 ret = mString.charAt (offset); 308 309 return (ret); 310 } 311 312 323 public void getCharacters (char[] array, int offset, int start, int end) throws IOException 324 { 325 if (null == mString) 326 throw new IOException ("source is closed"); 327 else 328 { 329 if (end > mOffset) 330 throw new IOException ("read beyond current offset"); 331 else 332 mString.getChars (start, end, array, offset); 333 } 334 } 335 336 345 public String getString (int offset, int length) throws IOException 346 { 347 String ret; 348 349 if (null == mString) 350 throw new IOException ("source is closed"); 351 else 352 { 353 if (offset + length > mOffset) 354 throw new IOException ("read beyond end of string"); 355 else 356 ret = mString.substring (offset, offset + length); 357 } 358 359 return (ret); 360 } 361 362 371 public void getCharacters (StringBuffer buffer, int offset, int length) throws IOException 372 { 373 if (null == mString) 374 throw new IOException ("source is closed"); 375 else 376 { 377 if (offset + length > mOffset) 378 throw new IOException ("read beyond end of string"); 379 else 380 buffer.append (mString.substring (offset, offset + length)); 381 } 382 } 383 384 394 public void destroy () throws IOException 395 { 396 mString = null; 397 } 398 399 404 public int offset () 405 { 406 int ret; 407 408 if (null == mString) 409 ret = EOF; 410 else 411 ret = mOffset; 412 413 return (ret); 414 } 415 416 421 public int available () 422 { 423 int ret; 424 425 if (null == mString) 426 ret = 0; 427 else 428 ret = mString.length () - mOffset; 429 430 return (ret); 431 } 432 } 433 | Popular Tags |