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