FileDocCategorySizeDatePackage
ListServlets.javaAPI DocExample6398Sat Jan 24 10:44:40 GMT 2004je3.xml

ListServlets

public class ListServlets extends DefaultHandler
Parse a web.xml file using the SAX2 API. This class extends DefaultHandler so that instances can serve as SAX2 event handlers, and can be notified by the parser of parsing events. We simply override the methods that receive events we're interested in

Fields Summary
HashMap
nameToClass
HashMap
nameToID
HashMap
nameToPatterns
StringBuffer
accumulator
String
servletName
String
servletClass
String
servletPattern
String
servletID
Constructors Summary
Methods Summary
public voidcharacters(char[] buffer, int start, int length)

	accumulator.append(buffer, start, length);
    
public voidendDocument()

	// Note the powerful uses of the Collections framework.  In two lines
	// we get the key objects of a Map as a Set, convert them to a List,
	// and sort that List alphabetically.
	List servletNames = new ArrayList(nameToClass.keySet());
	Collections.sort(servletNames);
	// Loop through servlet names
	for(Iterator iterator = servletNames.iterator(); iterator.hasNext();) {
	    String name = (String)iterator.next();
	    // For each name get class and URL patterns and print them.
	    String classname = (String)nameToClass.get(name);
	    String id = (String)nameToID.get(name);
	    List patterns = (List)nameToPatterns.get(name);
	    System.out.println("Servlet: " + name);
	    System.out.println("Class: " + classname);
	    if (id != null) System.out.println("ID: " + id);
	    if (patterns != null) {
		System.out.println("Patterns:");
		for(Iterator i = patterns.iterator(); i.hasNext(); ) {
		    System.out.println("\t" + i.next());
		}
	    }
	    System.out.println();
	}
    
public voidendElement(java.lang.String namespaceURL, java.lang.String localName, java.lang.String qname)

	// Since we've indicated that we don't want name-space aware 
	// parsing, the element name is in qname.  If we were doing
	// namespaces, then qname would include the name, colon and prefix, 
	// and localName would be the name without the the prefix or colon.
	if (qname.equals("servlet-name")) {        // Store servlet name
	    servletName = accumulator.toString().trim();
	}
	else if (qname.equals("servlet-class")) {  // Store servlet class
	    servletClass = accumulator.toString().trim();
	}
	else if (qname.equals("url-pattern")) {    // Store servlet pattern
	    servletPattern = accumulator.toString().trim();
	}
	else if (qname.equals("servlet")) {        // Map name to class
	    nameToClass.put(servletName, servletClass);
	    nameToID.put(servletName, servletID);
	}
	else if (qname.equals("servlet-mapping")) {// Map name to pattern
	    List patterns = (List)nameToPatterns.get(servletName);
	    if (patterns == null) {
		patterns = new ArrayList();
		nameToPatterns.put(servletName, patterns);
	    }
	    patterns.add(servletPattern);
	}
    
public voiderror(org.xml.sax.SAXParseException exception)

	System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
    
public voidfatalError(org.xml.sax.SAXParseException exception)

	System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
	throw(exception);
    
public static voidmain(java.lang.String[] args)
The main method sets things up for parsing

	// We use a SAXParserFactory to obtain a SAXParser, which
	// encapsulates a SAXReader.
	SAXParserFactory factory = SAXParserFactory.newInstance();
	factory.setValidating(false);     // We don't want validation
	factory.setNamespaceAware(false); // No namespaces please
        // Create a SAXParser object from the factory
	SAXParser parser = factory.newSAXParser();
        // Now parse the file specified on the command line using
	// an instance of this class to handle the parser callbacks
	parser.parse(new File(args[0]), new ListServlets());
    
public voidstartDocument()

	accumulator = new StringBuffer();
	nameToClass = new HashMap();
	nameToID = new HashMap();
	nameToPatterns = new HashMap();
    
public voidstartElement(java.lang.String namespaceURL, java.lang.String localName, java.lang.String qname, org.xml.sax.Attributes attributes)

	accumulator.setLength(0);
	// If its a servlet tag, look for id attribute
	if (qname.equals("servlet")) servletID = attributes.getValue("id");
    
public voidwarning(org.xml.sax.SAXParseException exception)

	System.err.println("WARNING: line " + exception.getLineNumber() + ": "+
			   exception.getMessage());