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