duke@1: /* jjg@1483: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javadoc; duke@1: jjg@1411: import java.io.PrintWriter; jjg@1411: import java.util.Locale; duke@1: duke@1: import com.sun.javadoc.*; duke@1: import com.sun.tools.javac.util.Context; jjg@1411: import com.sun.tools.javac.util.JCDiagnostic; jjg@1413: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; jjg@1411: import com.sun.tools.javac.util.JavacMessages; jjg@1357: import com.sun.tools.javac.util.Log; duke@1: duke@1: /** duke@1: * Utility for integrating with javadoc tools and for localization. duke@1: * Handle Resources. Access to error and warning counts. duke@1: * Message formatting. duke@1: *
duke@1: * Also provides implementation for DocErrorReporter. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. jjg@1359: * duke@1: * @see java.util.ResourceBundle duke@1: * @see java.text.MessageFormat duke@1: * @author Neal Gafter (rewrite) duke@1: */ duke@1: public class Messager extends Log implements DocErrorReporter { jjg@1411: public static final SourcePosition NOPOS = null; duke@1: duke@1: /** Get the current messager, which is also the compiler log. */ duke@1: public static Messager instance0(Context context) { duke@1: Log instance = context.get(logKey); duke@1: if (instance == null || !(instance instanceof Messager)) duke@1: throw new InternalError("no messager instance!"); duke@1: return (Messager)instance; duke@1: } duke@1: jjg@893: public static void preRegister(Context context, duke@1: final String programName) { duke@1: context.put(logKey, new Context.Factory() { jjg@893: public Log make(Context c) { jjg@893: return new Messager(c, duke@1: programName); duke@1: } duke@1: }); duke@1: } jjg@893: public static void preRegister(Context context, duke@1: final String programName, duke@1: final PrintWriter errWriter, duke@1: final PrintWriter warnWriter, duke@1: final PrintWriter noticeWriter) { duke@1: context.put(logKey, new Context.Factory() { jjg@893: public Log make(Context c) { jjg@893: return new Messager(c, duke@1: programName, duke@1: errWriter, duke@1: warnWriter, duke@1: noticeWriter); duke@1: } duke@1: }); duke@1: } duke@1: duke@1: public class ExitJavadoc extends Error { duke@1: private static final long serialVersionUID = 0; duke@1: } duke@1: jjg@584: final String programName; duke@1: jjg@1411: private Locale locale; jjg@1411: private final JavacMessages messages; jjg@1411: private final JCDiagnostic.Factory javadocDiags; duke@1: duke@1: /** The default writer for diagnostics duke@1: */ duke@1: static final PrintWriter defaultErrWriter = new PrintWriter(System.err); duke@1: static final PrintWriter defaultWarnWriter = new PrintWriter(System.err); duke@1: static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out); duke@1: duke@1: /** duke@1: * Constructor duke@1: * @param programName Name of the program (for error messages). duke@1: */ duke@1: protected Messager(Context context, String programName) { duke@1: this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter); duke@1: } duke@1: duke@1: /** duke@1: * Constructor duke@1: * @param programName Name of the program (for error messages). duke@1: * @param errWriter Stream for error messages duke@1: * @param warnWriter Stream for warnings duke@1: * @param noticeWriter Stream for other messages duke@1: */ jjg@198: @SuppressWarnings("deprecation") duke@1: protected Messager(Context context, duke@1: String programName, duke@1: PrintWriter errWriter, duke@1: PrintWriter warnWriter, duke@1: PrintWriter noticeWriter) { duke@1: super(context, errWriter, warnWriter, noticeWriter); jjg@1411: messages = JavacMessages.instance(context); jjg@1411: messages.add("com.sun.tools.javadoc.resources.javadoc"); jjg@1411: javadocDiags = new JCDiagnostic.Factory(messages, "javadoc"); duke@1: this.programName = programName; duke@1: } duke@1: jjg@1411: public void setLocale(Locale locale) { jjg@1411: this.locale = locale; duke@1: } duke@1: duke@1: /** duke@1: * get and format message string from resource duke@1: * duke@1: * @param key selects message from resource jjg@1411: * @param args arguments for the message duke@1: */ jjg@1411: String getText(String key, Object... args) { jjg@1411: return messages.getLocalizedString(locale, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Print error message, increment error count. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param msg message to print duke@1: */ duke@1: public void printError(String msg) { duke@1: printError(null, msg); duke@1: } duke@1: duke@1: /** duke@1: * Print error message, increment error count. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param pos the position where the error occurs duke@1: * @param msg message to print duke@1: */ duke@1: public void printError(SourcePosition pos, String msg) { jjg@1413: if (diagListener != null) { jjg@1413: report(DiagnosticType.ERROR, pos, msg); jjg@1413: return; jjg@1413: } jjg@1413: jjg@584: if (nerrors < MaxErrors) { jjg@584: String prefix = (pos == null) ? programName : pos.toString(); jjg@584: errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); jjg@584: errWriter.flush(); jjg@584: prompt(); jjg@584: nerrors++; jjg@584: } duke@1: } duke@1: duke@1: /** duke@1: * Print warning message, increment warning count. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param msg message to print duke@1: */ duke@1: public void printWarning(String msg) { duke@1: printWarning(null, msg); duke@1: } duke@1: duke@1: /** duke@1: * Print warning message, increment warning count. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param pos the position where the error occurs duke@1: * @param msg message to print duke@1: */ duke@1: public void printWarning(SourcePosition pos, String msg) { jjg@1413: if (diagListener != null) { jjg@1413: report(DiagnosticType.WARNING, pos, msg); jjg@1413: return; jjg@1413: } jjg@1413: jjg@584: if (nwarnings < MaxWarnings) { jjg@584: String prefix = (pos == null) ? programName : pos.toString(); jjg@584: warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); jjg@584: warnWriter.flush(); jjg@584: nwarnings++; jjg@584: } duke@1: } duke@1: duke@1: /** duke@1: * Print a message. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param msg message to print duke@1: */ duke@1: public void printNotice(String msg) { duke@1: printNotice(null, msg); duke@1: } duke@1: duke@1: /** duke@1: * Print a message. duke@1: * Part of DocErrorReporter. duke@1: * duke@1: * @param pos the position where the error occurs duke@1: * @param msg message to print duke@1: */ duke@1: public void printNotice(SourcePosition pos, String msg) { jjg@1413: if (diagListener != null) { jjg@1413: report(DiagnosticType.NOTE, pos, msg); jjg@1413: return; jjg@1413: } jjg@1413: duke@1: if (pos == null) duke@1: noticeWriter.println(msg); duke@1: else duke@1: noticeWriter.println(pos + ": " + msg); duke@1: noticeWriter.flush(); duke@1: } duke@1: duke@1: /** duke@1: * Print error message, increment error count. duke@1: * duke@1: * @param key selects message from resource duke@1: */ jjg@1411: public void error(SourcePosition pos, String key, Object... args) { jjg@1411: printError(pos, getText(key, args)); duke@1: } duke@1: duke@1: /** duke@1: * Print warning message, increment warning count. duke@1: * duke@1: * @param key selects message from resource duke@1: */ jjg@1411: public void warning(SourcePosition pos, String key, Object... args) { jjg@1411: printWarning(pos, getText(key, args)); duke@1: } duke@1: duke@1: /** duke@1: * Print a message. duke@1: * duke@1: * @param key selects message from resource duke@1: */ jjg@1411: public void notice(String key, Object... args) { jjg@1411: printNotice(getText(key, args)); duke@1: } duke@1: duke@1: /** duke@1: * Return total number of errors, including those recorded duke@1: * in the compilation log. duke@1: */ duke@1: public int nerrors() { return nerrors; } duke@1: duke@1: /** duke@1: * Return total number of warnings, including those recorded duke@1: * in the compilation log. duke@1: */ duke@1: public int nwarnings() { return nwarnings; } duke@1: duke@1: /** duke@1: * Print exit message. duke@1: */ duke@1: public void exitNotice() { duke@1: if (nerrors > 0) { duke@1: notice((nerrors > 1) ? "main.errors" : "main.error", duke@1: "" + nerrors); duke@1: } duke@1: if (nwarnings > 0) { duke@1: notice((nwarnings > 1) ? "main.warnings" : "main.warning", duke@1: "" + nwarnings); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Force program exit, e.g., from a fatal error. duke@1: *

duke@1: * TODO: This method does not really belong here. duke@1: */ duke@1: public void exit() { duke@1: throw new ExitJavadoc(); duke@1: } jjg@1413: jjg@1413: private void report(DiagnosticType type, SourcePosition pos, String msg) { jjg@1413: switch (type) { jjg@1413: case ERROR: jjg@1413: case WARNING: jjg@1413: Object prefix = (pos == null) ? programName : pos; jjg@1413: report(javadocDiags.create(type, null, null, "msg", prefix, msg)); jjg@1413: break; jjg@1413: jjg@1413: case NOTE: jjg@1413: String key = (pos == null) ? "msg" : "pos.msg"; jjg@1413: report(javadocDiags.create(type, null, null, key, pos, msg)); jjg@1413: break; jjg@1413: jjg@1413: default: jjg@1413: throw new IllegalArgumentException(type.toString()); jjg@1413: } jjg@1413: } duke@1: }