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