HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
PROFESSIONAL XML PART 5 - THE RULE-BASED DESIGN PATTERN

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

An alternative way of structuring a SAX application, which again has the objective of separating functions and keeping the structure modular and simple, is a rule-based approach. In general rule-based programs use an "Event-Condition-Action" model: they contain a collection of rules of the form "if this event occurs under these conditions, perform this action". Rule based programming can thus be seen as a natural extension of event-based programming.


This free tutorial is a sample from the book Professional XML.


The processing model of XSL (discussed in Chapter 9) can be seen as an example of rule-based programming. Each XSL template constitutes one rule: the event is the processing of a node in the source document; the condition is the pattern that controls which template is activated, and the action is the body of the template. We can use the same concepts in a SAX application.

The diagram below illustrates the structure of a rule-based SAX application. The input from the XML parser is fed into a switch, which evaluates the events against the defined conditions, and decides which actions to invoke. The actions are then passed to processing modules each of which is designed to perform one specific task.

There are all sorts of ways conditions and actions could be implemented, but we'll describe a very simple implementation, where the condition is based only on element type.

Firstly, let's write the DocumentHandler. We'll call it Switcher because its job is to switch processing to a piece of code that handles the specific element type.

What Switcher does is to maintain a set of rules as a Hashtable. The set of rules is indexed by element type. The application can nominate a class called an ElementHandler to process a particular element type. When the parser notifies an element start tag, the appropriate ElementHandler is located in the set of rules, and it is called to process the start tag. At the same time, the ElementHandler is remembered on a stack, so that the same ElementHandler can be used to process the end tag and any character data occurring immediately within this element.

Here’s the Switcher code:

import org.xml.sax.*;
import java.util.*;

/**
* Switcher is a DocumentHandler that directs events to 
* an appropriate element handler based on the element type.
*/

public class Switcher extends HandlerBase 
{

 private Hashtable rules = new Hashtable();
 private Stack stack = new Stack();

 /**
 * Define processing for an element type.
 */

 public void setElementHandler(String name, ElementHandler handler) 
 {
   rules.put(name, handler);
 }
 
 /**
 * Start of an element. Decide what handler to use, and call it.
 */
 
 public void startElement (String name, AttributeList atts) throws 
                            SAXException 
 {
   ElementHandler handler = (ElementHandler)rules.get(name);
   stack.push(handler);
   if (handler!=null) 
   {
     handler.startElement(name, atts);
   }
 }

 /**
 * End of an element.
 */

 public void endElement (String name) throws SAXException 
 {
   ElementHandler handler = (ElementHandler)stack.pop();
   if (handler!=null) 
   {
     handler.endElement(name);
   }   
 }

 /**
 * Character data.
 */
 
 public void characters (char[] ch, int start, int length) 
  throws SAXException
 {
   ElementHandler handler = (ElementHandler)stack.peek();
   if (handler!=null) 
   {
     handler.characters(ch, start, length);
   }  
 }

}

An ElementHandler is rather like a DocumentHandler, but it only ever gets to process a subset of the events: element start and end, and character data. So although we could use a DocumentHandler here, we've defined a special class. This serves both as a definition of the interface and as a superclass for real element handlers: good Java coding practice might suggest using a separate interface class, but this will do for now.

import org.xml.sax.*;

/**
* ElementHandler is a class that process the start and end tags and 
* character data
* for one element type. This class itself does nothing; the 
* real processing should
* be defined in a subclass
*/

public class ElementHandler {

 /**
 * Start of an element
 */
 
 public void startElement (String name, AttributeList atts) throws 
                            SAXException {}

 /**
 * End of an element
 */

 public void endElement (String name) throws SAXException {}

 /**
 * Character data
 */
 
 public void characters (char[] ch, int start, int length) throws 
                            SAXException {}

}

So far this is all completely general. We could use the Switcher and ElementHandler classes with any kind of document, to do any kind of processing. Now let's exploit them for a real application: we want to produce an HTML page showing selected data from our list of books.

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
A+ MODULE 5 - THE INTERNET
At the end of this course you will be able to: describe the functions of an operating system, describe the featur....
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
I-NET+ MODULE 8 - DEVELOPING A WEB SITE
On completion of this module, readers will be able to: create HTML pages incorporating different document-, parag....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 8th January 2009  © COPYRIGHT 2009 - VISUALSOFT