src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java

Thu, 09 Oct 2008 16:07:38 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:07:38 +0100
changeset 136
8eafba4f61be
parent 100
37551dc0f591
child 137
e4eaddca54b7
permissions
-rw-r--r--

6406133: JCDiagnostic.getMessage ignores locale argument
Summary: Compiler API should take into account locale settings
Reviewed-by: jjg

     1 /*
     2  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    25 package com.sun.tools.javac.util;
    27 import java.util.Collection;
    28 import java.util.Locale;
    29 import javax.tools.JavaFileObject;
    30 import java.util.ResourceBundle;
    32 import com.sun.tools.javac.api.DiagnosticFormatter;
    33 import com.sun.tools.javac.api.Formattable;
    34 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
    35 import com.sun.tools.javac.file.JavacFileManager;
    37 /**
    38  * This abstract class provides a basic implementation of the functionalities that should be provided
    39  * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
    40  *
    41  * <ul>
    42  *  <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
    43  *  Those implementations are specifically targeting JCDiagnostic objects.
    44  *  <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
    45  *  <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
    46  * <ul>
    47  *
    48  */
    49 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
    51     /**
    52      * JavacMessages object used by this formatter for i18n
    53      */
    54     protected JavacMessages messages;
    56     /**
    57      * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
    58      * @param messages
    59      */
    60     protected AbstractDiagnosticFormatter(JavacMessages messages) {
    61         this.messages = messages;
    62     }
    64     public String formatMessage(JCDiagnostic d, Locale l) {
    65         //this code should rely on the locale settings but it's not! See RFE 6443132
    66         Collection<String> args = formatArguments(d, l);
    67         return localize(l, d.getCode(), args.toArray());
    68     }
    70     public String formatKind(JCDiagnostic d, Locale l) {
    71         switch (d.getType()) {
    72             case FRAGMENT: return "";
    73             case NOTE:     return localize(l, "compiler.note.note");
    74             case WARNING:  return localize(l, "compiler.warn.warning");
    75             case ERROR:    return localize(l, "compiler.err.error");
    76             default:
    77                 throw new AssertionError("Unknown diagnostic type: " + d.getType());
    78         }
    79     }
    81     public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
    82         assert (d.getPosition() != Position.NOPOS);
    83         return String.valueOf(getPosition(d, pk));
    84     }
    85     //WHERE
    86     public long getPosition(JCDiagnostic d, PositionKind pk) {
    87         switch (pk) {
    88             case START: return d.getIntStartPosition();
    89             case END: return d.getIntEndPosition();
    90             case LINE: return d.getLineNumber();
    91             case COLUMN: return d.getColumnNumber();
    92             case OFFSET: return d.getIntPosition();
    93             default:
    94                 throw new AssertionError("Unknown diagnostic position: " + pk);
    95         }
    96     }
    98     public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
    99         assert (d.getSource() != null);
   100         return fullname ? d.getSourceName() : d.getSource().getName();
   101     }
   103     /**
   104      * Format the arguments of a given diagnostic.
   105      *
   106      * @param d diagnostic whose arguments are to be formatted
   107      * @param l locale object to be used for i18n
   108      * @return a Collection whose elements are the formatted arguments of the diagnostic
   109      */
   110     protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
   111         ListBuffer<String> buf = new ListBuffer<String>();
   112         for (Object o : d.getArgs()) {
   113            buf.append(formatArgument(d, o, l));
   114         }
   115         return buf.toList();
   116     }
   118     /**
   119      * Format a single argument of a given diagnostic.
   120      *
   121      * @param d diagnostic whose argument is to be formatted
   122      * @param arg argument to be formatted
   123      * @param l locale object to be used for i18n
   124      * @return string representation of the diagnostic argument
   125      */
   126     protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
   127         if (arg instanceof JCDiagnostic)
   128             return format((JCDiagnostic)arg, l);
   129         else if (arg instanceof Iterable<?>) {
   130             return formatIterable(d, (Iterable<?>)arg, l);
   131         }
   132         else if (arg instanceof JavaFileObject)
   133             return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
   134         else if (arg instanceof Formattable)
   135             return ((Formattable)arg).toString(l, messages);
   136         else
   137             return String.valueOf(arg);
   138     }
   140     /**
   141      * Format an iterable argument of a given diagnostic.
   142      *
   143      * @param d diagnostic whose argument is to be formatted
   144      * @param it iterable argument to be formatted
   145      * @param l locale object to be used for i18n
   146      * @return string representation of the diagnostic iterable argument
   147      */
   148     protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
   149         StringBuilder sbuf = new StringBuilder();
   150         String sep = "";
   151         for (Object o : it) {
   152             sbuf.append(sep);
   153             sbuf.append(formatArgument(d, o, l));
   154             sep = ",";
   155         }
   156         return sbuf.toString();
   157     }
   159     /**
   160      * Converts a String into a locale-dependent representation accordingly to a given locale
   161      *
   162      * @param l locale object to be used for i18n
   163      * @param key locale-independent key used for looking up in a resource file
   164      * @param args localization arguments
   165      * @return a locale-dependent string
   166      */
   167     protected String localize(Locale l, String key, Object... args) {
   168         return messages.getLocalizedString(l, key, args);
   169     }
   170 }

mercurial