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

Fri, 05 Aug 2011 19:41:05 -0700

author
ksrini
date
Fri, 05 Aug 2011 19:41:05 -0700
changeset 1065
e9f118c2bd3c
parent 912
5e6c661891da
child 1357
c75be5bc5283
permissions
-rw-r--r--

7064544: (javadoc) miscellaneous fixes requested by netbeans
Summary: Contributed by netbeans team, modified to suit by the langtools team.
Reviewed-by: jjg, bpatel

     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         if (messageRB == null) {
   147             try {
   148                 messageRB = ResourceBundle.getBundle(
   149                           "com.sun.tools.javadoc.resources.javadoc");
   150             } catch (MissingResourceException e) {
   151                 throw new Error("Fatal: Resource for javadoc is missing");
   152             }
   153         }
   154         return messageRB.getString(key);
   155     }
   157     /**
   158      * get and format message string from resource
   159      *
   160      * @param key selects message from resource
   161      */
   162     String getText(String key) {
   163         return getText(key, (String)null);
   164     }
   166     /**
   167      * get and format message string from resource
   168      *
   169      * @param key selects message from resource
   170      * @param a1 first argument
   171      */
   172     String getText(String key, String a1) {
   173         return getText(key, a1, null);
   174     }
   176     /**
   177      * get and format message string from resource
   178      *
   179      * @param key selects message from resource
   180      * @param a1 first argument
   181      * @param a2 second argument
   182      */
   183     String getText(String key, String a1, String a2) {
   184         return getText(key, a1, a2, null);
   185     }
   187     /**
   188      * get and format message string from resource
   189      *
   190      * @param key selects message from resource
   191      * @param a1 first argument
   192      * @param a2 second argument
   193      * @param a3 third argument
   194      */
   195     String getText(String key, String a1, String a2, String a3) {
   196         return getText(key, a1, a2, a3, null);
   197     }
   199     /**
   200      * get and format message string from resource
   201      *
   202      * @param key selects message from resource
   203      * @param a1 first argument
   204      * @param a2 second argument
   205      * @param a3 third argument
   206      * @param a4 fourth argument
   207      */
   208     String getText(String key, String a1, String a2, String a3,
   209                           String a4) {
   210         try {
   211             String message = getString(key);
   212             String[] args = new String[4];
   213             args[0] = a1;
   214             args[1] = a2;
   215             args[2] = a3;
   216             args[3] = a4;
   217             return MessageFormat.format(message, (Object[])args);
   218         } catch (MissingResourceException e) {
   219             return "********** Resource for javadoc is broken. There is no " +
   220                 key + " key in resource.";
   221         }
   222     }
   224     /**
   225      * Print error message, increment error count.
   226      * Part of DocErrorReporter.
   227      *
   228      * @param msg message to print
   229      */
   230     public void printError(String msg) {
   231         printError(null, msg);
   232     }
   234     /**
   235      * Print error message, increment error count.
   236      * Part of DocErrorReporter.
   237      *
   238      * @param pos the position where the error occurs
   239      * @param msg message to print
   240      */
   241     public void printError(SourcePosition pos, String msg) {
   242         if (nerrors < MaxErrors) {
   243             String prefix = (pos == null) ? programName : pos.toString();
   244             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   245             errWriter.flush();
   246             prompt();
   247             nerrors++;
   248         }
   249     }
   251     /**
   252      * Print warning message, increment warning count.
   253      * Part of DocErrorReporter.
   254      *
   255      * @param msg message to print
   256      */
   257     public void printWarning(String msg) {
   258         printWarning(null, msg);
   259     }
   261     /**
   262      * Print warning message, increment warning count.
   263      * Part of DocErrorReporter.
   264      *
   265      * @param pos the position where the error occurs
   266      * @param msg message to print
   267      */
   268     public void printWarning(SourcePosition pos, String msg) {
   269         if (nwarnings < MaxWarnings) {
   270             String prefix = (pos == null) ? programName : pos.toString();
   271             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   272             warnWriter.flush();
   273             nwarnings++;
   274         }
   275     }
   277     /**
   278      * Print a message.
   279      * Part of DocErrorReporter.
   280      *
   281      * @param msg message to print
   282      */
   283     public void printNotice(String msg) {
   284         printNotice(null, msg);
   285     }
   287     /**
   288      * Print a message.
   289      * Part of DocErrorReporter.
   290      *
   291      * @param pos the position where the error occurs
   292      * @param msg message to print
   293      */
   294     public void printNotice(SourcePosition pos, String msg) {
   295         if (pos == null)
   296             noticeWriter.println(msg);
   297         else
   298             noticeWriter.println(pos + ": " + msg);
   299         noticeWriter.flush();
   300     }
   302     /**
   303      * Print error message, increment error count.
   304      *
   305      * @param key selects message from resource
   306      */
   307     public void error(SourcePosition pos, String key) {
   308         printError(pos, getText(key));
   309     }
   311     /**
   312      * Print error message, increment error count.
   313      *
   314      * @param key selects message from resource
   315      * @param a1 first argument
   316      */
   317     public void error(SourcePosition pos, String key, String a1) {
   318         printError(pos, getText(key, a1));
   319     }
   321     /**
   322      * Print error message, increment error count.
   323      *
   324      * @param key selects message from resource
   325      * @param a1 first argument
   326      * @param a2 second argument
   327      */
   328     public void error(SourcePosition pos, String key, String a1, String a2) {
   329         printError(pos, getText(key, a1, a2));
   330     }
   332     /**
   333      * Print error message, increment error count.
   334      *
   335      * @param key selects message from resource
   336      * @param a1 first argument
   337      * @param a2 second argument
   338      * @param a3 third argument
   339      */
   340     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   341         printError(pos, getText(key, a1, a2, a3));
   342     }
   344     /**
   345      * Print warning message, increment warning count.
   346      *
   347      * @param key selects message from resource
   348      */
   349     public void warning(SourcePosition pos, String key) {
   350         printWarning(pos, getText(key));
   351     }
   353     /**
   354      * Print warning message, increment warning count.
   355      *
   356      * @param key selects message from resource
   357      * @param a1 first argument
   358      */
   359     public void warning(SourcePosition pos, String key, String a1) {
   360         printWarning(pos, getText(key, a1));
   361     }
   363     /**
   364      * Print warning message, increment warning count.
   365      *
   366      * @param key selects message from resource
   367      * @param a1 first argument
   368      * @param a2 second argument
   369      */
   370     public void warning(SourcePosition pos, String key, String a1, String a2) {
   371         printWarning(pos, getText(key, a1, a2));
   372     }
   374     /**
   375      * Print warning message, increment warning count.
   376      *
   377      * @param key selects message from resource
   378      * @param a1 first argument
   379      * @param a2 second argument
   380      * @param a3 third argument
   381      */
   382     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   383         printWarning(pos, getText(key, a1, a2, a3));
   384     }
   386     /**
   387      * Print warning message, increment warning count.
   388      *
   389      * @param key selects message from resource
   390      * @param a1 first argument
   391      * @param a2 second argument
   392      * @param a3 third argument
   393      */
   394     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   395                         String a4) {
   396         printWarning(pos, getText(key, a1, a2, a3, a4));
   397     }
   399     /**
   400      * Print a message.
   401      *
   402      * @param key selects message from resource
   403      */
   404     public void notice(String key) {
   405         printNotice(getText(key));
   406     }
   408     /**
   409      * Print a message.
   410      *
   411      * @param key selects message from resource
   412      * @param a1 first argument
   413      */
   414     public void notice(String key, String a1) {
   415         printNotice(getText(key, a1));
   416     }
   418     /**
   419      * Print a message.
   420      *
   421      * @param key selects message from resource
   422      * @param a1 first argument
   423      * @param a2 second argument
   424      */
   425     public void notice(String key, String a1, String a2) {
   426         printNotice(getText(key, a1, a2));
   427     }
   429     /**
   430      * Print a message.
   431      *
   432      * @param key selects message from resource
   433      * @param a1 first argument
   434      * @param a2 second argument
   435      * @param a3 third argument
   436      */
   437     public void notice(String key, String a1, String a2, String a3) {
   438         printNotice(getText(key, a1, a2, a3));
   439     }
   441     /**
   442      * Return total number of errors, including those recorded
   443      * in the compilation log.
   444      */
   445     public int nerrors() { return nerrors; }
   447     /**
   448      * Return total number of warnings, including those recorded
   449      * in the compilation log.
   450      */
   451     public int nwarnings() { return nwarnings; }
   453     /**
   454      * Print exit message.
   455      */
   456     public void exitNotice() {
   457         if (nerrors > 0) {
   458             notice((nerrors > 1) ? "main.errors" : "main.error",
   459                    "" + nerrors);
   460         }
   461         if (nwarnings > 0) {
   462             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   463                    "" + nwarnings);
   464         }
   465     }
   467     /**
   468      * Force program exit, e.g., from a fatal error.
   469      * <p>
   470      * TODO: This method does not really belong here.
   471      */
   472     public void exit() {
   473         throw new ExitJavadoc();
   474     }
   476 }

mercurial