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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 912
5e6c661891da
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1997, 2012, 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;  // Access to 'javac' output streams
    29 import java.text.MessageFormat;
    30 import java.util.MissingResourceException;
    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.Log;
    37 /**
    38  * Utility for integrating with javadoc tools and for localization.
    39  * Handle Resources. Access to error and warning counts.
    40  * Message formatting.
    41  * <br>
    42  * Also provides implementation for DocErrorReporter.
    43  *
    44  * @see java.util.ResourceBundle
    45  * @see java.text.MessageFormat
    46  * @author Neal Gafter (rewrite)
    47  */
    48 public class Messager extends Log implements DocErrorReporter {
    50     /** Get the current messager, which is also the compiler log. */
    51     public static Messager instance0(Context context) {
    52         Log instance = context.get(logKey);
    53         if (instance == null || !(instance instanceof Messager))
    54             throw new InternalError("no messager instance!");
    55         return (Messager)instance;
    56     }
    58     public static void preRegister(Context context,
    59                                    final String programName) {
    60         context.put(logKey, new Context.Factory<Log>() {
    61             public Log make(Context c) {
    62                 return new Messager(c,
    63                                     programName);
    64             }
    65         });
    66     }
    67     public static void preRegister(Context context,
    68                                    final String programName,
    69                                    final PrintWriter errWriter,
    70                                    final PrintWriter warnWriter,
    71                                    final PrintWriter noticeWriter) {
    72         context.put(logKey, new Context.Factory<Log>() {
    73             public Log make(Context c) {
    74                 return new Messager(c,
    75                                     programName,
    76                                     errWriter,
    77                                     warnWriter,
    78                                     noticeWriter);
    79             }
    80         });
    81     }
    83     public class ExitJavadoc extends Error {
    84         private static final long serialVersionUID = 0;
    85     }
    87     final String programName;
    89     private ResourceBundle messageRB = null;
    91     /** The default writer for diagnostics
    92      */
    93     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
    94     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
    95     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
    97     /**
    98      * Constructor
    99      * @param programName  Name of the program (for error messages).
   100      */
   101     protected Messager(Context context, String programName) {
   102         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   103     }
   105     /**
   106      * Constructor
   107      * @param programName  Name of the program (for error messages).
   108      * @param errWriter    Stream for error messages
   109      * @param warnWriter   Stream for warnings
   110      * @param noticeWriter Stream for other messages
   111      */
   112     @SuppressWarnings("deprecation")
   113     protected Messager(Context context,
   114                        String programName,
   115                        PrintWriter errWriter,
   116                        PrintWriter warnWriter,
   117                        PrintWriter noticeWriter) {
   118         super(context, errWriter, warnWriter, noticeWriter);
   119         this.programName = programName;
   120     }
   122     @Override
   123     protected int getDefaultMaxErrors() {
   124         return Integer.MAX_VALUE;
   125     }
   127     @Override
   128     protected int getDefaultMaxWarnings() {
   129         return Integer.MAX_VALUE;
   130     }
   132     /**
   133      * Reset resource bundle, eg. locale has changed.
   134      */
   135     public void reset() {
   136         messageRB = null;
   137     }
   139     /**
   140      * Get string from ResourceBundle, initialize ResourceBundle
   141      * if needed.
   142      */
   143     private String getString(String key) {
   144         if (messageRB == null) {
   145             try {
   146                 messageRB = ResourceBundle.getBundle(
   147                           "com.sun.tools.javadoc.resources.javadoc");
   148             } catch (MissingResourceException e) {
   149                 throw new Error("Fatal: Resource for javadoc is missing");
   150             }
   151         }
   152         return messageRB.getString(key);
   153     }
   155     /**
   156      * get and format message string from resource
   157      *
   158      * @param key selects message from resource
   159      */
   160     String getText(String key) {
   161         return getText(key, (String)null);
   162     }
   164     /**
   165      * get and format message string from resource
   166      *
   167      * @param key selects message from resource
   168      * @param a1 first argument
   169      */
   170     String getText(String key, String a1) {
   171         return getText(key, a1, null);
   172     }
   174     /**
   175      * get and format message string from resource
   176      *
   177      * @param key selects message from resource
   178      * @param a1 first argument
   179      * @param a2 second argument
   180      */
   181     String getText(String key, String a1, String a2) {
   182         return getText(key, a1, a2, null);
   183     }
   185     /**
   186      * get and format message string from resource
   187      *
   188      * @param key selects message from resource
   189      * @param a1 first argument
   190      * @param a2 second argument
   191      * @param a3 third argument
   192      */
   193     String getText(String key, String a1, String a2, String a3) {
   194         return getText(key, a1, a2, a3, null);
   195     }
   197     /**
   198      * get and format message string from resource
   199      *
   200      * @param key selects message from resource
   201      * @param a1 first argument
   202      * @param a2 second argument
   203      * @param a3 third argument
   204      * @param a4 fourth argument
   205      */
   206     String getText(String key, String a1, String a2, String a3,
   207                           String a4) {
   208         try {
   209             String message = getString(key);
   210             String[] args = new String[4];
   211             args[0] = a1;
   212             args[1] = a2;
   213             args[2] = a3;
   214             args[3] = a4;
   215             return MessageFormat.format(message, (Object[])args);
   216         } catch (MissingResourceException e) {
   217             return "********** Resource for javadoc is broken. There is no " +
   218                 key + " key in resource.";
   219         }
   220     }
   222     /**
   223      * Print error message, increment error count.
   224      * Part of DocErrorReporter.
   225      *
   226      * @param msg message to print
   227      */
   228     public void printError(String msg) {
   229         printError(null, msg);
   230     }
   232     /**
   233      * Print error message, increment error count.
   234      * Part of DocErrorReporter.
   235      *
   236      * @param pos the position where the error occurs
   237      * @param msg message to print
   238      */
   239     public void printError(SourcePosition pos, String msg) {
   240         if (nerrors < MaxErrors) {
   241             String prefix = (pos == null) ? programName : pos.toString();
   242             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   243             errWriter.flush();
   244             prompt();
   245             nerrors++;
   246         }
   247     }
   249     /**
   250      * Print warning message, increment warning count.
   251      * Part of DocErrorReporter.
   252      *
   253      * @param msg message to print
   254      */
   255     public void printWarning(String msg) {
   256         printWarning(null, msg);
   257     }
   259     /**
   260      * Print warning message, increment warning count.
   261      * Part of DocErrorReporter.
   262      *
   263      * @param pos the position where the error occurs
   264      * @param msg message to print
   265      */
   266     public void printWarning(SourcePosition pos, String msg) {
   267         if (nwarnings < MaxWarnings) {
   268             String prefix = (pos == null) ? programName : pos.toString();
   269             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   270             warnWriter.flush();
   271             nwarnings++;
   272         }
   273     }
   275     /**
   276      * Print a message.
   277      * Part of DocErrorReporter.
   278      *
   279      * @param msg message to print
   280      */
   281     public void printNotice(String msg) {
   282         printNotice(null, msg);
   283     }
   285     /**
   286      * Print a message.
   287      * Part of DocErrorReporter.
   288      *
   289      * @param pos the position where the error occurs
   290      * @param msg message to print
   291      */
   292     public void printNotice(SourcePosition pos, String msg) {
   293         if (pos == null)
   294             noticeWriter.println(msg);
   295         else
   296             noticeWriter.println(pos + ": " + msg);
   297         noticeWriter.flush();
   298     }
   300     /**
   301      * Print error message, increment error count.
   302      *
   303      * @param key selects message from resource
   304      */
   305     public void error(SourcePosition pos, String key) {
   306         printError(pos, getText(key));
   307     }
   309     /**
   310      * Print error message, increment error count.
   311      *
   312      * @param key selects message from resource
   313      * @param a1 first argument
   314      */
   315     public void error(SourcePosition pos, String key, String a1) {
   316         printError(pos, getText(key, a1));
   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      */
   326     public void error(SourcePosition pos, String key, String a1, String a2) {
   327         printError(pos, getText(key, a1, a2));
   328     }
   330     /**
   331      * Print error message, increment error count.
   332      *
   333      * @param key selects message from resource
   334      * @param a1 first argument
   335      * @param a2 second argument
   336      * @param a3 third argument
   337      */
   338     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   339         printError(pos, getText(key, a1, a2, a3));
   340     }
   342     /**
   343      * Print warning message, increment warning count.
   344      *
   345      * @param key selects message from resource
   346      */
   347     public void warning(SourcePosition pos, String key) {
   348         printWarning(pos, getText(key));
   349     }
   351     /**
   352      * Print warning message, increment warning count.
   353      *
   354      * @param key selects message from resource
   355      * @param a1 first argument
   356      */
   357     public void warning(SourcePosition pos, String key, String a1) {
   358         printWarning(pos, getText(key, a1));
   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      */
   368     public void warning(SourcePosition pos, String key, String a1, String a2) {
   369         printWarning(pos, getText(key, a1, a2));
   370     }
   372     /**
   373      * Print warning message, increment warning count.
   374      *
   375      * @param key selects message from resource
   376      * @param a1 first argument
   377      * @param a2 second argument
   378      * @param a3 third argument
   379      */
   380     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   381         printWarning(pos, getText(key, a1, a2, a3));
   382     }
   384     /**
   385      * Print warning message, increment warning count.
   386      *
   387      * @param key selects message from resource
   388      * @param a1 first argument
   389      * @param a2 second argument
   390      * @param a3 third argument
   391      */
   392     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   393                         String a4) {
   394         printWarning(pos, getText(key, a1, a2, a3, a4));
   395     }
   397     /**
   398      * Print a message.
   399      *
   400      * @param key selects message from resource
   401      */
   402     public void notice(String key) {
   403         printNotice(getText(key));
   404     }
   406     /**
   407      * Print a message.
   408      *
   409      * @param key selects message from resource
   410      * @param a1 first argument
   411      */
   412     public void notice(String key, String a1) {
   413         printNotice(getText(key, a1));
   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      */
   423     public void notice(String key, String a1, String a2) {
   424         printNotice(getText(key, a1, a2));
   425     }
   427     /**
   428      * Print a message.
   429      *
   430      * @param key selects message from resource
   431      * @param a1 first argument
   432      * @param a2 second argument
   433      * @param a3 third argument
   434      */
   435     public void notice(String key, String a1, String a2, String a3) {
   436         printNotice(getText(key, a1, a2, a3));
   437     }
   439     /**
   440      * Return total number of errors, including those recorded
   441      * in the compilation log.
   442      */
   443     public int nerrors() { return nerrors; }
   445     /**
   446      * Return total number of warnings, including those recorded
   447      * in the compilation log.
   448      */
   449     public int nwarnings() { return nwarnings; }
   451     /**
   452      * Print exit message.
   453      */
   454     public void exitNotice() {
   455         if (nerrors > 0) {
   456             notice((nerrors > 1) ? "main.errors" : "main.error",
   457                    "" + nerrors);
   458         }
   459         if (nwarnings > 0) {
   460             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   461                    "" + nwarnings);
   462         }
   463     }
   465     /**
   466      * Force program exit, e.g., from a fatal error.
   467      * <p>
   468      * TODO: This method does not really belong here.
   469      */
   470     public void exit() {
   471         throw new ExitJavadoc();
   472     }
   474 }

mercurial