1 36 37 package org.openlaszlo.iv.flash.js; 38 39 import org.mozilla.javascript.*; 40 import java.text.MessageFormat ; 41 import java.io.*; 42 import java.util.*; 43 44 45 import org.openlaszlo.iv.flash.util.*; 46 47 51 public class IVErrorReporter implements ErrorReporter { 52 53 public IVErrorReporter(boolean reportWarnings) { 54 this.reportWarnings = reportWarnings; 55 } 56 57 62 public static String getMessage(String messageId) { 63 return getMessage(messageId, (Object []) null); 64 } 65 66 public static String getMessage(String messageId, String argument) { 67 Object [] args = { argument }; 68 return getMessage(messageId, args); 69 } 70 71 public static String getMessage(String messageId, Object arg1, Object arg2) { 72 Object [] args = { arg1, arg2 }; 73 return getMessage(messageId, args); 74 } 75 76 public static String getMessage(String messageId, Object [] args) { 77 Context cx = Context.getCurrentContext(); 78 Locale locale = cx == null ? Locale.getDefault() : cx.getLocale(); 79 80 ResourceBundle rb = ResourceBundle.getBundle 82 ("org.mozilla.javascript.tools.resources.Messages", locale); 83 84 String formatString; 85 try { 86 formatString = rb.getString(messageId); 87 } catch (java.util.MissingResourceException mre) { 88 throw new RuntimeException ("no message resource found for message property "+ messageId); 89 } 90 91 if (args == null) { 92 return formatString; 93 } else { 94 MessageFormat formatter = new MessageFormat (formatString); 95 return formatter.format(args); 96 } 97 } 98 99 public void warning(String message, String sourceName, int line, 100 String lineSource, int lineOffset) 101 { 102 if (!reportWarnings) 103 return; 104 Object [] errArgs = { formatMessage(message, sourceName, line) }; 105 message = getMessage("msg.warning", errArgs); 106 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + message}); 107 if (null != lineSource) { 108 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + lineSource}); 109 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + buildIndicator(lineOffset)}); 110 } 111 } 112 113 public void error(String message, String sourceName, int line, 114 String lineSource, int lineOffset) 115 { 116 hasReportedErrorFlag = true; 117 message = formatMessage(message, sourceName, line); 118 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + message}); 119 if (null != lineSource) { 120 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + lineSource}); 121 Log.logRB(Resource.JSERROR, new Object [] {messagePrefix + buildIndicator(lineOffset)}); 122 } 123 } 124 125 public EvaluatorException runtimeError(String message, String sourceName, 126 int line, String lineSource, 127 int lineOffset) 128 { 129 error(message, sourceName, line, lineSource, lineOffset); 130 return new EvaluatorException(message); 131 } 132 133 public boolean hasReportedError() { 134 return hasReportedErrorFlag; 135 } 136 137 public boolean isReportingWarnings() { 138 return this.reportWarnings; 139 } 140 141 public void setIsReportingWarnings(boolean reportWarnings) { 142 this.reportWarnings = reportWarnings; 143 } 144 145 private String formatMessage(String message, String sourceName, int line) { 146 if (line > 0) { 147 if (sourceName != null) { 148 Object [] errArgs = { sourceName, new Integer (line), message }; 149 return getMessage("msg.format3", errArgs); 150 } else { 151 Object [] errArgs = { new Integer (line), message }; 152 return getMessage("msg.format2", errArgs); 153 } 154 } else { 155 Object [] errArgs = { message }; 156 return getMessage("msg.format1", errArgs); 157 } 158 } 159 160 private String buildIndicator(int offset){ 161 StringBuffer sb = new StringBuffer (); 162 for (int i = 0; i < offset-1; i++) 163 sb.append("."); 164 sb.append("^"); 165 return sb.toString(); 166 } 167 168 private final String messagePrefix = "js: "; 169 private boolean hasReportedErrorFlag; 170 private boolean reportWarnings; 171 } 172 173 | Popular Tags |