KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > trace > SaxonEventMulticaster


1 package com.icl.saxon.trace;
2 import com.icl.saxon.om.NodeInfo;
3 import com.icl.saxon.NodeHandler;
4 import com.icl.saxon.Context;
5
6 import java.util.EventListener JavaDoc;
7
8 /**
9  * A class which implements efficient and thread-safe multi-cast event
10  * dispatching for the TraceListener evants.
11  *
12  * Grabbed from java.awt.AWTEventMulticaster
13  */

14 public class SaxonEventMulticaster implements TraceListener {
15
16   protected final EventListener JavaDoc a, b;
17
18   /**
19    * Creates an event multicaster instance which chains listener-a
20    * with listener-b.
21    * @param a listener-a
22    * @param b listener-b
23    */

24   protected SaxonEventMulticaster(EventListener JavaDoc a, EventListener JavaDoc b) {
25     this.a = a; this.b = b;
26   }
27
28   /**
29    * Removes a listener from this multicaster and returns the
30    * resulting multicast listener.
31    * @param oldl the listener to be removed
32    */

33   protected EventListener JavaDoc remove(EventListener JavaDoc oldl) {
34     if (oldl == a) return b;
35     if (oldl == b) return a;
36     EventListener JavaDoc a2 = removeInternal(a, oldl);
37     EventListener JavaDoc b2 = removeInternal(b, oldl);
38     if (a2 == a && b2 == b) {
39     return this; // it's not here
40
}
41     return addInternal(a2, b2);
42   }
43
44   /**
45   * Called at start
46   */

47
48   public void open() {
49     ((TraceListener)a).open();
50     ((TraceListener)b).open();
51   }
52
53   /**
54   * Called at end
55   */

56
57   public void close() {
58     ((TraceListener)a).close();
59     ((TraceListener)b).close();
60   }
61   
62
63   /**
64    * Called for all top level elements
65    */

66   public void toplevel(NodeInfo element)
67   {
68     ((TraceListener)a).toplevel(element);
69     ((TraceListener)b).toplevel(element);
70   }
71
72   /**
73    * Called when a node of the source tree gets processed
74    */

75   public void enterSource(NodeHandler handler, Context context)
76   {
77     ((TraceListener)a).enterSource(handler, context);
78     ((TraceListener)b).enterSource(handler, context);
79   }
80
81   /**
82    * Called after a node of the source tree got processed
83    */

84   public void leaveSource(NodeHandler handler, Context context)
85   {
86     ((TraceListener)a).leaveSource(handler, context);
87     ((TraceListener)b).leaveSource(handler, context);
88   }
89
90   /**
91    * Called when an element of the stylesheet gets processed
92    */

93   public void enter(NodeInfo element, Context context)
94   {
95     ((TraceListener)a).enter(element, context);
96     ((TraceListener)b).enter(element, context);
97   }
98
99   /**
100    * Called after an element of the stylesheet got processed
101    */

102   public void leave(NodeInfo element, Context context)
103   {
104     ((TraceListener)a).leave(element, context);
105     ((TraceListener)b).leave(element, context);
106   }
107
108   /**
109    * Adds trace-listener-a with trace-listener-b and
110    * returns the resulting multicast listener.
111    * @param a trace-listener-a
112    * @param b trace-listener-b
113    */

114   public static TraceListener add(TraceListener a, TraceListener b) {
115     return (TraceListener)addInternal(a, b);
116   }
117
118   /**
119    * Removes the old trace-listener from trace-listener-l and
120    * returns the resulting multicast listener.
121    * @param l trace-listener-l
122    * @param oldl the trace-listener being removed
123    */

124   public static TraceListener remove(TraceListener l, TraceListener oldl) {
125     return (TraceListener) removeInternal(l, oldl);
126   }
127
128   /**
129    * Returns the resulting multicast listener from adding listener-a
130    * and listener-b together.
131    * If listener-a is null, it returns listener-b;
132    * If listener-b is null, it returns listener-a
133    * If neither are null, then it creates and returns
134    * a new EventMulticaster instance which chains a with b.
135    * @param a event listener-a
136    * @param b event listener-b
137    */

138   protected static EventListener JavaDoc addInternal(EventListener JavaDoc a, EventListener JavaDoc b) {
139     if (a == null) return b;
140     if (b == null) return a;
141     return new SaxonEventMulticaster(a, b);
142   }
143   
144   /**
145    * Returns the resulting multicast listener after removing the
146    * old listener from listener-l.
147    * If listener-l equals the old listener OR listener-l is null,
148    * returns null.
149    * Else if listener-l is an instance of SaxonEventMulticaster,
150    * then it removes the old listener from it.
151    * Else, returns listener l.
152    * @param l the listener being removed from
153    * @param oldl the listener being removed
154    */

155   protected static EventListener JavaDoc removeInternal(EventListener JavaDoc l, EventListener JavaDoc oldl) {
156     if (l == oldl || l == null) {
157     return null;
158     } else if (l instanceof SaxonEventMulticaster) {
159     return ((SaxonEventMulticaster)l).remove(oldl);
160     } else {
161     return l; // it's not here
162
}
163   }
164
165 }
166
167 // Contributor(s):
168
// This module is from Edwin Glaser (edwin@pannenleiter.de)
169
// Modified by MHK for Saxon 6.1 to remove "throws SAXException" from all methods
170
//
171
Popular Tags