FileDocCategorySizeDatePackage
ErrorDispatcher.javaAPI DocApache Tomcat 6.0.1421107Fri Jul 20 04:20:32 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 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

        return createJavacError(fname, page, errMsgBuf, lineNum, null);
    
public static JavacErrorDetailcreateJavacError(java.lang.String fname, Node.Nodes page, java.lang.StringBuffer errMsgBuf, int lineNum, org.apache.jasper.JspCompilationContext ctxt)

param
fname
param
page
param
errMsgBuf
param
lineNum
param
ctxt
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);
        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,
                    ctxt);
        } 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(Mark where, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2)

	dispatch(where, errCode, new Object[] {arg1, arg2}, null);
    
public voidjspError(Mark where, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3)

        dispatch(where, errCode, new Object[] {arg1, arg2, arg3}, null);
    
public voidjspError(Node n, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2)

	dispatch(n.getStart(), errCode, new Object[] {arg1, arg2}, null);
    
public voidjspError(Node n, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3)

	dispatch(n.getStart(), errCode, new Object[] {arg1, arg2, arg3}, null);
    
public voidjspError(java.lang.Exception e)

	dispatch(null, null, null, 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(Node n, java.lang.String errCode)

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

	dispatch(null, errCode, new Object[] {arg}, null);
    
public voidjspError(Mark where, java.lang.String errCode, java.lang.String arg)

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

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

	dispatch(null, errCode, new Object[] {arg1, arg2}, null);
    
public voidjspError(java.lang.String errCode, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3)

	dispatch(null, errCode, new Object[] {arg1, arg2, arg3}, null);
    
public static JavacErrorDetail[]parseJavacErrors(java.lang.String errMsg, java.lang.String fname, Node.Nodes page)
Parses the given error message into an array of javac compilation error messages (one per javac compilation error line number).

param
errMsg Error message
param
fname Name of Java source file whose compilation failed
param
page Node representation of JSP page from which the Java source file was generated
return
Array of javac compilation errors, or null if the given error message does not contain any compilation error line numbers


	return parseJavacMessage(errMsg, fname, page);
    
private static JavacErrorDetail[]parseJavacMessage(java.lang.String errMsg, java.lang.String fname, Node.Nodes page)


        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) {
                    lineNum = -1;
                }
                
                errMsgBuf = new StringBuffer();
                
                javacError = createJavacError(fname, page, 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();
        
        JavacErrorDetail[] errDetails = null;
        if (errors.size() > 0) {
            errDetails = new JavacErrorDetail[errors.size()];
            errors.toArray(errDetails);
        }
        
        return errDetails;