FileDocCategorySizeDatePackage
ListServlets2.javaAPI DocExample2181Sat Jan 24 10:44:40 GMT 2004je3.xml

ListServlets2

public class ListServlets2 extends Object
Parse a web.xml file using the TAX pull parser and print out the servlet name-to-class and name-to-url mappings.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

	// Create a TAX.Parser instance to parse the specified file
	TAX.Parser parser = new TAX.Parser(new FileReader(args[0]));
	// By default the parser returns TAG, TEXT and ENDTAG tokens and
	// skips others. Configure it to skip ENDTAG tokens, too.
	parser.ignoreTokens(TAX.ENDTAG);

	// Now loop through all tokens until the end of the file
	for(TAX.Token t = parser.next(); t != null; t = parser.next()) {
	    // If it is not a tag, we're not interested
	    if (t.type() != TAX.TAG) continue;
	    
	    if (t.text().equals("servlet")) {   // We found <servlet>
		parser.expect("servlet-name");  // Require <servlet-name> next
		t = parser.expect(TAX.TEXT);    // Require text token next
		String name = t.text();         // Get text from the token
		parser.expect("servlet-class"); // Require <servlet-class> next
		t = parser.expect(TAX.TEXT);    // Require text token 
		// Output name to class mapping
		System.out.println("Servlet " + name + 
				   " implemented by " + t.text());
	    }
	    else if (t.text().equals("servlet-mapping")) {
		// Now we do the same thing for <servlet-mapping> tags
		parser.expect("servlet-name");
		String name = parser.expect(TAX.TEXT).text();
		parser.expect("url-pattern");
		String mapping = parser.expect(TAX.TEXT).text();
		System.out.println("Servlet " + name + 
				   " mapped to " + mapping);
	    }
	}