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

Mon, 25 Mar 2013 16:55:14 -0700

author
mfang
date
Mon, 25 Mar 2013 16:55:14 -0700
changeset 1658
fdf30b225e1c
parent 1483
d2eb08b3f64f
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8010521: jdk8 l10n resource file translation update 2
Reviewed-by: naoto, yhuang

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

mercurial