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

Wed, 10 Oct 2012 16:48:21 -0700

author
jjg
date
Wed, 10 Oct 2012 16:48:21 -0700
changeset 1359
25e14ad23cef
parent 1357
c75be5bc5283
child 1411
467f4f754368
permissions
-rw-r--r--

8000665: fix "internal API" comments on javadoc files
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  *  <p><b>This is NOT part of any supported API.
    45  *  If you write code that depends on this, you do so at your own risk.
    46  *  This code and its internal interfaces are subject to change or
    47  *  deletion without notice.</b>
    48  *
    49  * @see java.util.ResourceBundle
    50  * @see java.text.MessageFormat
    51  * @author Neal Gafter (rewrite)
    52  */
    53 public class Messager extends Log implements DocErrorReporter {
    55     /** Get the current messager, which is also the compiler log. */
    56     public static Messager instance0(Context context) {
    57         Log instance = context.get(logKey);
    58         if (instance == null || !(instance instanceof Messager))
    59             throw new InternalError("no messager instance!");
    60         return (Messager)instance;
    61     }
    63     public static void preRegister(Context context,
    64                                    final String programName) {
    65         context.put(logKey, new Context.Factory<Log>() {
    66             public Log make(Context c) {
    67                 return new Messager(c,
    68                                     programName);
    69             }
    70         });
    71     }
    72     public static void preRegister(Context context,
    73                                    final String programName,
    74                                    final PrintWriter errWriter,
    75                                    final PrintWriter warnWriter,
    76                                    final PrintWriter noticeWriter) {
    77         context.put(logKey, new Context.Factory<Log>() {
    78             public Log make(Context c) {
    79                 return new Messager(c,
    80                                     programName,
    81                                     errWriter,
    82                                     warnWriter,
    83                                     noticeWriter);
    84             }
    85         });
    86     }
    88     public class ExitJavadoc extends Error {
    89         private static final long serialVersionUID = 0;
    90     }
    92     final String programName;
    94     private ResourceBundle messageRB = null;
    96     /** The default writer for diagnostics
    97      */
    98     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
    99     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
   100     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
   102     /**
   103      * Constructor
   104      * @param programName  Name of the program (for error messages).
   105      */
   106     protected Messager(Context context, String programName) {
   107         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   108     }
   110     /**
   111      * Constructor
   112      * @param programName  Name of the program (for error messages).
   113      * @param errWriter    Stream for error messages
   114      * @param warnWriter   Stream for warnings
   115      * @param noticeWriter Stream for other messages
   116      */
   117     @SuppressWarnings("deprecation")
   118     protected Messager(Context context,
   119                        String programName,
   120                        PrintWriter errWriter,
   121                        PrintWriter warnWriter,
   122                        PrintWriter noticeWriter) {
   123         super(context, errWriter, warnWriter, noticeWriter);
   124         this.programName = programName;
   125     }
   127     @Override
   128     protected int getDefaultMaxErrors() {
   129         return Integer.MAX_VALUE;
   130     }
   132     @Override
   133     protected int getDefaultMaxWarnings() {
   134         return Integer.MAX_VALUE;
   135     }
   137     /**
   138      * Reset resource bundle, eg. locale has changed.
   139      */
   140     public void reset() {
   141         messageRB = null;
   142     }
   144     /**
   145      * Get string from ResourceBundle, initialize ResourceBundle
   146      * if needed.
   147      */
   148     private String getString(String key) {
   149         if (messageRB == null) {
   150             try {
   151                 messageRB = ResourceBundle.getBundle(
   152                           "com.sun.tools.javadoc.resources.javadoc");
   153             } catch (MissingResourceException e) {
   154                 throw new Error("Fatal: Resource for javadoc is missing");
   155             }
   156         }
   157         return messageRB.getString(key);
   158     }
   160     /**
   161      * get and format message string from resource
   162      *
   163      * @param key selects message from resource
   164      */
   165     String getText(String key) {
   166         return getText(key, (String)null);
   167     }
   169     /**
   170      * get and format message string from resource
   171      *
   172      * @param key selects message from resource
   173      * @param a1 first argument
   174      */
   175     String getText(String key, String a1) {
   176         return getText(key, a1, null);
   177     }
   179     /**
   180      * get and format message string from resource
   181      *
   182      * @param key selects message from resource
   183      * @param a1 first argument
   184      * @param a2 second argument
   185      */
   186     String getText(String key, String a1, String a2) {
   187         return getText(key, a1, a2, 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      */
   198     String getText(String key, String a1, String a2, String a3) {
   199         return getText(key, a1, a2, a3, null);
   200     }
   202     /**
   203      * get and format message string from resource
   204      *
   205      * @param key selects message from resource
   206      * @param a1 first argument
   207      * @param a2 second argument
   208      * @param a3 third argument
   209      * @param a4 fourth argument
   210      */
   211     String getText(String key, String a1, String a2, String a3,
   212                           String a4) {
   213         try {
   214             String message = getString(key);
   215             String[] args = new String[4];
   216             args[0] = a1;
   217             args[1] = a2;
   218             args[2] = a3;
   219             args[3] = a4;
   220             return MessageFormat.format(message, (Object[])args);
   221         } catch (MissingResourceException e) {
   222             return "********** Resource for javadoc is broken. There is no " +
   223                 key + " key in resource.";
   224         }
   225     }
   227     /**
   228      * Print error message, increment error count.
   229      * Part of DocErrorReporter.
   230      *
   231      * @param msg message to print
   232      */
   233     public void printError(String msg) {
   234         printError(null, msg);
   235     }
   237     /**
   238      * Print error message, increment error count.
   239      * Part of DocErrorReporter.
   240      *
   241      * @param pos the position where the error occurs
   242      * @param msg message to print
   243      */
   244     public void printError(SourcePosition pos, String msg) {
   245         if (nerrors < MaxErrors) {
   246             String prefix = (pos == null) ? programName : pos.toString();
   247             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   248             errWriter.flush();
   249             prompt();
   250             nerrors++;
   251         }
   252     }
   254     /**
   255      * Print warning message, increment warning count.
   256      * Part of DocErrorReporter.
   257      *
   258      * @param msg message to print
   259      */
   260     public void printWarning(String msg) {
   261         printWarning(null, msg);
   262     }
   264     /**
   265      * Print warning message, increment warning count.
   266      * Part of DocErrorReporter.
   267      *
   268      * @param pos the position where the error occurs
   269      * @param msg message to print
   270      */
   271     public void printWarning(SourcePosition pos, String msg) {
   272         if (nwarnings < MaxWarnings) {
   273             String prefix = (pos == null) ? programName : pos.toString();
   274             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   275             warnWriter.flush();
   276             nwarnings++;
   277         }
   278     }
   280     /**
   281      * Print a message.
   282      * Part of DocErrorReporter.
   283      *
   284      * @param msg message to print
   285      */
   286     public void printNotice(String msg) {
   287         printNotice(null, msg);
   288     }
   290     /**
   291      * Print a message.
   292      * Part of DocErrorReporter.
   293      *
   294      * @param pos the position where the error occurs
   295      * @param msg message to print
   296      */
   297     public void printNotice(SourcePosition pos, String msg) {
   298         if (pos == null)
   299             noticeWriter.println(msg);
   300         else
   301             noticeWriter.println(pos + ": " + msg);
   302         noticeWriter.flush();
   303     }
   305     /**
   306      * Print error message, increment error count.
   307      *
   308      * @param key selects message from resource
   309      */
   310     public void error(SourcePosition pos, String key) {
   311         printError(pos, getText(key));
   312     }
   314     /**
   315      * Print error message, increment error count.
   316      *
   317      * @param key selects message from resource
   318      * @param a1 first argument
   319      */
   320     public void error(SourcePosition pos, String key, String a1) {
   321         printError(pos, getText(key, a1));
   322     }
   324     /**
   325      * Print error message, increment error count.
   326      *
   327      * @param key selects message from resource
   328      * @param a1 first argument
   329      * @param a2 second argument
   330      */
   331     public void error(SourcePosition pos, String key, String a1, String a2) {
   332         printError(pos, getText(key, a1, a2));
   333     }
   335     /**
   336      * Print error message, increment error count.
   337      *
   338      * @param key selects message from resource
   339      * @param a1 first argument
   340      * @param a2 second argument
   341      * @param a3 third argument
   342      */
   343     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   344         printError(pos, getText(key, a1, a2, a3));
   345     }
   347     /**
   348      * Print warning message, increment warning count.
   349      *
   350      * @param key selects message from resource
   351      */
   352     public void warning(SourcePosition pos, String key) {
   353         printWarning(pos, getText(key));
   354     }
   356     /**
   357      * Print warning message, increment warning count.
   358      *
   359      * @param key selects message from resource
   360      * @param a1 first argument
   361      */
   362     public void warning(SourcePosition pos, String key, String a1) {
   363         printWarning(pos, getText(key, a1));
   364     }
   366     /**
   367      * Print warning message, increment warning count.
   368      *
   369      * @param key selects message from resource
   370      * @param a1 first argument
   371      * @param a2 second argument
   372      */
   373     public void warning(SourcePosition pos, String key, String a1, String a2) {
   374         printWarning(pos, getText(key, a1, a2));
   375     }
   377     /**
   378      * Print warning message, increment warning count.
   379      *
   380      * @param key selects message from resource
   381      * @param a1 first argument
   382      * @param a2 second argument
   383      * @param a3 third argument
   384      */
   385     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   386         printWarning(pos, getText(key, a1, a2, a3));
   387     }
   389     /**
   390      * Print warning message, increment warning count.
   391      *
   392      * @param key selects message from resource
   393      * @param a1 first argument
   394      * @param a2 second argument
   395      * @param a3 third argument
   396      */
   397     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   398                         String a4) {
   399         printWarning(pos, getText(key, a1, a2, a3, a4));
   400     }
   402     /**
   403      * Print a message.
   404      *
   405      * @param key selects message from resource
   406      */
   407     public void notice(String key) {
   408         printNotice(getText(key));
   409     }
   411     /**
   412      * Print a message.
   413      *
   414      * @param key selects message from resource
   415      * @param a1 first argument
   416      */
   417     public void notice(String key, String a1) {
   418         printNotice(getText(key, a1));
   419     }
   421     /**
   422      * Print a message.
   423      *
   424      * @param key selects message from resource
   425      * @param a1 first argument
   426      * @param a2 second argument
   427      */
   428     public void notice(String key, String a1, String a2) {
   429         printNotice(getText(key, a1, a2));
   430     }
   432     /**
   433      * Print a message.
   434      *
   435      * @param key selects message from resource
   436      * @param a1 first argument
   437      * @param a2 second argument
   438      * @param a3 third argument
   439      */
   440     public void notice(String key, String a1, String a2, String a3) {
   441         printNotice(getText(key, a1, a2, a3));
   442     }
   444     /**
   445      * Return total number of errors, including those recorded
   446      * in the compilation log.
   447      */
   448     public int nerrors() { return nerrors; }
   450     /**
   451      * Return total number of warnings, including those recorded
   452      * in the compilation log.
   453      */
   454     public int nwarnings() { return nwarnings; }
   456     /**
   457      * Print exit message.
   458      */
   459     public void exitNotice() {
   460         if (nerrors > 0) {
   461             notice((nerrors > 1) ? "main.errors" : "main.error",
   462                    "" + nerrors);
   463         }
   464         if (nwarnings > 0) {
   465             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   466                    "" + nwarnings);
   467         }
   468     }
   470     /**
   471      * Force program exit, e.g., from a fatal error.
   472      * <p>
   473      * TODO: This method does not really belong here.
   474      */
   475     public void exit() {
   476         throw new ExitJavadoc();
   477     }
   479 }

mercurial