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.

MessageHandler.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources."
  5. */
  6. package org.apache.fop.messaging;
  7. import java.io.*;
  8. import java.util.*;
  9. import org.apache.log.*;
  10. /**
  11. * The class MessageHandler contains the static methods log and error which
  12. * should be used for any end user information instead of System.out.print() or
  13. * System.err.print(). The class defines several output methods:
  14. * writing to the screen (default), logging to a file, creating message events and repressing all
  15. * output. If you don't want to change the default behaviour, you should be
  16. * happy with MessageHandler.log(message) and MessageHandler.error(message)<br>
  17. * The class MessageHandler also supports the setting of an id. If set every message
  18. * has as a prefix an identifying string. That way Fop probably can also be used in
  19. * environments, where more than one Fop instance are running in same JVM.<br>
  20. * If Fop is embedded in a gui application or for any reasons the existing
  21. * messaging system doesn't meet the programmer's requirements, one can add
  22. * a MessageEvent listener to MessageHandler and handle the incoming messages
  23. * in an appropriate way. See the class DefaultMessageListener, which is a trivial
  24. * implementation of the MessageListener.
  25. * Here is an example how to configure MessageHandler for the DefaultMessageListener (anybody
  26. * can provide his own listener by extending MessageListener<br>
  27. * <code>
  28. * MessageHandler.setOutputMethod(MessageHandler.EVENT);
  29. * MessageHandler.addListener(new DefaultMessageListener());
  30. * </code><br>
  31. * This examples shows, how to redirect the messages to a log file called fop.log.
  32. * All messages are appended to this file.
  33. * <code>
  34. * MessageHandler.setOutputMethod(MessageHandler.FILE);
  35. * MessageHandler.setLogfileName("\\fop.log",true);
  36. * </code>
  37. */
  38. public class MessageHandler {
  39. public static final int SCREEN = 0;
  40. public static final int FILE = 1;
  41. public static final int EVENT = 2;
  42. public static final int NONE = 3; // this should always be the last method
  43. private static String logfileName = "fop.log";
  44. private static PrintWriter writer;
  45. private static int outputMethod = SCREEN;
  46. private static boolean fileOpened = false;
  47. private static boolean appendToFile = true;
  48. private static String message = "";
  49. private static String prefix = "";
  50. private static Vector listeners = new Vector();
  51. private static boolean IDisSet = false;
  52. private static boolean quiet = false;
  53. /**
  54. * helper class to access the message
  55. * @return a string containing the message
  56. */
  57. private static String getMessage() {
  58. return message;
  59. }
  60. /**
  61. * helper class which sets the message
  62. * and adds a prefix which can contain
  63. * the id of the thread which uses this messagehandler
  64. */
  65. private static void setMessage(String m) {
  66. if (IDisSet) {
  67. message = getID() + ":" + m;
  68. } else {
  69. message = m;
  70. }
  71. }
  72. // temp workaround
  73. private static Logger logger = null;
  74. /**
  75. * informs the user of the message
  76. * @param message the message for the user
  77. */
  78. public static void log(String message) {
  79. if (!quiet) {
  80. if(logger == null) {
  81. logger = Hierarchy.getDefaultHierarchy().getLoggerFor("fop");
  82. }
  83. setMessage(message);
  84. switch (outputMethod) {
  85. case SCREEN:
  86. logger.debug(getMessage());
  87. break;
  88. case FILE:
  89. if (fileOpened) {
  90. writer.print(getMessage());
  91. writer.flush();
  92. } else {
  93. openFile();
  94. writer.print(getMessage());
  95. writer.flush();
  96. }
  97. break;
  98. case EVENT:
  99. setMessage(message);
  100. Enumeration enum = listeners.elements();
  101. while (enum.hasMoreElements()) {
  102. ((MessageListener)enum.nextElement()).processMessage(new MessageEvent(getMessage()));
  103. }
  104. break;
  105. case NONE:
  106. // do nothing
  107. break;
  108. default:
  109. logger.debug(message);
  110. }
  111. }
  112. }
  113. /**
  114. * convenience method which adds a return to the message
  115. * @param message the message for the user
  116. */
  117. public static void logln(String message) {
  118. log(message);
  119. }
  120. /**
  121. * error warning for the user
  122. * @param errorMessage contains the warning string
  123. */
  124. public static void error(String errorMessage) {
  125. if(logger == null) {
  126. logger = Hierarchy.getDefaultHierarchy().getLoggerFor("fop");
  127. }
  128. setMessage(errorMessage);
  129. switch (outputMethod) {
  130. case SCREEN:
  131. logger.error(getMessage());
  132. break;
  133. case FILE:
  134. if (fileOpened) {
  135. writer.print(getMessage());
  136. writer.flush();
  137. } else {
  138. openFile();
  139. writer.print(getMessage());
  140. writer.flush();
  141. }
  142. break;
  143. case EVENT:
  144. setMessage(message);
  145. Enumeration enum = listeners.elements();
  146. while (enum.hasMoreElements()) {
  147. MessageEvent messEv = new MessageEvent(getMessage());
  148. messEv.setMessageType(MessageEvent.ERROR);
  149. ((MessageListener)enum.nextElement()).processMessage(messEv);
  150. }
  151. break;
  152. case NONE:
  153. // do nothing
  154. break;
  155. default:
  156. logger.error(errorMessage);
  157. }
  158. }
  159. /**
  160. * convenience method which adds a return to the error message
  161. * @param errorMessage the message for the user
  162. */
  163. public static void errorln(String errorMessage) {
  164. error(errorMessage);
  165. }
  166. /**
  167. * adds a MessageListener which listens for MessageEvents
  168. * @param MessageListener the listener to add
  169. */
  170. public static void addListener(MessageListener listener) {
  171. listeners.addElement(listener);
  172. }
  173. /**
  174. * removes a MessageListener
  175. * @param MessageListener the listener to remove
  176. */
  177. public static void removeListener(MessageListener listener) {
  178. listeners.removeElement(listener);
  179. }
  180. /**
  181. * sets the output method
  182. * @param method the output method to use, allowed values are<br>
  183. * MessageHandler.SCREEN, MessageHandler.FILE, MessageHandler.EVENT
  184. * MessageHandler.NONE
  185. */
  186. public static void setOutputMethod(int method) {
  187. if (method > NONE) {
  188. MessageHandler.error("Error: Unknown output method");
  189. } else {
  190. outputMethod = method;
  191. }
  192. }
  193. /**
  194. * informs what output method is set
  195. * @return the output method
  196. */
  197. public static int getOutputMethod() {
  198. return outputMethod;
  199. }
  200. /**
  201. * sets the logfile name
  202. * @param filename name of the logfile
  203. * @param append if true, the logfile is appended
  204. */
  205. public static void setLogfileName(String filename, boolean append) {
  206. logfileName = filename;
  207. appendToFile = append;
  208. }
  209. /**
  210. * returns the logfile name
  211. * @return String containing the logfile name
  212. */
  213. public static String getLogfileName() {
  214. return logfileName;
  215. }
  216. /**
  217. * helper file which opens the file for output method FILE
  218. */
  219. private static void openFile() {
  220. try {
  221. writer =
  222. new PrintWriter(new FileWriter(logfileName, appendToFile),
  223. true);
  224. writer.println("\n==============================================");
  225. fileOpened = true;
  226. } catch (IOException ioe) {
  227. System.err.println("Error: " + ioe);
  228. }
  229. }
  230. /**
  231. * if set to true an id string is prefixed to every message
  232. * uses the thread info as an id for the message producer. Should be used if
  233. * more than one instance of Fop is running in the same JVM
  234. * this id becomes a prefix to every message
  235. */
  236. private static String getID() {
  237. return Thread.currentThread().toString();
  238. }
  239. /**
  240. * if set to true an id string is prefixed to every message
  241. * uses the thread info as an id for the message producer. Should be used if
  242. * more than one instance of Fop is running in the same JVM
  243. * this id becomes a prefix to every message
  244. *
  245. * @param id boolean (default is false)
  246. */
  247. public static void setID(boolean id) {
  248. IDisSet = id;
  249. }
  250. /**
  251. * if set to true all normal messages are suppressed.
  252. * error messages are displayed allthesame
  253. *
  254. * @param quietMode boolean (default is false)
  255. */
  256. public static void setQuiet(boolean quietMode) {
  257. quiet = quietMode;
  258. }
  259. }