langtools/src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
changeset 942 98e1d4f8aacd
child 1109 853d8c191eac
equal deleted inserted replaced
941:2589047a9c5d 942:98e1d4f8aacd
       
     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;
       
    26 
       
    27 import java.util.Locale;
       
    28 
       
    29 import com.sun.tools.javac.api.Formattable;
       
    30 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
       
    31 
       
    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 {
       
    39 
       
    40     /**
       
    41      * Create a formatter based on the supplied options.
       
    42      * @param msgs
       
    43      */
       
    44     public RawDiagnosticFormatter(Messages msgs) {
       
    45         super(null);
       
    46     }
       
    47 
       
    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, 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     }
       
    71 
       
    72     @Override
       
    73     public String formatSource(JCDiagnostic d,Locale l) {
       
    74         assert(d.getSource() != null);
       
    75         return d.getSource().getName();
       
    76     }
       
    77 
       
    78     @Override
       
    79     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
       
    80         String s;
       
    81         if (arg instanceof Formattable)
       
    82             s = arg.toString();
       
    83         else
       
    84             s = super.formatArgument(diag, arg, null);
       
    85         if (arg instanceof JCDiagnostic)
       
    86             return "(" + s + ")";
       
    87         else
       
    88             return s;
       
    89     }
       
    90 
       
    91     @Override
       
    92     protected String localize(Locale l, String s, Object... args) {
       
    93         StringBuffer buf = new StringBuffer();
       
    94         buf.append(s);
       
    95         String sep = ": ";
       
    96         for (Object o : args) {
       
    97             buf.append(sep);
       
    98             buf.append(o);
       
    99             sep = ", ";
       
   100         }
       
   101         return buf.toString();
       
   102     }
       
   103 
       
   104     public boolean displaySource(JCDiagnostic d) {
       
   105         return false;
       
   106     }
       
   107 }