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

Thu, 09 Oct 2008 16:07:38 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:07:38 +0100
changeset 136
8eafba4f61be
parent 100
37551dc0f591
child 137
e4eaddca54b7
permissions
-rw-r--r--

6406133: JCDiagnostic.getMessage ignores locale argument
Summary: Compiler API should take into account locale settings
Reviewed-by: jjg

     1 /*
     2  * Copyright 2008 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.Locale;
    29 import com.sun.tools.javac.api.Formattable;
    30 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
    32 /**
    33  * A raw formatter for diagnostic messages.
    34  * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
    35  * or not the source name and position are set. This formatter provides a standardized, localize-independent
    36  * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
    37  */
    38 public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
    40     /**
    41      * Create a formatter based on the supplied options.
    42      * @param msgs
    43      */
    44     public RawDiagnosticFormatter(JavacMessages msgs) {
    45         super(null);
    46     }
    48     //provide common default formats
    49     public String format(JCDiagnostic d, Locale l) {
    50         try {
    51             StringBuffer buf = new StringBuffer();
    52             if (d.getPosition() != Position.NOPOS) {
    53                 buf.append(formatSource(d, false, null));
    54                 buf.append(':');
    55                 buf.append(formatPosition(d, LINE, null));
    56                 buf.append(':');
    57                 buf.append(formatPosition(d, COLUMN, null));
    58                 buf.append(':');
    59             }
    60             else
    61                 buf.append('-');
    62             buf.append(' ');
    63             buf.append(formatMessage(d, null));
    64             return buf.toString();
    65         }
    66         catch (Exception e) {
    67             e.printStackTrace();
    68             return null;
    69         }
    70     }
    72     @Override
    73     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
    74         String s;
    75         if (arg instanceof Formattable)
    76             s = arg.toString();
    77         else
    78             s = super.formatArgument(diag, arg, null);
    79         if (arg instanceof JCDiagnostic)
    80             return "(" + s + ")";
    81         else
    82             return s;
    83     }
    85     @Override
    86     protected String localize(Locale l, String s, Object... args) {
    87         StringBuffer buf = new StringBuffer();
    88         buf.append(s);
    89         String sep = ": ";
    90         for (Object o : args) {
    91             buf.append(sep);
    92             buf.append(o);
    93             sep = ", ";
    94         }
    95         return buf.toString();
    96     }
    98     public boolean displaySource(JCDiagnostic d) {
    99         return false;
   100     }
   101 }

mercurial