tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r491614 - in /tomcat/tc6.0.x/trunk: java/org/apache/jasper/compiler/ java/org/apache/jasper/servlet/ webapps/docs/
Date Mon, 01 Jan 2007 18:55:09 GMT
Author: markt
Date: Mon Jan  1 10:55:08 2007
New Revision: 491614

URL: http://svn.apache.org/viewvc?view=rev&rev=491614
Log:
Port fix from TC5 to display the JSP source when a compilation error occurs

Modified:
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/DefaultErrorHandler.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JDTCompiler.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java
    tomcat/tc6.0.x/trunk/java/org/apache/jasper/servlet/JspServletWrapper.java
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/DefaultErrorHandler.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/DefaultErrorHandler.java?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/DefaultErrorHandler.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/DefaultErrorHandler.java Mon Jan
 1 10:55:08 2007
@@ -67,22 +67,23 @@
         StringBuffer buf = new StringBuffer();
         
         for (int i=0; i < details.length; i++) {
+            buf.append("\n");
             if (details[i].getJspBeginLineNumber() >= 0) {
                 args = new Object[] {
                         new Integer(details[i].getJspBeginLineNumber()), 
                         details[i].getJspFileName() };
+                buf.append("\n");
                 buf.append(Localizer.getMessage("jsp.error.single.line.number",
                         args));
-                buf.append("\n"); 
+                buf.append("\n");
+                buf.append(details[i].getErrorMessage());
+                buf.append("\n");
+                buf.append(details[i].getJspExtract());
             }
-            
-            buf.append(
-                    Localizer.getMessage("jsp.error.corresponding.servlet"));
-            buf.append(details[i].getErrorMessage());
-            buf.append("\n\n");
         }
-        
-        throw new JasperException(Localizer.getMessage("jsp.error.unable.compile") + "\n\n"
+ buf);
+        buf.append("\n\nStacktrace:");
+        throw new JasperException(
+                Localizer.getMessage("jsp.error.unable.compile") + ": " + buf);
     }
     
     /**

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java Mon Jan  1 10:55:08
2007
@@ -23,6 +23,7 @@
 import java.util.ArrayList;
 
 import org.apache.jasper.JasperException;
+import org.apache.jasper.JspCompilationContext;
 import org.xml.sax.SAXException;
 
 /**
@@ -501,8 +502,25 @@
      * @return JavacErrorDetail The error details
      * @throws JasperException
      */
