Encapsulating HTML Tags
When you call the getTag function of the HTML parse class, you are given an HTMLTag object. This object completely encapsulates the HTML tag that was just parsed. The HTMLTag class is shown in Listing 6.3.
Listing 6.3: HTML Tags (HTMLTag.java)
package com.heatonresearch.httprecipes.html; import java.util.*; /** * The Heaton Research Spider Copyright 2007 by Heaton * Research, Inc. * * HTTP Programming Recipes for Java ISBN: 0-9773206-6-9 * http://www.heatonresearch.com/articles/series/16/ * * HTMLTag: This class holds a single HTML tag. This class * subclasses the AttributeList class. This allows the * HTMLTag class to hold a collection of attributes, just as * an actual HTML tag does. * * This class is released under the: * GNU Lesser General Public License (LGPL) * http://www.gnu.org/copyleft/lesser.html * * @author Jeff Heaton * @version 1.1 */ public class HTMLTag { /* * The attributes */ private Map<String, String> attributes = new HashMap<String, String>(); /** * The tag name. */ private String name = ""; /* * Is this both a beginning and ending tag. */ private boolean ending; public void clear() { this.attributes.clear(); this.name = ""; this.ending = false; } /** * Get the value of the specified attribute. * * @param name * The name of an attribute. * @return The value of the specified attribute. */ public String getAttributeValue(String name) { return this.attributes.get(name.toLowerCase()); } /** * Get the tag name. */ public String getName() { return this.name; } /** * @return the ending */ public boolean isEnding() { return this.ending; } /** * Set a HTML attribute. * * @param name * The name of the attribute. * @param value * The value of the attribute. */ public void setAttribute(String name, String value) { this.attributes.put(name.toLowerCase(), value); } /** * @param ending * The ending to set. */ public void setEnding(boolean ending) { this.ending = ending; } /** * Set the tag name. */ public void setName(String s) { this.name = s; } /** * Convert this tag back into string form, with the * beginning < and ending >. * * @param id * A zero based index that specifies the * attribute to retrieve. * @return The Attribute object that was found. */ @Override public String toString() { StringBuilder buffer = new StringBuilder("<"); buffer.append(this.name); Set<String> set = this.attributes.keySet(); for (String key : set) { String value = this.attributes.get(key); buffer.append(' '); if (value == null) { buffer.append("\""); buffer.append(key); buffer.append("\""); } 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(' ');If a value is not present, then just display the key, which is the name of the attribute. The key will be enclosed in quotes.
if (value == null)
{
buffer.append("\"");
buffer.append(key);
buffer.append("\"");If a value is present, then display the key followed by an equals sign, followed by the value. The value will be enclosed in quotes.
} else
{
buffer.append(key);
buffer.append("=\"");
buffer.append(value);
buffer.append("\"");
}
}Once all of the attributes have been display, a trailing greater-than is appended to the StringBuilder object, to end the tag.
buffer.append(">");
return buffer.toString();Once the loop completes, the StringBuilder object is converted to a String, by calling its toString method. This String is returned.




