The JobListHandler constructor O takes a single argument of Handler. This value may be null. If null, Message passing is omitted from operation, as we will show. When reading from a local storage file, this Handler argument is null. When reading data from the server over the internet, with a potentially slow connection, the Message-passing code is utilized to provide feedback for the user in the form of a Progress-Dialog. The ProgressDialog code is shown later in this chapter in the discussion of the RefreshJobs Activity. A local copy of the Handler Q is set up when using the ProgressDialog, as described in O.

The getList() D method is invoked when parsing is complete. The role of getList is to return a copy of the JobList that was constructed during the parse process. When the startDocument() callback method © is invoked by the SAX parser, the initial class instances are established. The endDocument() method © is invoked by the SAX parser when all of the document has been consumed. This is an opportunity for the Handler to perform additional cleanup as necessary. In our example, a message is posted to the user by sending a Message.

For each element in the XML file, the SAX parser follows the same pattern: startElement is invoked, characters() is invoked (one or more times), and endElement is invoked. In the startElement method, we initialize our StringBuilder and evaluate the element name. If the name is "job," we initialize our class-level JobEntry instance.

In the endElement() method, the element name is evaluated. If the element name is "job" G, the JobListHandler adds this JobEntry to the JobList data member, _joblist with a call to addJob(). Also in the endElement() method, the data members of the JobEntry instance (_job) are updated. Please see the full source code for more details.

The characters() method is invoked by the SAX parser whenever data is available for storage. The JobListHandler simply appends this string data to a StringBuilder instance H each time it is invoked. It is possible that the characters method is invoked more than once for a particular element's data. That is the rationale behind using a StringBuilder instead of a single String variable; StringBuilder is a more efficient class for constructing strings from multiple substrings.

After this lengthy but important look into the data structures and the accompanying explanations, it is time to return to the higher-level functionality of the application.

0 0

Post a comment