Java Reference
In-Depth Information
The peek function of the PeekableInputStream is very handy for eliminating
white space. By peeking ahead, and seeing if the next character is white space or not, you can
decide if you need to remove it.
while (Character.isWhitespace((char) source.peek()))
{
source.read();
}
As you can see, from the above code, white space characters are read, and thus removed,
one by one, until peek finds a non-white space character.
Parse a String with parseString
Strings occur often inside of HTML documents, particularly when used with HTML attri-
butes. For example, consider the following HTML tags, all of which have the same meaning.
<img src="/images/logo.gif">
<img src='/images/logo.gif'>
<img src=/images/logo.gif>
The first line is the most common. It uses double quotes to delineate the string value.
The second uses single quotes. Though not the preferred method, the third uses no delimiter
at all. All three methods are common in HTML, so the ParseHTML class uses a function,
named parseString that handles all three.
First, the parseString method creates a StringBuilder to hold the parsed
string. Next the parseString method checks to see if there is a leading delimiter, which
could be either a single or double quote.
StringBuilder result = new StringBuilder();
if ("\"\'".indexOf(source.peek()) != -1)
{
To read in the delimited string, characters are read in until we reach the end of the string,
or the end of the file. The parseSpecialCharacter function is used to convert any
special HTML characters.
int delim = source.read();
while (source.peek() != delim && source.peek() != -1)
{
result.append(this.parseSpecialCharacter());
}
Next, read the ending delimiter, if present. If end of file was found first, the ending delim-
iter might not be present.
if ("\"\'".indexOf(source.peek()) != -1)
source.read();
Search WWH ::




Custom Search