View Javadoc

1   /*
2   * Copyright 1999-2004 The Apache Software Foundation
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.apache.struts.flow.core.javascript;
17  
18  import org.apache.struts.flow.core.Logger;
19  import org.apache.struts.flow.core.location.Location;
20  import org.apache.struts.flow.core.location.LocationImpl;
21  import org.mozilla.javascript.ErrorReporter;
22  import org.mozilla.javascript.EvaluatorException;
23  import org.mozilla.javascript.tools.ToolErrorReporter;
24  
25  /***
26   * Implements a Rhino JavaScript {@link
27   * org.mozilla.javascript.ErrorReporter}. 
28   * Like ToolErrorReporter but logs to supplied logger instead of stdout
29   *
30   * @version CVS $Id: JSErrorReporter.java 292797 2005-09-30 16:05:46Z sylvain $
31   */
32  public class JSErrorReporter implements ErrorReporter
33  {
34    private Logger logger;
35    private Location location;
36    private StringBuffer message;
37  
38    public JSErrorReporter(Logger logger)
39    {
40        this.logger = logger;
41    }
42    
43    private void appendMessage(String text, String sourceName, int line, int column) {
44        if (location == null) {
45            location = new LocationImpl(null, sourceName, line, column);
46            message = new StringBuffer();
47        } else {
48            // Append a linefeed
49            message.append("\n");
50        }
51        
52        message.append(text);
53    }
54  
55    public void error(String message,
56                      String sourceName, int line,
57                      String lineSrc, int column)
58    {
59        String errMsg = getErrorMessage("msg.error", message, 
60                                        sourceName, line, lineSrc, column);
61        appendMessage(errMsg, sourceName, line, column);
62        System.err.println(errMsg);
63        logger.error(errMsg);
64    }
65  
66    public void warning(String message, String sourceName, int line,
67                        String lineSrc, int column)
68    {
69        String errMsg = getErrorMessage("msg.warning", message, 
70                                      sourceName, line, lineSrc, column);
71        appendMessage(errMsg, sourceName, line, column);
72        System.err.println(errMsg);
73        logger.warn(errMsg);
74    }
75      
76    public EvaluatorException runtimeError(String message, String sourceName,
77                                           int line, String lineSrc,
78                                           int column)
79    {
80        String errMsg = getErrorMessage("msg.error", message,
81                                        sourceName, line,
82                                        lineSrc, column);
83        appendMessage(errMsg, sourceName, line, column);
84        System.err.println(errMsg);
85        // FIXME(SW): need to build a locatable extension to EvaluatorException
86        return new EvaluatorException(this.message.toString());
87    }
88  
89    /***
90     * Formats error message
91     *
92     * @param type a <code>String</code> value, indicating the error
93     * type (error or warning)
94     * @param message a <code>String</code> value, the error or warning
95     * message
96     * @param line an <code>int</code> value, the original cummulative
97     * line number
98     * @param lineSource a <code>String</code> value, the text of the
99     * line in the file
100    * @param column an <code>int</code> value, the column in
101    * <code>lineSource</code> where the error appeared
102    * @return a <code>String</code> value, the aggregated error
103    * message, with the source file and line number adjusted to the
104    * real values
105    */
106     String getErrorMessage(String type,
107                            String message,
108                            String sourceName, int line,
109                            String lineSource, int column)
110     {
111         if (line > 0) {
112             if (sourceName != null) {
113                 Object[] errArgs = { sourceName, new Integer(line), message };
114                 return ToolErrorReporter.getMessage("msg.format3", errArgs);
115           } else {
116               Object[] errArgs = { new Integer(line), message };
117               return ToolErrorReporter.getMessage("msg.format2", errArgs);
118             }
119         } else {
120             Object[] errArgs = { message };
121             return ToolErrorReporter.getMessage("msg.format1", errArgs);
122         }
123     }
124 }