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

Thu, 18 Sep 2008 18:39:44 -0700

author
jjg
date
Thu, 18 Sep 2008 18:39:44 -0700
changeset 115
829dea15ff99
parent 1
9a66ca7c79fa
child 198
b4b1f7732289
permissions
-rw-r--r--

6744408: Extra ouput is appearing in stderr
Reviewed-by: bpatel

     1 /*
     2  * Copyright 1997-2004 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javadoc;
    28 import java.io.PrintWriter;
    29 import java.text.MessageFormat;
    30 import java.util.ResourceBundle;
    31 import java.util.MissingResourceException;
    33 import com.sun.javadoc.*;
    35 import com.sun.tools.javac.util.Context;
    37 import com.sun.tools.javac.util.Log;  // Access to 'javac' output streams
    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  * @see java.util.ResourceBundle
    47  * @see java.text.MessageFormat
    48  * @author Neal Gafter (rewrite)
    49  */
    50 public class Messager extends Log implements DocErrorReporter {
    52     /** Get the current messager, which is also the compiler log. */
    53     public static Messager instance0(Context context) {
    54         Log instance = context.get(logKey);
    55         if (instance == null || !(instance instanceof Messager))
    56             throw new InternalError("no messager instance!");
    57         return (Messager)instance;
    58     }
    60     public static void preRegister(final Context context,
    61                                    final String programName) {
    62         context.put(logKey, new Context.Factory<Log>() {
    63             public Log make() {
    64                 return new Messager(context,
    65                                     programName);
    66             }
    67         });
    68     }
    69     public static void preRegister(final Context context,
    70                                    final String programName,
    71                                    final PrintWriter errWriter,
    72                                    final PrintWriter warnWriter,
    73                                    final PrintWriter noticeWriter) {
    74         context.put(logKey, new Context.Factory<Log>() {
    75             public Log make() {
    76                 return new Messager(context,
    77                                     programName,
    78                                     errWriter,
    79                                     warnWriter,
    80                                     noticeWriter);
    81             }
    82         });
    83     }
    85     public class ExitJavadoc extends Error {
    86         private static final long serialVersionUID = 0;
    87     }
    89     private final String programName;
    91     private ResourceBundle messageRB = null;
    93     /** The default writer for diagnostics
    94      */
    95     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
    96     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
    97     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
    99     /**
   100      * Constructor
   101      * @param programName  Name of the program (for error messages).
   102      */
   103     protected Messager(Context context, String programName) {
   104         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   105     }
   107     /**
   108      * Constructor
   109      * @param programName  Name of the program (for error messages).
   110      * @param errWriter    Stream for error messages
   111      * @param warnWriter   Stream for warnings
   112      * @param noticeWriter Stream for other messages
   113      */
   114     protected Messager(Context context,
   115                        String programName,
   116                        PrintWriter errWriter,
   117                        PrintWriter warnWriter,
   118                        PrintWriter noticeWriter) {
   119         super(context, errWriter, warnWriter, noticeWriter);
   120         this.programName = programName;
   121     }
   123     /**
   124      * Reset resource bundle, eg. locale has changed.
   125      */
   126     public void reset() {
   127         messageRB = null;
   128     }
   130     /**
   131      * Get string from ResourceBundle, initialize ResourceBundle
   132      * if needed.
   133      */
   134     private String getString(String key) {
   135         ResourceBundle messageRB = this.messageRB;
   136         if (messageRB == null) {
   137             try {
   138                 this.messageRB = messageRB =
   139                     ResourceBundle.getBundle(
   140                           "com.sun.tools.javadoc.resources.javadoc");
   141             } catch (MissingResourceException e) {
   142                 throw new Error("Fatal: Resource for javadoc is missing");
   143             }
   144         }
   145         return messageRB.getString(key);
   146     }
   148     /**
   149      * get and format message string from resource
   150      *
   151      * @param key selects message from resource
   152      */
   153     String getText(String key) {
   154         return getText(key, (String)null);
   155     }
   157     /**
   158      * get and format message string from resource
   159      *
   160      * @param key selects message from resource
   161      * @param a1 first argument
   162      */
   163     String getText(String key, String a1) {
   164         return getText(key, a1, null);
   165     }
   167     /**
   168      * get and format message string from resource
   169      *
   170      * @param key selects message from resource
   171      * @param a1 first argument
   172      * @param a2 second argument
   173      */
   174     String getText(String key, String a1, String a2) {
   175         return getText(key, a1, a2, null);
   176     }
   178     /**
   179      * get and format message string from resource
   180      *
   181      * @param key selects message from resource
   182      * @param a1 first argument
   183      * @param a2 second argument
   184      * @param a3 third argument
   185      */
   186     String getText(String key, String a1, String a2, String a3) {
   187         return getText(key, a1, a2, a3, null);
   188     }
   190     /**
   191      * get and format message string from resource
   192      *
   193      * @param key selects message from resource
   194      * @param a1 first argument
   195      * @param a2 second argument
   196      * @param a3 third argument
   197      * @param a4 fourth argument
   198      */
   199     String getText(String key, String a1, String a2, String a3,
   200                           String a4) {
   201         try {
   202             String message = getString(key);
   203             String[] args = new String[4];
   204             args[0] = a1;
   205             args[1] = a2;
   206             args[2] = a3;
   207             args[3] = a4;
   208             return MessageFormat.format(message, (Object[])args);
   209         } catch (MissingResourceException e) {
   210             return "********** Resource for javadoc is broken. There is no " +
   211                 key + " key in resource.";
   212         }
   213     }
   215     /**
   216      * Print error message, increment error count.
   217      * Part of DocErrorReporter.
   218      *
   219      * @param msg message to print
   220      */
   221     public void printError(String msg) {
   222         printError(null, msg);
   223     }
   225     /**
   226      * Print error message, increment error count.
   227      * Part of DocErrorReporter.
   228      *
   229      * @param pos the position where the error occurs
   230      * @param msg message to print
   231      */
   232     public void printError(SourcePosition pos, String msg) {
   233         String prefix = (pos == null) ? programName : pos.toString();
   234         errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   235         errWriter.flush();
   236         prompt();
   237         nerrors++;
   238     }
   240     /**
   241      * Print warning message, increment warning count.
   242      * Part of DocErrorReporter.
   243      *
   244      * @param msg message to print
   245      */
   246     public void printWarning(String msg) {
   247         printWarning(null, msg);
   248     }
   250     /**
   251      * Print warning message, increment warning count.
   252      * Part of DocErrorReporter.
   253      *
   254      * @param pos the position where the error occurs
   255      * @param msg message to print
   256      */
   257     public void printWarning(SourcePosition pos, String msg) {
   258         String prefix = (pos == null) ? programName : pos.toString();
   259         warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   260         warnWriter.flush();
   261         nwarnings++;
   262     }
   264     /**
   265      * Print a message.
   266      * Part of DocErrorReporter.
   267      *
   268      * @param msg message to print
   269      */
   270     public void printNotice(String msg) {
   271         printNotice(null, msg);
   272     }
   274     /**
   275      * Print a message.
   276      * Part of DocErrorReporter.
   277      *
   278      * @param pos the position where the error occurs
   279      * @param msg message to print
   280      */
   281     public void printNotice(SourcePosition pos, String msg) {
   282         if (pos == null)
   283             noticeWriter.println(msg);
   284         else
   285             noticeWriter.println(pos + ": " + msg);
   286         noticeWriter.flush();
   287     }
   289     /**
   290      * Print error message, increment error count.
   291      *
   292      * @param key selects message from resource
   293      */
   294     public void error(SourcePosition pos, String key) {
   295         printError(pos, getText(key));
   296     }
   298     /**
   299      * Print error message, increment error count.
   300      *
   301      * @param key selects message from resource
   302      * @param a1 first argument
   303      */
   304     public void error(SourcePosition pos, String key, String a1) {
   305         printError(pos, getText(key, a1));
   306     }
   308     /**
   309      * Print error message, increment error count.
   310      *
   311      * @param key selects message from resource
   312      * @param a1 first argument
   313      * @param a2 second argument
   314      */
   315     public void error(SourcePosition pos, String key, String a1, String a2) {
   316         printError(pos, getText(key, a1, a2));
   317     }
   319     /**
   320      * Print error message, increment error count.
   321      *
   322      * @param key selects message from resource
   323      * @param a1 first argument
   324      * @param a2 second argument
   325      * @param a3 third argument
   326      */
   327     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   328         printError(pos, getText(key, a1, a2, a3));
   329     }
   331     /**
   332      * Print warning message, increment warning count.
   333      *
   334      * @param key selects message from resource
   335      */
   336     public void warning(SourcePosition pos, String key) {
   337         printWarning(pos, getText(key));
   338     }
   340     /**
   341      * Print warning message, increment warning count.
   342      *
   343      * @param key selects message from resource
   344      * @param a1 first argument
   345      */
   346     public void warning(SourcePosition pos, String key, String a1) {
   347         printWarning(pos, getText(key, a1));
   348     }
   350     /**
   351      * Print warning message, increment warning count.
   352      *
   353      * @param key selects message from resource
   354      * @param a1 first argument
   355      * @param a2 second argument
   356      */
   357     public void warning(SourcePosition pos, String key, String a1, String a2) {
   358         printWarning(pos, getText(key, a1, a2));
   359     }
   361     /**
   362      * Print warning message, increment warning count.
   363      *
   364      * @param key selects message from resource
   365      * @param a1 first argument
   366      * @param a2 second argument
   367      * @param a3 third argument
   368      */
   369     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   370         printWarning(pos, getText(key, a1, a2, a3));
   371     }
   373     /**
   374      * Print warning message, increment warning count.
   375      *
   376      * @param key selects message from resource
   377      * @param a1 first argument
   378      * @param a2 second argument
   379      * @param a3 third argument
   380      */
   381     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   382                         String a4) {
   383         printWarning(pos, getText(key, a1, a2, a3, a4));
   384     }
   386     /**
   387      * Print a message.
   388      *
   389      * @param key selects message from resource
   390      */
   391     public void notice(String key) {
   392         printNotice(getText(key));
   393     }
   395     /**
   396      * Print a message.
   397      *
   398      * @param key selects message from resource
   399      * @param a1 first argument
   400      */
   401     public void notice(String key, String a1) {
   402         printNotice(getText(key, a1));
   403     }
   405     /**
   406      * Print a message.
   407      *
   408      * @param key selects message from resource
   409      * @param a1 first argument
   410      * @param a2 second argument
   411      */
   412     public void notice(String key, String a1, String a2) {
   413         printNotice(getText(key, a1, a2));
   414     }
   416     /**
   417      * Print a message.
   418      *
   419      * @param key selects message from resource
   420      * @param a1 first argument
   421      * @param a2 second argument
   422      * @param a3 third argument
   423      */
   424     public void notice(String key, String a1, String a2, String a3) {
   425         printNotice(getText(key, a1, a2, a3));
   426     }
   428     /**
   429      * Return total number of errors, including those recorded
   430      * in the compilation log.
   431      */
   432     public int nerrors() { return nerrors; }
   434     /**
   435      * Return total number of warnings, including those recorded
   436      * in the compilation log.
   437      */
   438     public int nwarnings() { return nwarnings; }
   440     /**
   441      * Print exit message.
   442      */
   443     public void exitNotice() {
   444         int nerrors = nerrors();
   445         int nwarnings = nwarnings();
   446         if (nerrors > 0) {
   447             notice((nerrors > 1) ? "main.errors" : "main.error",
   448                    "" + nerrors);
   449         }
   450         if (nwarnings > 0) {
   451             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   452                    "" + nwarnings);
   453         }
   454     }
   456     /**
   457      * Force program exit, e.g., from a fatal error.
   458      * <p>
   459      * TODO: This method does not really belong here.
   460      */
   461     public void exit() {
   462         throw new ExitJavadoc();
   463     }
   465 }

mercurial