001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Norris Boyd
026: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038:
039: package org.mozilla.javascript;
040:
041: /**
042: * This is the default error reporter for JavaScript.
043: *
044: * @author Norris Boyd
045: */
046: class DefaultErrorReporter implements ErrorReporter {
047: static final DefaultErrorReporter instance = new DefaultErrorReporter();
048:
049: private boolean forEval;
050: private ErrorReporter chainedReporter;
051:
052: private DefaultErrorReporter() {
053: }
054:
055: static ErrorReporter forEval(ErrorReporter reporter) {
056: DefaultErrorReporter r = new DefaultErrorReporter();
057: r.forEval = true;
058: r.chainedReporter = reporter;
059: return r;
060: }
061:
062: public void warning(String message, String sourceURI, int line,
063: String lineText, int lineOffset) {
064: if (chainedReporter != null) {
065: chainedReporter.warning(message, sourceURI, line, lineText,
066: lineOffset);
067: } else {
068: // Do nothing
069: }
070: }
071:
072: public void error(String message, String sourceURI, int line,
073: String lineText, int lineOffset) {
074: if (forEval) {
075: // Assume error message strings that start with "TypeError: "
076: // should become TypeError exceptions. A bit of a hack, but we
077: // don't want to change the ErrorReporter interface.
078: String error = "SyntaxError";
079: final String TYPE_ERROR_NAME = "TypeError";
080: final String DELIMETER = ": ";
081: final String prefix = TYPE_ERROR_NAME + DELIMETER;
082: if (message.startsWith(prefix)) {
083: error = TYPE_ERROR_NAME;
084: message = message.substring(prefix.length());
085: }
086: throw ScriptRuntime.constructError(error, message,
087: sourceURI, line, lineText, lineOffset);
088: }
089: if (chainedReporter != null) {
090: chainedReporter.error(message, sourceURI, line, lineText,
091: lineOffset);
092: } else {
093: throw runtimeError(message, sourceURI, line, lineText,
094: lineOffset);
095: }
096: }
097:
098: public EvaluatorException runtimeError(String message,
099: String sourceURI, int line, String lineText, int lineOffset) {
100: if (chainedReporter != null) {
101: return chainedReporter.runtimeError(message, sourceURI,
102: line, lineText, lineOffset);
103: } else {
104: return new EvaluatorException(message, sourceURI, line,
105: lineText, lineOffset);
106: }
107: }
108: }
|