Java Reference
In-Depth Information
} else
{
buffer.append(key);
buffer.append("=\"");
buffer.append(value);
buffer.append("\"");
}
}
if (this.ending)
{
buffer.append('/');
}
buffer.append(">");
return buffer.toString();
}
}
The HTML tag class contains two properties, which are used to hold the HTML tag.
• attributes
• name
The
attributes
variable contains a Map, which holds all of the name value pairs
that make up the HTML attributes. The
name
attribute contains a
String
that holds the
name of the HTML tag.
Most of the code in Listing 6.3 is contained in the
toString
function. The
toString
function is responsible for converting this
HTMLTag
object back into a textual HTML tag.
The first thing that the
toString
function does is to create a
StringBuilder
to
hold the textual tag, as it is created. The
StringBuilder
object begins with a less-than
character and then the tag name.
StringBuilder buffer = new StringBuilder("<");
buffer.append(name);
Next, a loop is entered to display each of the attributes. The attribute's value is read into
a
String
object, named
value
. A leading space is placed in front of each attribute.
Set<String> set = attributes.keySet();
for (String key : set)
{
String value = attributes.get(key);
buffer.append(' ');