package javajaxb;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
// SAX for ErrorHandler
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
// Generated web.xml classes
import javajaxb.generated.web.*;
public class WebAppDisplayer {
/** The descriptor to read in */
private File descriptor;
/** The output file to write to */
private File outputFile;
/** The object tree read in */
private WebApp webApp;
/**
* <p>
* This takes in the descriptor to be processed.
* </p>
*
* @param descriptor the file for the web.xml to process
* @param outputFile the file to write changes to.
*/
public WebAppDisplayer(File descriptor, File outputFile) {
this.descriptor = descriptor;
this.outputFile = outputFile;
}
/**
* <p>
* This will display some basic information about the
* web.xml deployment descriptor.
* </p>
*
* @param validate whether or not to validate the descriptor when processing
*/
public void display(boolean validate) throws IOException {
WebAppUnmarshaller.setErrorHandler(new CommandLineErrorHandler());
System.out.print("\n\nProcessing ");
if (validate) {
System.out.print("and Validating");
}
System.out.println("...");
webApp = WebAppUnmarshaller.unmarshal(descriptor, validate);
System.out.println("\nProcessed Web XML...");
// Display some information
System.out.println("Application Display Name: " + webApp.getDisplayName());
System.out.println("Application Display Name: " + webApp.getDescription());
System.out.println("Number of servlets: " +
webApp.getServletList().size() + "\n");
// List the servlets
System.out.println("Listing servlets...");
for (Iterator i = webApp.getServletList().iterator(); i.hasNext(); ) {
Servlet servlet = (Servlet)i.next();
System.out.println(" * Servlet name: " + servlet.getServletName());
System.out.println(" * Servlet class: " +
servlet.getServletClass() + "\n");
}
}
/**
* <p>
* This will make some simple changes to the descriptor and write it back
* out to a new file.
* </p>
*/
public void modify() throws IOException {
// Change the encoding
webApp.setOutputEncoding("ISO-8859-1");
// Change the DTD to a local version
webApp.setDocType("web-app", null, "dtds/sun/web-app_2_2.dtd");
// Modify the display name
webApp.setDisplayName(webApp.getDisplayName() +
" [Modified by WebAppDisplayer]");
// Add a new servlet
Servlet servlet = new ServletImpl();
servlet.setServletName("WelcomeServlet");
servlet.setServletClass("javajaxb.servlet.WelcomeServlet");
webApp.addServlet(servlet);
// marshal
webApp.marshal(outputFile);
}
public static void main(String[] args) {
try {
if (args.length != 2) {
System.out.println("Usage: java javajaxb.WebAppDisplayer " +
"[web.xml filename] [output.xml filename]");
return;
}
WebAppDisplayer displayer =
new WebAppDisplayer(new File(args[0]), new File(args[1]));
displayer.display(true);
displayer.modify();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* <p>
* <code>CommandLineErrorHandler</code> is a simple SAX
* <code>ErrorHandler</code> that prints out errors and fatal errors to
* the command line. Warnings are ignored.
* </p>
*/
class CommandLineErrorHandler implements ErrorHandler {
/**
* <p>
* Indicates a warning has occurred.
* </p>
*
* @param e the parsing exception that triggered this warning.
* @throws <code>SAXException</code> - allows for the exception to be
* re-thrown.
*/
public void warning(SAXParseException e) throws SAXException {
// No action... warnings are OK
}
/**
* <p>
* Indicates an error has occurred.
* </p>
*
* @param e the parsing exception that triggered this error.
* @throws <code>SAXException</code> - allows for the exception to be
* re-thrown.
*/
public void error(SAXParseException e) throws SAXException {
System.out.println("Error occurred: " + e.getMessage());
throw e;
}
/**
* <p>
* Indicates a fatal error has occurred.
* </p>
*
* @param e the parsing exception that triggered this fatal error.
* @throws <code>SAXException</code> - allows for the exception to be
* re-thrown.
*/
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("Fatal error occurred: " + e.getMessage());
throw e;
}
} |