-    public static JavacErrorDetail createJavacError(String fname, Node.Nodes page, 
-            StringBuffer errMsgBuf, int lineNum) throws JasperException {
+    public static JavacErrorDetail createJavacError(String fname,
+            Node.Nodes page, StringBuffer errMsgBuf, int lineNum)
+    throws JasperException {
+        return createJavacError(fname, page, errMsgBuf, lineNum, null);
+    }
+    
+    
+    /**
+     * @param fname
+     * @param page
+     * @param errMsgBuf
+     * @param lineNum
+     * @param ctxt
+     * @return JavacErrorDetail The error details
+     * @throws JasperException
+     */
+    public static JavacErrorDetail createJavacError(String fname,
+            Node.Nodes page, StringBuffer errMsgBuf, int lineNum,
+            JspCompilationContext ctxt) throws JasperException {
         JavacErrorDetail javacError;
         // Attempt to map javac error line number to line in JSP page
         ErrorVisitor errVisitor = new ErrorVisitor(lineNum);
@@ -514,7 +532,8 @@
                     lineNum,
                     errNode.getStart().getFile(),
                     errNode.getStart().getLineNumber(),
-                    errMsgBuf);
+                    errMsgBuf,
+                    ctxt);
         } else {
             /*
              * javac error line number cannot be mapped to JSP page

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JDTCompiler.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JDTCompiler.java?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JDTCompiler.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JDTCompiler.java Mon Jan  1 10:55:08
2007
@@ -352,7 +352,7 @@
                                     try {
                                         problemList.add(ErrorDispatcher.createJavacError
                                                 (name, pageNodes, new StringBuffer(problem.getMessage()),

-                                                        problem.getSourceLineNumber()));
+                                                        problem.getSourceLineNumber(), ctxt));
                                     } catch (JasperException e) {
                                         log.error("Error visiting node", e);
                                     }

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java Mon Jan  1
10:55:08 2007
@@ -17,6 +17,16 @@
 
 package org.apache.jasper.compiler;
 
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.jasper.JspCompilationContext;
+
 /**
  * Class providing details about a javac compilation error.
  *
@@ -30,6 +40,7 @@
     private String jspFileName;
     private int jspBeginLineNum;
     private StringBuffer errMsg;
+    private String jspExtract = null;
 
     /**
      * Constructor.
@@ -40,12 +51,12 @@
      * @param errMsg The compilation error message
      */
     public JavacErrorDetail(String javaFileName,
-			    int javaLineNum,
-			    StringBuffer errMsg) {
+                            int javaLineNum,
+                            StringBuffer errMsg) {
 
-	this.javaFileName = javaFileName;
-	this.javaLineNum = javaLineNum;
-	this.errMsg = errMsg;
+        this.javaFileName = javaFileName;
+        this.javaLineNum = javaLineNum;
+        this.errMsg = errMsg;
         this.jspBeginLineNum = -1;
     }
 
@@ -62,14 +73,69 @@
      * @param errMsg The compilation error message
      */
     public JavacErrorDetail(String javaFileName,
-			    int javaLineNum,
-			    String jspFileName,
-			    int jspBeginLineNum,
-			    StringBuffer errMsg) {
+                            int javaLineNum,
+                            String jspFileName,
+                            int jspBeginLineNum,
+                            StringBuffer errMsg) {
+
+        this(javaFileName, javaLineNum, jspFileName, jspBeginLineNum, errMsg,
+                null);
+    }
 
+    public JavacErrorDetail(String javaFileName,
+            int javaLineNum,
+            String jspFileName,
+            int jspBeginLineNum,
+            StringBuffer errMsg,
+            JspCompilationContext ctxt) {
+        
         this(javaFileName, javaLineNum, errMsg);
-	this.jspFileName = jspFileName;
-	this.jspBeginLineNum = jspBeginLineNum;
+        this.jspFileName = jspFileName;
+        this.jspBeginLineNum = jspBeginLineNum;
+        
+        if (jspBeginLineNum > 0 && ctxt != null) {
+            try {
+                // Read both files in, so we can inspect them
+                String[] jspLines = readFile
+                    (ctxt.getResourceAsStream(jspFileName));
+    
+                String[] javaLines = readFile
+                    (new FileInputStream(ctxt.getServletJavaFileName()));
+    
+                // If the line contains the opening of a multi-line scriptlet
+                // block, then the JSP line number we got back is probably
+                // faulty.  Scan forward to match the java line...
+                if (jspLines[jspBeginLineNum-1].lastIndexOf("<%") >
+                    jspLines[jspBeginLineNum-1].lastIndexOf("%>")) {
+                    String javaLine = javaLines[javaLineNum-1].trim();
+    
+                    for (int i=jspBeginLineNum-1; i<jspLines.length; i++) {
+                        if (jspLines[i].indexOf(javaLine) != -1) {
+                            // Update jsp line number
+                            this.jspBeginLineNum = i+1;
+                            break;
+                        }
+                    }
+                }
+    
+                // copy out a fragment of JSP to display to the user
+                StringBuffer fragment = new StringBuffer(1024);
+                int startIndex = Math.max(0, this.jspBeginLineNum-1-3);
+                int endIndex = Math.min(
+                        jspLines.length-1, this.jspBeginLineNum-1+3);
+    
+                for (int i=startIndex;i<=endIndex; ++i) {
+                    fragment.append(i+1);
+                    fragment.append(": ");
+                    fragment.append(jspLines[i]);
+                    fragment.append("\n");
+                }
+                jspExtract = fragment.toString();
+    
+            } catch (IOException ioe) {
+                // Can't read files - ignore
+            }
+        }
     }
 
     /**
@@ -79,7 +145,7 @@
      * @return Java source file name
      */
     public String getJavaFileName() {
-	return this.javaFileName;
+        return this.javaFileName;
     }
 
     /**
@@ -88,7 +154,7 @@
      * @return Compilation error line number
      */
     public int getJavaLineNumber() {
-	return this.javaLineNum;
+        return this.javaLineNum;
     }
 
     /**
@@ -98,7 +164,7 @@
      * @return JSP file from which the Java source file was generated.
      */
     public String getJspFileName() {
-	return this.jspFileName;
+        return this.jspFileName;
     }
 
     /**
@@ -109,7 +175,7 @@
      * compilation error
      */
     public int getJspBeginLineNumber() {
-	return this.jspBeginLineNum;
+        return this.jspBeginLineNum;
     }
 
     /**
@@ -118,6 +184,31 @@
      * @return Compilation error message
      */
     public String getErrorMessage() {
-	return this.errMsg.toString();
+        return this.errMsg.toString();
+    }
+    
+    /**
+     * Gets the extract of the JSP that corresponds to this message.
+     *
+     * @return Extract of JSP where error occurred
+     */
+    public String getJspExtract() {
+        return this.jspExtract;
+    }
+    
+    /**
+     * Reads a text file from an input stream into a String[]. Used to read in
+     * the JSP and generated Java file when generating error messages.
+     */
+    private String[] readFile(InputStream s) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(s));
+        List lines = new ArrayList();
+        String line;
+
+        while ( (line = reader.readLine()) != null ) {
+            lines.add(line);
+        }
+
+        return (String[]) lines.toArray( new String[lines.size()] );
     }
 }

