FileDocCategorySizeDatePackage
ErrorDispatcher.javaAPI DocGlassfish v2 API18144Fri May 04 22:32:52 BST 2007org.apache.jasper.compiler

ErrorDispatcher

public class ErrorDispatcher extends Object
Class responsible for dispatching JSP parse and javac compilation errors to the configured error handler. This class is also responsible for localizing any error codes before they are passed on to the configured error handler. In the case of a Java compilation error, the compiler error message is parsed into an array of JavacErrorDetail instances, which is passed on to the configured error handler.
author
Jan Luehe
author
Kin-man Chung

Fields Summary
private static final ResourceBundle
bundle
private ErrorHandler
errHandler
private boolean
jspcMode
Constructors Summary
public ErrorDispatcher(boolean jspcMode)



    /*
     * Constructor.
     *
     * @param jspcMode true if compilation has been initiated by JspC, false
     * otherwise
     */
       
	// XXX check web.xml for custom error handler
	errHandler = new DefaultErrorHandler();
        this.jspcMode = jspcMode;
    
Methods Summary
public static JavacErrorDetailcreateJavacError(java.lang.String fname, Node.Nodes page, java.lang.StringBuffer errMsgBuf, int lineNum)

param
fname
param
page
param
errMsgBuf
param
lineNum
return
JavacErrorDetail The error details
throws
JasperException


        JavacErrorDetail javacError;
        // Attempt to map javac error line number to line in JSP page
        ErrorVisitor errVisitor = new ErrorVisitor(lineNum);
        if (page != null)
            page.visit(errVisitor);
        Node errNode = errVisitor.getJspSourceNode();
        if ((errNode != null) && (errNode.getStart() != null)) {
            javacError = new JavacErrorDetail(
                    fname,
                    lineNum,
                    errNode.getStart().getFile(),
                    errNode.getStart().getLineNumber(),
                    errMsgBuf);
        } else {
            /*
             * javac error line number cannot be mapped to JSP page
             * line number. For example, this is the case if a 
             * scriptlet is missing a closing brace, which causes
             * havoc with the try-catch-finally block that the code
             * generator places around all generated code: As a result
             * of this, the javac error line numbers will be outside
             * the range of begin and end java line numbers that were
             * generated for the scriptlet, and therefore cannot be
             * mapped to the start line number of the scriptlet in the
             * JSP page.
             * Include just the javac error info in the error detail.
             */
            javacError = new JavacErrorDetail(
                    fname,
                    lineNum,
                    errMsgBuf);
        }
        return javacError;
    
private voiddispatch(Mark where, java.lang.String errCode, java.lang.Object[] args, java.lang.Exception e)

	String file = null;
	String errMsg = null;
	int line = -1;
	int column = -1;
	boolean hasLocation = false;

	// Localize
	if (errCode != null) {
	    errMsg = Localizer.getMessage(errCode, args);
	} else if (e != null) {
	    // give a hint about what's wrong
	    errMsg = e.getMessage();
	}

	// Get error location
	if (where != null) {
            if (jspcMode) {
                // Get the full URL of the resource that caused the error
                try {
                    file = where.getURL().toString();
                } catch (MalformedURLException me) {
                    // Fallback to using context-relative path
                    file = where.getFile();
                }
            } else {
                // Get the context-relative resource path, so as to not
                // disclose any local filesystem details
                file = where.getFile();
            }
	    line = where.getLineNumber();
	    column = where.getColumnNumber();
	    hasLocation = true;
	}

	// Get nested exception
	Exception nestedEx = e;
	if ((e instanceof SAXException)
	        && (((SAXException) e).getException() != null)) {
	    nestedEx = ((SAXException) e).getException();
	}

	if (hasLocation) {
	    errHandler.jspError(file, line, column, errMsg, nestedEx);
	} else {
	    errHandler.jspError(errMsg, nestedEx);
	}
    
public voidjavacError(JavacErrorDetail[] javacErrors)


        errHandler.javacError(javacErrors);
    
public voidjavacError(java.lang.String errorReport, java.lang.Exception e)


        errHandler.javacError(errorReport, e);
    
