Java Reference
In-Depth Information
A straightforward implementation of these two methods requires that the
container holds a reference to the first component, and each component has
a reference to the next element. In addition it is useful to have a method to
test whether a given element is composite or not; this will make the code
that iterates on the elements simpler.
Decision point
How do elements serialize their HTML representation?
The problem is to transform a two-dimensional representation (the tree
structure of the objects) into a mono-dimensional or serialized represent-
ation (the sequence of characters that form an HTML document). Most of
the elements in HTML have an opening and a closing tag; in their serialized
form they are represented by putting the children between the opening
tag and the closing tag. The object structure can be serialized by means of
revisiting the tree.
Two methods should be provided to read the opening and closing tags:
getOpeningTag() and getClosingTag() . In the case of Text , both methods
should return an empty string. This solution can be combined with the
iteration in order to obtain a straightforward serialization method.
The Page class defines a serialization method that is able to serialize the
entire web page. The serialization procedure is fairly simple thanks to the
methods provided by class Element . For each element the serialization
should start by printing the opening tag and terminate by printing the
closing tag. In between, if the element is composed, the procedure should
iterate on all the contained elements and print them. Since each contained
element should be printed using the same procedure, a recursive procedure
fits our needs very well. The pseudocode for such a method is the following:
serialize_all (Element current ){
print( current.getOpeningTag() );
if ( current.isComposite() ){
Element child # firstSibling;
while (child! # null ){
serialize (child);
child # child.nextSibling();
}
}
print( current.getClosingTag() )
}
This solution would work fine for all elements except Text: it has neither
an opening nor a closing tag, but it has a different kind of information that
needs to be serialized. A simple and smart solution to this problem is to
Search WWH ::




Custom Search