14
14
package org.aspectj.bridge;
17
import java.io.IOException;
18
import java.io.OutputStream;
19
import java.io.PrintStream;
20
import java.io.PrintWriter;
21
import java.io.StringWriter;
22
import java.util.ArrayList;
23
import java.util.Arrays;
24
import java.util.Collection;
25
import java.util.Collections;
26
import java.util.Iterator;
27
import java.util.List;
19
29
import org.aspectj.bridge.IMessage.Kind;
20
import org.aspectj.util.*;
30
import org.aspectj.util.LangUtil;
23
33
* Convenience API's for constructing, printing, and sending messages.
25
35
public class MessageUtil {
27
// ------ some constant, content-less messages
28
// no variants for "info" or "debug", which should always have content
29
public static final IMessage ABORT_NOTHING_TO_RUN
30
= new Message("aborting - nothing to run", IMessage.ABORT, null, null);
32
public static final IMessage FAIL_INCOMPLETE
33
= new Message("run not completed", IMessage.FAIL, null, null);
35
public static final IMessage ABORT_NOMESSAGE
36
= new Message("", IMessage.ABORT, null, null);
38
public static final IMessage FAIL_NOMESSAGE
39
= new Message("", IMessage.FAIL, null, null);
41
public static final IMessage ERROR_NOMESSAGE
42
= new Message("", IMessage.ERROR, null, null);
44
public static final IMessage WARNING_NOMESSAGE
45
= new Message("", IMessage.WARNING, null, null);
48
/** handle abort message (ignored if handler is null) */
49
public static boolean abort(IMessageHandler handler, String message) {
50
return ((null != handler)
51
&& handler.handleMessage(abort(message)));
54
/** create and handle exception message (ignored if handler is null) */
55
public static boolean abort(IMessageHandler handler, String message, Throwable t) {
56
if (handler != null) return handler.handleMessage(abort(message, t));
60
/** create and handle fail message (ignored if handler is null) */
61
public static boolean fail(IMessageHandler handler, String message) {
62
return ((null != handler)
63
&& handler.handleMessage(fail(message)));
66
// /** create and handle fail message from reader (ignored if handler is null) */
67
// public static boolean fail(IMessageHandler handler, String message, LineReader reader) {
68
// return ((null != handler)
69
// && handler.handleMessage(fail(message, reader)));
72
/** create and handle fail message (ignored if handler is null) */
73
public static boolean fail(IMessageHandler handler, String message, Throwable thrown) {
74
return ((null != handler)
75
&& handler.handleMessage(fail(message, thrown)));
78
/** create and handle error message (ignored if handler is null) */
79
public static boolean error(IMessageHandler handler, String message) {
80
return ((null != handler)
81
&& handler.handleMessage(error(message)));
84
/** create and handle warn message (ignored if handler is null) */
85
public static boolean warn(IMessageHandler handler, String message) {
86
return ((null != handler)
87
&& handler.handleMessage(warn(message)));
90
/** create and handle debug message (ignored if handler is null) */
91
public static boolean debug(IMessageHandler handler, String message) {
92
return ((null != handler)
93
&& handler.handleMessage(debug(message)));
96
/** create and handle info message (ignored if handler is null) */
97
public static boolean info(IMessageHandler handler, String message) {
98
return ((null != handler)
99
&& handler.handleMessage(info(message)));
102
/** @return ABORT_NOMESSAGE if message is empty or IMessage otherwise */ //
103
public static IMessage abort(String message) {
104
if (LangUtil.isEmpty(message)) {
105
return ABORT_NOMESSAGE;
107
return new Message(message, IMessage.ABORT, null, null);
111
/** @return abort IMessage with thrown and message o
112
* ABORT_NOMESSAGE if both are empty/null */ //
113
public static IMessage abort(String message, Throwable thrown) {
114
if (!LangUtil.isEmpty(message)) {
115
return new Message(message, IMessage.ABORT, thrown, null);
116
} else if (null == thrown) {
117
return ABORT_NOMESSAGE;
119
return new Message(thrown.getMessage(), IMessage.ABORT, thrown, null);
123
/** @return FAIL_NOMESSAGE if message is empty or IMessage otherwise */
124
public static IMessage fail(String message) {
125
if (LangUtil.isEmpty(message)) {
126
return FAIL_NOMESSAGE;
128
return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
130
// return fail(message, (LineReader) null);
135
* Create fail message.
136
* If message is empty but thrown is not, use thrown.getMessage() as the message.
137
* If message is empty and thrown is null, return FAIL_NOMESSAGE.
138
* @return FAIL_NOMESSAGE if thrown is null and message is empty
139
* or IMessage FAIL with message and thrown otherwise
141
public static IMessage fail(String message, Throwable thrown) {
142
if (LangUtil.isEmpty(message)) {
143
if (null == thrown) {
144
return FAIL_NOMESSAGE;
146
return new Message(thrown.getMessage(), IMessage.FAIL, thrown, null);
149
return new Message(message, IMessage.FAIL, thrown, null);
154
* @return IMessage with IMessage.Kind FAIL and message as text
155
* and soure location from reader
157
// public static IMessage fail(String message) {//, LineReader reader) {
158
// ISourceLocation loc = null;
159
// if (null == reader) {
160
// loc = ISourceLocation.EMPTY;
162
// int line = reader.getLineNumber();
166
// loc = new SourceLocation(reader.getFile(), line, line, 0);
168
// return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
171
/** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */ //
172
public static IMessage error(String message, ISourceLocation location) {
173
if (LangUtil.isEmpty(message)) {
174
return ERROR_NOMESSAGE;
176
return new Message(message, IMessage.ERROR, null, location);
180
/** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */ //
181
public static IMessage warn(String message, ISourceLocation location) {
182
if (LangUtil.isEmpty(message)) {
183
return WARNING_NOMESSAGE;
185
return new Message(message, IMessage.WARNING, null, location);
188
/** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */ //
189
public static IMessage error(String message) {
190
if (LangUtil.isEmpty(message)) {
191
return ERROR_NOMESSAGE;
193
return new Message(message, IMessage.ERROR, null, null);
197
/** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */ //
198
public static IMessage warn(String message) {
199
if (LangUtil.isEmpty(message)) {
200
return WARNING_NOMESSAGE;
202
return new Message(message, IMessage.WARNING, null, null);
206
/** @return IMessage.DEBUG message with message content */
207
public static IMessage debug(String message) {
208
return new Message(message, IMessage.DEBUG, null, null);
211
/** @return IMessage.INFO message with message content */
212
public static IMessage info(String message) {
213
return new Message(message, IMessage.INFO, null, null);
216
// /** @return ISourceLocation with the current File/line of the reader */
217
// public static ISourceLocation makeSourceLocation(LineReader reader) {
218
// LangUtil.throwIaxIfNull(reader, "reader");
220
// int line = reader.getLineNumber();
224
// return new SourceLocation(reader.getFile(), line, line, 0);
227
// ------------------------ printing messages
229
* Print total counts message to the print stream, starting each on a new line
230
* @param messageHolder
233
public static void printMessageCounts(PrintStream out, IMessageHolder messageHolder) {
234
if ((null == out) || (null == messageHolder)) {
237
printMessageCounts(out, messageHolder, "");
240
public static void printMessageCounts(PrintStream out, IMessageHolder holder, String prefix) {
241
out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
37
// ------ some constant, content-less messages
38
// no variants for "info" or "debug", which should always have content
39
public static final IMessage ABORT_NOTHING_TO_RUN = new Message("aborting - nothing to run", IMessage.ABORT, null, null);
41
public static final IMessage FAIL_INCOMPLETE = new Message("run not completed", IMessage.FAIL, null, null);
43
public static final IMessage ABORT_NOMESSAGE = new Message("", IMessage.ABORT, null, null);
45
public static final IMessage FAIL_NOMESSAGE = new Message("", IMessage.FAIL, null, null);
47
public static final IMessage ERROR_NOMESSAGE = new Message("", IMessage.ERROR, null, null);
49
public static final IMessage WARNING_NOMESSAGE = new Message("", IMessage.WARNING, null, null);
51
/** handle abort message (ignored if handler is null) */
52
public static boolean abort(IMessageHandler handler, String message) {
53
return ((null != handler) && handler.handleMessage(abort(message)));
56
/** create and handle exception message (ignored if handler is null) */
57
public static boolean abort(IMessageHandler handler, String message, Throwable t) {
58
if (handler != null) {
59
return handler.handleMessage(abort(message, t));
64
/** create and handle fail message (ignored if handler is null) */
65
public static boolean fail(IMessageHandler handler, String message) {
66
return ((null != handler) && handler.handleMessage(fail(message)));
69
// /** create and handle fail message from reader (ignored if handler is null) */
70
// public static boolean fail(IMessageHandler handler, String message, LineReader reader) {
71
// return ((null != handler)
72
// && handler.handleMessage(fail(message, reader)));
75
/** create and handle fail message (ignored if handler is null) */
76
public static boolean fail(IMessageHandler handler, String message, Throwable thrown) {
77
return ((null != handler) && handler.handleMessage(fail(message, thrown)));
80
/** create and handle error message (ignored if handler is null) */
81
public static boolean error(IMessageHandler handler, String message) {
82
return ((null != handler) && handler.handleMessage(error(message)));
85
/** create and handle warn message (ignored if handler is null) */
86
public static boolean warn(IMessageHandler handler, String message) {
87
return ((null != handler) && handler.handleMessage(warn(message)));
90
/** create and handle debug message (ignored if handler is null) */
91
public static boolean debug(IMessageHandler handler, String message) {
92
return ((null != handler) && handler.handleMessage(debug(message)));
95
/** create and handle info message (ignored if handler is null) */
96
public static boolean info(IMessageHandler handler, String message) {
97
return ((null != handler) && handler.handleMessage(info(message)));
100
/** @return ABORT_NOMESSAGE if message is empty or IMessage otherwise */
102
public static IMessage abort(String message) {
103
if (LangUtil.isEmpty(message)) {
104
return ABORT_NOMESSAGE;
106
return new Message(message, IMessage.ABORT, null, null);
111
* @return abort IMessage with thrown and message o ABORT_NOMESSAGE if both are empty/null
114
public static IMessage abort(String message, Throwable thrown) {
115
if (!LangUtil.isEmpty(message)) {
116
return new Message(message, IMessage.ABORT, thrown, null);
117
} else if (null == thrown) {
118
return ABORT_NOMESSAGE;
120
return new Message(thrown.getMessage(), IMessage.ABORT, thrown, null);
124
/** @return FAIL_NOMESSAGE if message is empty or IMessage otherwise */
125
public static IMessage fail(String message) {
126
if (LangUtil.isEmpty(message)) {
127
return FAIL_NOMESSAGE;
129
return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
131
// return fail(message, (LineReader) null);
136
* Create fail message. If message is empty but thrown is not, use thrown.getMessage() as the message. If message is empty and
137
* thrown is null, return FAIL_NOMESSAGE.
139
* @return FAIL_NOMESSAGE if thrown is null and message is empty or IMessage FAIL with message and thrown otherwise
141
public static IMessage fail(String message, Throwable thrown) {
142
if (LangUtil.isEmpty(message)) {
143
if (null == thrown) {
144
return FAIL_NOMESSAGE;
146
return new Message(thrown.getMessage(), IMessage.FAIL, thrown, null);
149
return new Message(message, IMessage.FAIL, thrown, null);
154
* @return IMessage with IMessage.Kind FAIL and message as text and soure location from reader
156
// public static IMessage fail(String message) {//, LineReader reader) {
157
// ISourceLocation loc = null;
158
// if (null == reader) {
159
// loc = ISourceLocation.EMPTY;
161
// int line = reader.getLineNumber();
165
// loc = new SourceLocation(reader.getFile(), line, line, 0);
167
// return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
170
/** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
172
public static IMessage error(String message, ISourceLocation location) {
173
if (LangUtil.isEmpty(message)) {
174
return ERROR_NOMESSAGE;
176
return new Message(message, IMessage.ERROR, null, location);
180
/** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
182
public static IMessage warn(String message, ISourceLocation location) {
183
if (LangUtil.isEmpty(message)) {
184
return WARNING_NOMESSAGE;
186
return new Message(message, IMessage.WARNING, null, location);
190
/** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
192
public static IMessage error(String message) {
193
if (LangUtil.isEmpty(message)) {
194
return ERROR_NOMESSAGE;
196
return new Message(message, IMessage.ERROR, null, null);
200
/** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
202
public static IMessage warn(String message) {
203
if (LangUtil.isEmpty(message)) {
204
return WARNING_NOMESSAGE;
206
return new Message(message, IMessage.WARNING, null, null);
210
/** @return IMessage.DEBUG message with message content */
211
public static IMessage debug(String message) {
212
return new Message(message, IMessage.DEBUG, null, null);
215
/** @return IMessage.INFO message with message content */
216
public static IMessage info(String message) {
217
return new Message(message, IMessage.INFO, null, null);
220
// /** @return ISourceLocation with the current File/line of the reader */
221
// public static ISourceLocation makeSourceLocation(LineReader reader) {
222
// LangUtil.throwIaxIfNull(reader, "reader");
224
// int line = reader.getLineNumber();
228
// return new SourceLocation(reader.getFile(), line, line, 0);
231
// ------------------------ printing messages
233
* Print total counts message to the print stream, starting each on a new line
235
* @param messageHolder
238
public static void printMessageCounts(PrintStream out, IMessageHolder messageHolder) {
239
if ((null == out) || (null == messageHolder)) {
242
printMessageCounts(out, messageHolder, "");
245
public static void printMessageCounts(PrintStream out, IMessageHolder holder, String prefix) {
246
out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
244
250
* Print all message to the print stream, starting each on a new line
245
252
* @param messageHolder
247
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
254
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
249
256
public static void print(PrintStream out, IMessageHolder messageHolder) {
250
print(out, messageHolder, (String) null, (IMessageRenderer) null, (IMessageHandler) null);
254
* Print all message to the print stream, starting each on a new line,
256
* @param messageHolder
258
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
260
public static void print(PrintStream out, IMessageHolder holder, String prefix) {
261
print(out, holder, prefix, (IMessageRenderer) null, (IMessageHandler) null);
265
* Print all message to the print stream, starting each on a new line,
266
* with a prefix and using a renderer.
267
* @param messageHolder
269
* @param renderer IMessageRender to render result - use MESSAGE_LINE if null
270
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
272
public static void print(PrintStream out, IMessageHolder holder, String prefix,
273
IMessageRenderer renderer) {
274
print(out, holder, prefix, renderer, (IMessageHandler) null);
278
* Print all message to the print stream, starting each on a new line,
279
* with a prefix and using a renderer.
280
* The first line renders a summary: {prefix}MessageHolder: {summary}
281
* Each message line has the following form:
282
* <pre>{prefix}[{kind} {index}]: {rendering}</pre>
283
* (where "{index}" (length 3) is the position within
284
* the set of like-kinded messages, ignoring selector omissions.
285
* Renderers are free to render multi-line output.
286
* @param out the PrintStream sink - return silently if null
287
* @param messageHolder the IMessageHolder with the messages to print
288
* @param renderer IMessageRender to render result - use MESSAGE_ALL if null
289
* @param selector IMessageHandler to select messages to render - if null, do all non-null
291
public static void print(PrintStream out, IMessageHolder holder, String prefix,
292
IMessageRenderer renderer, IMessageHandler selector) {
293
print(out, holder, prefix, renderer, selector, true);
296
public static void print(PrintStream out, IMessageHolder holder, String prefix,
297
IMessageRenderer renderer, IMessageHandler selector, boolean printSummary) {
298
if ((null == out) || (null == holder)) {
301
if (null == renderer) {
302
renderer = MESSAGE_ALL;
304
if (null == selector) {
308
out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
310
for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
311
IMessage.Kind kind = (IMessage.Kind) iter.next();
312
if (!selector.isIgnoring(kind)) {
313
IMessage[] messages = holder.getMessages(kind, IMessageHolder.EQUAL);
314
for (int i = 0; i < messages.length; i++) {
315
if (selector.handleMessage(messages[i])) {
316
String label = (null == prefix
318
: prefix + "[" + kind + " " + LangUtil.toSizedString(i, 3) + "]: ");
319
out.println(label + renderer.renderToString(messages[i]));
326
public static String toShortString(IMessage message) {
327
if (null == message) {
330
String m = message.getMessage();
331
Throwable t = message.getThrown();
333
return (message.getKind() + (null == m ? "" : ": " + m)
334
+ (null == t ? "" : ": " + LangUtil.unqualifiedClassName(t)));
337
/** @return int number of message of this kind (optionally or greater) in list */
338
public static int numMessages(List messages, Kind kind, boolean orGreater) {
339
if (LangUtil.isEmpty(messages)) {
342
IMessageHandler selector = makeSelector(kind, orGreater, null);
343
IMessage[] result = visitMessages(messages, selector, true, false);
344
return result.length;
349
* Select all messages in holder except those of the same
350
* kind (optionally or greater).
351
* If kind is null, then all messages are rejected,
352
* so an empty list is returned.
353
* @return unmodifiable list of specified IMessage
355
public static IMessage[] getMessagesExcept(IMessageHolder holder,
356
final IMessage.Kind kind, final boolean orGreater) {
357
if ((null == holder) || (null == kind)) {
358
return new IMessage[0];
361
IMessageHandler selector = new IMessageHandler(){
362
public boolean handleMessage(IMessage message) {
363
IMessage.Kind test = message.getKind();
365
? kind.isSameOrLessThan(test)
368
public boolean isIgnoring(Kind kind) {
371
public void dontIgnore(IMessage.Kind kind) {
257
print(out, messageHolder, (String) null, (IMessageRenderer) null, (IMessageHandler) null);
261
* Print all message to the print stream, starting each on a new line, with a prefix.
263
* @param messageHolder
265
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
267
public static void print(PrintStream out, IMessageHolder holder, String prefix) {
268
print(out, holder, prefix, (IMessageRenderer) null, (IMessageHandler) null);
272
* Print all message to the print stream, starting each on a new line, with a prefix and using a renderer.
274
* @param messageHolder
276
* @param renderer IMessageRender to render result - use MESSAGE_LINE if null
277
* @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
279
public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer) {
280
print(out, holder, prefix, renderer, (IMessageHandler) null);
284
* Print all message to the print stream, starting each on a new line, with a prefix and using a renderer. The first line
285
* renders a summary: {prefix}MessageHolder: {summary} Each message line has the following form:
288
* {prefix}[{kind} {index}]: {rendering}
291
* (where "{index}" (length 3) is the position within the set of like-kinded messages, ignoring selector omissions. Renderers
292
* are free to render multi-line output.
294
* @param out the PrintStream sink - return silently if null
295
* @param messageHolder the IMessageHolder with the messages to print
296
* @param renderer IMessageRender to render result - use MESSAGE_ALL if null
297
* @param selector IMessageHandler to select messages to render - if null, do all non-null
299
public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
300
IMessageHandler selector) {
301
print(out, holder, prefix, renderer, selector, true);
304
public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
305
IMessageHandler selector, boolean printSummary) {
306
if ((null == out) || (null == holder)) {
309
if (null == renderer) {
310
renderer = MESSAGE_ALL;
312
if (null == selector) {
316
out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
318
for (IMessage.Kind kind : IMessage.KINDS) {
319
if (!selector.isIgnoring(kind)) {
320
IMessage[] messages = holder.getMessages(kind, IMessageHolder.EQUAL);
321
for (int i = 0; i < messages.length; i++) {
322
if (selector.handleMessage(messages[i])) {
323
String label = (null == prefix ? "" : prefix + "[" + kind + " " + LangUtil.toSizedString(i, 3) + "]: ");
324
out.println(label + renderer.renderToString(messages[i]));
331
public static String toShortString(IMessage message) {
332
if (null == message) {
335
String m = message.getMessage();
336
Throwable t = message.getThrown();
338
return (message.getKind() + (null == m ? "" : ": " + m) + (null == t ? "" : ": " + LangUtil.unqualifiedClassName(t)));
341
/** @return int number of message of this kind (optionally or greater) in list */
342
public static int numMessages(List<IMessage> messages, Kind kind, boolean orGreater) {
343
if (LangUtil.isEmpty(messages)) {
346
IMessageHandler selector = makeSelector(kind, orGreater, null);
347
IMessage[] result = visitMessages(messages, selector, true, false);
348
return result.length;
352
* Select all messages in holder except those of the same kind (optionally or greater). If kind is null, then all messages are
353
* rejected, so an empty list is returned.
355
* @return unmodifiable list of specified IMessage
357
public static IMessage[] getMessagesExcept(IMessageHolder holder, final IMessage.Kind kind, final boolean orGreater) {
358
if ((null == holder) || (null == kind)) {
359
return new IMessage[0];
362
IMessageHandler selector = new IMessageHandler() {
363
public boolean handleMessage(IMessage message) {
364
IMessage.Kind test = message.getKind();
365
return (!(orGreater ? kind.isSameOrLessThan(test) : kind == test));
368
public boolean isIgnoring(Kind kind) {
372
public void dontIgnore(IMessage.Kind kind) {
374
376
public void ignore(Kind kind) {
377
return visitMessages(holder, selector, true, false);
379
return visitMessages(holder, selector, true, false);
380
/** @return unmodifiable list of IMessage complying with parameters */
381
public static List getMessages(IMessageHolder holder,
382
IMessage.Kind kind, boolean orGreater, String infix) { // XXX untested
383
if (null == holder) {
384
return Collections.EMPTY_LIST;
386
if ((null == kind) && LangUtil.isEmpty(infix)) {
387
return holder.getUnmodifiableListView();
389
IMessageHandler selector = makeSelector(kind, orGreater, infix);
390
IMessage[] messages = visitMessages(holder, selector, true, false);
391
if (LangUtil.isEmpty(messages)) {
392
return Collections.EMPTY_LIST;
394
return Collections.unmodifiableList(Arrays.asList(messages));
382
/** @return unmodifiable list of IMessage complying with parameters */
383
public static List<IMessage> getMessages(IMessageHolder holder, IMessage.Kind kind, boolean orGreater, String infix) {
384
if (null == holder) {
385
return Collections.emptyList();
387
if ((null == kind) && LangUtil.isEmpty(infix)) {
388
return holder.getUnmodifiableListView();
390
IMessageHandler selector = makeSelector(kind, orGreater, infix);
391
IMessage[] messages = visitMessages(holder, selector, true, false);
392
if (LangUtil.isEmpty(messages)) {
393
return Collections.emptyList();
395
return Collections.unmodifiableList(Arrays.asList(messages));
398
399
* Extract messages of type kind from the input list.
400
401
* @param messages if null, return EMPTY_LIST
401
402
* @param kind if null, return messages
402
* @see MessageHandler#getMessages(Kind)
403
* @see MessageHandler#getMessages(Kind)
404
public static List getMessages(List messages, IMessage.Kind kind) {
405
if (null == messages) {
406
return Collections.EMPTY_LIST;
411
ArrayList result = new ArrayList();
412
for (Iterator iter = messages.iterator(); iter.hasNext();) {
413
IMessage element = (IMessage) iter.next();
414
if (kind == element.getKind()) {
418
if (0 == result.size()) {
419
return Collections.EMPTY_LIST;
405
public static List<IMessage> getMessages(List<IMessage> messages, IMessage.Kind kind) {
406
if (null == messages) {
407
return Collections.emptyList();
412
ArrayList<IMessage> result = new ArrayList<IMessage>();
413
for (IMessage message : messages) {
414
if (kind == message.getKind()) {
418
if (0 == result.size()) {
419
return Collections.emptyList();
425
425
* Map to the kind of messages associated with this string key.
426
427
* @param kind the String representing the kind of message (IMessage.Kind.toString())
427
428
* @return Kind the associated IMessage.Kind, or null if not found
429
430
public static IMessage.Kind getKind(String kind) {
431
kind = kind.toLowerCase();
432
for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
433
IMessage.Kind k = (IMessage.Kind) iter.next();
434
if (kind.equals(k.toString())) {
432
kind = kind.toLowerCase();
433
for (IMessage.Kind k : IMessage.KINDS) {
434
if (kind.equals(k.toString())) {
443
* Run visitor over the set of messages in holder,
444
* optionally accumulating those accepted by the visitor
446
public static IMessage[] visitMessages(IMessageHolder holder,
447
IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
448
if (null == holder) {
449
return IMessage.RA_IMessage;
451
return visitMessages(holder.getUnmodifiableListView(), visitor, accumulate, abortOnFail);
456
* Run visitor over the set of messages in holder,
457
* optionally accumulating those accepted by the visitor
459
public static IMessage[] visitMessages(IMessage[] messages,
460
IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
461
if (LangUtil.isEmpty(messages)) {
462
return IMessage.RA_IMessage;
464
return visitMessages(Arrays.asList(messages), visitor, accumulate, abortOnFail);
469
* Run visitor over a collection of messages,
470
* optionally accumulating those accepted by the visitor
471
* @param messages if null or empty, return IMessage.RA_IMessage
472
* @param visitor run visitor.handleMessage(message) on each
473
* message - if null and messages not empty, IllegalArgumentException
474
* @param accumulate if true, then return accepted IMessage[]
475
* @param abortOnFail if true and visitor returns false, stop visiting
476
* @return IMessage.RA_IMessage if collection is empty, if not accumulate,
477
* or if visitor accepted no IMessage,
478
* or IMessage[] of accepted messages otherwise
479
* @throws IllegalArgumentException if any in collection are not instanceof IMessage
481
public static IMessage[] visitMessages(Collection /*IMessage*/ messages,
482
IMessageHandler visitor, final boolean accumulate, final boolean abortOnFail) {
483
if (LangUtil.isEmpty(messages)) {
484
return IMessage.RA_IMessage;
486
LangUtil.throwIaxIfNull(visitor, "visitor");
487
ArrayList result = (accumulate ? new ArrayList() : null);
488
for (Iterator iter = messages.iterator(); iter.hasNext();) {
489
Object o = iter.next();
490
LangUtil.throwIaxIfFalse(o instanceof IMessage, "expected IMessage, got " + o);
491
IMessage m = (IMessage) o;
492
if (visitor.handleMessage(m)) {
496
} else if (abortOnFail) {
500
if (!accumulate || (0 == result.size())) {
501
return IMessage.RA_IMessage;
503
return (IMessage[]) result.toArray(IMessage.RA_IMessage);
508
* Make an IMessageHandler that handles IMessage if they have the right kind
509
* (or greater) and contain some infix String.
510
* @param kind the IMessage.Kind required of the message
511
* @param orGreater if true, also accept messages with greater kinds, as
512
* defined by IMessage.Kind.COMPARATOR
513
* @param infix the String text to require in the message - may be null or empty
514
* to accept any message with the specified kind.
515
* @return IMessageHandler selector that works to param specs
517
public static IMessageHandler makeSelector(IMessage.Kind kind, boolean orGreater, String infix) {
518
if (!orGreater && LangUtil.isEmpty(infix)) {
519
if (kind == IMessage.ABORT) {
521
} else if (kind == IMessage.DEBUG) {
523
} else if (kind == IMessage.DEBUG) {
525
} else if (kind == IMessage.ERROR) {
527
} else if (kind == IMessage.FAIL) {
529
} else if (kind == IMessage.INFO) {
531
} else if (kind == IMessage.WARNING) {
535
return new KindSelector(kind, orGreater, infix);
538
// ------------------ visitors to select messages
539
public static final IMessageHandler PICK_ALL = new KindSelector((IMessage.Kind) null);
540
public static final IMessageHandler PICK_ABORT = new KindSelector(IMessage.ABORT);
541
public static final IMessageHandler PICK_DEBUG = new KindSelector(IMessage.DEBUG);
542
public static final IMessageHandler PICK_ERROR = new KindSelector(IMessage.ERROR);
543
public static final IMessageHandler PICK_FAIL = new KindSelector(IMessage.FAIL);
544
public static final IMessageHandler PICK_INFO = new KindSelector(IMessage.INFO);
545
public static final IMessageHandler PICK_WARNING = new KindSelector(IMessage.WARNING);
546
public static final IMessageHandler PICK_ABORT_PLUS = new KindSelector(IMessage.ABORT, true);
547
public static final IMessageHandler PICK_DEBUG_PLUS = new KindSelector(IMessage.DEBUG, true);
548
public static final IMessageHandler PICK_ERROR_PLUS = new KindSelector(IMessage.ERROR, true);
549
public static final IMessageHandler PICK_FAIL_PLUS = new KindSelector(IMessage.FAIL, true);
550
public static final IMessageHandler PICK_INFO_PLUS = new KindSelector(IMessage.INFO, true);
551
public static final IMessageHandler PICK_WARNING_PLUS = new KindSelector(IMessage.WARNING, true);
554
/** implementation for PICK_... constants */
555
private static class KindSelector implements IMessageHandler {
556
final IMessage.Kind sought;
560
KindSelector(IMessage.Kind sought) {
564
KindSelector(IMessage.Kind sought, boolean floor) {
565
this(sought, floor, null);
568
KindSelector(IMessage.Kind sought, boolean floor, String infix) {
569
this.sought = sought;
571
this.infix = (LangUtil.isEmpty(infix) ? null : infix);
574
/** @return false if this message is null,
575
* of true if we seek any kind (null)
576
* or if this has the exact kind we seek
577
* and this has any text sought
579
public boolean handleMessage(IMessage message) {
580
return ((null != message) && !isIgnoring(message.getKind())
584
/** @return true if handleMessage would return false for a message of this kind */
585
public boolean isIgnoring(IMessage.Kind kind) {
587
return ((null != sought) && (sought != kind));
588
} else if (null == sought) {
591
return (0 < IMessage.Kind.COMPARATOR.compare(sought, kind));
595
public void dontIgnore(IMessage.Kind kind) {
599
private boolean textIn(IMessage message) {
603
String text = message.getMessage();
604
return (text.indexOf(infix)!=-1);
443
* Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
445
public static IMessage[] visitMessages(IMessageHolder holder, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
446
if (null == holder) {
447
return IMessage.RA_IMessage;
449
return visitMessages(holder.getUnmodifiableListView(), visitor, accumulate, abortOnFail);
454
* Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
456
public static IMessage[] visitMessages(IMessage[] messages, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
457
if (LangUtil.isEmpty(messages)) {
458
return IMessage.RA_IMessage;
460
return visitMessages(Arrays.asList(messages), visitor, accumulate, abortOnFail);
465
* Run visitor over a collection of messages, optionally accumulating those accepted by the visitor
467
* @param messages if null or empty, return IMessage.RA_IMessage
468
* @param visitor run visitor.handleMessage(message) on each message - if null and messages not empty, IllegalArgumentException
469
* @param accumulate if true, then return accepted IMessage[]
470
* @param abortOnFail if true and visitor returns false, stop visiting
471
* @return IMessage.RA_IMessage if collection is empty, if not accumulate, or if visitor accepted no IMessage, or IMessage[] of
472
* accepted messages otherwise
473
* @throws IllegalArgumentException if any in collection are not instanceof IMessage
475
public static IMessage[] visitMessages(Collection<IMessage> messages, IMessageHandler visitor, final boolean accumulate,
476
final boolean abortOnFail) {
477
if (LangUtil.isEmpty(messages)) {
478
return IMessage.RA_IMessage;
480
LangUtil.throwIaxIfNull(visitor, "visitor");
481
ArrayList<IMessage> result = (accumulate ? new ArrayList<IMessage>() : null);
482
for (IMessage m : messages) {
483
if (visitor.handleMessage(m)) {
487
} else if (abortOnFail) {
491
if (!accumulate || (0 == result.size())) {
492
return IMessage.RA_IMessage;
494
return result.toArray(IMessage.RA_IMessage);
499
* Make an IMessageHandler that handles IMessage if they have the right kind (or greater) and contain some infix String.
501
* @param kind the IMessage.Kind required of the message
502
* @param orGreater if true, also accept messages with greater kinds, as defined by IMessage.Kind.COMPARATOR
503
* @param infix the String text to require in the message - may be null or empty to accept any message with the specified kind.
504
* @return IMessageHandler selector that works to param specs
506
public static IMessageHandler makeSelector(IMessage.Kind kind, boolean orGreater, String infix) {
507
if (!orGreater && LangUtil.isEmpty(infix)) {
508
if (kind == IMessage.ABORT) {
510
} else if (kind == IMessage.DEBUG) {
512
} else if (kind == IMessage.DEBUG) {
514
} else if (kind == IMessage.ERROR) {
516
} else if (kind == IMessage.FAIL) {
518
} else if (kind == IMessage.INFO) {
520
} else if (kind == IMessage.WARNING) {
524
return new KindSelector(kind, orGreater, infix);
527
// ------------------ visitors to select messages
528
public static final IMessageHandler PICK_ALL = new KindSelector((IMessage.Kind) null);
529
public static final IMessageHandler PICK_ABORT = new KindSelector(IMessage.ABORT);
530
public static final IMessageHandler PICK_DEBUG = new KindSelector(IMessage.DEBUG);
531
public static final IMessageHandler PICK_ERROR = new KindSelector(IMessage.ERROR);
532
public static final IMessageHandler PICK_FAIL = new KindSelector(IMessage.FAIL);
533
public static final IMessageHandler PICK_INFO = new KindSelector(IMessage.INFO);
534
public static final IMessageHandler PICK_WARNING = new KindSelector(IMessage.WARNING);
535
public static final IMessageHandler PICK_ABORT_PLUS = new KindSelector(IMessage.ABORT, true);
536
public static final IMessageHandler PICK_DEBUG_PLUS = new KindSelector(IMessage.DEBUG, true);
537
public static final IMessageHandler PICK_ERROR_PLUS = new KindSelector(IMessage.ERROR, true);
538
public static final IMessageHandler PICK_FAIL_PLUS = new KindSelector(IMessage.FAIL, true);
539
public static final IMessageHandler PICK_INFO_PLUS = new KindSelector(IMessage.INFO, true);
540
public static final IMessageHandler PICK_WARNING_PLUS = new KindSelector(IMessage.WARNING, true);
542
/** implementation for PICK_... constants */
543
private static class KindSelector implements IMessageHandler {
544
final IMessage.Kind sought;
548
KindSelector(IMessage.Kind sought) {
552
KindSelector(IMessage.Kind sought, boolean floor) {
553
this(sought, floor, null);
556
KindSelector(IMessage.Kind sought, boolean floor, String infix) {
557
this.sought = sought;
559
this.infix = (LangUtil.isEmpty(infix) ? null : infix);
563
* @return false if this message is null, of true if we seek any kind (null) or if this has the exact kind we seek and this
564
* has any text sought
566
public boolean handleMessage(IMessage message) {
567
return ((null != message) && !isIgnoring(message.getKind()) && textIn(message));
570
/** @return true if handleMessage would return false for a message of this kind */
571
public boolean isIgnoring(IMessage.Kind kind) {
573
return ((null != sought) && (sought != kind));
574
} else if (null == sought) {
577
return (0 < IMessage.Kind.COMPARATOR.compare(sought, kind));
581
public void dontIgnore(IMessage.Kind kind) {
585
private boolean textIn(IMessage message) {
589
String text = message.getMessage();
590
return (text.indexOf(infix) != -1);
607
593
public void ignore(Kind kind) {
611
// ------------------ components to render messages
612
/** parameterize rendering behavior for messages */
613
public static interface IMessageRenderer {
614
String renderToString(IMessage message);
618
/** render message more verbosely if it is worse */
619
public static final IMessageRenderer MESSAGE_SCALED = new IMessageRenderer() {
620
public String toString() { return "MESSAGE_SCALED"; }
621
public String renderToString(IMessage message) {
622
if (null == message) {
623
return "((IMessage) null)";
625
IMessage.Kind kind = message.getKind();
627
if ((kind == IMessage.ABORT) || (kind == IMessage.FAIL)) {
629
} else if ((kind == IMessage.ERROR) || (kind == IMessage.WARNING)) {
634
String result = null;
637
result = MESSAGE_TOSTRING.renderToString(message);
640
result = MESSAGE_LINE.renderToString(message);
643
result = MESSAGE_SHORT.renderToString(message);
646
Throwable thrown = message.getThrown();
647
if (null != thrown) {
649
result += "Thrown: \n" + LangUtil.renderExceptionShort(thrown);
651
result += "Thrown: \n" + LangUtil.renderException(thrown);
659
/** render message as label, i.e., less than 33 char */
660
public static final IMessageRenderer MESSAGE_LABEL = new IMessageRenderer() {
661
public String toString() { return "MESSAGE_LABEL"; }
662
public String renderToString(IMessage message) {
663
if (null == message) {
664
return "((IMessage) null)";
666
return renderMessageLine(message, 5, 5, 32);
670
/** render message as label, i.e., less than 33 char, with no source location */
671
public static final IMessageRenderer MESSAGE_LABEL_NOLOC = new IMessageRenderer() {
672
public String toString() { return "MESSAGE_LABEL_NOLOC"; }
673
public String renderToString(IMessage message) {
674
if (null == message) {
675
return "((IMessage) null)";
677
return renderMessageLine(message, 10, 0, 32);
681
/** render message as line, i.e., less than 75 char, no internal line sep */
682
public static final IMessageRenderer MESSAGE_LINE = new IMessageRenderer() {
683
public String toString() { return "MESSAGE_LINE"; }
684
public String renderToString(IMessage message) {
685
if (null == message) {
686
return "((IMessage) null)";
688
return renderMessageLine(message, 8, 2, 74);
692
/** render message as line, i.e., less than 75 char, no internal line sep,
693
* trying to trim text as needed to end with a full source location */
694
public static final IMessageRenderer MESSAGE_LINE_FORCE_LOC = new IMessageRenderer() {
695
public String toString() { return "MESSAGE_LINE_FORCE_LOC"; }
696
public String renderToString(IMessage message) {
697
if (null == message) {
698
return "((IMessage) null)";
700
return renderMessageLine(message, 2, 40, 74);
704
/** render message without restriction, up to 10K, including throwable */
705
public static final IMessageRenderer MESSAGE_ALL = new IMessageRenderer() {
706
public String toString() { return "MESSAGE_ALL"; }
707
public String renderToString(IMessage message) {
708
return renderMessage(message);
712
// /** render message without restriction, up to 10K, including (but eliding) throwable */
713
// public static final IMessageRenderer MESSAGE_ALL_ELIDED= new IMessageRenderer() {
714
// public String toString() { return "MESSAGE_ALL_ELIDED"; }
715
// public String renderToString(IMessage message) {
716
// return renderMessage(message, true);
720
/** render message without restriction, except any Throwable thrown */
721
public static final IMessageRenderer MESSAGE_MOST = new IMessageRenderer() {
722
public String toString() { return "MESSAGE_MOST"; }
723
public String renderToString(IMessage message) {
724
if (null == message) {
725
return "((IMessage) null)";
727
return renderMessageLine(message, 1, 1, 10000);
731
/** render message as wide line, i.e., less than 256 char, no internal line sep,
732
* except any Throwable thrown
734
public static final IMessageRenderer MESSAGE_WIDELINE = new IMessageRenderer() {
735
public String toString() { return "MESSAGE_WIDELINE"; }
736
public String renderToString(IMessage message) {
737
if (null == message) {
738
return "((IMessage) null)";
740
return renderMessageLine(message, 8, 2, 255); // XXX revert to 256
743
/** render message using its toString() or "((IMessage) null)" */
744
public static final IMessageRenderer MESSAGE_TOSTRING = new IMessageRenderer() {
745
public String toString() { return "MESSAGE_TOSTRING"; }
746
public String renderToString(IMessage message) {
747
if (null == message) {
748
return "((IMessage) null)";
750
return message.toString();
754
/** render message using toShortString(IMessage)" */
755
public static final IMessageRenderer MESSAGE_SHORT = new IMessageRenderer() {
756
public String toString() { return "MESSAGE_SHORT"; }
757
public String renderToString(IMessage message) {
758
return toShortString(message);
763
* This renders IMessage as String, ignoring empty elements
764
* and eliding any thrown stack traces.
765
* @return "((IMessage) null)" if null or String rendering otherwise,
766
* including everything (esp. throwable stack trace)
767
* @see renderSourceLocation(ISourceLocation loc)
769
public static String renderMessage(IMessage message) {
770
return renderMessage(message, true);
774
* This renders IMessage as String, ignoring empty elements
775
* and eliding any thrown.
776
* @return "((IMessage) null)" if null or String rendering otherwise,
777
* including everything (esp. throwable stack trace)
778
* @see renderSourceLocation(ISourceLocation loc)
780
public static String renderMessage(IMessage message, boolean elide) {
781
if (null == message) {
782
return "((IMessage) null)";
785
ISourceLocation loc = message.getSourceLocation();
786
String locString = (null == loc ? "" : " at " + loc);
788
String result = message.getKind() + locString + " " + message.getMessage();
790
Throwable thrown = message.getThrown();
791
if (thrown != null) {
792
result += " -- " + LangUtil.renderExceptionShort(thrown);
793
result += "\n" + LangUtil.renderException(thrown, elide);
796
if (message.getExtraSourceLocations().isEmpty()) {
800
return addExtraSourceLocations(message, result);
804
public static String addExtraSourceLocations(
597
// ------------------ components to render messages
598
/** parameterize rendering behavior for messages */
599
public static interface IMessageRenderer {
600
String renderToString(IMessage message);
603
/** render message more verbosely if it is worse */
604
public static final IMessageRenderer MESSAGE_SCALED = new IMessageRenderer() {
605
public String toString() {
606
return "MESSAGE_SCALED";
609
public String renderToString(IMessage message) {
610
if (null == message) {
611
return "((IMessage) null)";
613
IMessage.Kind kind = message.getKind();
615
if ((kind == IMessage.ABORT) || (kind == IMessage.FAIL)) {
617
} else if ((kind == IMessage.ERROR) || (kind == IMessage.WARNING)) {
622
String result = null;
625
result = MESSAGE_TOSTRING.renderToString(message);
628
result = MESSAGE_LINE.renderToString(message);
631
result = MESSAGE_SHORT.renderToString(message);
634
Throwable thrown = message.getThrown();
635
if (null != thrown) {
637
result += "Thrown: \n" + LangUtil.renderExceptionShort(thrown);
639
result += "Thrown: \n" + LangUtil.renderException(thrown);
647
/** render message as label, i.e., less than 33 char */
648
public static final IMessageRenderer MESSAGE_LABEL = new IMessageRenderer() {
649
public String toString() {
650
return "MESSAGE_LABEL";
653
public String renderToString(IMessage message) {
654
if (null == message) {
655
return "((IMessage) null)";
657
return renderMessageLine(message, 5, 5, 32);
661
/** render message as label, i.e., less than 33 char, with no source location */
662
public static final IMessageRenderer MESSAGE_LABEL_NOLOC = new IMessageRenderer() {
663
public String toString() {
664
return "MESSAGE_LABEL_NOLOC";
667
public String renderToString(IMessage message) {
668
if (null == message) {
669
return "((IMessage) null)";
671
return renderMessageLine(message, 10, 0, 32);
675
/** render message as line, i.e., less than 75 char, no internal line sep */
676
public static final IMessageRenderer MESSAGE_LINE = new IMessageRenderer() {
677
public String toString() {
678
return "MESSAGE_LINE";
681
public String renderToString(IMessage message) {
682
if (null == message) {
683
return "((IMessage) null)";
685
return renderMessageLine(message, 8, 2, 74);
690
* render message as line, i.e., less than 75 char, no internal line sep, trying to trim text as needed to end with a full
693
public static final IMessageRenderer MESSAGE_LINE_FORCE_LOC = new IMessageRenderer() {
694
public String toString() {
695
return "MESSAGE_LINE_FORCE_LOC";
698
public String renderToString(IMessage message) {
699
if (null == message) {
700
return "((IMessage) null)";
702
return renderMessageLine(message, 2, 40, 74);
706
/** render message without restriction, up to 10K, including throwable */
707
public static final IMessageRenderer MESSAGE_ALL = new IMessageRenderer() {
708
public String toString() {
709
return "MESSAGE_ALL";
712
public String renderToString(IMessage message) {
713
return renderMessage(message);
717
// /** render message without restriction, up to 10K, including (but eliding) throwable */
718
// public static final IMessageRenderer MESSAGE_ALL_ELIDED= new IMessageRenderer() {
719
// public String toString() { return "MESSAGE_ALL_ELIDED"; }
720
// public String renderToString(IMessage message) {
721
// return renderMessage(message, true);
725
/** render message without restriction, except any Throwable thrown */
726
public static final IMessageRenderer MESSAGE_MOST = new IMessageRenderer() {
727
public String toString() {
728
return "MESSAGE_MOST";
731
public String renderToString(IMessage message) {
732
if (null == message) {
733
return "((IMessage) null)";
735
return renderMessageLine(message, 1, 1, 10000);
740
* render message as wide line, i.e., less than 256 char, no internal line sep, except any Throwable thrown
742
public static final IMessageRenderer MESSAGE_WIDELINE = new IMessageRenderer() {
743
public String toString() {
744
return "MESSAGE_WIDELINE";
747
public String renderToString(IMessage message) {
748
if (null == message) {
749
return "((IMessage) null)";
751
return renderMessageLine(message, 8, 2, 255); // XXX revert to 256
754
/** render message using its toString() or "((IMessage) null)" */
755
public static final IMessageRenderer MESSAGE_TOSTRING = new IMessageRenderer() {
756
public String toString() {
757
return "MESSAGE_TOSTRING";
760
public String renderToString(IMessage message) {
761
if (null == message) {
762
return "((IMessage) null)";
764
return message.toString();
768
/** render message using toShortString(IMessage)" */
769
public static final IMessageRenderer MESSAGE_SHORT = new IMessageRenderer() {
770
public String toString() {
771
return "MESSAGE_SHORT";
774
public String renderToString(IMessage message) {
775
return toShortString(message);
780
* This renders IMessage as String, ignoring empty elements and eliding any thrown stack traces.
782
* @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
783
* @see renderSourceLocation(ISourceLocation loc)
785
public static String renderMessage(IMessage message) {
786
return renderMessage(message, true);
790
* This renders IMessage as String, ignoring empty elements and eliding any thrown.
792
* @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
793
* @see renderSourceLocation(ISourceLocation loc)
795
public static String renderMessage(IMessage message, boolean elide) {
796
if (null == message) {
797
return "((IMessage) null)";
800
ISourceLocation loc = message.getSourceLocation();
801
String locString = (null == loc ? "" : " at " + loc);
803
String result = message.getKind() + locString + " " + message.getMessage();
805
Throwable thrown = message.getThrown();
806
if (thrown != null) {
807
result += " -- " + LangUtil.renderExceptionShort(thrown);
808
result += "\n" + LangUtil.renderException(thrown, elide);
811
if (message.getExtraSourceLocations().isEmpty()) {
814
return addExtraSourceLocations(message, result);
818
public static String addExtraSourceLocations(IMessage message, String baseMessage) {
808
819
StringWriter buf = new StringWriter();
809
820
PrintWriter writer = new PrintWriter(buf);
810
821
writer.println(baseMessage);
811
for (Iterator iter = message.getExtraSourceLocations().iterator(); iter.hasNext();) {
812
ISourceLocation element = (ISourceLocation) iter.next();
814
writer.print("\tsee also: " + element.toString());
815
if (iter.hasNext()) {
821
catch (IOException ioe) {}
822
return buf.getBuffer().toString();
831
* Render ISourceLocation to String, ignoring empty elements
832
* (null or ISourceLocation.NO_FILE or ISourceLocation.NO_COLUMN
833
* (though implementations may return 0 from getColumn() when
834
* passed NO_COLUMN as input)).
835
* @return "((ISourceLocation) null)" if null or String rendering
836
* <pre>{file:}line{:column}</pre>
839
public static String renderSourceLocation(ISourceLocation loc) {
841
return "((ISourceLocation) null)";
843
StringBuffer sb = new StringBuffer();
845
File sourceFile = loc.getSourceFile();
846
if (sourceFile != ISourceLocation.NO_FILE) {
847
sb.append(sourceFile.getPath());
850
int line = loc.getLine();
851
sb.append("" + line);
853
int column = loc.getColumn();
854
if (column != ISourceLocation.NO_COLUMN) {
855
sb.append(":" + column);
858
return sb.toString();
862
* Render message in a line.
863
* IMessage.Kind is always printed, then any unqualified exception class,
864
* then the remainder of text and location according to their relative scale,
865
* all to fit in max characters or less.
866
* This does not render thrown except for the unqualified class name
867
* @param max the number of characters - forced to 32..10000
868
* @param textScale relative proportion to spend on message and/or exception
869
* message, relative to source location - if 0, message is suppressed
870
* @param locScale relative proportion to spend on source location
872
* @return "((IMessage) null)" or message per spec
874
public static String renderMessageLine(
880
if (null == message) {
881
return "((IMessage) null)";
885
} else if (max > 10000) {
889
textScale = -textScale;
892
locScale = -locScale;
895
String text = message.getMessage();
896
Throwable thrown = message.getThrown();
897
ISourceLocation sl = message.getSourceLocation();
898
IMessage.Kind kind = message.getKind();
899
StringBuffer result = new StringBuffer();
900
result.append(kind.toString());
902
if (null != thrown) {
903
result.append(LangUtil.unqualifiedClassName(thrown) + " ");
904
if ((null == text) || ("".equals(text))) {
905
text = thrown.getMessage();
909
if (0 == textScale) {
911
} else if ((null != text) && (null != thrown)) {
912
// decide between message and exception text?
913
String s = thrown.getMessage();
914
if ((null != s) && (0 < s.length())) {
919
if ((0 != locScale) && (null != sl)) {
920
File f = sl.getSourceFile();
921
if (f == ISourceLocation.NO_FILE) {
927
int line = sl.getLine();
928
int col = sl.getColumn();
929
int end = sl.getEndLine();
930
if ((0 == line) && (0 == col) && (0 == end)) {
931
// ignore numbers if default
933
loc += ":" + line + (col == 0 ? "" : ":" + col);
934
if (line != end) { // XXX consider suppressing nonstandard...
938
if (!LangUtil.isEmpty(loc)) {
939
loc = "@[" + loc; // matching "]" added below after clipping
943
// now budget between text and loc
944
float totalScale = locScale + textScale;
945
float remainder = max - result.length() - 4;
946
if ((remainder > 0) && (0 < totalScale)) {
947
int textSize = (int) (remainder * textScale/totalScale);
948
int locSize = (int) (remainder * locScale/totalScale);
949
// adjust for underutilization
950
int extra = locSize - loc.length();
952
locSize = loc.length();
955
extra = textSize - text.length();
957
textSize = text.length();
958
if (locSize < loc.length()) {
962
if (locSize > loc.length()) {
963
locSize = loc.length();
965
if (textSize > text.length()) {
966
textSize = text.length();
969
result.append(text.substring(0, textSize));
975
result.append(loc.substring(0, locSize) + "]");
978
return result.toString();
822
for (Iterator<ISourceLocation> iter = message.getExtraSourceLocations().iterator(); iter.hasNext();) {
823
ISourceLocation element = iter.next();
824
if (element != null) {
825
writer.print("\tsee also: " + element.toString());
826
if (iter.hasNext()) {
833
} catch (IOException ioe) {
835
return buf.getBuffer().toString();
839
* Render ISourceLocation to String, ignoring empty elements (null or ISourceLocation.NO_FILE or ISourceLocation.NO_COLUMN
840
* (though implementations may return 0 from getColumn() when passed NO_COLUMN as input)).
842
* @return "((ISourceLocation) null)" if null or String rendering
845
* {file:}line{:column}
849
public static String renderSourceLocation(ISourceLocation loc) {
851
return "((ISourceLocation) null)";
853
StringBuffer sb = new StringBuffer();
855
File sourceFile = loc.getSourceFile();
856
if (sourceFile != ISourceLocation.NO_FILE) {
857
sb.append(sourceFile.getPath());
860
int line = loc.getLine();
861
sb.append("" + line);
863
int column = loc.getColumn();
864
if (column != ISourceLocation.NO_COLUMN) {
865
sb.append(":" + column);
868
return sb.toString();
872
* Render message in a line. IMessage.Kind is always printed, then any unqualified exception class, then the remainder of text
873
* and location according to their relative scale, all to fit in max characters or less. This does not render thrown except for
874
* the unqualified class name
876
* @param max the number of characters - forced to 32..10000
877
* @param textScale relative proportion to spend on message and/or exception message, relative to source location - if 0,
878
* message is suppressed
879
* @param locScale relative proportion to spend on source location suppressed if 0
880
* @return "((IMessage) null)" or message per spec
882
public static String renderMessageLine(IMessage message, int textScale, int locScale, int max) {
884
if (null == message) {
885
return "((IMessage) null)";
889
} else if (max > 10000) {
893
textScale = -textScale;
896
locScale = -locScale;
899
String text = message.getMessage();
900
Throwable thrown = message.getThrown();
901
ISourceLocation sl = message.getSourceLocation();
902
IMessage.Kind kind = message.getKind();
903
StringBuffer result = new StringBuffer();
904
result.append(kind.toString());
906
if (null != thrown) {
907
result.append(LangUtil.unqualifiedClassName(thrown) + " ");
908
if ((null == text) || ("".equals(text))) {
909
text = thrown.getMessage();
913
if (0 == textScale) {
915
} else if ((null != text) && (null != thrown)) {
916
// decide between message and exception text?
917
String s = thrown.getMessage();
918
if ((null != s) && (0 < s.length())) {
923
if ((0 != locScale) && (null != sl)) {
924
File f = sl.getSourceFile();
925
if (f == ISourceLocation.NO_FILE) {
931
int line = sl.getLine();
932
int col = sl.getColumn();
933
int end = sl.getEndLine();
934
if ((0 == line) && (0 == col) && (0 == end)) {
935
// ignore numbers if default
937
loc += ":" + line + (col == 0 ? "" : ":" + col);
938
if (line != end) { // XXX consider suppressing nonstandard...
942
if (!LangUtil.isEmpty(loc)) {
943
loc = "@[" + loc; // matching "]" added below after clipping
947
// now budget between text and loc
948
float totalScale = locScale + textScale;
949
float remainder = max - result.length() - 4;
950
if ((remainder > 0) && (0 < totalScale)) {
951
int textSize = (int) (remainder * textScale / totalScale);
952
int locSize = (int) (remainder * locScale / totalScale);
953
// adjust for underutilization
954
int extra = locSize - loc.length();
956
locSize = loc.length();
959
extra = textSize - text.length();
961
textSize = text.length();
962
if (locSize < loc.length()) {
966
if (locSize > loc.length()) {
967
locSize = loc.length();
969
if (textSize > text.length()) {
970
textSize = text.length();
973
result.append(text.substring(0, textSize));
979
result.append(loc.substring(0, locSize) + "]");
982
return result.toString();
981
985
/** @return String of the form "{(# {type}) }.." for message kinds, skipping 0 */
982
public static String renderCounts(IMessageHolder holder) {
983
if (0 == holder.numMessages(null, false)) {
984
return "(0 messages)";
986
StringBuffer sb = new StringBuffer();
987
for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
988
IMessage.Kind kind = (IMessage.Kind) iter.next();
986
public static String renderCounts(IMessageHolder holder) {
987
if (0 == holder.numMessages(null, false)) {
988
return "(0 messages)";
990
StringBuffer sb = new StringBuffer();
991
for (IMessage.Kind kind : IMessage.KINDS) {
989
992
int num = holder.numMessages(kind, false);
991
sb.append(" (" + num + " " + kind + ") ");
994
sb.append(" (" + num + " " + kind + ") ");
994
return sb.toString();
998
* Factory for handler adapted to PrintStream
999
* XXX weak - only handles println(String)
1000
* @param handler the IMessageHandler sink for the messages generated
1001
* @param kind the IMessage.Kind of message to create
1002
* @param overage the OuputStream for text not captured by the handler
1003
* (if null, System.out used)
1004
* @throws IllegalArgumentException if kind or handler is null
1006
public static PrintStream handlerPrintStream(final IMessageHandler handler,
1007
final IMessage.Kind kind, final OutputStream overage, final String prefix) {
1008
LangUtil.throwIaxIfNull(handler, "handler");
1009
LangUtil.throwIaxIfNull(kind, "kind");
1010
class HandlerPrintStream extends PrintStream {
1011
HandlerPrintStream() {
1012
super(null == overage ? System.out : overage);
1014
public void println() {
1017
public void println(Object o) {
1018
println(null == o ? "null" : o.toString());
997
return sb.toString();
1001
* Factory for handler adapted to PrintStream XXX weak - only handles println(String)
1003
* @param handler the IMessageHandler sink for the messages generated
1004
* @param kind the IMessage.Kind of message to create
1005
* @param overage the OuputStream for text not captured by the handler (if null, System.out used)
1006
* @throws IllegalArgumentException if kind or handler is null
1008
public static PrintStream handlerPrintStream(final IMessageHandler handler, final IMessage.Kind kind,
1009
final OutputStream overage, final String prefix) {
1010
LangUtil.throwIaxIfNull(handler, "handler");
1011
LangUtil.throwIaxIfNull(kind, "kind");
1012
class HandlerPrintStream extends PrintStream {
1013
HandlerPrintStream() {
1014
super(null == overage ? System.out : overage);
1017
public void println() {
1021
public void println(Object o) {
1022
println(null == o ? "null" : o.toString());
1020
1025
public void println(String input) {
1021
String textMessage = (null == prefix ? input : prefix + input);
1022
IMessage m = new Message(textMessage, kind, null, null);
1023
handler.handleMessage(m);
1026
return new HandlerPrintStream();
1029
/** utility class */
1030
private MessageUtil() {}
1033
* Handle all messages in the second handler using the first
1034
* @param handler the IMessageHandler sink for all messages in source
1035
* @param holder the IMessageHolder source for all messages to handle
1036
* @param fastFail if true, stop on first failure
1037
* @return false if any sink.handleMessage(..) failed
1039
public static boolean handleAll(
1040
IMessageHandler sink,
1041
IMessageHolder source,
1043
return handleAll(sink, source, null, true, fastFail);
1047
* Handle messages in the second handler using the first
1048
* @param handler the IMessageHandler sink for all messages in source
1049
* @param holder the IMessageHolder source for all messages to handle
1050
* @param kind the IMessage.Kind to select, if not null
1051
* @param orGreater if true, also accept greater kinds
1052
* @param fastFail if true, stop on first failure
1053
* @return false if any sink.handleMessage(..) failed
1055
public static boolean handleAll(
1056
IMessageHandler sink,
1057
IMessageHolder source,
1061
LangUtil.throwIaxIfNull(sink, "sink");
1062
LangUtil.throwIaxIfNull(source, "source");
1063
return handleAll(sink, source.getMessages(kind, orGreater), fastFail);
1067
* Handle messages in the second handler using the first
1068
* if they are NOT of this kind (optionally, or greater).
1069
* If you pass null as the kind, then all messages are
1070
* ignored and this returns true.
1071
* @param handler the IMessageHandler sink for all messages in source
1072
* @param holder the IMessageHolder source for all messages to handle
1073
* @param kind the IMessage.Kind to reject, if not null
1074
* @param orGreater if true, also reject greater kinds
1075
* @param fastFail if true, stop on first failure
1076
* @return false if any sink.handleMessage(..) failed
1078
public static boolean handleAllExcept(
1079
IMessageHandler sink,
1080
IMessageHolder source,
1084
LangUtil.throwIaxIfNull(sink, "sink");
1085
LangUtil.throwIaxIfNull(source, "source");
1089
IMessage[] messages = getMessagesExcept(source, kind, orGreater);
1090
return handleAll(sink, messages, fastFail);
1094
* Handle messages in the sink.
1095
* @param handler the IMessageHandler sink for all messages in source
1096
* @param sources the IMessage[] messages to handle
1097
* @param fastFail if true, stop on first failure
1098
* @return false if any sink.handleMessage(..) failed
1099
* @throws IllegalArgumentException if sink is null
1101
public static boolean handleAll(
1102
IMessageHandler sink,
1105
LangUtil.throwIaxIfNull(sink, "sink");
1106
if (LangUtil.isEmpty(sources)) {
1109
boolean result = true;
1110
for (int i = 0; i < sources.length; i++) {
1111
if (!sink.handleMessage(sources[i])) {
1026
String textMessage = (null == prefix ? input : prefix + input);
1027
IMessage m = new Message(textMessage, kind, null, null);
1028
handler.handleMessage(m);
1031
return new HandlerPrintStream();
1034
/** utility class */
1035
private MessageUtil() {
1039
* Handle all messages in the second handler using the first
1041
* @param handler the IMessageHandler sink for all messages in source
1042
* @param holder the IMessageHolder source for all messages to handle
1043
* @param fastFail if true, stop on first failure
1044
* @return false if any sink.handleMessage(..) failed
1046
public static boolean handleAll(IMessageHandler sink, IMessageHolder source, boolean fastFail) {
1047
return handleAll(sink, source, null, true, fastFail);
1051
* Handle messages in the second handler using the first
1053
* @param handler the IMessageHandler sink for all messages in source
1054
* @param holder the IMessageHolder source for all messages to handle
1055
* @param kind the IMessage.Kind to select, if not null
1056
* @param orGreater if true, also accept greater kinds
1057
* @param fastFail if true, stop on first failure
1058
* @return false if any sink.handleMessage(..) failed
1060
public static boolean handleAll(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
1062
LangUtil.throwIaxIfNull(sink, "sink");
1063
LangUtil.throwIaxIfNull(source, "source");
1064
return handleAll(sink, source.getMessages(kind, orGreater), fastFail);
1068
* Handle messages in the second handler using the first if they are NOT of this kind (optionally, or greater). If you pass null
1069
* as the kind, then all messages are ignored and this returns true.
1071
* @param handler the IMessageHandler sink for all messages in source
1072
* @param holder the IMessageHolder source for all messages to handle
1073
* @param kind the IMessage.Kind to reject, if not null
1074
* @param orGreater if true, also reject greater kinds
1075
* @param fastFail if true, stop on first failure
1076
* @return false if any sink.handleMessage(..) failed
1078
public static boolean handleAllExcept(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
1080
LangUtil.throwIaxIfNull(sink, "sink");
1081
LangUtil.throwIaxIfNull(source, "source");
1085
IMessage[] messages = getMessagesExcept(source, kind, orGreater);
1086
return handleAll(sink, messages, fastFail);
1090
* Handle messages in the sink.
1092
* @param handler the IMessageHandler sink for all messages in source
1093
* @param sources the IMessage[] messages to handle
1094
* @param fastFail if true, stop on first failure
1095
* @return false if any sink.handleMessage(..) failed
1096
* @throws IllegalArgumentException if sink is null
1098
public static boolean handleAll(IMessageHandler sink, IMessage[] sources, boolean fastFail) {
1099
LangUtil.throwIaxIfNull(sink, "sink");
1100
if (LangUtil.isEmpty(sources)) {
1103
boolean result = true;
1104
for (int i = 0; i < sources.length; i++) {
1105
if (!sink.handleMessage(sources[i])) {