mcimadamore@83: /* ohair@798: * Copyright (c) 2008, 2010, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this mcimadamore@83: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. mcimadamore@83: */ mcimadamore@83: package com.sun.tools.javac.util; mcimadamore@83: mcimadamore@221: import java.util.Collection; mcimadamore@221: import java.util.EnumSet; mcimadamore@83: import java.util.Locale; jjg@776: import javax.tools.JavaFileObject; mcimadamore@83: mcimadamore@221: import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*; mcimadamore@83: import com.sun.tools.javac.api.Formattable; jjg@415: import com.sun.tools.javac.file.BaseFileObject; mcimadamore@221: import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration; mcimadamore@221: 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. jjg@333: * jjg@581: *

This is NOT part of any supported API. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. mcimadamore@83: */ mcimadamore@221: public final 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@221: public RawDiagnosticFormatter(Options options) { mcimadamore@221: super(null, new SimpleConfiguration(options, mcimadamore@221: EnumSet.of(DiagnosticPart.SUMMARY, mcimadamore@221: DiagnosticPart.DETAILS, mcimadamore@221: DiagnosticPart.SUBDIAGNOSTICS))); mcimadamore@83: } mcimadamore@83: mcimadamore@83: //provide common default formats mcimadamore@238: public String formatDiagnostic(JCDiagnostic d, Locale l) { mcimadamore@83: try { jjg@776: StringBuilder buf = new StringBuilder(); 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: } jjg@776: else if (d.getSource() != null && d.getSource().getKind() == JavaFileObject.Kind.CLASS) { jjg@776: buf.append(formatSource(d, false, null)); jjg@776: buf.append(":-:-:"); jjg@776: } mcimadamore@83: else mcimadamore@83: buf.append('-'); mcimadamore@83: buf.append(' '); mcimadamore@83: buf.append(formatMessage(d, null)); jjg@776: if (displaySource(d)) { jjg@776: buf.append("\n"); jjg@776: buf.append(formatSourceLine(d, 0)); jjg@776: } mcimadamore@83: return buf.toString(); mcimadamore@83: } mcimadamore@83: catch (Exception e) { jjg@776: //e.printStackTrace(); mcimadamore@83: return null; mcimadamore@83: } mcimadamore@83: } mcimadamore@83: mcimadamore@221: public String formatMessage(JCDiagnostic d, Locale l) { mcimadamore@221: StringBuilder buf = new StringBuilder(); mcimadamore@221: Collection args = formatArguments(d, l); mcimadamore@238: buf.append(localize(null, d.getCode(), args.toArray())); mcimadamore@221: if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) { mcimadamore@221: List subDiags = formatSubdiagnostics(d, null); mcimadamore@221: if (subDiags.nonEmpty()) { mcimadamore@238: String sep = ""; mcimadamore@221: buf.append(",{"); mcimadamore@221: for (String sub : formatSubdiagnostics(d, null)) { mcimadamore@221: buf.append(sep); jjg@776: buf.append("("); jjg@776: buf.append(sub); jjg@776: buf.append(")"); mcimadamore@221: sep = ","; mcimadamore@221: } mcimadamore@221: buf.append('}'); mcimadamore@221: } mcimadamore@221: } mcimadamore@221: return buf.toString(); mcimadamore@221: } mcimadamore@221: 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(); jjg@415: else if (arg instanceof BaseFileObject) jjg@415: s = ((BaseFileObject) arg).getShortName(); 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@238: mcimadamore@238: @Override mcimadamore@238: protected String localize(Locale l, String key, Object... args) { mcimadamore@238: StringBuilder buf = new StringBuilder(); mcimadamore@238: buf.append(key); mcimadamore@238: String sep = ": "; mcimadamore@238: for (Object o : args) { mcimadamore@238: buf.append(sep); mcimadamore@238: buf.append(o); mcimadamore@238: sep = ", "; mcimadamore@238: } mcimadamore@238: return buf.toString(); mcimadamore@238: } mcimadamore@288: mcimadamore@288: @Override mcimadamore@288: public boolean isRaw() { mcimadamore@288: return true; mcimadamore@288: } mcimadamore@83: }