AbstractDiagnosticFormatter.java revision 135
82N/A/*
82N/A * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
82N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82N/A *
82N/A * This code is free software; you can redistribute it and/or modify it
82N/A * under the terms of the GNU General Public License version 2 only, as
82N/A * published by the Free Software Foundation. Sun designates this
82N/A * particular file as subject to the "Classpath" exception as provided
82N/A * by Sun in the LICENSE file that accompanied this code.
82N/A *
82N/A * This code is distributed in the hope that it will be useful, but WITHOUT
82N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
82N/A * version 2 for more details (a copy is included in the LICENSE file that
82N/A * accompanied this code).
82N/A *
82N/A * You should have received a copy of the GNU General Public License version
82N/A * 2 along with this work; if not, write to the Free Software Foundation,
82N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82N/A *
82N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
82N/A * CA 95054 USA or visit www.sun.com if you need additional information or
82N/A * have any questions.
82N/A */
82N/Apackage com.sun.tools.javac.util;
82N/A
82N/Aimport java.util.Collection;
82N/Aimport java.util.Locale;
82N/Aimport javax.tools.JavaFileObject;
135N/Aimport java.util.ResourceBundle;
82N/A
82N/Aimport com.sun.tools.javac.api.DiagnosticFormatter;
82N/Aimport com.sun.tools.javac.api.Formattable;
82N/Aimport com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
82N/Aimport com.sun.tools.javac.file.JavacFileManager;
82N/A
82N/A/**
82N/A * This abstract class provides a basic implementation of the functionalities that should be provided
82N/A * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
82N/A *
82N/A * <ul>
82N/A * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
82N/A * Those implementations are specifically targeting JCDiagnostic objects.
82N/A * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
82N/A * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
82N/A * <ul>
82N/A *
82N/A */
82N/Apublic abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
82N/A
82N/A /**
135N/A * JavacMessages object used by this formatter for i18n
82N/A */
135N/A protected JavacMessages messages;
82N/A
82N/A /**
135N/A * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
82N/A * @param messages
82N/A */
135N/A protected AbstractDiagnosticFormatter(JavacMessages messages) {
82N/A this.messages = messages;
82N/A }
82N/A
82N/A public String formatMessage(JCDiagnostic d, Locale l) {
82N/A //this code should rely on the locale settings but it's not! See RFE 6443132
82N/A Collection<String> args = formatArguments(d, l);
82N/A return localize(l, d.getCode(), args.toArray());
82N/A }
82N/A
82N/A public String formatKind(JCDiagnostic d, Locale l) {
82N/A switch (d.getType()) {
82N/A case FRAGMENT: return "";
82N/A case NOTE: return localize(l, "compiler.note.note");
82N/A case WARNING: return localize(l, "compiler.warn.warning");
82N/A case ERROR: return localize(l, "compiler.err.error");
82N/A default:
82N/A throw new AssertionError("Unknown diagnostic type: " + d.getType());
82N/A }
82N/A }
82N/A
82N/A public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
82N/A assert (d.getPosition() != Position.NOPOS);
82N/A return String.valueOf(getPosition(d, pk));
82N/A }
82N/A //WHERE
82N/A public long getPosition(JCDiagnostic d, PositionKind pk) {
82N/A switch (pk) {
82N/A case START: return d.getIntStartPosition();
82N/A case END: return d.getIntEndPosition();
82N/A case LINE: return d.getLineNumber();
82N/A case COLUMN: return d.getColumnNumber();
82N/A case OFFSET: return d.getIntPosition();
82N/A default:
82N/A throw new AssertionError("Unknown diagnostic position: " + pk);
82N/A }
82N/A }
82N/A
99N/A public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
82N/A assert (d.getSource() != null);
99N/A return fullname ? d.getSourceName() : d.getSource().getName();
82N/A }
82N/A
82N/A /**
82N/A * Format the arguments of a given diagnostic.
82N/A *
82N/A * @param d diagnostic whose arguments are to be formatted
82N/A * @param l locale object to be used for i18n
82N/A * @return a Collection whose elements are the formatted arguments of the diagnostic
82N/A */
82N/A protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
82N/A ListBuffer<String> buf = new ListBuffer<String>();
82N/A for (Object o : d.getArgs()) {
82N/A buf.append(formatArgument(d, o, l));
82N/A }
82N/A return buf.toList();
82N/A }
82N/A
82N/A /**
82N/A * Format a single argument of a given diagnostic.
82N/A *
82N/A * @param d diagnostic whose argument is to be formatted
82N/A * @param arg argument to be formatted
82N/A * @param l locale object to be used for i18n
82N/A * @return string representation of the diagnostic argument
82N/A */
82N/A protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
82N/A if (arg instanceof JCDiagnostic)
82N/A return format((JCDiagnostic)arg, l);
82N/A else if (arg instanceof Iterable<?>) {
82N/A return formatIterable(d, (Iterable<?>)arg, l);
82N/A }
82N/A else if (arg instanceof JavaFileObject)
82N/A return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
82N/A else if (arg instanceof Formattable)
135N/A return ((Formattable)arg).toString(l, messages);
82N/A else
82N/A return String.valueOf(arg);
82N/A }
82N/A
82N/A /**
82N/A * Format an iterable argument of a given diagnostic.
82N/A *
82N/A * @param d diagnostic whose argument is to be formatted
82N/A * @param it iterable argument to be formatted
82N/A * @param l locale object to be used for i18n
82N/A * @return string representation of the diagnostic iterable argument
82N/A */
82N/A protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
82N/A StringBuilder sbuf = new StringBuilder();
82N/A String sep = "";
82N/A for (Object o : it) {
82N/A sbuf.append(sep);
82N/A sbuf.append(formatArgument(d, o, l));
82N/A sep = ",";
82N/A }
82N/A return sbuf.toString();
82N/A }
82N/A
82N/A /**
82N/A * Converts a String into a locale-dependent representation accordingly to a given locale
82N/A *
82N/A * @param l locale object to be used for i18n
82N/A * @param key locale-independent key used for looking up in a resource file
82N/A * @param args localization arguments
82N/A * @return a locale-dependent string
82N/A */
82N/A protected String localize(Locale l, String key, Object... args) {
135N/A return messages.getLocalizedString(l, key, args);
82N/A }
82N/A}