KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > trace > TraceEventMulticaster


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

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

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

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

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

55
56   public void close() {
57     ((TraceListener)a).close();
58     ((TraceListener)b).close();
59   }
60
61
62   /**
63    * Called when an element of the stylesheet gets processed
64    */

65   public void enter(InstructionInfo element, XPathContext context)
66   {
67     ((TraceListener)a).enter(element, context);
68     ((TraceListener)b).enter(element, context);
69   }
70
71   /**
72    * Called after an element of the stylesheet got processed
73    */

74   public void leave(InstructionInfo element)
75   {
76     ((TraceListener)a).leave(element);
77     ((TraceListener)b).leave(element);
78   }
79
80   /**
81    * Called when an item becomes current
82    */

83   public void startCurrentItem(Item item)
84   {
85     ((TraceListener)a).startCurrentItem(item);
86     ((TraceListener)b).startCurrentItem(item);
87   }
88
89   /**
90    * Called when an item ceases to be the current item
91    */

92   public void endCurrentItem(Item item)
93   {
94     ((TraceListener)a).endCurrentItem(item);
95     ((TraceListener)b).endCurrentItem(item);
96   }
97
98   /**
99    * Adds trace-listener-a with trace-listener-b and
100    * returns the resulting multicast listener.
101    * @param a trace-listener-a
102    * @param b trace-listener-b
103    */

104   public static TraceListener add(TraceListener a, TraceListener b) {
105     return (TraceListener)addInternal(a, b);
106   }
107
108   /**
109    * Removes the old trace-listener from trace-listener-l and
110    * returns the resulting multicast listener.
111    * @param l trace-listener-l
112    * @param oldl the trace-listener being removed
113    */

114   public static TraceListener remove(TraceListener l, TraceListener oldl) {
115     return (TraceListener) removeInternal(l, oldl);
116   }
117
118   /**
119    * Returns the resulting multicast listener from adding listener-a
120    * and listener-b together.
121    * If listener-a is null, it returns listener-b;
122    * If listener-b is null, it returns listener-a
123    * If neither are null, then it creates and returns
124    * a new EventMulticaster instance which chains a with b.
125    * @param a event listener-a
126    * @param b event listener-b
127    */

128   protected static EventListener JavaDoc addInternal(EventListener JavaDoc a, EventListener JavaDoc b) {
129     if (a == null) return b;
130     if (b == null) return a;
131     return new TraceEventMulticaster(a, b);
132   }
133
134   /**
135    * Returns the resulting multicast listener after removing the
136    * old listener from listener-l.
137    * If listener-l equals the old listener OR listener-l is null,
138    * returns null.
139    * Else if listener-l is an instance of SaxonEventMulticaster,
140    * then it removes the old listener from it.
141    * Else, returns listener l.
142    * @param l the listener being removed from
143    * @param oldl the listener being removed
144    */

145
146   protected static EventListener JavaDoc removeInternal(EventListener JavaDoc l, EventListener JavaDoc oldl) {
147     if (l == oldl || l == null) {
148     return null;
149     } else if (l instanceof TraceEventMulticaster) {
150     return ((TraceEventMulticaster)l).remove(oldl);
151     } else {
152     return l; // it's not here
153
}
154   }
155
156 }
157
158 // Contributor(s):
159
// This module is from Edwin Glaser (edwin@pannenleiter.de)
160
// Modified by MHK for Saxon 6.1 to remove "throws SAXException" from all methods
161
//
162
Popular Tags