1 /* |
|
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javadoc; |
|
27 |
|
28 import java.io.PrintWriter; |
|
29 import java.util.Locale; |
|
30 import java.util.ResourceBundle; |
|
31 |
|
32 import com.sun.javadoc.*; |
|
33 import com.sun.tools.javac.util.Context; |
|
34 import com.sun.tools.javac.util.JCDiagnostic; |
|
35 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
|
36 import com.sun.tools.javac.util.JavacMessages; |
|
37 import com.sun.tools.javac.util.Log; |
|
38 |
|
39 /** |
|
40 * Utility for integrating with javadoc tools and for localization. |
|
41 * Handle Resources. Access to error and warning counts. |
|
42 * Message formatting. |
|
43 * <br> |
|
44 * Also provides implementation for DocErrorReporter. |
|
45 * |
|
46 * <p><b>This is NOT part of any supported API. |
|
47 * If you write code that depends on this, you do so at your own risk. |
|
48 * This code and its internal interfaces are subject to change or |
|
49 * deletion without notice.</b> |
|
50 * |
|
51 * @see java.util.ResourceBundle |
|
52 * @see java.text.MessageFormat |
|
53 * @author Neal Gafter (rewrite) |
|
54 */ |
|
55 public class Messager extends Log implements DocErrorReporter { |
|
56 public static final SourcePosition NOPOS = null; |
|
57 |
|
58 /** Get the current messager, which is also the compiler log. */ |
|
59 public static Messager instance0(Context context) { |
|
60 Log instance = context.get(logKey); |
|
61 if (instance == null || !(instance instanceof Messager)) |
|
62 throw new InternalError("no messager instance!"); |
|
63 return (Messager)instance; |
|
64 } |
|
65 |
|
66 public static void preRegister(Context context, |
|
67 final String programName) { |
|
68 context.put(logKey, new Context.Factory<Log>() { |
|
69 public Log make(Context c) { |
|
70 return new Messager(c, |
|
71 programName); |
|
72 } |
|
73 }); |
|
74 } |
|
75 public static void preRegister(Context context, |
|
76 final String programName, |
|
77 final PrintWriter errWriter, |
|
78 final PrintWriter warnWriter, |
|
79 final PrintWriter noticeWriter) { |
|
80 context.put(logKey, new Context.Factory<Log>() { |
|
81 public Log make(Context c) { |
|
82 return new Messager(c, |
|
83 programName, |
|
84 errWriter, |
|
85 warnWriter, |
|
86 noticeWriter); |
|
87 } |
|
88 }); |
|
89 } |
|
90 |
|
91 public class ExitJavadoc extends Error { |
|
92 private static final long serialVersionUID = 0; |
|
93 } |
|
94 |
|
95 final String programName; |
|
96 |
|
97 private Locale locale; |
|
98 private final JavacMessages messages; |
|
99 private final JCDiagnostic.Factory javadocDiags; |
|
100 |
|
101 /** The default writer for diagnostics |
|
102 */ |
|
103 static final PrintWriter defaultErrWriter = new PrintWriter(System.err); |
|
104 static final PrintWriter defaultWarnWriter = new PrintWriter(System.err); |
|
105 static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out); |
|
106 |
|
107 /** |
|
108 * Constructor |
|
109 * @param programName Name of the program (for error messages). |
|
110 */ |
|
111 protected Messager(Context context, String programName) { |
|
112 this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Constructor |
|
117 * @param programName Name of the program (for error messages). |
|
118 * @param errWriter Stream for error messages |
|
119 * @param warnWriter Stream for warnings |
|
120 * @param noticeWriter Stream for other messages |
|
121 */ |
|
122 @SuppressWarnings("deprecation") |
|
123 protected Messager(Context context, |
|
124 String programName, |
|
125 PrintWriter errWriter, |
|
126 PrintWriter warnWriter, |
|
127 PrintWriter noticeWriter) { |
|
128 super(context, errWriter, warnWriter, noticeWriter); |
|
129 messages = JavacMessages.instance(context); |
|
130 messages.add(locale -> ResourceBundle.getBundle("com.sun.tools.javadoc.resources.javadoc", |
|
131 locale)); |
|
132 javadocDiags = new JCDiagnostic.Factory(messages, "javadoc"); |
|
133 this.programName = programName; |
|
134 |
|
135 } |
|
136 |
|
137 public void setLocale(Locale locale) { |
|
138 this.locale = locale; |
|
139 } |
|
140 |
|
141 /** |
|
142 * get and format message string from resource |
|
143 * |
|
144 * @param key selects message from resource |
|
145 * @param args arguments for the message |
|
146 */ |
|
147 String getText(String key, Object... args) { |
|
148 return messages.getLocalizedString(locale, key, args); |
|
149 } |
|
150 |
|
151 /** |
|
152 * Print error message, increment error count. |
|
153 * Part of DocErrorReporter. |
|
154 * |
|
155 * @param msg message to print |
|
156 */ |
|
157 public void printError(String msg) { |
|
158 printError(null, msg); |
|
159 } |
|
160 |
|
161 /** |
|
162 * Print error message, increment error count. |
|
163 * Part of DocErrorReporter. |
|
164 * |
|
165 * @param pos the position where the error occurs |
|
166 * @param msg message to print |
|
167 */ |
|
168 public void printError(SourcePosition pos, String msg) { |
|
169 if (diagListener != null) { |
|
170 report(DiagnosticType.ERROR, pos, msg); |
|
171 return; |
|
172 } |
|
173 |
|
174 if (nerrors < MaxErrors) { |
|
175 String prefix = (pos == null) ? programName : pos.toString(); |
|
176 errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); |
|
177 errWriter.flush(); |
|
178 prompt(); |
|
179 nerrors++; |
|
180 } |
|
181 } |
|
182 |
|
183 /** |
|
184 * Print warning message, increment warning count. |
|
185 * Part of DocErrorReporter. |
|
186 * |
|
187 * @param msg message to print |
|
188 */ |
|
189 public void printWarning(String msg) { |
|
190 printWarning(null, msg); |
|
191 } |
|
192 |
|
193 /** |
|
194 * Print warning message, increment warning count. |
|
195 * Part of DocErrorReporter. |
|
196 * |
|
197 * @param pos the position where the error occurs |
|
198 * @param msg message to print |
|
199 */ |
|
200 public void printWarning(SourcePosition pos, String msg) { |
|
201 if (diagListener != null) { |
|
202 report(DiagnosticType.WARNING, pos, msg); |
|
203 return; |
|
204 } |
|
205 |
|
206 if (nwarnings < MaxWarnings) { |
|
207 String prefix = (pos == null) ? programName : pos.toString(); |
|
208 warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); |
|
209 warnWriter.flush(); |
|
210 nwarnings++; |
|
211 } |
|
212 } |
|
213 |
|
214 /** |
|
215 * Print a message. |
|
216 * Part of DocErrorReporter. |
|
217 * |
|
218 * @param msg message to print |
|
219 */ |
|
220 public void printNotice(String msg) { |
|
221 printNotice(null, msg); |
|
222 } |
|
223 |
|
224 /** |
|
225 * Print a message. |
|
226 * Part of DocErrorReporter. |
|
227 * |
|
228 * @param pos the position where the error occurs |
|
229 * @param msg message to print |
|
230 */ |
|
231 public void printNotice(SourcePosition pos, String msg) { |
|
232 if (diagListener != null) { |
|
233 report(DiagnosticType.NOTE, pos, msg); |
|
234 return; |
|
235 } |
|
236 |
|
237 if (pos == null) |
|
238 noticeWriter.println(msg); |
|
239 else |
|
240 noticeWriter.println(pos + ": " + msg); |
|
241 noticeWriter.flush(); |
|
242 } |
|
243 |
|
244 /** |
|
245 * Print error message, increment error count. |
|
246 * |
|
247 * @param key selects message from resource |
|
248 */ |
|
249 public void error(SourcePosition pos, String key, Object... args) { |
|
250 printError(pos, getText(key, args)); |
|
251 } |
|
252 |
|
253 /** |
|
254 * Print warning message, increment warning count. |
|
255 * |
|
256 * @param key selects message from resource |
|
257 */ |
|
258 public void warning(SourcePosition pos, String key, Object... args) { |
|
259 printWarning(pos, getText(key, args)); |
|
260 } |
|
261 |
|
262 /** |
|
263 * Print a message. |
|
264 * |
|
265 * @param key selects message from resource |
|
266 */ |
|
267 public void notice(String key, Object... args) { |
|
268 printNotice(getText(key, args)); |
|
269 } |
|
270 |
|
271 /** |
|
272 * Return total number of errors, including those recorded |
|
273 * in the compilation log. |
|
274 */ |
|
275 public int nerrors() { return nerrors; } |
|
276 |
|
277 /** |
|
278 * Return total number of warnings, including those recorded |
|
279 * in the compilation log. |
|
280 */ |
|
281 public int nwarnings() { return nwarnings; } |
|
282 |
|
283 /** |
|
284 * Print exit message. |
|
285 */ |
|
286 public void exitNotice() { |
|
287 if (nerrors > 0) { |
|
288 notice((nerrors > 1) ? "main.errors" : "main.error", |
|
289 "" + nerrors); |
|
290 } |
|
291 if (nwarnings > 0) { |
|
292 notice((nwarnings > 1) ? "main.warnings" : "main.warning", |
|
293 "" + nwarnings); |
|
294 } |
|
295 } |
|
296 |
|
297 /** |
|
298 * Force program exit, e.g., from a fatal error. |
|
299 * <p> |
|
300 * TODO: This method does not really belong here. |
|
301 */ |
|
302 public void exit() { |
|
303 throw new ExitJavadoc(); |
|
304 } |
|
305 |
|
306 private void report(DiagnosticType type, SourcePosition pos, String msg) { |
|
307 switch (type) { |
|
308 case ERROR: |
|
309 case WARNING: |
|
310 Object prefix = (pos == null) ? programName : pos; |
|
311 report(javadocDiags.create(type, null, null, "msg", prefix, msg)); |
|
312 break; |
|
313 |
|
314 case NOTE: |
|
315 String key = (pos == null) ? "msg" : "pos.msg"; |
|
316 report(javadocDiags.create(type, null, null, key, pos, msg)); |
|
317 break; |
|
318 |
|
319 default: |
|
320 throw new IllegalArgumentException(type.toString()); |
|
321 } |
|
322 } |
|
323 } |
|