Top 10 in 2008
5 popular archives:
All selections are based on page views.
JW hot topic: Tech careers in a slump
Seems like new layoffs are announced every week, projects are dying and software developers are feeling the IT budget squeeze.
Being nervous isn't a crime, but you're better off with information, advice, and a plan.
From the IDG News Network:
Page 2 of 6
The last example from Part 1 employed a simple interface for our own handler classes:
public interface EndElementHandler
{
public void endElement(String lname, String content);
}
Since that code set the handler directly and used only element content, just one simple method was necessary. Now we want
to handle attributes as well as content. We also want to nest handlers -- have one handler pass off control to a separate
handler for processing a substructure, like the tracking information in our revised document. Figure 1 shows how this should
work when we're processing an option-trade element, for example.

Figure 1. Stackable handlers in action: tracking element within the stock-trade element
We'll need a more complex interface to handle these requirements; it'll need support for attributes and some way to set a
nested handler. Here's the definition of the StructureHandler interface we'll use for this purpose:
public interface StructureHandler
{
// Start of the root element in the structure being handled.
public void startElement(String lname, Attributes attrs);
// End of the root element in the structure being handled.
public void endElement(String lname, String content);
// Start of child element in the structure being handled -- this can
// invoke a nested handler, by passing back a non-null value.
public StructureHandler startChild(String lname, Attributes attrs);
// End of child element handled directly.
public void endDirectChild(String lname, String content);
// End of child element handled by nested handler.
public void endStructureChild(String lname, StructureHandler handler);
}
The above interface gives us the information necessary for handling our new, more complicated document format. The startElement() method call informs us that we're beginning our handling and gives a convenient hook for any initialization code. The endElement() method call then informs us when we're finished.
The startChild() method supplies the information for a child element start tag, and gives us the choice of handling it directly (by returning
null) or invoking a nested handler (by returning the handler instance). If we handle the child directly, we'll get a call to endDirectChild() on the end tag; if we invoke a nested handler, we'll get a call on endStructureChild() on the end tag. If we use the nested handler, we won't be called for anything between the start and end of the child element
-- the nested handler will instead be used for any contained children.
We want several classes to implement the StructureHandler interface; most won't actually use all the methods, though. To simplify our later code, we can define a simple base class
with dummy interface-method implementations. Our other classes can then subclass that base and override only those methods
they actually need to use.
Here's the base class implementation:
public class StructureHandlerBase implements StructureHandler
{
public void startElement(String lname, Attributes attrs) {}
public void endElement(String lname, String content) {}
public StructureHandler startChild(String lname, Attributes attrs) {
return null;
}
public void endDirectChild(String lname, String content) {}
public void endStructureChild(String lname, StructureHandler handler) {}
}
Not the most sophisticated code in the world, but it saves us duplicating these dummy methods in classes that don't need them.