01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.cocoon.components.flow.javascript;
18:
19: import org.mozilla.javascript.ErrorReporter;
20: import org.mozilla.javascript.EvaluatorException;
21: import org.mozilla.javascript.tools.ToolErrorReporter;
22: import org.apache.avalon.framework.logger.Logger;
23:
24: /**
25: * Implements a Rhino JavaScript {@link
26: * org.mozilla.javascript.ErrorReporter}.
27: * Like ToolErrorReporter but logs to supplied logger instead of stdout
28: *
29: * @version CVS $Id: JSErrorReporter.java 433543 2006-08-22 06:22:54Z crossley $
30: */
31: public class JSErrorReporter implements ErrorReporter {
32: private Logger logger;
33:
34: public JSErrorReporter(Logger logger) {
35: this .logger = logger;
36: }
37:
38: public void error(String message, String sourceName, int line,
39: String lineSrc, int column) {
40: String errMsg = getErrorMessage("msg.error", message,
41: sourceName, line, lineSrc, column);
42: System.err.println(errMsg);
43: logger.error(errMsg);
44: }
45:
46: public void warning(String message, String sourceName, int line,
47: String lineSrc, int column) {
48: String errMsg = getErrorMessage("msg.warning", message,
49: sourceName, line, lineSrc, column);
50: System.err.println(errMsg);
51: logger.warn(errMsg);
52: }
53:
54: public EvaluatorException runtimeError(String message,
55: String sourceName, int line, String lineSrc, int column) {
56: String errMsg = getErrorMessage("msg.error", message,
57: sourceName, line, lineSrc, column);
58: System.err.println(errMsg);
59: return new EvaluatorException(errMsg);
60: }
61:
62: /**
63: * Formats error message
64: *
65: * @param type a <code>String</code> value, indicating the error
66: * type (error or warning)
67: * @param message a <code>String</code> value, the error or warning
68: * message
69: * @param line an <code>int</code> value, the original cummulative
70: * line number
71: * @param lineSource a <code>String</code> value, the text of the
72: * line in the file
73: * @param column an <code>int</code> value, the column in
74: * <code>lineSource</code> where the error appeared
75: * @return a <code>String</code> value, the aggregated error
76: * message, with the source file and line number adjusted to the
77: * real values
78: */
79: String getErrorMessage(String type, String message,
80: String sourceName, int line, String lineSource, int column) {
81: if (line > 0) {
82: if (sourceName != null) {
83: Object[] errArgs = { sourceName, new Integer(line),
84: message };
85: return ToolErrorReporter.getMessage("msg.format3",
86: errArgs);
87: } else {
88: Object[] errArgs = { new Integer(line), message };
89: return ToolErrorReporter.getMessage("msg.format2",
90: errArgs);
91: }
92: } else {
93: Object[] errArgs = { message };
94: return ToolErrorReporter.getMessage("msg.format1", errArgs);
95: }
96: }
97: }
|