tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r489981 - in /tomcat: container/tc5.5.x/webapps/docs/ jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ jasper/tc5.5.x/src/share/org/apache/jasper/servlet/
Date Sun, 24 Dec 2006 04:28:39 GMT
Author: markt
Date: Sat Dec 23 20:28:38 2006
New Revision: 489981

URL: http://svn.apache.org/viewvc?view=rev&rev=489981
Log:
Display JSP source when a compilation error occurs and use the correct line number rather
than the start of the scriptlet block.

Modified:
    tomcat/container/tc5.5.x/webapps/docs/changelog.xml
    tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/DefaultErrorHandler.java
    tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ErrorDispatcher.java
    tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JDTCompiler.java
    tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JavacErrorDetail.java
    tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/servlet/JspServletWrapper.java

Modified: tomcat/container/tc5.5.x/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/container/tc5.5.x/webapps/docs/changelog.xml?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/container/tc5.5.x/webapps/docs/changelog.xml (original)
+++ tomcat/container/tc5.5.x/webapps/docs/changelog.xml Sat Dec 23 20:28:38 2006
@@ -185,6 +185,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> 
   <subsection name="Webapps">

Modified: tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/DefaultErrorHandler.java
URL: http://svn.apache.org/viewvc/tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/DefaultErrorHandler.java?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/DefaultErrorHandler.java (original)
+++ tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/DefaultErrorHandler.java Sat
Dec 23 20:28:38 2006
@@ -67,22 +67,22 @@
         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(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/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ErrorDispatcher.java
URL: http://svn.apache.org/viewvc/tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ErrorDispatcher.java?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ErrorDispatcher.java (original)
+++ tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/ErrorDispatcher.java Sat Dec
23 20:28:38 2006
@@ -23,6 +23,7 @@
 import java.net.MalformedURLException;
 
 import org.apache.jasper.JasperException;
+import org.apache.jasper.JspCompilationContext;
 import org.xml.sax.SAXException;
 
 /**
@@ -501,8 +502,24 @@
      * @return JavacErrorDetail The error details
      * @throws JasperException
      */
-    public static JavacErrorDetail createJavacError(String fname, Node.Nodes page, 
+    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 +531,8 @@
                     lineNum,
                     errNode.getStart().getFile(),
                     errNode.getStart().getLineNumber(),
-                    errMsgBuf);
+                    errMsgBuf,
+                    ctxt);
         } else {
             /*
              * javac error line number cannot be mapped to JSP page

Modified: tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JDTCompiler.java
URL: http://svn.apache.org/viewvc/tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JDTCompiler.java?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JDTCompiler.java (original)
+++ tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JDTCompiler.java Sat Dec 23
20:28:38 2006
@@ -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/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JavacErrorDetail.java
URL: http://svn.apache.org/viewvc/tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JavacErrorDetail.java?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JavacErrorDetail.java (original)
+++ tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/compiler/JavacErrorDetail.java Sat Dec
23 20:28:38 2006
@@ -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.
@@ -67,11 +78,65 @@
                             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;
-    }
+        
+        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
+            }
+        }
 
+    }
     /**
      * Gets the name of the Java source file in which the compilation error
      * occurred.
@@ -119,5 +184,30 @@
      */
     public String getErrorMessage() {
         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/jasper/tc5.5.x/src/share/org/apache/jasper/servlet/JspServletWrapper.java
URL: http://svn.apache.org/viewvc/tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/servlet/JspServletWrapper.java?view=diff&rev=489981&r1=489980&r2=489981
==============================================================================
--- tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/servlet/JspServletWrapper.java (original)
+++ tomcat/jasper/tc5.5.x/src/share/org/apache/jasper/servlet/JspServletWrapper.java Sat Dec
23 20:28:38 2006
@@ -17,16 +17,9 @@
 
 package org.apache.jasper.servlet;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.IOException;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
@@ -458,10 +451,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,
+                        this.ctxt);
 
                 // If the line number is less than one we couldn't find out
                 // where in the JSP things went wrong
@@ -470,64 +464,18 @@
                     throw new JasperException(ex);
                 }
 
-                // 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(
-                                           "Exception in JSP: " + detail.getJspFileName()
+ ":" +
-                                           jspLineNumber + "\n\n" + buffer + "\n\nStacktrace:",
ex);
+                return new JasperException("Exception in JSP: " +
+                        detail.getJspFileName() + ":" + jspLineNumber + "\n\n" +
+                        detail.getJspExtract() + "\n\nStacktrace:", 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()] );
     }
 
 }



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


Mime
View raw message