View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.struts2.jasper.compiler;
19  
20  import org.apache.struts2.jasper.JasperException;
21  
22  /***
23   * Default implementation of ErrorHandler interface.
24   *
25   * @author Jan Luehe
26   */
27  class DefaultErrorHandler implements ErrorHandler {
28  
29      /*
30      * Processes the given JSP parse error.
31      *
32      * @param fname Name of the JSP file in which the parse error occurred
33      * @param line Parse error line number
34      * @param column Parse error column number
35      * @param errMsg Parse error message
36      * @param exception Parse exception
37      */
38      public void jspError(String fname, int line, int column, String errMsg,
39                           Exception ex) throws JasperException {
40          throw new JasperException(fname + "(" + line + "," + column + ")"
41                  + " " + errMsg, ex);
42      }
43  
44      /*
45      * Processes the given JSP parse error.
46      *
47      * @param errMsg Parse error message
48      * @param exception Parse exception
49      */
50      public void jspError(String errMsg, Exception ex) throws JasperException {
51          throw new JasperException(errMsg, ex);
52      }
53  
54      /*
55      * Processes the given javac compilation errors.
56      *
57      * @param details Array of JavacErrorDetail instances corresponding to the
58      * compilation errors
59      */
60      public void javacError(JavacErrorDetail[] details) throws JasperException {
61  
62          if (details == null) {
63              return;
64          }
65  
66          Object[] args = null;
67          StringBuffer buf = new StringBuffer();
68  
69          for (int i = 0; i < details.length; i++) {
70              buf.append("\n");
71              if (details[i].getJspBeginLineNumber() >= 0) {
72                  args = new Object[]{
73                          new Integer(details[i].getJspBeginLineNumber()),
74                          details[i].getJspFileName()};
75                  buf.append("\n");
76                  buf.append(Localizer.getMessage("jsp.error.single.line.number",
77                          args));
78                  buf.append("\n");
79                  buf.append(details[i].getErrorMessage());
80                  buf.append("\n");
81                  buf.append(details[i].getJspExtract());
82              } else {
83                  args = new Object[]{
84                          new Integer(details[i].getJavaLineNumber())};
85                  buf.append("\n\n");
86                  buf.append(Localizer.getMessage("jsp.error.java.line.number",
87                          args));
88                  buf.append("\n");
89                  buf.append(details[i].getErrorMessage());
90              }
91          }
92          buf.append("\n\nStacktrace:");
93          throw new JasperException(Localizer.getMessage("jsp.error.unable.compile") + ": " + buf);
94      }
95  
96      /***
97       * Processes the given javac error report and exception.
98       *
99       * @param errorReport Compilation error report
100      * @param exception   Compilation exception
101      */
102     public void javacError(String errorReport, Exception exception)
103             throws JasperException {
104 
105         throw new JasperException(
106                 Localizer.getMessage("jsp.error.unable.compile"), exception);
107     }
108 
109 }