04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

430 CHAPTER 22 ■ PROJECT 3: XML FOR ALL OCCASIONS<br />

Figure 22-1. A generated Web page<br />

Second Implementation<br />

Because the SAX mechanism is so low-level and basic, you may often find it useful to write a<br />

mix-in class that handles some administrative details such as gathering character data, managing<br />

Boolean state variables (such as passthrough), or dispatching the events to your own custom<br />

event handlers. The state and data handling is pretty simple in this project, so let’s focus on the<br />

handler dispatch.<br />

A Dispatcher Mix-In Class<br />

Rather than having to write large if statements in the standard generic event handlers (such as<br />

startElement), it would be nice to just write your own specific ones (such as startPage) and<br />

have them called automatically. You can implement that functionality in a mix-in class, and<br />

then subclass the mix-in along with ContentHandler.<br />

■Note As mentioned in Chapter 7, a mix-in is a class with limited functionality that is meant to be subclassed<br />

along with some other more substantial class.<br />

You want the following functionality in your program: When startElement is called with a<br />

name such as 'foo', it should attempt to find an event handler called startFoo and call it with<br />

the given attributes. Similarly, if endElement is called with 'foo', it should try to call endFoo. If,<br />

in any of these methods, the given handler is not found, a method called defaultStart (or<br />

defaultEnd, respectively) will be called, if present. If the default handler isn’t present either,<br />

nothing should be done.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!