| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.tools; |
| 8 |
|
| 9 |
import org.mozilla.javascript.*; |
| 10 |
|
| 11 |
import java.text.MessageFormat; |
| 12 |
import java.io.*; |
| 13 |
import java.util.*; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class ToolErrorReporter implements ErrorReporter { |
| 21 |
|
| 22 |
public ToolErrorReporter(boolean reportWarnings) { |
| 23 |
this(reportWarnings, System.err); |
| 24 |
} |
| 25 |
|
| 26 |
public ToolErrorReporter(boolean reportWarnings, PrintStream err) { |
| 27 |
this.reportWarnings = reportWarnings; |
| 28 |
this.err = err; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static String getMessage(String messageId) { |
| 37 |
return getMessage(messageId, (Object []) null); |
| 38 |
} |
| 39 |
|
| 40 |
public static String getMessage(String messageId, String argument) { |
| 41 |
Object[] args = { argument }; |
| 42 |
return getMessage(messageId, args); |
| 43 |
} |
| 44 |
|
| 45 |
public static String getMessage(String messageId, Object arg1, Object arg2) |
| 46 |
{ |
| 47 |
Object[] args = { arg1, arg2 }; |
| 48 |
return getMessage(messageId, args); |
| 49 |
} |
| 50 |
|
| 51 |
public static String getMessage(String messageId, Object[] args) { |
| 52 |
Context cx = Context.getCurrentContext(); |
| 53 |
Locale locale = cx == null ? Locale.getDefault() : cx.getLocale(); |
| 54 |
|
| 55 |
|
| 56 |
ResourceBundle rb = ResourceBundle.getBundle |
| 57 |
("org.mozilla.javascript.tools.resources.Messages", locale); |
| 58 |
|
| 59 |
String formatString; |
| 60 |
try { |
| 61 |
formatString = rb.getString(messageId); |
| 62 |
} catch (java.util.MissingResourceException mre) { |
| 63 |
throw new RuntimeException("no message resource found for message property " |
| 64 |
+ messageId); |
| 65 |
} |
| 66 |
|
| 67 |
if (args == null) { |
| 68 |
return formatString; |
| 69 |
} else { |
| 70 |
MessageFormat formatter = new MessageFormat(formatString); |
| 71 |
return formatter.format(args); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
private static String getExceptionMessage(RhinoException ex) |
| 76 |
{ |
| 77 |
String msg; |
| 78 |
if (ex instanceof JavaScriptException) { |
| 79 |
msg = getMessage("msg.uncaughtJSException", ex.details()); |
| 80 |
} else if (ex instanceof EcmaError) { |
| 81 |
msg = getMessage("msg.uncaughtEcmaError", ex.details()); |
| 82 |
} else if (ex instanceof EvaluatorException) { |
| 83 |
msg = ex.details(); |
| 84 |
} else { |
| 85 |
msg = ex.toString(); |
| 86 |
} |
| 87 |
return msg; |
| 88 |
} |
| 89 |
|
| 90 |
public void warning(String message, String sourceName, int line, |
| 91 |
String lineSource, int lineOffset) |
| 92 |
{ |
| 93 |
if (!reportWarnings) |
| 94 |
return; |
| 95 |
reportErrorMessage(message, sourceName, line, lineSource, lineOffset, |
| 96 |
true); |
| 97 |
} |
| 98 |
|
| 99 |
public void error(String message, String sourceName, int line, |
| 100 |
String lineSource, int lineOffset) |
| 101 |
{ |
| 102 |
hasReportedErrorFlag = true; |
| 103 |
reportErrorMessage(message, sourceName, line, lineSource, lineOffset, |
| 104 |
false); |
| 105 |
} |
| 106 |
|
| 107 |
public EvaluatorException runtimeError(String message, String sourceName, |
| 108 |
int line, String lineSource, |
| 109 |
int lineOffset) |
| 110 |
{ |
| 111 |
return new EvaluatorException(message, sourceName, line, |
| 112 |
lineSource, lineOffset); |
| 113 |
} |
| 114 |
|
| 115 |
public boolean hasReportedError() { |
| 116 |
return hasReportedErrorFlag; |
| 117 |
} |
| 118 |
|
| 119 |
public boolean isReportingWarnings() { |
| 120 |
return this.reportWarnings; |
| 121 |
} |
| 122 |
|
| 123 |
public void setIsReportingWarnings(boolean reportWarnings) { |
| 124 |
this.reportWarnings = reportWarnings; |
| 125 |
} |
| 126 |
|
| 127 |
public static void reportException(ErrorReporter er, RhinoException ex) |
| 128 |
{ |
| 129 |
if (er instanceof ToolErrorReporter) { |
| 130 |
((ToolErrorReporter)er).reportException(ex); |
| 131 |
} else { |
| 132 |
String msg = getExceptionMessage(ex); |
| 133 |
er.error(msg, ex.sourceName(), ex.lineNumber(), |
| 134 |
ex.lineSource(), ex.columnNumber()); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void reportException(RhinoException ex) |
| 139 |
{ |
| 140 |
if (ex instanceof WrappedException) { |
| 141 |
WrappedException we = (WrappedException)ex; |
| 142 |
we.printStackTrace(err); |
| 143 |
} else { |
| 144 |
String lineSeparator = |
| 145 |
SecurityUtilities.getSystemProperty("line.separator"); |
| 146 |
String msg = getExceptionMessage(ex) + lineSeparator + |
| 147 |
ex.getScriptStackTrace(); |
| 148 |
reportErrorMessage(msg, ex.sourceName(), ex.lineNumber(), |
| 149 |
ex.lineSource(), ex.columnNumber(), false); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
private void reportErrorMessage(String message, String sourceName, int line, |
| 154 |
String lineSource, int lineOffset, |
| 155 |
boolean justWarning) |
| 156 |
{ |
| 157 |
if (line > 0) { |
| 158 |
String lineStr = String.valueOf(line); |
| 159 |
if (sourceName != null) { |
| 160 |
Object[] args = { sourceName, lineStr, message }; |
| 161 |
message = getMessage("msg.format3", args); |
| 162 |
} else { |
| 163 |
Object[] args = { lineStr, message }; |
| 164 |
message = getMessage("msg.format2", args); |
| 165 |
} |
| 166 |
} else { |
| 167 |
Object[] args = { message }; |
| 168 |
message = getMessage("msg.format1", args); |
| 169 |
} |
| 170 |
if (justWarning) { |
| 171 |
message = getMessage("msg.warning", message); |
| 172 |
} |
| 173 |
err.println(messagePrefix + message); |
| 174 |
if (null != lineSource) { |
| 175 |
err.println(messagePrefix + lineSource); |
| 176 |
err.println(messagePrefix + buildIndicator(lineOffset)); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private String buildIndicator(int offset){ |
| 181 |
StringBuilder sb = new StringBuilder(); |
| 182 |
for (int i = 0; i < offset-1; i++) |
| 183 |
sb.append("."); |
| 184 |
sb.append("^"); |
| 185 |
return sb.toString(); |
| 186 |
} |
| 187 |
|
| 188 |
private final static String messagePrefix = "js: "; |
| 189 |
private boolean hasReportedErrorFlag; |
| 190 |
private boolean reportWarnings; |
| 191 |
private PrintStream err; |
| 192 |
} |