aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/messaging/MessageHandler.java
blob: 5fe7f758b9a3a716545c63b5483db010233a5a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================
 
    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:
 
 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.
 
 4. The names "Fop" and  "Apache Software Foundation"  must not be used to
    endorse  or promote  products derived  from this  software without  prior
    written permission. For written permission, please contact
    apache@apache.org.
 
 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.
 
 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 James Tauber <jtauber@jtauber.com>. For more  information on the Apache 
 Software Foundation, please see <http://www.apache.org/>.
 
 */

package org.apache.fop.messaging;

import java.io.*;
import java.util.*;
import javax.swing.*;

/** The class MessageHandler contains the static methods log and error which
 *  should be used for any end user information instead of System.out.print() or
 *  System.err.print(). The class defines several output methods:
 *  writing to the screen (default), logging to a file, creating message events and repressing all
 *  output. If you don't want to change the default behaviour, you should be
 *  happy with MessageHandler.log(message) and MessageHandler.error(message)<br>
 *  The class MessageHandler also supports the setting of an id. If set every message
 *  has as a prefix an identifying string. That way Fop probably can also be used in 
 *  environments, where more than one Fop instance are running in same  JVM.<br> 
 *  If Fop is embedded in a gui application or for any reasons the existing 
 *  messaging system doesn't meet the programmer's requirements, one can add
 *  a MessageEvent listener to MessageHandler and handle the incoming messages 
 *  in an appropriate way. See the class DefaultMessageListener, which is a trivial 
 *  implementation of the MessageListener.  
 *  Here is an example how to configure MessageHandler for the DefaultMessageListener (anybody
 *  can provide his own listener by extending MessageListener<br>
 *  <code>
 *  MessageHandler.setOutputMethod(MessageHandler.EVENT);
 *  MessageHandler.addListener(new DefaultMessageListener());
 *  </code><br>
 *  This examples shows, how to redirect the messages to a log file called fop.log. 
 *  All messages are appended to this file.
 *  <code>
 *   MessageHandler.setOutputMethod(MessageHandler.FILE);
 *   MessageHandler.setLogfileName("\\fop.log",true);
 *  </code>
 */

public class MessageHandler {
  public static final int SCREEN  = 0;
  public static final int FILE    = 1;
  public static final int EVENT   = 2;
  public static final int NONE    = 3; //this should always be the last method

  private static String logfileName = "fop.log";
  private static PrintWriter writer;
  private static int outputMethod = SCREEN;
  private static boolean fileOpened = false;
  private static boolean appendToFile = true;
  private static String message = "";
  private static String prefix ="";
  private static Vector listeners = new Vector();
  private static boolean IDisSet = false;
  /**
   *  helper class to access the message
   *  @return a string containing the message
   */

  private static String getMessage () {
    return message;
  }

  /**
   *  helper class which sets the message
   *  and adds a prefix which can contain
   *  the id of the thread which uses this messagehandler
   */
  private static void setMessage (String m) {
    if (IDisSet) {
      message = getID() + ":" + m;
    } else {
      message = m;
    }
  }

  /**
   *  informs the user of the message
   *  @param message the message for the user
   */
  public static void log (String message) {
    setMessage(message);
    switch (outputMethod) {
      case SCREEN:
        System.out.print(getMessage ());
        break;
      case FILE:
        if (fileOpened) {
          writer.print(getMessage ());
          writer.flush();
        } else {
          openFile();
          writer.print(getMessage ());
          writer.flush();
        }
        break;
      case EVENT:
        setMessage(message);
        Enumeration enum = listeners.elements();
        while (enum.hasMoreElements()) {
          ((MessageListener) enum.nextElement()).processMessage(new MessageEvent(getMessage()));
        }
        break;
      case NONE:
        //do nothing
        break;
      default:
        System.out.print(message);
    }
  }

  /**
   *  convenience method which adds a return to the message
   *  @param message the message for the user
   */
  public static void logln (String message) {
    log (message+"\n");
  }

  /**
   *  error warning for the user
   *  @param errorMessage contains the warning string
   */

  public static void error (String errorMessage) {
    setMessage(errorMessage);
    switch (outputMethod) {
      case SCREEN:
        System.err.print(getMessage());
        break;
      case FILE:
          if (fileOpened) {
            writer.print(getMessage());
            writer.flush();
          } else {
            openFile();
            writer.print(getMessage());
            writer.flush();
          }
        break;
      case EVENT:
        setMessage(message);
        Enumeration enum = listeners.elements();
        while (enum.hasMoreElements()) {
          MessageEvent messEv  = new MessageEvent(getMessage());
          messEv.setMessageType(MessageEvent.ERROR);
          ((MessageListener) enum.nextElement()).processMessage(messEv);
        }
        break;
      case NONE:
        //do nothing
        break;
      default:
        System.err.print(errorMessage);
    }
  }

  /**
   *  convenience method which adds a return to the error message
   *  @param errorMessage the message for the user
   */
  public static void errorln (String errorMessage) {
    error (errorMessage+"\n");
  }

  /**
   *  adds a MessageListener which listens for MessageEvents
   *  @param MessageListener the listener to add
   */
  public static void addListener(MessageListener listener) {
    listeners.addElement(listener);
  }

  /**
   *  removes a MessageListener
   *  @param MessageListener the listener to remove
   */
  public static void removeListener(MessageListener listener) {
    listeners.removeElement(listener);
  }

  /**
   *  sets the output method
   *  @param method the output method to use, allowed values are<br>
   *  MessageHandler.SCREEN, MessageHandler.FILE, MessageHandler.EVENT
   *  MessageHandler.NONE
   */
  public static void setOutputMethod (int method) {
    if (method > NONE) {
      MessageHandler.error("Error: Unknown output method");
    } else {
      outputMethod = method;
    }
  }

  /**
   *  informs what output method is set
   *  @return the output method
   */
  public static int getOutputMethod () {
    return outputMethod;
  }

  /**
   *  sets the logfile name
   *  @param filename name of the logfile
   *  @param append if true, the logfile is appended 
   */
  public static void setLogfileName (String filename, boolean append) {
    logfileName = filename;
    appendToFile = append;
  }

  /**
   *  returns the logfile name
   *  @return String containing the logfile name
   */
  public static String getLogfileName () {
    return logfileName;
  }

  /**
   *  helper file which opens the file for output method FILE
   */
  private static void openFile () {
    try {
      writer = new PrintWriter (new FileWriter (logfileName,appendToFile),true);
      writer.println("\n==============================================");
      fileOpened = true;
    } catch (IOException ioe) {
      System.err.println("Error: " + ioe);
    }
  }

  /**
   *  if set to true an id string is prefixed to every message
   *  uses the thread info as an id for the message producer. Should be used if
   *  more than one instance of Fop is running in the same JVM
   *  this id becomes a prefix to every message
   */
  private static String getID () {
    return Thread.currentThread().toString() ;
  }

  /**
   * if set to true an id string is prefixed to every message
   *  uses the thread info as an id for the message producer. Should be used if
   *  more than one instance of Fop is running in the same JVM
   *  this id becomes a prefix to every message
   *
   *  @param id boolean (default is false)
   */

  public static void setID (boolean id) {
    IDisSet = id;
  }
}