src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.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 2005-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  */
    26 package com.sun.tools.javac.util;
    28 import java.util.HashMap;
    29 import java.util.Locale;
    30 import java.util.Map;
    31 import javax.tools.JavaFileObject;
    33 import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicFormatKind.*;
    34 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
    36 /**
    37  * A basic formatter for diagnostic messages.
    38  * The basic formatter will format a diagnostic according to one of three format patterns, depending on whether
    39  * or not the source name and position are set. The formatter supports a printf-like string for patterns
    40  * with the following special characters:
    41  * <ul>
    42  * <li>%b: the base of the source name
    43  * <li>%f: the source name (full absolute path)
    44  * <li>%l: the line number of the diagnostic, derived from the character offset
    45  * <li>%c: the column number of the diagnostic, derived from the character offset
    46  * <li>%o: the character offset of the diagnostic if set
    47  * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
    48  * <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is
    49  *        shown if the type is ERROR and if a source name is set
    50  * <li>%m: the text or the diagnostic, including any appropriate arguments
    51  * <li>%_: space delimiter, useful for formatting purposes
    52  * </ul>
    53  */
    54 public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter {
    56     protected Map<BasicFormatKind, String> availableFormats;
    58     /**
    59      * Create a basic formatter based on the supplied options.
    60      *
    61      * @param opts list of command-line options
    62      * @param msgs JavacMessages object used for i18n
    63      */
    64     BasicDiagnosticFormatter(Options opts, JavacMessages msgs) {
    65         this(msgs); //common init
    66         String fmt = opts.get("diags");
    67         if (fmt != null) {
    68             String[] formats = fmt.split("\\|");
    69             switch (formats.length) {
    70                 case 3:
    71                     availableFormats.put(DEFAULT_CLASS_FORMAT, formats[2]);
    72                 case 2:
    73                     availableFormats.put(DEFAULT_NO_POS_FORMAT, formats[1]);
    74                 default:
    75                     availableFormats.put(DEFAULT_POS_FORMAT, formats[0]);
    76             }
    77         }
    78     }
    80     /**
    81      * Create a standard basic formatter
    82      *
    83      * @param msgs JavacMessages object used for i18n
    84      */
    85     public BasicDiagnosticFormatter(JavacMessages msgs) {
    86         super(msgs);
    87         availableFormats = new HashMap<BasicFormatKind, String>();
    88         availableFormats.put(DEFAULT_POS_FORMAT, "%f:%l:%_%t%m");
    89         availableFormats.put(DEFAULT_NO_POS_FORMAT, "%p%m");
    90         availableFormats.put(DEFAULT_CLASS_FORMAT, "%f:%_%t%m");
    91     }
    93     public String format(JCDiagnostic d, Locale l) {
    94         if (l == null)
    95             l = messages.getCurrentLocale();
    96         String format = selectFormat(d);
    97         StringBuilder buf = new StringBuilder();
    98         for (int i = 0; i < format.length(); i++) {
    99             char c = format.charAt(i);
   100             boolean meta = false;
   101             if (c == '%' && i < format.length() - 1) {
   102                 meta = true;
   103                 c = format.charAt(++i);
   104             }
   105             buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
   106         }
   107         return buf.toString();
   108     }
   110     protected String formatMeta(char c, JCDiagnostic d, Locale l) {
   111         switch (c) {
   112             case 'b':
   113                 return formatSource(d, false, l);
   114             case 'e':
   115                 return formatPosition(d, END, l);
   116             case 'f':
   117                 return formatSource(d, true, l);
   118             case 'l':
   119                 return formatPosition(d, LINE, l);
   120             case 'c':
   121                 return formatPosition(d, COLUMN, l);
   122             case 'o':
   123                 return formatPosition(d, OFFSET, l);
   124             case 'p':
   125                 return formatKind(d, l);
   126             case 's':
   127                 return formatPosition(d, START, l);
   128             case 't': {
   129                 boolean usePrefix;
   130                 switch (d.getType()) {
   131                 case FRAGMENT:
   132                     usePrefix = false;
   133                     break;
   134                 case ERROR:
   135                     usePrefix = (d.getIntPosition() == Position.NOPOS);
   136                     break;
   137                 default:
   138                     usePrefix = true;
   139                 }
   140                 if (usePrefix)
   141                     return formatKind(d, l);
   142                 else
   143                     return "";
   144             }
   145             case 'm':
   146                 return formatMessage(d, l);
   147             case '_':
   148                 return " ";
   149             case '%':
   150                 return "%";
   151             default:
   152                 return String.valueOf(c);
   153         }
   154     }
   156     private String selectFormat(JCDiagnostic d) {
   157         DiagnosticSource source = d.getDiagnosticSource();
   158         String format = availableFormats.get(DEFAULT_NO_POS_FORMAT);
   159         if (source != null) {
   160             if (d.getIntPosition() != Position.NOPOS) {
   161                 format = availableFormats.get(DEFAULT_POS_FORMAT);
   162             } else if (source.getFile() != null &&
   163                        source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
   164                 format = availableFormats.get(DEFAULT_CLASS_FORMAT);
   165             }
   166         }
   167         return format;
   168     }
   170     public boolean displaySource(JCDiagnostic d) {
   171         return true;
   172     }
   174     /**
   175      * This enum contains all the kinds of formatting patterns supported
   176      * by a basic diagnostic formatter.
   177      */
   178     public enum BasicFormatKind {
   179         /**
   180         * A format string to be used for diagnostics with a given position.
   181         */
   182         DEFAULT_POS_FORMAT,
   183         /**
   184         * A format string to be used for diagnostics without a given position.
   185         */
   186         DEFAULT_NO_POS_FORMAT,
   187         /**
   188         * A format string to be used for diagnostics regarding classfiles
   189         */
   190         DEFAULT_CLASS_FORMAT;
   191     }
   192 }

mercurial