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

mcimadamore@83 1 /*
mcimadamore@83 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@83 7 * published by the Free Software Foundation. Sun designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
mcimadamore@83 9 * by Sun in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
mcimadamore@83 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
mcimadamore@83 22 * CA 95054 USA or visit www.sun.com if you need additional information or
mcimadamore@83 23 * have any questions.
mcimadamore@83 24 */
mcimadamore@83 25 package com.sun.tools.javac.util;
mcimadamore@83 26
mcimadamore@83 27 import java.util.Collection;
mcimadamore@83 28 import java.util.Locale;
mcimadamore@83 29 import javax.tools.JavaFileObject;
mcimadamore@136 30 import java.util.ResourceBundle;
mcimadamore@83 31
mcimadamore@83 32 import com.sun.tools.javac.api.DiagnosticFormatter;
mcimadamore@83 33 import com.sun.tools.javac.api.Formattable;
mcimadamore@83 34 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
mcimadamore@83 35 import com.sun.tools.javac.file.JavacFileManager;
mcimadamore@83 36
mcimadamore@83 37 /**
mcimadamore@83 38 * This abstract class provides a basic implementation of the functionalities that should be provided
mcimadamore@83 39 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
mcimadamore@83 40 *
mcimadamore@83 41 * <ul>
mcimadamore@83 42 * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
mcimadamore@83 43 * Those implementations are specifically targeting JCDiagnostic objects.
mcimadamore@83 44 * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
mcimadamore@83 45 * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
mcimadamore@83 46 * <ul>
mcimadamore@83 47 *
mcimadamore@83 48 */
mcimadamore@83 49 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
mcimadamore@83 50
mcimadamore@83 51 /**
mcimadamore@136 52 * JavacMessages object used by this formatter for i18n
mcimadamore@83 53 */
mcimadamore@136 54 protected JavacMessages messages;
mcimadamore@83 55
mcimadamore@83 56 /**
mcimadamore@136 57 * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
mcimadamore@83 58 * @param messages
mcimadamore@83 59 */
mcimadamore@136 60 protected AbstractDiagnosticFormatter(JavacMessages messages) {
mcimadamore@83 61 this.messages = messages;
mcimadamore@83 62 }
mcimadamore@83 63
mcimadamore@83 64 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@83 65 //this code should rely on the locale settings but it's not! See RFE 6443132
mcimadamore@83 66 Collection<String> args = formatArguments(d, l);
mcimadamore@83 67 return localize(l, d.getCode(), args.toArray());
mcimadamore@83 68 }
mcimadamore@83 69
mcimadamore@83 70 public String formatKind(JCDiagnostic d, Locale l) {
mcimadamore@83 71 switch (d.getType()) {
mcimadamore@83 72 case FRAGMENT: return "";
mcimadamore@83 73 case NOTE: return localize(l, "compiler.note.note");
mcimadamore@83 74 case WARNING: return localize(l, "compiler.warn.warning");
mcimadamore@83 75 case ERROR: return localize(l, "compiler.err.error");
mcimadamore@83 76 default:
mcimadamore@83 77 throw new AssertionError("Unknown diagnostic type: " + d.getType());
mcimadamore@83 78 }
mcimadamore@83 79 }
mcimadamore@83 80
mcimadamore@83 81 public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
mcimadamore@83 82 assert (d.getPosition() != Position.NOPOS);
mcimadamore@83 83 return String.valueOf(getPosition(d, pk));
mcimadamore@83 84 }
mcimadamore@83 85 //WHERE
mcimadamore@83 86 public long getPosition(JCDiagnostic d, PositionKind pk) {
mcimadamore@83 87 switch (pk) {
mcimadamore@83 88 case START: return d.getIntStartPosition();
mcimadamore@83 89 case END: return d.getIntEndPosition();
mcimadamore@83 90 case LINE: return d.getLineNumber();
mcimadamore@83 91 case COLUMN: return d.getColumnNumber();
mcimadamore@83 92 case OFFSET: return d.getIntPosition();
mcimadamore@83 93 default:
mcimadamore@83 94 throw new AssertionError("Unknown diagnostic position: " + pk);
mcimadamore@83 95 }
mcimadamore@83 96 }
mcimadamore@83 97
mcimadamore@100 98 public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
mcimadamore@83 99 assert (d.getSource() != null);
mcimadamore@100 100 return fullname ? d.getSourceName() : d.getSource().getName();
mcimadamore@83 101 }
mcimadamore@83 102
mcimadamore@83 103 /**
mcimadamore@83 104 * Format the arguments of a given diagnostic.
mcimadamore@83 105 *
mcimadamore@83 106 * @param d diagnostic whose arguments are to be formatted
mcimadamore@83 107 * @param l locale object to be used for i18n
mcimadamore@83 108 * @return a Collection whose elements are the formatted arguments of the diagnostic
mcimadamore@83 109 */
mcimadamore@83 110 protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
mcimadamore@83 111 ListBuffer<String> buf = new ListBuffer<String>();
mcimadamore@83 112 for (Object o : d.getArgs()) {
mcimadamore@83 113 buf.append(formatArgument(d, o, l));
mcimadamore@83 114 }
mcimadamore@83 115 return buf.toList();
mcimadamore@83 116 }
mcimadamore@83 117
mcimadamore@83 118 /**
mcimadamore@83 119 * Format a single argument of a given diagnostic.
mcimadamore@83 120 *
mcimadamore@83 121 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 122 * @param arg argument to be formatted
mcimadamore@83 123 * @param l locale object to be used for i18n
mcimadamore@83 124 * @return string representation of the diagnostic argument
mcimadamore@83 125 */
mcimadamore@83 126 protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
mcimadamore@83 127 if (arg instanceof JCDiagnostic)
mcimadamore@83 128 return format((JCDiagnostic)arg, l);
mcimadamore@83 129 else if (arg instanceof Iterable<?>) {
mcimadamore@83 130 return formatIterable(d, (Iterable<?>)arg, l);
mcimadamore@83 131 }
mcimadamore@83 132 else if (arg instanceof JavaFileObject)
mcimadamore@83 133 return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
mcimadamore@83 134 else if (arg instanceof Formattable)
mcimadamore@136 135 return ((Formattable)arg).toString(l, messages);
mcimadamore@83 136 else
mcimadamore@83 137 return String.valueOf(arg);
mcimadamore@83 138 }
mcimadamore@83 139
mcimadamore@83 140 /**
mcimadamore@83 141 * Format an iterable argument of a given diagnostic.
mcimadamore@83 142 *
mcimadamore@83 143 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 144 * @param it iterable argument to be formatted
mcimadamore@83 145 * @param l locale object to be used for i18n
mcimadamore@83 146 * @return string representation of the diagnostic iterable argument
mcimadamore@83 147 */
mcimadamore@83 148 protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
mcimadamore@83 149 StringBuilder sbuf = new StringBuilder();
mcimadamore@83 150 String sep = "";
mcimadamore@83 151 for (Object o : it) {
mcimadamore@83 152 sbuf.append(sep);
mcimadamore@83 153 sbuf.append(formatArgument(d, o, l));
mcimadamore@83 154 sep = ",";
mcimadamore@83 155 }
mcimadamore@83 156 return sbuf.toString();
mcimadamore@83 157 }
mcimadamore@83 158
mcimadamore@83 159 /**
mcimadamore@83 160 * Converts a String into a locale-dependent representation accordingly to a given locale
mcimadamore@83 161 *
mcimadamore@83 162 * @param l locale object to be used for i18n
mcimadamore@83 163 * @param key locale-independent key used for looking up in a resource file
mcimadamore@83 164 * @param args localization arguments
mcimadamore@83 165 * @return a locale-dependent string
mcimadamore@83 166 */
mcimadamore@83 167 protected String localize(Locale l, String key, Object... args) {
mcimadamore@136 168 return messages.getLocalizedString(l, key, args);
mcimadamore@83 169 }
mcimadamore@83 170 }

mercurial