KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > javascript > JSErrorReporter


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.cocoon.components.flow.javascript;
17
18 import org.mozilla.javascript.ErrorReporter;
19 import org.mozilla.javascript.EvaluatorException;
20 import org.mozilla.javascript.tools.ToolErrorReporter;
21 import org.apache.avalon.framework.logger.Logger;
22
23 /**
24  * Implements a Rhino JavaScript {@link
25  * org.mozilla.javascript.ErrorReporter}.
26  * Like ToolErrorReporter but logs to supplied logger instead of stdout
27  *
28  * @version CVS $Id: JSErrorReporter.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class JSErrorReporter implements ErrorReporter
31 {
32   private Logger logger;
33
34   public JSErrorReporter(Logger logger)
35   {
36       this.logger = logger;
37   }
38
39   public void error(String JavaDoc message,
40                     String JavaDoc sourceName, int line,
41                     String JavaDoc lineSrc, int column)
42   {
43       String JavaDoc errMsg = getErrorMessage("msg.error", message,
44                                       sourceName, line, lineSrc, column);
45       System.err.println(errMsg);
46       logger.error(errMsg);
47   }
48
49   public void warning(String JavaDoc message, String JavaDoc sourceName, int line,
50                       String JavaDoc lineSrc, int column)
51   {
52       String JavaDoc errMsg = getErrorMessage("msg.warning", message,
53                                     sourceName, line, lineSrc, column);
54       System.err.println(errMsg);
55       logger.warn(errMsg);
56   }
57     
58   public EvaluatorException runtimeError(String JavaDoc message, String JavaDoc sourceName,
59                                          int line, String JavaDoc lineSrc,
60                                          int column)
61   {
62       String JavaDoc errMsg = getErrorMessage("msg.error", message,
63                                       sourceName, line,
64                                       lineSrc, column);
65       System.err.println(errMsg);
66       return new EvaluatorException(errMsg);
67   }
68
69   /**
70    * Formats error message
71    *
72    * @param type a <code>String</code> value, indicating the error
73    * type (error or warning)
74    * @param message a <code>String</code> value, the error or warning
75    * message
76    * @param line an <code>int</code> value, the original cummulative
77    * line number
78    * @param lineSource a <code>String</code> value, the text of the
79    * line in the file
80    * @param column an <code>int</code> value, the column in
81    * <code>lineSource</code> where the error appeared
82    * @return a <code>String</code> value, the aggregated error
83    * message, with the source file and line number adjusted to the
84    * real values
85    */

86     String JavaDoc getErrorMessage(String JavaDoc type,
87                            String JavaDoc message,
88                            String JavaDoc sourceName, int line,
89                            String JavaDoc lineSource, int column)
90     {
91         if (line > 0) {
92             if (sourceName != null) {
93                 Object JavaDoc[] errArgs = { sourceName, new Integer JavaDoc(line), message };
94                 return ToolErrorReporter.getMessage("msg.format3", errArgs);
95           } else {
96               Object JavaDoc[] errArgs = { new Integer JavaDoc(line), message };
97               return ToolErrorReporter.getMessage("msg.format2", errArgs);
98             }
99         } else {
100             Object JavaDoc[] errArgs = { message };
101             return ToolErrorReporter.getMessage("msg.format1", errArgs);
102         }
103     }
104 }
105
Popular Tags