ErrorDispatcherpublic 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. |
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 JavacErrorDetail | createJavacError(java.lang.String fname, Node.Nodes page, java.lang.StringBuffer errMsgBuf, int lineNum)
return createJavacError(fname, page, errMsgBuf, lineNum, null);
| public static JavacErrorDetail | createJavacError(java.lang.String fname, Node.Nodes page, java.lang.StringBuffer errMsgBuf, int lineNum, org.apache.jasper.JspCompilationContext ctxt)
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 void | dispatch(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 void | javacError(JavacErrorDetail[] javacErrors)
errHandler.javacError(javacErrors);
| public void | javacError(java.lang.String errorReport, java.lang.Exception e)
errHandler.javacError(errorReport, e);
| public void | jspError(Mark where, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2)
dispatch(where, errCode, new Object[] {arg1, arg2}, null);
| public void | jspError(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 void | jspError(Node n, java.lang.String errCode, java.lang.String arg1, java.lang.String arg2)
dispatch(n.getStart(), errCode, new Object[] {arg1, arg2}, null);
| public void | jspError(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 void | jspError(java.lang.Exception e)
dispatch(null, null, null, e);
| public void | jspError(java.lang.String errCode, java.lang.String arg, java.lang.Exception e)
dispatch(null, errCode, new Object[] {arg}, e);
| public void | jspError(Node n, java.lang.String errCode, java.lang.String arg, java.lang.Exception e)
dispatch(n.getStart(), errCode, new Object[] {arg}, e);
| public void | jspError(java.lang.String errCode)
dispatch(null, errCode, null, null);
| public void | jspError(Mark where, java.lang.String errCode)
dispatch(where, errCode, null, null);
| public void | jspError(Node n, java.lang.String errCode)
dispatch(n.getStart(), errCode, null, null);
| public void | jspError(java.lang.String errCode, java.lang.String arg)
dispatch(null, errCode, new Object[] {arg}, null);
| public void | jspError(Mark where, java.lang.String errCode, java.lang.String arg)
dispatch(where, errCode, new Object[] {arg}, null);
| public void | jspError(Node n, java.lang.String errCode, java.lang.String arg)
dispatch(n.getStart(), errCode, new Object[] {arg}, null);
| public void | jspError(java.lang.String errCode, java.lang.String arg1, java.lang.String arg2)
dispatch(null, errCode, new Object[] {arg1, arg2}, null);
| public void | jspError(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).
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;
|
|