Modified: tomcat/tc6.0.x/trunk/java/org/apache/jasper/servlet/JspServletWrapper.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/jasper/servlet/JspServletWrapper.java?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/jasper/servlet/JspServletWrapper.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/jasper/servlet/JspServletWrapper.java Mon Jan  1
10:55:08 2007
@@ -17,15 +17,9 @@
 
 package org.apache.jasper.servlet;
 
-import java.io.BufferedReader;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
@@ -504,10 +498,11 @@
             else {
                 int javaLineNumber = jspFrame.getLineNumber();
                 JavacErrorDetail detail = ErrorDispatcher.createJavacError(
-                                                                           jspFrame.getMethodName(),
-                                                                           this.ctxt.getCompiler().getPageNodes(),
-                                                                           null,
-                                                                           javaLineNumber);
+                        jspFrame.getMethodName(),
+                        this.ctxt.getCompiler().getPageNodes(),
+                        null,
+                        javaLineNumber,
+                        ctxt);
 
                 // If the line number is less than one we couldn't find out
                 // where in the JSP things went wrong
@@ -517,69 +512,26 @@
                 }
 
                 if (options.getDisplaySourceFragment()) {
-
-                    // Read both files in, so we can inspect them
-                    String[] jspLines = readFile
-                    (this.ctxt.getResourceAsStream(detail.getJspFileName()));
-
-                    String[] javaLines = readFile
-                    (new FileInputStream(this.ctxt.getServletJavaFileName()));
-
-                    // If the line contains the opening of a multi-line scriptlet
-                    // block, then the JSP line number we got back is probably
-                    // faulty.  Scan forward to match the java line...
-                    if (jspLines[jspLineNumber-1].lastIndexOf("<%") >
-                    jspLines[jspLineNumber-1].lastIndexOf("%>")) {
-                        String javaLine = javaLines[javaLineNumber-1].trim();
-
-                        for (int i=jspLineNumber-1; i<jspLines.length; i++) {
-                            if (jspLines[i].indexOf(javaLine) != -1) {
-                                jspLineNumber = i+1;
-                                break;
-                            }
-                        }
-                    }
-
-                    // copy out a fragment of JSP to display to the user
-                    StringBuffer buffer = new StringBuffer(1024);
-                    int startIndex = Math.max(0, jspLineNumber-1-3);
-                    int endIndex = Math.min(jspLines.length-1, jspLineNumber-1+3);
-
-                    for (int i=startIndex;i<=endIndex; ++i) {
-                        buffer.append(i+1);
-                        buffer.append(": ");
-                        buffer.append(jspLines[i]);
-                        buffer.append("\n");
-                    }
-
                     return new JasperException(Localizer.getMessage
-                            ("jsp.exception", detail.getJspFileName(), "" + jspLineNumber)
+ "\n" + buffer, ex);
+                            ("jsp.exception", detail.getJspFileName(),
+                                    "" + jspLineNumber) +
+                                    "\n\n" + detail.getJspExtract() +
+                                    "\n\nStacktrace:", ex);
                     
                 } else {
                     return new JasperException(Localizer.getMessage
-                            ("jsp.exception", detail.getJspFileName(), "" + jspLineNumber),
ex);
+                            ("jsp.exception", detail.getJspFileName(),
+                                    "" + jspLineNumber), ex);
                 }
             }
         } catch (Exception je) {
             // If anything goes wrong, just revert to the original behaviour
-            return new JasperException(ex);
-        }
-    }
-
-    /**
-     * Reads a text file from an input stream into a String[]. Used to read in
-     * the JSP and generated Java file when generating error messages.
-     */
-    private String[] readFile(InputStream s) throws IOException {
-        BufferedReader reader = new BufferedReader(new InputStreamReader(s));
-        List lines = new ArrayList();
-        String line;
-
-        while ( (line = reader.readLine()) != null ) {
-            lines.add(line);
+            if (ex instanceof JasperException) {
+                return (JasperException) ex;
+            } else {
+                return new JasperException(ex);
+            }
         }
-
-        return (String[]) lines.toArray( new String[lines.size()] );
     }
 
 }

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?view=diff&rev=491614&r1=491613&r2=491614
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Mon Jan  1 10:55:08 2007
@@ -21,6 +21,10 @@
         When displaying JSP source after an exception, handle included files.
         (markt)
       </fix>
+      <fix>
+        Display the JSP source when a compilation error occurs and display
+        the correct line number rather than start of a scriptlet block. (markt)
+      </fix>
     </changelog>
   </subsection>
 </section>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message