Debian Codegraph @

net/sf/saxon/trace/TraceEventMulticaster.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.trace;
9
10 import net.sf.saxon.Controller;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.lib.Logger;
13 import net.sf.saxon.lib.TraceListener;
14 import net.sf.saxon.lib.TraceListener2;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.trans.SimpleMode;
17
18 /**
19 * A class which implements efficient and thread-safe multi-cast event
20 * dispatching for the TraceListener evants.
21 */
22 public class TraceEventMulticaster implements TraceListener {
23
24 protected final TraceListener a, b;
25
26 /**
27 * Creates an event multicaster instance which chains listener-a
28 * with listener-b.
29 *
30 * @param a listener-a
31 * @param b listener-b
32 */
33 protected TraceEventMulticaster(TraceListener a, TraceListener b) {
34 this.a = a;
35 this.b = b;
36 }
37
38 public void setOutputDestination(Logger stream) {
39 a.setOutputDestination(stream);
40 b.setOutputDestination(stream);
41 }
42
43 /**
44 * Removes a listener from this multicaster and returns the
45 * resulting multicast listener.
46 *
47 * @param oldl the listener to be removed
48 */
49 /*@Nullable*/
50 protected TraceListener remove(TraceListener oldl) {
51 if (oldl == a) {
52 return b;
53 }
54 if (oldl == b) {
55 return a;
56 }
57 TraceListener a2 = removeInternal(a, oldl);
58 TraceListener b2 = removeInternal(b, oldl);
59 if (a2 == a && b2 == b) {
60 return this; // it's not here
61 }
62 return addInternal(a2, b2);
63 }
64
65 /**
66 * Called at start
67 */
68
69 public void open(Controller controller) {
70 a.open(controller);
71 b.open(controller);
72 }
73
74 /**
75 * Called at end
76 */
77
78 public void close() {
79 a.close();
80 b.close();
81 }
82
83
84 /**
85 * Called when an element of the stylesheet gets processed
86 */
87 public void enter(InstructionInfo element, XPathContext context) {
88 a.enter(element, context);
89 b.enter(element, context);
90 }
91
92 /**
93 * Called after an element of the stylesheet got processed
94 */
95 public void leave(InstructionInfo element) {
96 a.leave(element);
97 b.leave(element);
98 }
99
100 /**
101 * Called when an item becomes current
102 */
103 public void startCurrentItem(Item item) {
104 a.startCurrentItem(item);
105 b.startCurrentItem(item);
106 }
107
108 /**
109 * Called when an item ceases to be the current item
110 */
111 public void endCurrentItem(Item item) {
112 a.endCurrentItem(item);
113 b.endCurrentItem(item);
114 }
115 /**
116 * Called at the start of a rule search
117 */
118 public void startRuleSearch() {
119 if (a instanceof TraceListener2) {
120 ((TraceListener2) a).startRuleSearch();
121 }
122 if (b instanceof TraceListener2) {
123 ((TraceListener2) b).startRuleSearch();
124 }
125 }
126
127 /**
128 * Called at the end of a rule search
129 * @param rule the rule (or possible built-in ruleset) that has been selected
130 * @param mode
131 * @param item
132 */
133 public void endRuleSearch(Object rule, SimpleMode mode, Item item) {
134 if (a instanceof TraceListener2) {
135 ((TraceListener2) a).endRuleSearch(rule, mode, item);
136 }
137 if (b instanceof TraceListener2) {
138 ((TraceListener2) b).endRuleSearch(rule, mode, item);
139 }
140 }
141
142 /**
143 * Adds trace-listener-a with trace-listener-b and
144 * returns the resulting multicast listener.
145 *
146 * @param a trace-listener-a
147 * @param b trace-listener-b
148 */
149 public static TraceListener add(TraceListener a, TraceListener b) {
150 return (TraceListener) addInternal(a, b);
151 }
152
153 /**
154 * Removes the old trace-listener from trace-listener-l and
155 * returns the resulting multicast listener.
156 *
157 * @param l trace-listener-l
158 * @param oldl the trace-listener being removed
159 */
160 public static TraceListener remove(TraceListener l, TraceListener oldl) {
161 return (TraceListener) removeInternal(l, oldl);
162 }
163
164 /**
165 * Returns the resulting multicast listener from adding listener-a
166 * and listener-b together.
167 * If listener-a is null, it returns listener-b;
168 * If listener-b is null, it returns listener-a
169 * If neither are null, then it creates and returns
170 * a new EventMulticaster instance which chains a with b.
171 *
172 * @param a event listener-a
173 * @param b event listener-b
174 */
175 protected static TraceListener addInternal(TraceListener a, TraceListener b) {
176 if (a == null) {
177 return b;
178 }
179 if (b == null) {
180 return a;
181 }
182 return new TraceEventMulticaster(a, b);
183 }
184
185 /**
186 * Returns the resulting multicast listener after removing the
187 * old listener from listener-l.
188 * If listener-l equals the old listener OR listener-l is null,
189 * returns null.
190 * Else if listener-l is an instance of SaxonEventMulticaster,
191 * then it removes the old listener from it.
192 * Else, returns listener l.
193 *
194 * @param l the listener being removed from
195 * @param oldl the listener being removed
196 */
197
198 protected static TraceListener removeInternal(TraceListener l, TraceListener oldl) {
199 if (l == oldl || l == null) {
200 return null;
201 } else if (l instanceof TraceEventMulticaster) {
202 return ((TraceEventMulticaster) l).remove(oldl);
203 } else {
204 return l; // it's not here
205 }
206 }
207
208 }
209