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

changeset 168
4cdaaf4c5dca
parent 137
e4eaddca54b7
child 221
6ada6122dd4f
equal deleted inserted replaced
166:32e309883246 168:4cdaaf4c5dca
25 package com.sun.tools.javac.util; 25 package com.sun.tools.javac.util;
26 26
27 import java.util.Collection; 27 import java.util.Collection;
28 import java.util.Locale; 28 import java.util.Locale;
29 import javax.tools.JavaFileObject; 29 import javax.tools.JavaFileObject;
30 import java.util.ResourceBundle;
31 30
32 import com.sun.tools.javac.api.DiagnosticFormatter; 31 import com.sun.tools.javac.api.DiagnosticFormatter;
33 import com.sun.tools.javac.api.Formattable; 32 import com.sun.tools.javac.api.Formattable;
34 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind; 33 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
35 import com.sun.tools.javac.file.JavacFileManager; 34 import com.sun.tools.javac.file.JavacFileManager;
36 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; 35 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
36 import static com.sun.tools.javac.util.LayoutCharacters.*;
37 37
38 /** 38 /**
39 * This abstract class provides a basic implementation of the functionalities that should be provided 39 * This abstract class provides a basic implementation of the functionalities that should be provided
40 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are: 40 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
41 * 41 *
70 this.showSource = showSource; 70 this.showSource = showSource;
71 } 71 }
72 72
73 public String formatMessage(JCDiagnostic d, Locale l) { 73 public String formatMessage(JCDiagnostic d, Locale l) {
74 //this code should rely on the locale settings but it's not! See RFE 6443132 74 //this code should rely on the locale settings but it's not! See RFE 6443132
75 StringBuilder buf = new StringBuilder();
75 Collection<String> args = formatArguments(d, l); 76 Collection<String> args = formatArguments(d, l);
76 return localize(l, d.getCode(), args.toArray()); 77 buf.append(localize(l, d.getCode(), args.toArray()));
78 if (d.isMultiline()) {
79 buf.append(formatSubdiagnostics(d, l));
80 }
81 return buf.toString();
77 } 82 }
78 83
79 public String formatKind(JCDiagnostic d, Locale l) { 84 public String formatKind(JCDiagnostic d, Locale l) {
80 switch (d.getType()) { 85 switch (d.getType()) {
81 case FRAGMENT: return ""; 86 case FRAGMENT: return "";
161 sbuf.append(sep); 166 sbuf.append(sep);
162 sbuf.append(formatArgument(d, o, l)); 167 sbuf.append(formatArgument(d, o, l));
163 sep = ","; 168 sep = ",";
164 } 169 }
165 return sbuf.toString(); 170 return sbuf.toString();
171 }
172
173 /**
174 * Format all the subdiagnostics attached to a given diagnostic
175 *
176 * @param d diagnostic whose subdiagnostics are to be formatted
177 * @param l locale object to be used for i18n
178 * @return string representation of the subdiagnostics
179 */
180 protected String formatSubdiagnostics(JCDiagnostic d, Locale l) {
181 StringBuilder buf = new StringBuilder();
182 for (JCDiagnostic d2 : d.getSubdiagnostics()) {
183 buf.append('\n');
184 String subdiagMsg = format(d2, l);
185 buf.append(indent(subdiagMsg, DiagInc));
186 }
187 return buf.toString();
166 } 188 }
167 189
168 /** Format the faulty source code line and point to the error. 190 /** Format the faulty source code line and point to the error.
169 * @param d The diagnostic for which the error line should be printed 191 * @param d The diagnostic for which the error line should be printed
170 */ 192 */
199 } 221 }
200 222
201 public boolean displaySource(JCDiagnostic d) { 223 public boolean displaySource(JCDiagnostic d) {
202 return showSource && d.getType() != FRAGMENT; 224 return showSource && d.getType() != FRAGMENT;
203 } 225 }
226
227 /**
228 * Creates a string with a given amount of empty spaces. Useful for
229 * indenting the text of a diagnostic message.
230 *
231 * @param nSpaces the amount of spaces to be added to the result string
232 * @return the indentation string
233 */
234 protected String indentString(int nSpaces) {
235 String spaces = " ";
236 if (nSpaces <= spaces.length())
237 return spaces.substring(0, nSpaces);
238 else {
239 StringBuilder buf = new StringBuilder();
240 for (int i = 0 ; i < nSpaces ; i++)
241 buf.append(" ");
242 return buf.toString();
243 }
244 }
245
246 /**
247 * Indent a string by prepending a given amount of empty spaces to each line
248 * of the string
249 *
250 * @param s the string to be indented
251 * @param nSpaces the amount of spaces that should be prepended to each line
252 * of the string
253 * @return an indented string
254 */
255 protected String indent(String s, int nSpaces) {
256 String indent = indentString(nSpaces);
257 StringBuilder buf = new StringBuilder();
258 String nl = "";
259 for (String line : s.split("\n")) {
260 buf.append(nl);
261 buf.append(indent + line);
262 nl = "\n";
263 }
264 return buf.toString();
265 }
204 } 266 }

mercurial