Java Reference
In-Depth Information
Once the attribute name has been read, we must check to see if there is an attribute
value. If there is an attribute value the next character will be an equal sign. If an equal sign is
present, then read the attribute value.
// is there a value?
eatWhitespace();
if (source.peek() == '=')
{
source.read();
attributeValue = parseString();
}
Once the attribute has been read the attribute value is set. If there is no attribute value,
then the attribute will be set to null .
tag.setAttribute(attributeName, attributeValue);
}
source.read();
Once the tag name, and all attributes have been read, call source.read() to read
past the ending greater than sign.
Parse an Attribute Name with parseAttributeName
The parseAttributeName function is called to parse the name of an attribute.
This function begins by checking to see if there is a single or double quote around the attri-
bute name. As an example of a HTML tag that has an attribute with a double quote, consider
the following tag.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
The above tag contains two name-only attributes: HTML and PUBLIC . Additionally, it
contains a third double quote-delineated attribute.
The parseAttributeName function uses the peek function to determine if the
attribute name is enclosed in either single or double quotes. If the name is not quoted, then
create a StringBuilder object to hold the attribute name.
eatWhitespace();
if ("\"\'".indexOf(source.peek()) == -1)
{
StringBuilder buffer = new StringBuilder();
If the attribute name is not delineated, then read the attribute name until either an equals
sign or a tag ending greater-than sign is encountered.
while (!Character.isWhitespace(source.peek())
&& source.peek() != '='
&& source.peek() != '>' && (source.peek() != -1))
Search WWH ::




Custom Search