public voidjspError(java.lang.String errCode, java.lang.String arg, java.lang.Exception e)

	dispatch(null, errCode, new Object[] {arg}, e);
    
public voidjspError(Node n, java.lang.String errCode, java.lang.String arg, java.lang.Exception e)

	dispatch(n.getStart(), errCode, new Object[] {arg}, e);
    
public voidjspError(java.lang.String errCode)

	dispatch(null, errCode, null, null);
    
public voidjspError(Mark where, java.lang.String errCode)

	dispatch(where, errCode, null, null);
    
public voidjspError(Mark where, java.lang.Exception e)

	dispatch(where, e.getMessage(), null, e);
    
public voidjspError(Node n, java.lang.String errCode)

	dispatch(n.getStart(), errCode, null, null);
    
public voidjspError(java.lang.String errCode, java.lang.String args)

	dispatch(null, errCode, args, null);
    
public voidjspError(Mark where, java.lang.String errCode, java.lang.String args)

	dispatch(where, errCode, args, null);
    
public voidjspError(Node n, java.lang.String errCode, java.lang.String args)

	dispatch(n.getStart(), errCode, args, null);
    
public voidjspError(java.lang.Exception e)

	dispatch(null, null, null, e);
    
public static JavacErrorDetail[]parseJavacMessage(Node.Nodes pageNodes, java.lang.String errMsg, java.lang.String fname)


	ArrayList<JavacErrorDetail> errors = new ArrayList<JavacErrorDetail>();
	StringBuffer errMsgBuf = null;
	int lineNum = -1;
        JavacErrorDetail javacError = null;

        BufferedReader reader = new BufferedReader(new StringReader(errMsg));

        /*
         * Parse compilation errors. Each compilation error consists of a file
         * path and error line number, followed by a number of lines describing
         * the error.
         */
        String line = null;
        while ((line = reader.readLine()) != null) {

            /*
	     * Error line number is delimited by set of colons.
	     * Ignore colon following drive letter on Windows (fromIndex = 2).
	     * XXX Handle deprecation warnings that don't have line info
	     */
            int beginColon = line.indexOf(':", 2); 
            int endColon = line.indexOf(':", beginColon + 1);
            if ((beginColon >= 0) && (endColon >= 0)) {
                if (javacError != null) {
                    // add previous error to error vector
                    errors.add(javacError);
		}

		String lineNumStr = line.substring(beginColon + 1, endColon);
                try {
                    lineNum = Integer.parseInt(lineNumStr);
                } catch (NumberFormatException e) {
                    // XXX
                }

                errMsgBuf = new StringBuffer();

                javacError = createJavacError(fname, pageNodes, 
                                              errMsgBuf, lineNum);
            }

            // Ignore messages preceding first error
            if (errMsgBuf != null) {
                errMsgBuf.append(line);
                errMsgBuf.append("\n");
            }
        }

        // Add last error to error vector
        if (javacError != null) {
            errors.add(javacError);
        } 
        reader.close();
        return errors.toArray(new JavacErrorDetail[0]);
    
public voidthrowException(Mark where, org.apache.jasper.JasperException je)
Creates and throws a new exception from the given JasperException, by prepending the given location information (containing file name, line number, and column number) to the message of the given exception, and copying the stacktrace of the given exception to the new exception.

param
where The location information (containing file name, line number, and column number) to prepend
param
je The JasperException to amend


	if (where == null) {
            throw je;
        }

	// Get file location
        String file = null;
        if (jspcMode) {
            // Get the full URL of the resource that caused the error
            try {
                file = where.getURL().toString();
            } catch (MalformedURLException me) {
                // Fallback to using context-relative path
                file = where.getFile();
            }
        } else {
            // Get the context-relative resource path, so as to not
            // disclose any local filesystem details
            file = where.getFile();
        }

	JasperException newEx = new JasperException(file + "("
                + where.getLineNumber() + "," + where.getColumnNumber()
                + ")" + " " + je.getMessage());
        newEx.setStackTrace(je.getStackTrace());
 
        throw newEx;