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