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: // <netbeans>
065: , String id, Object params
066: // </netbeans>
067: ) {
068: if (chainedReporter != null) {
069: chainedReporter.warning(message, sourceURI, line, lineText,
070: lineOffset
071: // <netbeans>
072: , id, params
073: // </netbeans>
074: );
075: } else {
076: // Do nothing
077: }
078: }
079:
080: public void error(String message, String sourceURI, int line,
081: String lineText, int lineOffset
082: // <netbeans>
083: , String id, Object params
084: // </netbeans>
085: ) {
086: if (forEval) {
087: // Assume error message strings that start with "TypeError: "
088: // should become TypeError exceptions. A bit of a hack, but we
089: // don't want to change the ErrorReporter interface.
090: String error = "SyntaxError";
091: final String TYPE_ERROR_NAME = "TypeError";
092: final String DELIMETER = ": ";
093: final String prefix = TYPE_ERROR_NAME + DELIMETER;
094: if (message.startsWith(prefix)) {
095: error = TYPE_ERROR_NAME;
096: message = message.substring(prefix.length());
097: }
098: throw ScriptRuntime.constructError(error, message,
099: sourceURI, line, lineText, lineOffset);
100: }
101: if (chainedReporter != null) {
102: chainedReporter.error(message, sourceURI, line, lineText,
103: lineOffset
104: // <netbeans>
105: , id, params
106: // </netbeans>
107: );
108: } else {
109: throw runtimeError(message, sourceURI, line, lineText,
110: lineOffset);
111: }
112: }
113:
114: public EvaluatorException runtimeError(String message,
115: String sourceURI, int line, String lineText, int lineOffset) {
116: if (chainedReporter != null) {
117: return chainedReporter.runtimeError(message, sourceURI,
118: line, lineText, lineOffset);
119: } else {
120: return new EvaluatorException(message, sourceURI, line,
121: lineText, lineOffset);
122: }
123: }
124: }
|