src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java

Mon, 28 Jul 2008 10:22:10 +0100

author
mcimadamore
date
Mon, 28 Jul 2008 10:22:10 +0100
changeset 83
37470f5ea179
child 100
37551dc0f591
permissions
-rw-r--r--

6720185: DiagnosticFormatter refactoring
Summary: Brand new hierarchy of diagnostic formatters for achieving better reusability
Reviewed-by: jjg

mcimadamore@83 1 /*
mcimadamore@83 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@83 7 * published by the Free Software Foundation. Sun designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
mcimadamore@83 9 * by Sun in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
mcimadamore@83 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
mcimadamore@83 22 * CA 95054 USA or visit www.sun.com if you need additional information or
mcimadamore@83 23 * have any questions.
mcimadamore@83 24 */
mcimadamore@83 25 package com.sun.tools.javac.util;
mcimadamore@83 26
mcimadamore@83 27 import java.util.Locale;
mcimadamore@83 28
mcimadamore@83 29 import com.sun.tools.javac.api.Formattable;
mcimadamore@83 30 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
mcimadamore@83 31
mcimadamore@83 32 /**
mcimadamore@83 33 * A raw formatter for diagnostic messages.
mcimadamore@83 34 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
mcimadamore@83 35 * or not the source name and position are set. This formatter provides a standardized, localize-independent
mcimadamore@83 36 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
mcimadamore@83 37 */
mcimadamore@83 38 public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
mcimadamore@83 39
mcimadamore@83 40 /**
mcimadamore@83 41 * Create a formatter based on the supplied options.
mcimadamore@83 42 * @param msgs
mcimadamore@83 43 */
mcimadamore@83 44 public RawDiagnosticFormatter(Messages msgs) {
mcimadamore@83 45 super(null);
mcimadamore@83 46 }
mcimadamore@83 47
mcimadamore@83 48 //provide common default formats
mcimadamore@83 49 public String format(JCDiagnostic d, Locale l) {
mcimadamore@83 50 try {
mcimadamore@83 51 StringBuffer buf = new StringBuffer();
mcimadamore@83 52 if (d.getPosition() != Position.NOPOS) {
mcimadamore@83 53 buf.append(formatSource(d, null));
mcimadamore@83 54 buf.append(':');
mcimadamore@83 55 buf.append(formatPosition(d, LINE, null));
mcimadamore@83 56 buf.append(':');
mcimadamore@83 57 buf.append(formatPosition(d, COLUMN, null));
mcimadamore@83 58 buf.append(':');
mcimadamore@83 59 }
mcimadamore@83 60 else
mcimadamore@83 61 buf.append('-');
mcimadamore@83 62 buf.append(' ');
mcimadamore@83 63 buf.append(formatMessage(d, null));
mcimadamore@83 64 return buf.toString();
mcimadamore@83 65 }
mcimadamore@83 66 catch (Exception e) {
mcimadamore@83 67 e.printStackTrace();
mcimadamore@83 68 return null;
mcimadamore@83 69 }
mcimadamore@83 70 }
mcimadamore@83 71
mcimadamore@83 72 @Override
mcimadamore@83 73 public String formatSource(JCDiagnostic d,Locale l) {
mcimadamore@83 74 assert(d.getSource() != null);
mcimadamore@83 75 return d.getSource().getName();
mcimadamore@83 76 }
mcimadamore@83 77
mcimadamore@83 78 @Override
mcimadamore@83 79 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
mcimadamore@83 80 String s;
mcimadamore@83 81 if (arg instanceof Formattable)
mcimadamore@83 82 s = arg.toString();
mcimadamore@83 83 else
mcimadamore@83 84 s = super.formatArgument(diag, arg, null);
mcimadamore@83 85 if (arg instanceof JCDiagnostic)
mcimadamore@83 86 return "(" + s + ")";
mcimadamore@83 87 else
mcimadamore@83 88 return s;
mcimadamore@83 89 }
mcimadamore@83 90
mcimadamore@83 91 @Override
mcimadamore@83 92 protected String localize(Locale l, String s, Object... args) {
mcimadamore@83 93 StringBuffer buf = new StringBuffer();
mcimadamore@83 94 buf.append(s);
mcimadamore@83 95 String sep = ": ";
mcimadamore@83 96 for (Object o : args) {
mcimadamore@83 97 buf.append(sep);
mcimadamore@83 98 buf.append(o);
mcimadamore@83 99 sep = ", ";
mcimadamore@83 100 }
mcimadamore@83 101 return buf.toString();
mcimadamore@83 102 }
mcimadamore@83 103
mcimadamore@83 104 public boolean displaySource(JCDiagnostic d) {
mcimadamore@83 105 return false;
mcimadamore@83 106 }
mcimadamore@83 107 }

mercurial