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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 776
3c32c90031fd
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * 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.file.BaseFileObject;
    34 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
    36 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
    38 /**
    39  * A raw formatter for diagnostic messages.
    40  * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
    41  * or not the source name and position are set. This formatter provides a standardized, localize-independent
    42  * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
    43  *
    44  * <p><b>This is NOT part of any supported API.
    45  * If you write code that depends on this, you do so at your own risk.
    46  * This code and its internal interfaces are subject to change or
    47  * deletion without notice.</b>
    48  */
    49 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
    51     /**
    52      * Create a formatter based on the supplied options.
    53      * @param msgs
    54      */
    55     public RawDiagnosticFormatter(Options options) {
    56         super(null, new SimpleConfiguration(options,
    57                 EnumSet.of(DiagnosticPart.SUMMARY,
    58                         DiagnosticPart.DETAILS,
    59                         DiagnosticPart.SUBDIAGNOSTICS)));
    60     }
    62     //provide common default formats
    63     public String formatDiagnostic(JCDiagnostic d, Locale l) {
    64         try {
    65             StringBuffer buf = new StringBuffer();
    66             if (d.getPosition() != Position.NOPOS) {
    67                 buf.append(formatSource(d, false, null));
    68                 buf.append(':');
    69                 buf.append(formatPosition(d, LINE, null));
    70                 buf.append(':');
    71                 buf.append(formatPosition(d, COLUMN, null));
    72                 buf.append(':');
    73             }
    74             else
    75                 buf.append('-');
    76             buf.append(' ');
    77             buf.append(formatMessage(d, null));
    78             if (displaySource(d))
    79                 buf.append("\n" + formatSourceLine(d, 0));
    80             return buf.toString();
    81         }
    82         catch (Exception e) {
    83             e.printStackTrace();
    84             return null;
    85         }
    86     }
    88     public String formatMessage(JCDiagnostic d, Locale l) {
    89         StringBuilder buf = new StringBuilder();
    90         Collection<String> args = formatArguments(d, l);
    91         buf.append(localize(null, d.getCode(), args.toArray()));
    92         if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
    93             List<String> subDiags = formatSubdiagnostics(d, null);
    94             if (subDiags.nonEmpty()) {
    95                 String sep = "";
    96                 buf.append(",{");
    97                 for (String sub : formatSubdiagnostics(d, null)) {
    98                     buf.append(sep);
    99                     buf.append("(" + sub + ")");
   100                     sep = ",";
   101                 }
   102                 buf.append('}');
   103             }
   104         }
   105         return buf.toString();
   106     }
   108     @Override
   109     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
   110         String s;
   111         if (arg instanceof Formattable)
   112             s = arg.toString();
   113         else if (arg instanceof BaseFileObject)
   114             s = ((BaseFileObject) arg).getShortName();
   115         else
   116             s = super.formatArgument(diag, arg, null);
   117         if (arg instanceof JCDiagnostic)
   118             return "(" + s + ")";
   119         else
   120             return s;
   121     }
   123     @Override
   124     protected String localize(Locale l, String key, Object... args) {
   125         StringBuilder buf = new StringBuilder();
   126         buf.append(key);
   127         String sep = ": ";
   128         for (Object o : args) {
   129             buf.append(sep);
   130             buf.append(o);
   131             sep = ", ";
   132         }
   133         return buf.toString();
   134     }
   136     @Override
   137     public boolean isRaw() {
   138         return true;
   139     }
   140 }

mercurial