src/share/classes/com/sun/tools/javadoc/Messager.java

Thu, 15 Nov 2012 14:41:31 -0800

author
jjg
date
Thu, 15 Nov 2012 14:41:31 -0800
changeset 1411
467f4f754368
parent 1359
25e14ad23cef
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8003257: refactor javadoc tool option handling
Reviewed-by: darcy

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

mercurial