You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageUtil.java 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.OutputStream;
  17. import java.io.PrintStream;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.aspectj.bridge.IMessage.Kind;
  27. import org.aspectj.util.LangUtil;
  28. /**
  29. * Convenience API's for constructing, printing, and sending messages.
  30. */
  31. public class MessageUtil {
  32. // ------ some constant, content-less messages
  33. // no variants for "info" or "debug", which should always have content
  34. public static final IMessage ABORT_NOTHING_TO_RUN = new Message("aborting - nothing to run", IMessage.ABORT, null, null);
  35. public static final IMessage FAIL_INCOMPLETE = new Message("run not completed", IMessage.FAIL, null, null);
  36. public static final IMessage ABORT_NOMESSAGE = new Message("", IMessage.ABORT, null, null);
  37. public static final IMessage FAIL_NOMESSAGE = new Message("", IMessage.FAIL, null, null);
  38. public static final IMessage ERROR_NOMESSAGE = new Message("", IMessage.ERROR, null, null);
  39. public static final IMessage WARNING_NOMESSAGE = new Message("", IMessage.WARNING, null, null);
  40. /** handle abort message (ignored if handler is null) */
  41. public static boolean abort(IMessageHandler handler, String message) {
  42. return ((null != handler) && handler.handleMessage(abort(message)));
  43. }
  44. /** create and handle exception message (ignored if handler is null) */
  45. public static boolean abort(IMessageHandler handler, String message, Throwable t) {
  46. if (handler != null) {
  47. return handler.handleMessage(abort(message, t));
  48. }
  49. return false;
  50. }
  51. /** create and handle fail message (ignored if handler is null) */
  52. public static boolean fail(IMessageHandler handler, String message) {
  53. return ((null != handler) && handler.handleMessage(fail(message)));
  54. }
  55. // /** create and handle fail message from reader (ignored if handler is null) */
  56. // public static boolean fail(IMessageHandler handler, String message, LineReader reader) {
  57. // return ((null != handler)
  58. // && handler.handleMessage(fail(message, reader)));
  59. // }
  60. /** create and handle fail message (ignored if handler is null) */
  61. public static boolean fail(IMessageHandler handler, String message, Throwable thrown) {
  62. return ((null != handler) && handler.handleMessage(fail(message, thrown)));
  63. }
  64. /** create and handle error message (ignored if handler is null) */
  65. public static boolean error(IMessageHandler handler, String message) {
  66. return ((null != handler) && handler.handleMessage(error(message)));
  67. }
  68. /** create and handle warn message (ignored if handler is null) */
  69. public static boolean warn(IMessageHandler handler, String message) {
  70. return ((null != handler) && handler.handleMessage(warn(message)));
  71. }
  72. /** create and handle debug message (ignored if handler is null) */
  73. public static boolean debug(IMessageHandler handler, String message) {
  74. return ((null != handler) && handler.handleMessage(debug(message)));
  75. }
  76. /** create and handle info message (ignored if handler is null) */
  77. public static boolean info(IMessageHandler handler, String message) {
  78. return ((null != handler) && handler.handleMessage(info(message)));
  79. }
  80. /** @return ABORT_NOMESSAGE if message is empty or IMessage otherwise */
  81. //
  82. public static IMessage abort(String message) {
  83. if (LangUtil.isEmpty(message)) {
  84. return ABORT_NOMESSAGE;
  85. } else {
  86. return new Message(message, IMessage.ABORT, null, null);
  87. }
  88. }
  89. /**
  90. * @return abort IMessage with thrown and message o ABORT_NOMESSAGE if both are empty/null
  91. */
  92. //
  93. public static IMessage abort(String message, Throwable thrown) {
  94. if (!LangUtil.isEmpty(message)) {
  95. return new Message(message, IMessage.ABORT, thrown, null);
  96. } else if (null == thrown) {
  97. return ABORT_NOMESSAGE;
  98. } else {
  99. return new Message(thrown.getMessage(), IMessage.ABORT, thrown, null);
  100. }
  101. }
  102. /** @return FAIL_NOMESSAGE if message is empty or IMessage otherwise */
  103. public static IMessage fail(String message) {
  104. if (LangUtil.isEmpty(message)) {
  105. return FAIL_NOMESSAGE;
  106. } else {
  107. return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
  108. // return fail(message, (LineReader) null);
  109. }
  110. }
  111. /**
  112. * Create fail message. If message is empty but thrown is not, use thrown.getMessage() as the message. If message is empty and
  113. * thrown is null, return FAIL_NOMESSAGE.
  114. *
  115. * @return FAIL_NOMESSAGE if thrown is null and message is empty or IMessage FAIL with message and thrown otherwise
  116. */
  117. public static IMessage fail(String message, Throwable thrown) {
  118. if (LangUtil.isEmpty(message)) {
  119. if (null == thrown) {
  120. return FAIL_NOMESSAGE;
  121. } else {
  122. return new Message(thrown.getMessage(), IMessage.FAIL, thrown, null);
  123. }
  124. } else {
  125. return new Message(message, IMessage.FAIL, thrown, null);
  126. }
  127. }
  128. /**
  129. * @return IMessage with IMessage.Kind FAIL and message as text and soure location from reader
  130. */
  131. // public static IMessage fail(String message) {//, LineReader reader) {
  132. // ISourceLocation loc = null;
  133. // if (null == reader) {
  134. // loc = ISourceLocation.EMPTY;
  135. // } else {
  136. // int line = reader.getLineNumber();
  137. // if (0 < line) {
  138. // line = 0;
  139. // }
  140. // loc = new SourceLocation(reader.getFile(), line, line, 0);
  141. // }
  142. // return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
  143. // }
  144. /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
  145. //
  146. public static IMessage error(String message, ISourceLocation location) {
  147. if (LangUtil.isEmpty(message)) {
  148. return ERROR_NOMESSAGE;
  149. } else {
  150. return new Message(message, IMessage.ERROR, null, location);
  151. }
  152. }
  153. /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
  154. //
  155. public static IMessage warn(String message, ISourceLocation location) {
  156. if (LangUtil.isEmpty(message)) {
  157. return WARNING_NOMESSAGE;
  158. } else {
  159. return new Message(message, IMessage.WARNING, null, location);
  160. }
  161. }
  162. /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
  163. //
  164. public static IMessage error(String message) {
  165. if (LangUtil.isEmpty(message)) {
  166. return ERROR_NOMESSAGE;
  167. } else {
  168. return new Message(message, IMessage.ERROR, null, null);
  169. }
  170. }
  171. /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
  172. //
  173. public static IMessage warn(String message) {
  174. if (LangUtil.isEmpty(message)) {
  175. return WARNING_NOMESSAGE;
  176. } else {
  177. return new Message(message, IMessage.WARNING, null, null);
  178. }
  179. }
  180. /** @return IMessage.DEBUG message with message content */
  181. public static IMessage debug(String message) {
  182. return new Message(message, IMessage.DEBUG, null, null);
  183. }
  184. /** @return IMessage.INFO message with message content */
  185. public static IMessage info(String message) {
  186. return new Message(message, IMessage.INFO, null, null);
  187. }
  188. // /** @return ISourceLocation with the current File/line of the reader */
  189. // public static ISourceLocation makeSourceLocation(LineReader reader) {
  190. // LangUtil.throwIaxIfNull(reader, "reader");
  191. //
  192. // int line = reader.getLineNumber();
  193. // if (0 < line) {
  194. // line = 0;
  195. // }
  196. // return new SourceLocation(reader.getFile(), line, line, 0);
  197. // }
  198. // ------------------------ printing messages
  199. /**
  200. * Print total counts message to the print stream, starting each on a new line
  201. *
  202. * @param messageHolder
  203. * @param out
  204. */
  205. public static void printMessageCounts(PrintStream out, IMessageHolder messageHolder) {
  206. if ((null == out) || (null == messageHolder)) {
  207. return;
  208. }
  209. printMessageCounts(out, messageHolder, "");
  210. }
  211. public static void printMessageCounts(PrintStream out, IMessageHolder holder, String prefix) {
  212. out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
  213. }
  214. /**
  215. * Print all message to the print stream, starting each on a new line
  216. *
  217. * @param messageHolder
  218. * @param out
  219. * @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
  220. */
  221. public static void print(PrintStream out, IMessageHolder messageHolder) {
  222. print(out, messageHolder, (String) null, (IMessageRenderer) null, (IMessageHandler) null);
  223. }
  224. /**
  225. * Print all message to the print stream, starting each on a new line, with a prefix.
  226. *
  227. * @param messageHolder
  228. * @param out
  229. * @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
  230. */
  231. public static void print(PrintStream out, IMessageHolder holder, String prefix) {
  232. print(out, holder, prefix, (IMessageRenderer) null, (IMessageHandler) null);
  233. }
  234. /**
  235. * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer.
  236. *
  237. * @param messageHolder
  238. * @param out
  239. * @param renderer IMessageRender to render result - use MESSAGE_LINE if null
  240. * @see #print(PrintStream, String, IMessageHolder, IMessageRenderer, IMessageHandler)
  241. */
  242. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer) {
  243. print(out, holder, prefix, renderer, (IMessageHandler) null);
  244. }
  245. /**
  246. * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer. The first line
  247. * renders a summary: {prefix}MessageHolder: {summary} Each message line has the following form:
  248. *
  249. * <pre>
  250. * {prefix}[{kind} {index}]: {rendering}
  251. * </pre>
  252. *
  253. * (where "{index}" (length 3) is the position within the set of like-kinded messages, ignoring selector omissions. Renderers
  254. * are free to render multi-line output.
  255. *
  256. * @param out the PrintStream sink - return silently if null
  257. * @param messageHolder the IMessageHolder with the messages to print
  258. * @param renderer IMessageRender to render result - use MESSAGE_ALL if null
  259. * @param selector IMessageHandler to select messages to render - if null, do all non-null
  260. */
  261. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
  262. IMessageHandler selector) {
  263. print(out, holder, prefix, renderer, selector, true);
  264. }
  265. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
  266. IMessageHandler selector, boolean printSummary) {
  267. if ((null == out) || (null == holder)) {
  268. return;
  269. }
  270. if (null == renderer) {
  271. renderer = MESSAGE_ALL;
  272. }
  273. if (null == selector) {
  274. selector = PICK_ALL;
  275. }
  276. if (printSummary) {
  277. out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
  278. }
  279. for (IMessage.Kind kind : IMessage.KINDS) {
  280. if (!selector.isIgnoring(kind)) {
  281. IMessage[] messages = holder.getMessages(kind, IMessageHolder.EQUAL);
  282. for (int i = 0; i < messages.length; i++) {
  283. if (selector.handleMessage(messages[i])) {
  284. String label = (null == prefix ? "" : prefix + "[" + kind + " " + LangUtil.toSizedString(i, 3) + "]: ");
  285. out.println(label + renderer.renderToString(messages[i]));
  286. }
  287. }
  288. }
  289. }
  290. }
  291. public static String toShortString(IMessage message) {
  292. if (null == message) {
  293. return "null";
  294. }
  295. String m = message.getMessage();
  296. Throwable t = message.getThrown();
  297. return (message.getKind() + (null == m ? "" : ": " + m) + (null == t ? "" : ": " + LangUtil.unqualifiedClassName(t)));
  298. }
  299. /** @return int number of message of this kind (optionally or greater) in list */
  300. public static int numMessages(List<IMessage> messages, Kind kind, boolean orGreater) {
  301. if (LangUtil.isEmpty(messages)) {
  302. return 0;
  303. }
  304. IMessageHandler selector = makeSelector(kind, orGreater, null);
  305. IMessage[] result = visitMessages(messages, selector, true, false);
  306. return result.length;
  307. }
  308. /**
  309. * Select all messages in holder except those of the same kind (optionally or greater). If kind is null, then all messages are
  310. * rejected, so an empty list is returned.
  311. *
  312. * @return unmodifiable list of specified IMessage
  313. */
  314. public static IMessage[] getMessagesExcept(IMessageHolder holder, final IMessage.Kind kind, final boolean orGreater) {
  315. if ((null == holder) || (null == kind)) {
  316. return new IMessage[0];
  317. }
  318. IMessageHandler selector = new IMessageHandler() {
  319. public boolean handleMessage(IMessage message) {
  320. IMessage.Kind test = message.getKind();
  321. return (!(orGreater ? kind.isSameOrLessThan(test) : kind == test));
  322. }
  323. public boolean isIgnoring(Kind kind) {
  324. return false;
  325. }
  326. public void dontIgnore(IMessage.Kind kind) {
  327. }
  328. public void ignore(Kind kind) {
  329. }
  330. };
  331. return visitMessages(holder, selector, true, false);
  332. }
  333. /** @return unmodifiable list of IMessage complying with parameters */
  334. public static List<IMessage> getMessages(IMessageHolder holder, IMessage.Kind kind, boolean orGreater, String infix) {
  335. if (null == holder) {
  336. return Collections.emptyList();
  337. }
  338. if ((null == kind) && LangUtil.isEmpty(infix)) {
  339. return holder.getUnmodifiableListView();
  340. }
  341. IMessageHandler selector = makeSelector(kind, orGreater, infix);
  342. IMessage[] messages = visitMessages(holder, selector, true, false);
  343. if (LangUtil.isEmpty(messages)) {
  344. return Collections.emptyList();
  345. }
  346. return Collections.unmodifiableList(Arrays.asList(messages));
  347. }
  348. /**
  349. * Extract messages of type kind from the input list.
  350. *
  351. * @param messages if null, return EMPTY_LIST
  352. * @param kind if null, return messages
  353. * @see MessageHandler#getMessages(Kind)
  354. */
  355. public static List<IMessage> getMessages(List<IMessage> messages, IMessage.Kind kind) {
  356. if (null == messages) {
  357. return Collections.emptyList();
  358. }
  359. if (null == kind) {
  360. return messages;
  361. }
  362. ArrayList<IMessage> result = new ArrayList<IMessage>();
  363. for (IMessage message : messages) {
  364. if (kind == message.getKind()) {
  365. result.add(message);
  366. }
  367. }
  368. if (0 == result.size()) {
  369. return Collections.emptyList();
  370. }
  371. return result;
  372. }
  373. /**
  374. * Map to the kind of messages associated with this string key.
  375. *
  376. * @param kind the String representing the kind of message (IMessage.Kind.toString())
  377. * @return Kind the associated IMessage.Kind, or null if not found
  378. */
  379. public static IMessage.Kind getKind(String kind) {
  380. if (null != kind) {
  381. kind = kind.toLowerCase();
  382. for (IMessage.Kind k : IMessage.KINDS) {
  383. if (kind.equals(k.toString())) {
  384. return k;
  385. }
  386. }
  387. }
  388. return null;
  389. }
  390. /**
  391. * Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
  392. */
  393. public static IMessage[] visitMessages(IMessageHolder holder, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
  394. if (null == holder) {
  395. return IMessage.RA_IMessage;
  396. } else {
  397. return visitMessages(holder.getUnmodifiableListView(), visitor, accumulate, abortOnFail);
  398. }
  399. }
  400. /**
  401. * Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
  402. */
  403. public static IMessage[] visitMessages(IMessage[] messages, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
  404. if (LangUtil.isEmpty(messages)) {
  405. return IMessage.RA_IMessage;
  406. } else {
  407. return visitMessages(Arrays.asList(messages), visitor, accumulate, abortOnFail);
  408. }
  409. }
  410. /**
  411. * Run visitor over a collection of messages, optionally accumulating those accepted by the visitor
  412. *
  413. * @param messages if null or empty, return IMessage.RA_IMessage
  414. * @param visitor run visitor.handleMessage(message) on each message - if null and messages not empty, IllegalArgumentException
  415. * @param accumulate if true, then return accepted IMessage[]
  416. * @param abortOnFail if true and visitor returns false, stop visiting
  417. * @return IMessage.RA_IMessage if collection is empty, if not accumulate, or if visitor accepted no IMessage, or IMessage[] of
  418. * accepted messages otherwise
  419. * @throws IllegalArgumentException if any in collection are not instanceof IMessage
  420. */
  421. public static IMessage[] visitMessages(Collection<IMessage> messages, IMessageHandler visitor, final boolean accumulate,
  422. final boolean abortOnFail) {
  423. if (LangUtil.isEmpty(messages)) {
  424. return IMessage.RA_IMessage;
  425. }
  426. LangUtil.throwIaxIfNull(visitor, "visitor");
  427. ArrayList<IMessage> result = (accumulate ? new ArrayList<IMessage>() : null);
  428. for (IMessage m : messages) {
  429. if (visitor.handleMessage(m)) {
  430. if (accumulate) {
  431. result.add(m);
  432. }
  433. } else if (abortOnFail) {
  434. break;
  435. }
  436. }
  437. if (!accumulate || (0 == result.size())) {
  438. return IMessage.RA_IMessage;
  439. } else {
  440. return result.toArray(IMessage.RA_IMessage);
  441. }
  442. }
  443. /**
  444. * Make an IMessageHandler that handles IMessage if they have the right kind (or greater) and contain some infix String.
  445. *
  446. * @param kind the IMessage.Kind required of the message
  447. * @param orGreater if true, also accept messages with greater kinds, as defined by IMessage.Kind.COMPARATOR
  448. * @param infix the String text to require in the message - may be null or empty to accept any message with the specified kind.
  449. * @return IMessageHandler selector that works to param specs
  450. */
  451. public static IMessageHandler makeSelector(IMessage.Kind kind, boolean orGreater, String infix) {
  452. if (!orGreater && LangUtil.isEmpty(infix)) {
  453. if (kind == IMessage.ABORT) {
  454. return PICK_ABORT;
  455. } else if (kind == IMessage.DEBUG) {
  456. return PICK_DEBUG;
  457. } else if (kind == IMessage.DEBUG) {
  458. return PICK_DEBUG;
  459. } else if (kind == IMessage.ERROR) {
  460. return PICK_ERROR;
  461. } else if (kind == IMessage.FAIL) {
  462. return PICK_FAIL;
  463. } else if (kind == IMessage.INFO) {
  464. return PICK_INFO;
  465. } else if (kind == IMessage.WARNING) {
  466. return PICK_WARNING;
  467. }
  468. }
  469. return new KindSelector(kind, orGreater, infix);
  470. }
  471. // ------------------ visitors to select messages
  472. public static final IMessageHandler PICK_ALL = new KindSelector((IMessage.Kind) null);
  473. public static final IMessageHandler PICK_ABORT = new KindSelector(IMessage.ABORT);
  474. public static final IMessageHandler PICK_DEBUG = new KindSelector(IMessage.DEBUG);
  475. public static final IMessageHandler PICK_ERROR = new KindSelector(IMessage.ERROR);
  476. public static final IMessageHandler PICK_FAIL = new KindSelector(IMessage.FAIL);
  477. public static final IMessageHandler PICK_INFO = new KindSelector(IMessage.INFO);
  478. public static final IMessageHandler PICK_WARNING = new KindSelector(IMessage.WARNING);
  479. public static final IMessageHandler PICK_ABORT_PLUS = new KindSelector(IMessage.ABORT, true);
  480. public static final IMessageHandler PICK_DEBUG_PLUS = new KindSelector(IMessage.DEBUG, true);
  481. public static final IMessageHandler PICK_ERROR_PLUS = new KindSelector(IMessage.ERROR, true);
  482. public static final IMessageHandler PICK_FAIL_PLUS = new KindSelector(IMessage.FAIL, true);
  483. public static final IMessageHandler PICK_INFO_PLUS = new KindSelector(IMessage.INFO, true);
  484. public static final IMessageHandler PICK_WARNING_PLUS = new KindSelector(IMessage.WARNING, true);
  485. /** implementation for PICK_... constants */
  486. private static class KindSelector implements IMessageHandler {
  487. final IMessage.Kind sought;
  488. final boolean floor;
  489. final String infix;
  490. KindSelector(IMessage.Kind sought) {
  491. this(sought, false);
  492. }
  493. KindSelector(IMessage.Kind sought, boolean floor) {
  494. this(sought, floor, null);
  495. }
  496. KindSelector(IMessage.Kind sought, boolean floor, String infix) {
  497. this.sought = sought;
  498. this.floor = floor;
  499. this.infix = (LangUtil.isEmpty(infix) ? null : infix);
  500. }
  501. /**
  502. * @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
  503. * has any text sought
  504. */
  505. public boolean handleMessage(IMessage message) {
  506. return ((null != message) && !isIgnoring(message.getKind()) && textIn(message));
  507. }
  508. /** @return true if handleMessage would return false for a message of this kind */
  509. public boolean isIgnoring(IMessage.Kind kind) {
  510. if (!floor) {
  511. return ((null != sought) && (sought != kind));
  512. } else if (null == sought) {
  513. return false;
  514. } else {
  515. return (0 < IMessage.Kind.COMPARATOR.compare(sought, kind));
  516. }
  517. }
  518. public void dontIgnore(IMessage.Kind kind) {
  519. }
  520. private boolean textIn(IMessage message) {
  521. if (null == infix) {
  522. return true;
  523. }
  524. String text = message.getMessage();
  525. return (text.contains(infix));
  526. }
  527. public void ignore(Kind kind) {
  528. }
  529. }
  530. // ------------------ components to render messages
  531. /** parameterize rendering behavior for messages */
  532. public static interface IMessageRenderer {
  533. String renderToString(IMessage message);
  534. }
  535. /** render message more verbosely if it is worse */
  536. public static final IMessageRenderer MESSAGE_SCALED = new IMessageRenderer() {
  537. public String toString() {
  538. return "MESSAGE_SCALED";
  539. }
  540. public String renderToString(IMessage message) {
  541. if (null == message) {
  542. return "((IMessage) null)";
  543. }
  544. IMessage.Kind kind = message.getKind();
  545. int level = 3;
  546. if ((kind == IMessage.ABORT) || (kind == IMessage.FAIL)) {
  547. level = 1;
  548. } else if ((kind == IMessage.ERROR) || (kind == IMessage.WARNING)) {
  549. level = 2;
  550. } else {
  551. level = 3;
  552. }
  553. String result = null;
  554. switch (level) {
  555. case (1):
  556. result = MESSAGE_TOSTRING.renderToString(message);
  557. break;
  558. case (2):
  559. result = MESSAGE_LINE.renderToString(message);
  560. break;
  561. case (3):
  562. result = MESSAGE_SHORT.renderToString(message);
  563. break;
  564. }
  565. Throwable thrown = message.getThrown();
  566. if (null != thrown) {
  567. if (level == 3) {
  568. result += "Thrown: \n" + LangUtil.renderExceptionShort(thrown);
  569. } else {
  570. result += "Thrown: \n" + LangUtil.renderException(thrown);
  571. }
  572. }
  573. return result;
  574. }
  575. };
  576. /** render message as label, i.e., less than 33 char */
  577. public static final IMessageRenderer MESSAGE_LABEL = new IMessageRenderer() {
  578. public String toString() {
  579. return "MESSAGE_LABEL";
  580. }
  581. public String renderToString(IMessage message) {
  582. if (null == message) {
  583. return "((IMessage) null)";
  584. }
  585. return renderMessageLine(message, 5, 5, 32);
  586. }
  587. };
  588. /** render message as label, i.e., less than 33 char, with no source location */
  589. public static final IMessageRenderer MESSAGE_LABEL_NOLOC = new IMessageRenderer() {
  590. public String toString() {
  591. return "MESSAGE_LABEL_NOLOC";
  592. }
  593. public String renderToString(IMessage message) {
  594. if (null == message) {
  595. return "((IMessage) null)";
  596. }
  597. return renderMessageLine(message, 10, 0, 32);
  598. }
  599. };
  600. /** render message as line, i.e., less than 75 char, no internal line sep */
  601. public static final IMessageRenderer MESSAGE_LINE = new IMessageRenderer() {
  602. public String toString() {
  603. return "MESSAGE_LINE";
  604. }
  605. public String renderToString(IMessage message) {
  606. if (null == message) {
  607. return "((IMessage) null)";
  608. }
  609. return renderMessageLine(message, 8, 2, 74);
  610. }
  611. };
  612. /**
  613. * 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
  614. * source location
  615. */
  616. public static final IMessageRenderer MESSAGE_LINE_FORCE_LOC = new IMessageRenderer() {
  617. public String toString() {
  618. return "MESSAGE_LINE_FORCE_LOC";
  619. }
  620. public String renderToString(IMessage message) {
  621. if (null == message) {
  622. return "((IMessage) null)";
  623. }
  624. return renderMessageLine(message, 2, 40, 74);
  625. }
  626. };
  627. /** render message without restriction, up to 10K, including throwable */
  628. public static final IMessageRenderer MESSAGE_ALL = new IMessageRenderer() {
  629. public String toString() {
  630. return "MESSAGE_ALL";
  631. }
  632. public String renderToString(IMessage message) {
  633. return renderMessage(message);
  634. }
  635. };
  636. // /** render message without restriction, up to 10K, including (but eliding) throwable */
  637. // public static final IMessageRenderer MESSAGE_ALL_ELIDED= new IMessageRenderer() {
  638. // public String toString() { return "MESSAGE_ALL_ELIDED"; }
  639. // public String renderToString(IMessage message) {
  640. // return renderMessage(message, true);
  641. // }
  642. // };
  643. /** render message without restriction, except any Throwable thrown */
  644. public static final IMessageRenderer MESSAGE_MOST = new IMessageRenderer() {
  645. public String toString() {
  646. return "MESSAGE_MOST";
  647. }
  648. public String renderToString(IMessage message) {
  649. if (null == message) {
  650. return "((IMessage) null)";
  651. }
  652. return renderMessageLine(message, 1, 1, 10000);
  653. }
  654. };
  655. /**
  656. * render message as wide line, i.e., less than 256 char, no internal line sep, except any Throwable thrown
  657. */
  658. public static final IMessageRenderer MESSAGE_WIDELINE = new IMessageRenderer() {
  659. public String toString() {
  660. return "MESSAGE_WIDELINE";
  661. }
  662. public String renderToString(IMessage message) {
  663. if (null == message) {
  664. return "((IMessage) null)";
  665. }
  666. return renderMessageLine(message, 8, 2, 255); // XXX revert to 256
  667. }
  668. };
  669. /** render message using its toString() or "((IMessage) null)" */
  670. public static final IMessageRenderer MESSAGE_TOSTRING = new IMessageRenderer() {
  671. public String toString() {
  672. return "MESSAGE_TOSTRING";
  673. }
  674. public String renderToString(IMessage message) {
  675. if (null == message) {
  676. return "((IMessage) null)";
  677. }
  678. return message.toString();
  679. }
  680. };
  681. /** render message using toShortString(IMessage)" */
  682. public static final IMessageRenderer MESSAGE_SHORT = new IMessageRenderer() {
  683. public String toString() {
  684. return "MESSAGE_SHORT";
  685. }
  686. public String renderToString(IMessage message) {
  687. return toShortString(message);
  688. }
  689. };
  690. /**
  691. * This renders IMessage as String, ignoring empty elements and eliding any thrown stack traces.
  692. *
  693. * @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
  694. * @see renderSourceLocation(ISourceLocation loc)
  695. */
  696. public static String renderMessage(IMessage message) {
  697. return renderMessage(message, true);
  698. }
  699. /**
  700. * This renders IMessage as String, ignoring empty elements and eliding any thrown.
  701. *
  702. * @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
  703. * @see renderSourceLocation(ISourceLocation loc)
  704. */
  705. public static String renderMessage(IMessage message, boolean elide) {
  706. if (null == message) {
  707. return "((IMessage) null)";
  708. }
  709. ISourceLocation loc = message.getSourceLocation();
  710. String locString = (null == loc ? "" : " at " + loc);
  711. String result = message.getKind() + locString + " " + message.getMessage();
  712. Throwable thrown = message.getThrown();
  713. if (thrown != null) {
  714. result += " -- " + LangUtil.renderExceptionShort(thrown);
  715. result += "\n" + LangUtil.renderException(thrown, elide);
  716. }
  717. if (message.getExtraSourceLocations().isEmpty()) {
  718. return result;
  719. } else {
  720. return addExtraSourceLocations(message, result);
  721. }
  722. }
  723. public static String addExtraSourceLocations(IMessage message, String baseMessage) {
  724. StringWriter buf = new StringWriter();
  725. PrintWriter writer = new PrintWriter(buf);
  726. writer.println(baseMessage);
  727. for (Iterator<ISourceLocation> iter = message.getExtraSourceLocations().iterator(); iter.hasNext();) {
  728. ISourceLocation element = iter.next();
  729. if (element != null) {
  730. writer.print("\tsee also: " + element.toString());
  731. if (iter.hasNext()) {
  732. writer.println();
  733. }
  734. }
  735. }
  736. try {
  737. buf.close();
  738. } catch (IOException ioe) {
  739. }
  740. return buf.getBuffer().toString();
  741. }
  742. /**
  743. * Render ISourceLocation to String, ignoring empty elements (null or ISourceLocation.NO_FILE or ISourceLocation.NO_COLUMN
  744. * (though implementations may return 0 from getColumn() when passed NO_COLUMN as input)).
  745. *
  746. * @return "((ISourceLocation) null)" if null or String rendering
  747. *
  748. * <pre>
  749. * {file:}line{:column}
  750. * </pre>
  751. *
  752. */
  753. public static String renderSourceLocation(ISourceLocation loc) {
  754. if (null == loc) {
  755. return "((ISourceLocation) null)";
  756. }
  757. StringBuffer sb = new StringBuffer();
  758. File sourceFile = loc.getSourceFile();
  759. if (sourceFile != ISourceLocation.NO_FILE) {
  760. sb.append(sourceFile.getPath());
  761. sb.append(":");
  762. }
  763. int line = loc.getLine();
  764. sb.append("" + line);
  765. int column = loc.getColumn();
  766. if (column != ISourceLocation.NO_COLUMN) {
  767. sb.append(":" + column);
  768. }
  769. return sb.toString();
  770. }
  771. /**
  772. * Render message in a line. IMessage.Kind is always printed, then any unqualified exception class, then the remainder of text
  773. * and location according to their relative scale, all to fit in max characters or less. This does not render thrown except for
  774. * the unqualified class name
  775. *
  776. * @param max the number of characters - forced to 32..10000
  777. * @param textScale relative proportion to spend on message and/or exception message, relative to source location - if 0,
  778. * message is suppressed
  779. * @param locScale relative proportion to spend on source location suppressed if 0
  780. * @return "((IMessage) null)" or message per spec
  781. */
  782. public static String renderMessageLine(IMessage message, int textScale, int locScale, int max) {
  783. if (null == message) {
  784. return "((IMessage) null)";
  785. }
  786. if (max < 32) {
  787. max = 32;
  788. } else if (max > 10000) {
  789. max = 10000;
  790. }
  791. if (0 > textScale) {
  792. textScale = -textScale;
  793. }
  794. if (0 > locScale) {
  795. locScale = -locScale;
  796. }
  797. String text = message.getMessage();
  798. Throwable thrown = message.getThrown();
  799. ISourceLocation sl = message.getSourceLocation();
  800. IMessage.Kind kind = message.getKind();
  801. StringBuffer result = new StringBuffer();
  802. result.append(kind.toString());
  803. result.append(": ");
  804. if (null != thrown) {
  805. result.append(LangUtil.unqualifiedClassName(thrown) + " ");
  806. if ((null == text) || ("".equals(text))) {
  807. text = thrown.getMessage();
  808. }
  809. }
  810. if (0 == textScale) {
  811. text = "";
  812. } else if ((null != text) && (null != thrown)) {
  813. // decide between message and exception text?
  814. String s = thrown.getMessage();
  815. if ((null != s) && (0 < s.length())) {
  816. text += " - " + s;
  817. }
  818. }
  819. String loc = "";
  820. if ((0 != locScale) && (null != sl)) {
  821. File f = sl.getSourceFile();
  822. if (f == ISourceLocation.NO_FILE) {
  823. f = null;
  824. }
  825. if (null != f) {
  826. loc = f.getName();
  827. }
  828. int line = sl.getLine();
  829. int col = sl.getColumn();
  830. int end = sl.getEndLine();
  831. if ((0 == line) && (0 == col) && (0 == end)) {
  832. // ignore numbers if default
  833. } else {
  834. loc += ":" + line + (col == 0 ? "" : ":" + col);
  835. if (line != end) { // XXX consider suppressing nonstandard...
  836. loc += ":" + end;
  837. }
  838. }
  839. if (!LangUtil.isEmpty(loc)) {
  840. loc = "@[" + loc; // matching "]" added below after clipping
  841. }
  842. }
  843. // now budget between text and loc
  844. float totalScale = locScale + textScale;
  845. float remainder = max - result.length() - 4;
  846. if ((remainder > 0) && (0 < totalScale)) {
  847. int textSize = (int) (remainder * textScale / totalScale);
  848. int locSize = (int) (remainder * locScale / totalScale);
  849. // adjust for underutilization
  850. int extra = locSize - loc.length();
  851. if (0 < extra) {
  852. locSize = loc.length();
  853. textSize += extra;
  854. }
  855. extra = textSize - text.length();
  856. if (0 < extra) {
  857. textSize = text.length();
  858. if (locSize < loc.length()) {
  859. locSize += extra;
  860. }
  861. }
  862. if (locSize > loc.length()) {
  863. locSize = loc.length();
  864. }
  865. if (textSize > text.length()) {
  866. textSize = text.length();
  867. }
  868. if (0 < textSize) {
  869. result.append(text.substring(0, textSize));
  870. }
  871. if (0 < locSize) {
  872. if (0 < textSize) {
  873. result.append(" ");
  874. }
  875. result.append(loc.substring(0, locSize) + "]");
  876. }
  877. }
  878. return result.toString();
  879. }
  880. /** @return String of the form "{(# {type}) }.." for message kinds, skipping 0 */
  881. public static String renderCounts(IMessageHolder holder) {
  882. if (0 == holder.numMessages(null, false)) {
  883. return "(0 messages)";
  884. }
  885. StringBuffer sb = new StringBuffer();
  886. for (IMessage.Kind kind : IMessage.KINDS) {
  887. int num = holder.numMessages(kind, false);
  888. if (0 < num) {
  889. sb.append(" (" + num + " " + kind + ") ");
  890. }
  891. }
  892. return sb.toString();
  893. }
  894. /**
  895. * Factory for handler adapted to PrintStream XXX weak - only handles println(String)
  896. *
  897. * @param handler the IMessageHandler sink for the messages generated
  898. * @param kind the IMessage.Kind of message to create
  899. * @param overage the OuputStream for text not captured by the handler (if null, System.out used)
  900. * @throws IllegalArgumentException if kind or handler is null
  901. */
  902. public static PrintStream handlerPrintStream(final IMessageHandler handler, final IMessage.Kind kind,
  903. final OutputStream overage, final String prefix) {
  904. LangUtil.throwIaxIfNull(handler, "handler");
  905. LangUtil.throwIaxIfNull(kind, "kind");
  906. class HandlerPrintStream extends PrintStream {
  907. HandlerPrintStream() {
  908. super(null == overage ? System.out : overage);
  909. }
  910. public void println() {
  911. println("");
  912. }
  913. public void println(Object o) {
  914. println(null == o ? "null" : o.toString());
  915. }
  916. public void println(String input) {
  917. String textMessage = (null == prefix ? input : prefix + input);
  918. IMessage m = new Message(textMessage, kind, null, null);
  919. handler.handleMessage(m);
  920. }
  921. }
  922. return new HandlerPrintStream();
  923. }
  924. /** utility class */
  925. private MessageUtil() {
  926. }
  927. /**
  928. * Handle all messages in the second handler using the first
  929. *
  930. * @param handler the IMessageHandler sink for all messages in source
  931. * @param holder the IMessageHolder source for all messages to handle
  932. * @param fastFail if true, stop on first failure
  933. * @return false if any sink.handleMessage(..) failed
  934. */
  935. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, boolean fastFail) {
  936. return handleAll(sink, source, null, true, fastFail);
  937. }
  938. /**
  939. * Handle messages in the second handler using the first
  940. *
  941. * @param handler the IMessageHandler sink for all messages in source
  942. * @param holder the IMessageHolder source for all messages to handle
  943. * @param kind the IMessage.Kind to select, if not null
  944. * @param orGreater if true, also accept greater kinds
  945. * @param fastFail if true, stop on first failure
  946. * @return false if any sink.handleMessage(..) failed
  947. */
  948. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  949. boolean fastFail) {
  950. LangUtil.throwIaxIfNull(sink, "sink");
  951. LangUtil.throwIaxIfNull(source, "source");
  952. return handleAll(sink, source.getMessages(kind, orGreater), fastFail);
  953. }
  954. /**
  955. * Handle messages in the second handler using the first if they are NOT of this kind (optionally, or greater). If you pass null
  956. * as the kind, then all messages are ignored and this returns true.
  957. *
  958. * @param handler the IMessageHandler sink for all messages in source
  959. * @param holder the IMessageHolder source for all messages to handle
  960. * @param kind the IMessage.Kind to reject, if not null
  961. * @param orGreater if true, also reject greater kinds
  962. * @param fastFail if true, stop on first failure
  963. * @return false if any sink.handleMessage(..) failed
  964. */
  965. public static boolean handleAllExcept(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  966. boolean fastFail) {
  967. LangUtil.throwIaxIfNull(sink, "sink");
  968. LangUtil.throwIaxIfNull(source, "source");
  969. if (null == kind) {
  970. return true;
  971. }
  972. IMessage[] messages = getMessagesExcept(source, kind, orGreater);
  973. return handleAll(sink, messages, fastFail);
  974. }
  975. /**
  976. * Handle messages in the sink.
  977. *
  978. * @param handler the IMessageHandler sink for all messages in source
  979. * @param sources the IMessage[] messages to handle
  980. * @param fastFail if true, stop on first failure
  981. * @return false if any sink.handleMessage(..) failed
  982. * @throws IllegalArgumentException if sink is null
  983. */
  984. public static boolean handleAll(IMessageHandler sink, IMessage[] sources, boolean fastFail) {
  985. LangUtil.throwIaxIfNull(sink, "sink");
  986. if (LangUtil.isEmpty(sources)) {
  987. return true;
  988. }
  989. boolean result = true;
  990. for (IMessage source : sources) {
  991. if (!sink.handleMessage(source)) {
  992. if (fastFail) {
  993. return false;
  994. }
  995. if (result) {
  996. result = false;
  997. }
  998. }
  999. }
  1000. return result;
  1001. }
  1002. }