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

Mon, 09 Mar 2009 23:53:41 -0700

author
tbell
date
Mon, 09 Mar 2009 23:53:41 -0700
changeset 240
8c55d5b0ed71
parent 229
03bcd66bd8e7
parent 238
86b60aa941c6
child 288
d402db1005ad
permissions
-rw-r--r--

Merge

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

mercurial