langtools/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.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.Collection;
       
    28 import java.util.Locale;
       
    29 import javax.tools.JavaFileObject;
       
    30 
       
    31 import com.sun.tools.javac.api.DiagnosticFormatter;
       
    32 import com.sun.tools.javac.api.Formattable;
       
    33 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
       
    34 import com.sun.tools.javac.file.JavacFileManager;
       
    35 
       
    36 /**
       
    37  * This abstract class provides a basic implementation of the functionalities that should be provided
       
    38  * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
       
    39  *
       
    40  * <ul>
       
    41  *  <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
       
    42  *  Those implementations are specifically targeting JCDiagnostic objects.
       
    43  *  <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
       
    44  *  <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
       
    45  * <ul>
       
    46  *
       
    47  */
       
    48 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
       
    49 
       
    50     /**
       
    51      * Messages object used by this formatter for i18n
       
    52      */
       
    53     protected Messages messages;
       
    54 
       
    55     /**
       
    56      * Initialize an AbstractDiagnosticFormatter by setting its Messages object
       
    57      * @param messages
       
    58      */
       
    59     protected AbstractDiagnosticFormatter(Messages messages) {
       
    60         this.messages = messages;
       
    61     }
       
    62 
       
    63     public String formatMessage(JCDiagnostic d, Locale l) {
       
    64         //this code should rely on the locale settings but it's not! See RFE 6443132
       
    65         Collection<String> args = formatArguments(d, l);
       
    66         return localize(l, d.getCode(), args.toArray());
       
    67     }
       
    68 
       
    69     public String formatKind(JCDiagnostic d, Locale l) {
       
    70         switch (d.getType()) {
       
    71             case FRAGMENT: return "";
       
    72             case NOTE:     return localize(l, "compiler.note.note");
       
    73             case WARNING:  return localize(l, "compiler.warn.warning");
       
    74             case ERROR:    return localize(l, "compiler.err.error");
       
    75             default:
       
    76                 throw new AssertionError("Unknown diagnostic type: " + d.getType());
       
    77         }
       
    78     }
       
    79 
       
    80     public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
       
    81         assert (d.getPosition() != Position.NOPOS);
       
    82         return String.valueOf(getPosition(d, pk));
       
    83     }
       
    84     //WHERE
       
    85     public long getPosition(JCDiagnostic d, PositionKind pk) {
       
    86         switch (pk) {
       
    87             case START: return d.getIntStartPosition();
       
    88             case END: return d.getIntEndPosition();
       
    89             case LINE: return d.getLineNumber();
       
    90             case COLUMN: return d.getColumnNumber();
       
    91             case OFFSET: return d.getIntPosition();
       
    92             default:
       
    93                 throw new AssertionError("Unknown diagnostic position: " + pk);
       
    94         }
       
    95     }
       
    96 
       
    97     public String formatSource(JCDiagnostic d,Locale l) {
       
    98         assert (d.getSource() != null);
       
    99         return d.getSource().getName();
       
   100     }
       
   101 
       
   102     /**
       
   103      * Format the arguments of a given diagnostic.
       
   104      *
       
   105      * @param d diagnostic whose arguments are to be formatted
       
   106      * @param l locale object to be used for i18n
       
   107      * @return a Collection whose elements are the formatted arguments of the diagnostic
       
   108      */
       
   109     protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
       
   110         ListBuffer<String> buf = new ListBuffer<String>();
       
   111         for (Object o : d.getArgs()) {
       
   112            buf.append(formatArgument(d, o, l));
       
   113         }
       
   114         return buf.toList();
       
   115     }
       
   116 
       
   117     /**
       
   118      * Format a single argument of a given diagnostic.
       
   119      *
       
   120      * @param d diagnostic whose argument is to be formatted
       
   121      * @param arg argument to be formatted
       
   122      * @param l locale object to be used for i18n
       
   123      * @return string representation of the diagnostic argument
       
   124      */
       
   125     protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
       
   126         if (arg instanceof JCDiagnostic)
       
   127             return format((JCDiagnostic)arg, l);
       
   128         else if (arg instanceof Iterable<?>) {
       
   129             return formatIterable(d, (Iterable<?>)arg, l);
       
   130         }
       
   131         else if (arg instanceof JavaFileObject)
       
   132             return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
       
   133         else if (arg instanceof Formattable)
       
   134             return ((Formattable)arg).toString(Messages.getDefaultBundle());
       
   135         else
       
   136             return String.valueOf(arg);
       
   137     }
       
   138 
       
   139     /**
       
   140      * Format an iterable argument of a given diagnostic.
       
   141      *
       
   142      * @param d diagnostic whose argument is to be formatted
       
   143      * @param it iterable argument to be formatted
       
   144      * @param l locale object to be used for i18n
       
   145      * @return string representation of the diagnostic iterable argument
       
   146      */
       
   147     protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
       
   148         StringBuilder sbuf = new StringBuilder();
       
   149         String sep = "";
       
   150         for (Object o : it) {
       
   151             sbuf.append(sep);
       
   152             sbuf.append(formatArgument(d, o, l));
       
   153             sep = ",";
       
   154         }
       
   155         return sbuf.toString();
       
   156     }
       
   157 
       
   158     /**
       
   159      * Converts a String into a locale-dependent representation accordingly to a given locale
       
   160      *
       
   161      * @param l locale object to be used for i18n
       
   162      * @param key locale-independent key used for looking up in a resource file
       
   163      * @param args localization arguments
       
   164      * @return a locale-dependent string
       
   165      */
       
   166     protected String localize(Locale l, String key, Object... args) {
       
   167         return messages.getLocalizedString(key, args);
       
   168     }
       
   169 }