KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > js > IVErrorReporter


1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * The contents of this file are subject to the Netscape Public
4  * License Version 1.1 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of
6  * the License at http://www.mozilla.org/NPL/
7  *
8  * Software distributed under the License is distributed on an "AS
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10  * implied. See the License for the specific language governing
11  * rights and limitations under the License.
12  *
13  * The Original Code is Rhino code, released
14  * May 6, 1998.
15  *
16  * The Initial Developer of the Original Code is Netscape
17  * Communications Corporation. Portions created by Netscape are
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All
19  * Rights Reserved.
20  *
21  * Contributor(s):
22  * Norris Boyd
23  * Kurt Westerfeld
24  *
25  * Alternatively, the contents of this file may be used under the
26  * terms of the GNU Public License (the "GPL"), in which case the
27  * provisions of the GPL are applicable instead of those above.
28  * If you wish to allow use of your version of this file only
29  * under the terms of the GPL and not to allow others to use your
30  * version of this file under the NPL, indicate your decision by
31  * deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL. If you do not delete
33  * the provisions above, a recipient may use your version of this
34  * file under either the NPL or the GPL.
35  */

36
37 package org.openlaszlo.iv.flash.js;
38
39 import org.mozilla.javascript.*;
40 import java.text.MessageFormat JavaDoc;
41 import java.io.*;
42 import java.util.*;
43
44
45 import org.openlaszlo.iv.flash.util.*;
46
47 /**
48  * Error reporter for tools.
49  *
50  */

51 public class IVErrorReporter implements ErrorReporter {
52
53     public IVErrorReporter(boolean reportWarnings) {
54         this.reportWarnings = reportWarnings;
55     }
56
57     /**
58      * Look up the message corresponding to messageId in the
59      * org.mozilla.javascript.tools.shell.resources.Messages property file.
60      * For internationalization support.
61      */

62     public static String JavaDoc getMessage(String JavaDoc messageId) {
63         return getMessage(messageId, (Object JavaDoc []) null);
64     }
65
66     public static String JavaDoc getMessage(String JavaDoc messageId, String JavaDoc argument) {
67         Object JavaDoc[] args = { argument };
68         return getMessage(messageId, args);
69     }
70
71     public static String JavaDoc getMessage(String JavaDoc messageId, Object JavaDoc arg1, Object JavaDoc arg2) {
72         Object JavaDoc[] args = { arg1, arg2 };
73         return getMessage(messageId, args);
74     }
75
76     public static String JavaDoc getMessage(String JavaDoc messageId, Object JavaDoc[] args) {
77         Context cx = Context.getCurrentContext();
78         Locale locale = cx == null ? Locale.getDefault() : cx.getLocale();
79
80         // ResourceBundle does cacheing.
81
ResourceBundle rb = ResourceBundle.getBundle
82             ("org.mozilla.javascript.tools.resources.Messages", locale);
83
84         String JavaDoc formatString;
85         try {
86             formatString = rb.getString(messageId);
87         } catch (java.util.MissingResourceException JavaDoc mre) {
88             throw new RuntimeException JavaDoc("no message resource found for message property "+ messageId);
89         }
90
91         if (args == null) {
92             return formatString;
93         } else {
94             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(formatString);
95             return formatter.format(args);
96         }
97     }
98
99     public void warning(String JavaDoc message, String JavaDoc sourceName, int line,
100                         String JavaDoc lineSource, int lineOffset)
101     {
102         if (!reportWarnings)
103             return;
104         Object JavaDoc[] errArgs = { formatMessage(message, sourceName, line) };
105         message = getMessage("msg.warning", errArgs);
106         Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + message});
107         if (null != lineSource) {
108             Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + lineSource});
109             Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + buildIndicator(lineOffset)});
110         }
111     }
112
113     public void error(String JavaDoc message, String JavaDoc sourceName, int line,
114                       String JavaDoc lineSource, int lineOffset)
115     {
116         hasReportedErrorFlag = true;
117         message = formatMessage(message, sourceName, line);
118         Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + message});
119         if (null != lineSource) {
120             Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + lineSource});
121             Log.logRB(Resource.JSERROR, new Object JavaDoc[] {messagePrefix + buildIndicator(lineOffset)});
122         }
123     }
124
125     public EvaluatorException runtimeError(String JavaDoc message, String JavaDoc sourceName,
126                                            int line, String JavaDoc 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 JavaDoc formatMessage(String JavaDoc message, String JavaDoc sourceName, int line) {
146         if (line > 0) {
147             if (sourceName != null) {
148                 Object JavaDoc[] errArgs = { sourceName, new Integer JavaDoc(line), message };
149                 return getMessage("msg.format3", errArgs);
150             } else {
151                 Object JavaDoc[] errArgs = { new Integer JavaDoc(line), message };
152                 return getMessage("msg.format2", errArgs);
153             }
154         } else {
155             Object JavaDoc[] errArgs = { message };
156             return getMessage("msg.format1", errArgs);
157         }
158     }
159
160     private String JavaDoc buildIndicator(int offset){
161         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc messagePrefix = "js: ";
169     private boolean hasReportedErrorFlag;
170     private boolean reportWarnings;
171 }
172
173
Popular Tags