Java Reference
In-Depth Information
Example 19−2: ListServlets2.java (continued)
* We simply override the methods that receive events we're interested in
**/
public class ListServlets2 extends org.xml.sax.helpers.DefaultHandler {
/** The main method sets things up for parsing */
public static void main(String[] args) throws IOException, SAXException {
// Create the parser we'll use. The parser implementation is a
// Xerces class, but we use it only through the SAX XMLReader API
org.xml.sax.XMLReader parser=new org.apache.xerces.parsers.SAXParser();
// Specify that we don't want validation. This is the SAX2
// API for requesting parser features. Note the use of a
// globally unique URL as the feature name. Non-validation is
// actually the default, so this line isn't really necessary.
parser.setFeature("http://xml.org/sax/features/validation", false);
// Instantiate this class to provide handlers for the parser and
// tell the parser about the handlers
ListServlets2 handler = new ListServlets2();
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
// Create an input source that describes the file to parse.
// Then tell the parser to parse input from that source
org.xml.sax.InputSource input=new InputSource(new FileReader(args[0]));
parser.parse(input);
}
HashMap nameToClass; // Map from servlet name to servlet class name
HashMap nameToPatterns; // Map from servlet name to url patterns
StringBuffer accumulator; // Accumulate text
String servletName, servletClass, servletPattern; // Remember text
// Called at the beginning of parsing. We use it as an init() method
public void startDocument() {
accumulator = new StringBuffer();
nameToClass = new HashMap();
nameToPatterns = new HashMap();
}
// When the parser encounters plain text (not XML elements), it calls
// this method, which accumulates them in a string buffer.
// Note that this method may be called multiple times, even with no
// intervening elements.
public void characters(char[] buffer, int start, int length) {
accumulator.append(buffer, start, length);
}
// At the beginning of each new element, erase any accumulated text.
public void startElement(String namespaceURL, String localName,
String qname, Attributes attributes) {
accumulator.setLength(0);
}
// Take special action when we reach the end of selected elements.
// Although we don't use a validating parser, this method does assume
// that the web.xml file we're parsing is valid.
public void endElement(String namespaceURL, String localName, String qname)
Search WWH ::




Custom Search