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

Thu, 15 Nov 2012 14:41:31 -0800

author
jjg
date
Thu, 15 Nov 2012 14:41:31 -0800
changeset 1411
467f4f754368
parent 1359
25e14ad23cef
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8003257: refactor javadoc tool option handling
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;
    29 import java.text.MessageFormat;
    30 import java.util.Locale;
    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.JCDiagnostic;
    36 import com.sun.tools.javac.util.JavacMessages;
    37 import com.sun.tools.javac.util.Log;
    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  *  <p><b>This is NOT part of any supported API.
    47  *  If you write code that depends on this, you do so at your own risk.
    48  *  This code and its internal interfaces are subject to change or
    49  *  deletion without notice.</b>
    50  *
    51  * @see java.util.ResourceBundle
    52  * @see java.text.MessageFormat
    53  * @author Neal Gafter (rewrite)
    54  */
    55 public class Messager extends Log implements DocErrorReporter {
    56     public static final SourcePosition NOPOS = null;
    58     /** Get the current messager, which is also the compiler log. */
    59     public static Messager instance0(Context context) {
    60         Log instance = context.get(logKey);
    61         if (instance == null || !(instance instanceof Messager))
    62             throw new InternalError("no messager instance!");
    63         return (Messager)instance;
    64     }
    66     public static void preRegister(Context context,
    67                                    final String programName) {
    68         context.put(logKey, new Context.Factory<Log>() {
    69             public Log make(Context c) {
    70                 return new Messager(c,
    71                                     programName);
    72             }
    73         });
    74     }
    75     public static void preRegister(Context context,
    76                                    final String programName,
    77                                    final PrintWriter errWriter,
    78                                    final PrintWriter warnWriter,
    79                                    final PrintWriter noticeWriter) {
    80         context.put(logKey, new Context.Factory<Log>() {
    81             public Log make(Context c) {
    82                 return new Messager(c,
    83                                     programName,
    84                                     errWriter,
    85                                     warnWriter,
    86                                     noticeWriter);
    87             }
    88         });
    89     }
    91     public class ExitJavadoc extends Error {
    92         private static final long serialVersionUID = 0;
    93     }
    95     final String programName;
    97     private Locale locale;
    98     private final JavacMessages messages;
    99     private final JCDiagnostic.Factory javadocDiags;
   101     /** The default writer for diagnostics
   102      */
   103     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
   104     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
   105     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
   107     /**
   108      * Constructor
   109      * @param programName  Name of the program (for error messages).
   110      */
   111     protected Messager(Context context, String programName) {
   112         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   113     }
   115     /**
   116      * Constructor
   117      * @param programName  Name of the program (for error messages).
   118      * @param errWriter    Stream for error messages
   119      * @param warnWriter   Stream for warnings
   120      * @param noticeWriter Stream for other messages
   121      */
   122     @SuppressWarnings("deprecation")
   123     protected Messager(Context context,
   124                        String programName,
   125                        PrintWriter errWriter,
   126                        PrintWriter warnWriter,
   127                        PrintWriter noticeWriter) {
   128         super(context, errWriter, warnWriter, noticeWriter);
   129         messages = JavacMessages.instance(context);
   130         messages.add("com.sun.tools.javadoc.resources.javadoc");
   131         javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
   132         this.programName = programName;
   133     }
   135     @Override
   136     protected int getDefaultMaxErrors() {
   137         return Integer.MAX_VALUE;
   138     }
   140     @Override
   141     protected int getDefaultMaxWarnings() {
   142         return Integer.MAX_VALUE;
   143     }
   145     public void setLocale(Locale locale) {
   146         this.locale = locale;
   147     }
   149     /**
   150      * get and format message string from resource
   151      *
   152      * @param key selects message from resource
   153      * @param args arguments for the message
   154      */
   155     String getText(String key, Object... args) {
   156         return messages.getLocalizedString(locale, key, args);
   157     }
   159     /**
   160      * Print error message, increment error count.
   161      * Part of DocErrorReporter.
   162      *
   163      * @param msg message to print
   164      */
   165     public void printError(String msg) {
   166         printError(null, msg);
   167     }
   169     /**
   170      * Print error message, increment error count.
   171      * Part of DocErrorReporter.
   172      *
   173      * @param pos the position where the error occurs
   174      * @param msg message to print
   175      */
   176     public void printError(SourcePosition pos, String msg) {
   177         if (nerrors < MaxErrors) {
   178             String prefix = (pos == null) ? programName : pos.toString();
   179             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   180             errWriter.flush();
   181             prompt();
   182             nerrors++;
   183         }
   184     }
   186     /**
   187      * Print warning message, increment warning count.
   188      * Part of DocErrorReporter.
   189      *
   190      * @param msg message to print
   191      */
   192     public void printWarning(String msg) {
   193         printWarning(null, msg);
   194     }
   196     /**
   197      * Print warning message, increment warning count.
   198      * Part of DocErrorReporter.
   199      *
   200      * @param pos the position where the error occurs
   201      * @param msg message to print
   202      */
   203     public void printWarning(SourcePosition pos, String msg) {
   204         if (nwarnings < MaxWarnings) {
   205             String prefix = (pos == null) ? programName : pos.toString();
   206             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   207             warnWriter.flush();
   208             nwarnings++;
   209         }
   210     }
   212     /**
   213      * Print a message.
   214      * Part of DocErrorReporter.
   215      *
   216      * @param msg message to print
   217      */
   218     public void printNotice(String msg) {
   219         printNotice(null, msg);
   220     }
   222     /**
   223      * Print a message.
   224      * Part of DocErrorReporter.
   225      *
   226      * @param pos the position where the error occurs
   227      * @param msg message to print
   228      */
   229     public void printNotice(SourcePosition pos, String msg) {
   230         if (pos == null)
   231             noticeWriter.println(msg);
   232         else
   233             noticeWriter.println(pos + ": " + msg);
   234         noticeWriter.flush();
   235     }
   237     /**
   238      * Print error message, increment error count.
   239      *
   240      * @param key selects message from resource
   241      */
   242     public void error(SourcePosition pos, String key, Object... args) {
   243         printError(pos, getText(key, args));
   244     }
   246     /**
   247      * Print warning message, increment warning count.
   248      *
   249      * @param key selects message from resource
   250      */
   251     public void warning(SourcePosition pos, String key, Object... args) {
   252         printWarning(pos, getText(key, args));
   253     }
   255     /**
   256      * Print a message.
   257      *
   258      * @param key selects message from resource
   259      */
   260     public void notice(String key, Object... args) {
   261         printNotice(getText(key, args));
   262     }
   264     /**
   265      * Return total number of errors, including those recorded
   266      * in the compilation log.
   267      */
   268     public int nerrors() { return nerrors; }
   270     /**
   271      * Return total number of warnings, including those recorded
   272      * in the compilation log.
   273      */
   274     public int nwarnings() { return nwarnings; }
   276     /**
   277      * Print exit message.
   278      */
   279     public void exitNotice() {
   280         if (nerrors > 0) {
   281             notice((nerrors > 1) ? "main.errors" : "main.error",
   282                    "" + nerrors);
   283         }
   284         if (nwarnings > 0) {
   285             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   286                    "" + nwarnings);
   287         }
   288     }
   290     /**
   291      * Force program exit, e.g., from a fatal error.
   292      * <p>
   293      * TODO: This method does not really belong here.
   294      */
   295     public void exit() {
   296         throw new ExitJavadoc();
   297     }
   298 }

mercurial