001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * The contents of this file are subject to the Netscape Public
004: * License Version 1.1 (the "License"); you may not use this file
005: * except in compliance with the License. You may obtain a copy of
006: * the License at http://www.mozilla.org/NPL/
007: *
008: * Software distributed under the License is distributed on an "AS
009: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
010: * implied. See the License for the specific language governing
011: * rights and limitations under the License.
012: *
013: * The Original Code is Rhino code, released
014: * May 6, 1998.
015: *
016: * The Initial Developer of the Original Code is Netscape
017: * Communications Corporation. Portions created by Netscape are
018: * Copyright (C) 1997-1999 Netscape Communications Corporation. All
019: * Rights Reserved.
020: *
021: * Contributor(s):
022: * Norris Boyd
023: * Kurt Westerfeld
024: *
025: * Alternatively, the contents of this file may be used under the
026: * terms of the GNU Public License (the "GPL"), in which case the
027: * provisions of the GPL are applicable instead of those above.
028: * If you wish to allow use of your version of this file only
029: * under the terms of the GPL and not to allow others to use your
030: * version of this file under the NPL, indicate your decision by
031: * deleting the provisions above and replace them with the notice
032: * and other provisions required by the GPL. If you do not delete
033: * the provisions above, a recipient may use your version of this
034: * file under either the NPL or the GPL.
035: */
036:
037: package org.openlaszlo.iv.flash.js;
038:
039: import org.mozilla.javascript.*;
040: import java.text.MessageFormat;
041: import java.io.*;
042: import java.util.*;
043:
044: import org.openlaszlo.iv.flash.util.*;
045:
046: /**
047: * Error reporter for tools.
048: *
049: */
050: public class IVErrorReporter implements ErrorReporter {
051:
052: public IVErrorReporter(boolean reportWarnings) {
053: this .reportWarnings = reportWarnings;
054: }
055:
056: /**
057: * Look up the message corresponding to messageId in the
058: * org.mozilla.javascript.tools.shell.resources.Messages property file.
059: * For internationalization support.
060: */
061: public static String getMessage(String messageId) {
062: return getMessage(messageId, (Object[]) null);
063: }
064:
065: public static String getMessage(String messageId, String argument) {
066: Object[] args = { argument };
067: return getMessage(messageId, args);
068: }
069:
070: public static String getMessage(String messageId, Object arg1,
071: Object arg2) {
072: Object[] args = { arg1, arg2 };
073: return getMessage(messageId, args);
074: }
075:
076: public static String getMessage(String messageId, Object[] args) {
077: Context cx = Context.getCurrentContext();
078: Locale locale = cx == null ? Locale.getDefault() : cx
079: .getLocale();
080:
081: // ResourceBundle does cacheing.
082: ResourceBundle rb = ResourceBundle.getBundle(
083: "org.mozilla.javascript.tools.resources.Messages",
084: locale);
085:
086: String formatString;
087: try {
088: formatString = rb.getString(messageId);
089: } catch (java.util.MissingResourceException mre) {
090: throw new RuntimeException(
091: "no message resource found for message property "
092: + messageId);
093: }
094:
095: if (args == null) {
096: return formatString;
097: } else {
098: MessageFormat formatter = new MessageFormat(formatString);
099: return formatter.format(args);
100: }
101: }
102:
103: public void warning(String message, String sourceName, int line,
104: String lineSource, int lineOffset) {
105: if (!reportWarnings)
106: return;
107: Object[] errArgs = { formatMessage(message, sourceName, line) };
108: message = getMessage("msg.warning", errArgs);
109: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
110: + message });
111: if (null != lineSource) {
112: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
113: + lineSource });
114: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
115: + buildIndicator(lineOffset) });
116: }
117: }
118:
119: public void error(String message, String sourceName, int line,
120: String lineSource, int lineOffset) {
121: hasReportedErrorFlag = true;
122: message = formatMessage(message, sourceName, line);
123: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
124: + message });
125: if (null != lineSource) {
126: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
127: + lineSource });
128: Log.logRB(Resource.JSERROR, new Object[] { messagePrefix
129: + buildIndicator(lineOffset) });
130: }
131: }
132:
133: public EvaluatorException runtimeError(String message,
134: String sourceName, int line, String lineSource,
135: int lineOffset) {
136: error(message, sourceName, line, lineSource, lineOffset);
137: return new EvaluatorException(message);
138: }
139:
140: public boolean hasReportedError() {
141: return hasReportedErrorFlag;
142: }
143:
144: public boolean isReportingWarnings() {
145: return this .reportWarnings;
146: }
147:
148: public void setIsReportingWarnings(boolean reportWarnings) {
149: this .reportWarnings = reportWarnings;
150: }
151:
152: private String formatMessage(String message, String sourceName,
153: int line) {
154: if (line > 0) {
155: if (sourceName != null) {
156: Object[] errArgs = { sourceName, new Integer(line),
157: message };
158: return getMessage("msg.format3", errArgs);
159: } else {
160: Object[] errArgs = { new Integer(line), message };
161: return getMessage("msg.format2", errArgs);
162: }
163: } else {
164: Object[] errArgs = { message };
165: return getMessage("msg.format1", errArgs);
166: }
167: }
168:
169: private String buildIndicator(int offset) {
170: StringBuffer sb = new StringBuffer();
171: for (int i = 0; i < offset - 1; i++)
172: sb.append(".");
173: sb.append("^");
174: return sb.toString();
175: }
176:
177: private final String messagePrefix = "js: ";
178: private boolean hasReportedErrorFlag;
179: private boolean reportWarnings;
180: }
|