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.Locale; mcimadamore@83: mcimadamore@83: import com.sun.tools.javac.api.Formattable; mcimadamore@83: import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; mcimadamore@83: mcimadamore@83: /** mcimadamore@83: * A raw formatter for diagnostic messages. mcimadamore@83: * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether mcimadamore@83: * or not the source name and position are set. This formatter provides a standardized, localize-independent mcimadamore@83: * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes. mcimadamore@83: */ mcimadamore@83: public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { mcimadamore@83: mcimadamore@83: /** mcimadamore@83: * Create a formatter based on the supplied options. mcimadamore@83: * @param msgs mcimadamore@83: */ mcimadamore@137: public RawDiagnosticFormatter(Options opts) { mcimadamore@137: super(null, opts, false); mcimadamore@83: } mcimadamore@83: mcimadamore@83: //provide common default formats mcimadamore@83: public String format(JCDiagnostic d, Locale l) { mcimadamore@83: try { mcimadamore@83: StringBuffer buf = new StringBuffer(); mcimadamore@83: if (d.getPosition() != Position.NOPOS) { mcimadamore@100: buf.append(formatSource(d, false, null)); mcimadamore@83: buf.append(':'); mcimadamore@83: buf.append(formatPosition(d, LINE, null)); mcimadamore@83: buf.append(':'); mcimadamore@83: buf.append(formatPosition(d, COLUMN, null)); mcimadamore@83: buf.append(':'); mcimadamore@83: } mcimadamore@83: else mcimadamore@83: buf.append('-'); mcimadamore@83: buf.append(' '); mcimadamore@83: buf.append(formatMessage(d, null)); mcimadamore@137: if (displaySource(d)) mcimadamore@137: buf.append("\n" + formatSourceLine(d)); mcimadamore@83: return buf.toString(); mcimadamore@83: } mcimadamore@83: catch (Exception e) { mcimadamore@83: e.printStackTrace(); mcimadamore@83: return null; mcimadamore@83: } mcimadamore@83: } mcimadamore@83: mcimadamore@83: @Override mcimadamore@83: protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { mcimadamore@83: String s; mcimadamore@83: if (arg instanceof Formattable) mcimadamore@83: s = arg.toString(); mcimadamore@83: else mcimadamore@83: s = super.formatArgument(diag, arg, null); mcimadamore@83: if (arg instanceof JCDiagnostic) mcimadamore@83: return "(" + s + ")"; mcimadamore@83: else mcimadamore@83: return s; mcimadamore@83: } mcimadamore@83: mcimadamore@83: @Override mcimadamore@168: protected String formatSubdiagnostics(JCDiagnostic d, Locale l) { mcimadamore@168: StringBuilder buf = new StringBuilder(); mcimadamore@168: String sep = ""; mcimadamore@168: buf.append(",{"); mcimadamore@168: for (JCDiagnostic d2 : d.getSubdiagnostics()) { mcimadamore@168: buf.append(sep); mcimadamore@168: buf.append("(" + format(d2, l) + ")"); mcimadamore@168: sep = ","; mcimadamore@168: } mcimadamore@168: buf.append('}'); mcimadamore@168: return buf.toString(); mcimadamore@168: } mcimadamore@168: mcimadamore@168: @Override mcimadamore@83: protected String localize(Locale l, String s, Object... args) { mcimadamore@83: StringBuffer buf = new StringBuffer(); mcimadamore@83: buf.append(s); mcimadamore@83: String sep = ": "; mcimadamore@83: for (Object o : args) { mcimadamore@83: buf.append(sep); mcimadamore@83: buf.append(o); mcimadamore@83: sep = ", "; mcimadamore@83: } mcimadamore@83: return buf.toString(); mcimadamore@83: } mcimadamore@83: }