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

Fri, 25 Feb 2011 12:09:33 -0800

author
jjg
date
Fri, 25 Feb 2011 12:09:33 -0800
changeset 893
8f0dcb9499db
parent 798
4868a36f6fd8
child 912
5e6c661891da
permissions
-rw-r--r--

7021650: fix Context issues
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 1997, 2011, 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.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(Context context,
    61                                    final String programName) {
    62         context.put(logKey, new Context.Factory<Log>() {
    63             public Log make(Context c) {
    64                 return new Messager(c,
    65                                     programName);
    66             }
    67         });
    68     }
    69     public static void preRegister(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(Context c) {
    76                 return new Messager(c,
    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     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     @SuppressWarnings("deprecation")
   115     protected Messager(Context context,
   116                        String programName,
   117                        PrintWriter errWriter,
   118                        PrintWriter warnWriter,
   119                        PrintWriter noticeWriter) {
   120         super(context, errWriter, warnWriter, noticeWriter);
   121         this.programName = programName;
   122     }
   124     @Override
   125     protected int getDefaultMaxErrors() {
   126         return Integer.MAX_VALUE;
   127     }
   129     @Override
   130     protected int getDefaultMaxWarnings() {
   131         return Integer.MAX_VALUE;
   132     }
   134     /**
   135      * Reset resource bundle, eg. locale has changed.
   136      */
   137     public void reset() {
   138         messageRB = null;
   139     }
   141     /**
   142      * Get string from ResourceBundle, initialize ResourceBundle
   143      * if needed.
   144      */
   145     private String getString(String key) {
   146         ResourceBundle messageRB = this.messageRB;
   147         if (messageRB == null) {
   148             try {
   149                 this.messageRB = messageRB =
   150                     ResourceBundle.getBundle(
   151                           "com.sun.tools.javadoc.resources.javadoc");
   152             } catch (MissingResourceException e) {
   153                 throw new Error("Fatal: Resource for javadoc is missing");
   154             }
   155         }
   156         return messageRB.getString(key);
   157     }
   159     /**
   160      * get and format message string from resource
   161      *
   162      * @param key selects message from resource
   163      */
   164     String getText(String key) {
   165         return getText(key, (String)null);
   166     }
   168     /**
   169      * get and format message string from resource
   170      *
   171      * @param key selects message from resource
   172      * @param a1 first argument
   173      */
   174     String getText(String key, String a1) {
   175         return getText(key, a1, 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      */
   185     String getText(String key, String a1, String a2) {
   186         return getText(key, a1, a2, null);
   187     }
   189     /**
   190      * get and format message string from resource
   191      *
   192      * @param key selects message from resource
   193      * @param a1 first argument
   194      * @param a2 second argument
   195      * @param a3 third argument
   196      */
   197     String getText(String key, String a1, String a2, String a3) {
   198         return getText(key, a1, a2, a3, null);
   199     }
   201     /**
   202      * get and format message string from resource
   203      *
   204      * @param key selects message from resource
   205      * @param a1 first argument
   206      * @param a2 second argument
   207      * @param a3 third argument
   208      * @param a4 fourth argument
   209      */
   210     String getText(String key, String a1, String a2, String a3,
   211                           String a4) {
   212         try {
   213             String message = getString(key);
   214             String[] args = new String[4];
   215             args[0] = a1;
   216             args[1] = a2;
   217             args[2] = a3;
   218             args[3] = a4;
   219             return MessageFormat.format(message, (Object[])args);
   220         } catch (MissingResourceException e) {
   221             return "********** Resource for javadoc is broken. There is no " +
   222                 key + " key in resource.";
   223         }
   224     }
   226     /**
   227      * Print error message, increment error count.
   228      * Part of DocErrorReporter.
   229      *
   230      * @param msg message to print
   231      */
   232     public void printError(String msg) {
   233         printError(null, msg);
   234     }
   236     /**
   237      * Print error message, increment error count.
   238      * Part of DocErrorReporter.
   239      *
   240      * @param pos the position where the error occurs
   241      * @param msg message to print
   242      */
   243     public void printError(SourcePosition pos, String msg) {
   244         if (nerrors < MaxErrors) {
   245             String prefix = (pos == null) ? programName : pos.toString();
   246             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   247             errWriter.flush();
   248             prompt();
   249             nerrors++;
   250         }
   251     }
   253     /**
   254      * Print warning message, increment warning count.
   255      * Part of DocErrorReporter.
   256      *
   257      * @param msg message to print
   258      */
   259     public void printWarning(String msg) {
   260         printWarning(null, msg);
   261     }
   263     /**
   264      * Print warning message, increment warning count.
   265      * Part of DocErrorReporter.
   266      *
   267      * @param pos the position where the error occurs
   268      * @param msg message to print
   269      */
   270     public void printWarning(SourcePosition pos, String msg) {
   271         if (nwarnings < MaxWarnings) {
   272             String prefix = (pos == null) ? programName : pos.toString();
   273             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   274             warnWriter.flush();
   275             nwarnings++;
   276         }
   277     }
   279     /**
   280      * Print a message.
   281      * Part of DocErrorReporter.
   282      *
   283      * @param msg message to print
   284      */
   285     public void printNotice(String msg) {
   286         printNotice(null, msg);
   287     }
   289     /**
   290      * Print a message.
   291      * Part of DocErrorReporter.
   292      *
   293      * @param pos the position where the error occurs
   294      * @param msg message to print
   295      */
   296     public void printNotice(SourcePosition pos, String msg) {
   297         if (pos == null)
   298             noticeWriter.println(msg);
   299         else
   300             noticeWriter.println(pos + ": " + msg);
   301         noticeWriter.flush();
   302     }
   304     /**
   305      * Print error message, increment error count.
   306      *
   307      * @param key selects message from resource
   308      */
   309     public void error(SourcePosition pos, String key) {
   310         printError(pos, getText(key));
   311     }
   313     /**
   314      * Print error message, increment error count.
   315      *
   316      * @param key selects message from resource
   317      * @param a1 first argument
   318      */
   319     public void error(SourcePosition pos, String key, String a1) {
   320         printError(pos, getText(key, a1));
   321     }
   323     /**
   324      * Print error message, increment error count.
   325      *
   326      * @param key selects message from resource
   327      * @param a1 first argument
   328      * @param a2 second argument
   329      */
   330     public void error(SourcePosition pos, String key, String a1, String a2) {
   331         printError(pos, getText(key, a1, a2));
   332     }
   334     /**
   335      * Print error message, increment error count.
   336      *
   337      * @param key selects message from resource
   338      * @param a1 first argument
   339      * @param a2 second argument
   340      * @param a3 third argument
   341      */
   342     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   343         printError(pos, getText(key, a1, a2, a3));
   344     }
   346     /**
   347      * Print warning message, increment warning count.
   348      *
   349      * @param key selects message from resource
   350      */
   351     public void warning(SourcePosition pos, String key) {
   352         printWarning(pos, getText(key));
   353     }
   355     /**
   356      * Print warning message, increment warning count.
   357      *
   358      * @param key selects message from resource
   359      * @param a1 first argument
   360      */
   361     public void warning(SourcePosition pos, String key, String a1) {
   362         printWarning(pos, getText(key, a1));
   363     }
   365     /**
   366      * Print warning message, increment warning count.
   367      *
   368      * @param key selects message from resource
   369      * @param a1 first argument
   370      * @param a2 second argument
   371      */
   372     public void warning(SourcePosition pos, String key, String a1, String a2) {
   373         printWarning(pos, getText(key, a1, a2));
   374     }
   376     /**
   377      * Print warning message, increment warning count.
   378      *
   379      * @param key selects message from resource
   380      * @param a1 first argument
   381      * @param a2 second argument
   382      * @param a3 third argument
   383      */
   384     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   385         printWarning(pos, getText(key, a1, a2, a3));
   386     }
   388     /**
   389      * Print warning message, increment warning count.
   390      *
   391      * @param key selects message from resource
   392      * @param a1 first argument
   393      * @param a2 second argument
   394      * @param a3 third argument
   395      */
   396     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   397                         String a4) {
   398         printWarning(pos, getText(key, a1, a2, a3, a4));
   399     }
   401     /**
   402      * Print a message.
   403      *
   404      * @param key selects message from resource
   405      */
   406     public void notice(String key) {
   407         printNotice(getText(key));
   408     }
   410     /**
   411      * Print a message.
   412      *
   413      * @param key selects message from resource
   414      * @param a1 first argument
   415      */
   416     public void notice(String key, String a1) {
   417         printNotice(getText(key, a1));
   418     }
   420     /**
   421      * Print a message.
   422      *
   423      * @param key selects message from resource
   424      * @param a1 first argument
   425      * @param a2 second argument
   426      */
   427     public void notice(String key, String a1, String a2) {
   428         printNotice(getText(key, a1, a2));
   429     }
   431     /**
   432      * Print a message.
   433      *
   434      * @param key selects message from resource
   435      * @param a1 first argument
   436      * @param a2 second argument
   437      * @param a3 third argument
   438      */
   439     public void notice(String key, String a1, String a2, String a3) {
   440         printNotice(getText(key, a1, a2, a3));
   441     }
   443     /**
   444      * Return total number of errors, including those recorded
   445      * in the compilation log.
   446      */
   447     public int nerrors() { return nerrors; }
   449     /**
   450      * Return total number of warnings, including those recorded
   451      * in the compilation log.
   452      */
   453     public int nwarnings() { return nwarnings; }
   455     /**
   456      * Print exit message.
   457      */
   458     public void exitNotice() {
   459         int nerrors = nerrors();
   460         int nwarnings = nwarnings();
   461         if (nerrors > 0) {
   462             notice((nerrors > 1) ? "main.errors" : "main.error",
   463                    "" + nerrors);
   464         }
   465         if (nwarnings > 0) {
   466             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   467                    "" + nwarnings);
   468         }
   469     }
   471     /**
   472      * Force program exit, e.g., from a fatal error.
   473      * <p>
   474      * TODO: This method does not really belong here.
   475      */
   476     public void exit() {
   477         throw new ExitJavadoc();
   478     }
   480 }

mercurial