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.

MessageWriter.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.PrintWriter;
  15. /**
  16. * An IMessageHandler implementation that writes all to a PrintWriter.
  17. * Clients may set this up to throw AbortException for FAIL or ERROR messages.
  18. * Subclasses may control whether messages are printed and how they
  19. * are rendered by overriding render(IMessage).
  20. */
  21. public class MessageWriter implements IMessageHandler {
  22. protected PrintWriter writer;
  23. protected boolean abortOnFailure;
  24. public MessageWriter(PrintWriter writer, boolean abortOnFailure) {
  25. this.writer = (null != writer ? writer : new PrintWriter(System.out));
  26. this.abortOnFailure = abortOnFailure;
  27. }
  28. /**
  29. * Handle message by printing and
  30. * (if abortOnFailure) throwing an AbortException if
  31. * the messages is a failure or an abort (but not for errors).
  32. * @see org.aspectj.bridge.IMessageHandler#handleMessage(IMessage)
  33. */
  34. public boolean handleMessage(IMessage message) throws AbortException {
  35. if ((null != message) && !isIgnoring(message.getKind())) {
  36. String result = render(message);
  37. if (null != result) {
  38. writer.println(result);
  39. writer.flush();
  40. if (abortOnFailure
  41. && (message.isFailed() || message.isAbort())) {
  42. throw new AbortException(message);
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. /**
  49. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(org.aspectj.bridge.IMessage.Kind)
  50. */
  51. public boolean isIgnoring(IMessage.Kind kind) {
  52. // XXX share MessageHandler implementation in superclass
  53. return false;
  54. }
  55. /**
  56. * No-op
  57. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(org.aspectj.bridge.IMessage.Kind)
  58. * @param kind
  59. */
  60. public void dontIgnore(IMessage.Kind kind) {
  61. }
  62. /**
  63. * No-op
  64. * @see org.aspectj.bridge.IMessageHandler#ignore(org.aspectj.bridge.IMessage.Kind)
  65. * @param kind
  66. */
  67. public void ignore(IMessage.Kind kind) {
  68. }
  69. /** @return null to not print, or message rendering (including newlines) */
  70. protected String render(IMessage message) {
  71. return message.toString();
  72. }
  73. }