22 * CA 95054 USA or visit www.sun.com if you need additional information or |
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 package com.sun.tools.javac.util; |
25 package com.sun.tools.javac.util; |
26 |
26 |
|
27 import java.util.Collection; |
|
28 import java.util.EnumSet; |
27 import java.util.Locale; |
29 import java.util.Locale; |
28 |
30 |
|
31 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*; |
29 import com.sun.tools.javac.api.Formattable; |
32 import com.sun.tools.javac.api.Formattable; |
|
33 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration; |
|
34 |
30 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
35 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
31 |
36 |
32 /** |
37 /** |
33 * A raw formatter for diagnostic messages. |
38 * A raw formatter for diagnostic messages. |
34 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether |
39 * 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 |
40 * 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. |
41 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes. |
37 */ |
42 */ |
38 public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { |
43 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { |
39 |
44 |
40 /** |
45 /** |
41 * Create a formatter based on the supplied options. |
46 * Create a formatter based on the supplied options. |
42 * @param msgs |
47 * @param msgs |
43 */ |
48 */ |
44 public RawDiagnosticFormatter(Options opts) { |
49 public RawDiagnosticFormatter(Options options) { |
45 super(null, opts, false); |
50 super(null, new SimpleConfiguration(options, |
|
51 EnumSet.of(DiagnosticPart.SUMMARY, |
|
52 DiagnosticPart.DETAILS, |
|
53 DiagnosticPart.SUBDIAGNOSTICS))); |
46 } |
54 } |
47 |
55 |
48 //provide common default formats |
56 //provide common default formats |
49 public String format(JCDiagnostic d, Locale l) { |
57 public String format(JCDiagnostic d, Locale l) { |
50 try { |
58 try { |
60 else |
68 else |
61 buf.append('-'); |
69 buf.append('-'); |
62 buf.append(' '); |
70 buf.append(' '); |
63 buf.append(formatMessage(d, null)); |
71 buf.append(formatMessage(d, null)); |
64 if (displaySource(d)) |
72 if (displaySource(d)) |
65 buf.append("\n" + formatSourceLine(d)); |
73 buf.append("\n" + formatSourceLine(d, 0)); |
66 return buf.toString(); |
74 return buf.toString(); |
67 } |
75 } |
68 catch (Exception e) { |
76 catch (Exception e) { |
69 e.printStackTrace(); |
77 e.printStackTrace(); |
70 return null; |
78 return null; |
71 } |
79 } |
|
80 } |
|
81 |
|
82 public String formatMessage(JCDiagnostic d, Locale l) { |
|
83 StringBuilder buf = new StringBuilder(); |
|
84 Collection<String> args = formatArguments(d, l); |
|
85 buf.append(d.getCode()); |
|
86 String sep = ": "; |
|
87 for (Object o : args) { |
|
88 buf.append(sep); |
|
89 buf.append(o); |
|
90 sep = ", "; |
|
91 } |
|
92 if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) { |
|
93 List<String> subDiags = formatSubdiagnostics(d, null); |
|
94 if (subDiags.nonEmpty()) { |
|
95 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(); |
72 } |
106 } |
73 |
107 |
74 @Override |
108 @Override |
75 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { |
109 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { |
76 String s; |
110 String s; |
81 if (arg instanceof JCDiagnostic) |
115 if (arg instanceof JCDiagnostic) |
82 return "(" + s + ")"; |
116 return "(" + s + ")"; |
83 else |
117 else |
84 return s; |
118 return s; |
85 } |
119 } |
86 |
|
87 @Override |
|
88 protected String formatSubdiagnostics(JCDiagnostic d, Locale l) { |
|
89 StringBuilder buf = new StringBuilder(); |
|
90 String sep = ""; |
|
91 buf.append(",{"); |
|
92 for (JCDiagnostic d2 : d.getSubdiagnostics()) { |
|
93 buf.append(sep); |
|
94 buf.append("(" + format(d2, l) + ")"); |
|
95 sep = ","; |
|
96 } |
|
97 buf.append('}'); |
|
98 return buf.toString(); |
|
99 } |
|
100 |
|
101 @Override |
|
102 protected String localize(Locale l, String s, Object... args) { |
|
103 StringBuffer buf = new StringBuffer(); |
|
104 buf.append(s); |
|
105 String sep = ": "; |
|
106 for (Object o : args) { |
|
107 buf.append(sep); |
|
108 buf.append(o); |
|
109 sep = ", "; |
|
110 } |
|
111 return buf.toString(); |
|
112 } |
|
113 } |
120 } |