KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > trace > TraceManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TraceManager.java,v 1.16 2004/02/16 23:00:27 minchau Exp $
18  */

19 package org.apache.xalan.trace;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.TooManyListenersException JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.xalan.templates.ElemTemplateElement;
26 import org.apache.xalan.transformer.TransformerImpl;
27 import org.apache.xpath.XPath;
28 import org.apache.xpath.objects.XObject;
29 import org.w3c.dom.Node JavaDoc;
30
31 /**
32  * This class manages trace listeners, and acts as an
33  * interface for the tracing functionality in Xalan.
34  */

35 public class TraceManager
36 {
37
38   /** A transformer instance */
39   private TransformerImpl m_transformer;
40
41   /**
42    * Constructor for the trace manager.
43    *
44    * @param transformer a non-null instance of a transformer
45    */

46   public TraceManager(TransformerImpl transformer)
47   {
48     m_transformer = transformer;
49   }
50
51   /**
52    * List of listeners who are interested in tracing what's
53    * being generated.
54    */

55   private Vector JavaDoc m_traceListeners = null;
56
57   /**
58    * Add a trace listener for the purposes of debugging and diagnosis.
59    * @param tl Trace listener to be added.
60    *
61    * @throws TooManyListenersException
62    */

63   public void addTraceListener(TraceListener tl)
64           throws TooManyListenersException JavaDoc
65   {
66
67     TransformerImpl.S_DEBUG = true;
68
69     if (null == m_traceListeners)
70       m_traceListeners = new Vector JavaDoc();
71
72     m_traceListeners.addElement(tl);
73   }
74
75   /**
76    * Remove a trace listener.
77    * @param tl Trace listener to be removed.
78    */

79   public void removeTraceListener(TraceListener tl)
80   {
81
82     if (null != m_traceListeners)
83     {
84       m_traceListeners.removeElement(tl);
85       
86       // The following line added to fix the bug#5140: hasTraceListeners() returns true
87
// after adding and removing a listener.
88
// Check: if m_traceListeners is empty, then set it to NULL.
89
if (0 == m_traceListeners.size()) m_traceListeners = null;
90     }
91   }
92
93   /**
94    * Fire a generate event.
95    *
96    * @param te Generate Event to fire
97    */

98   public void fireGenerateEvent(GenerateEvent te)
99   {
100
101     if (null != m_traceListeners)
102     {
103       int nListeners = m_traceListeners.size();
104
105       for (int i = 0; i < nListeners; i++)
106       {
107         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
108
109         tl.generated(te);
110       }
111     }
112   }
113
114   /**
115    * Tell if trace listeners are present.
116    *
117    * @return True if there are trace listeners
118    */

119   public boolean hasTraceListeners()
120   {
121     return (null != m_traceListeners);
122   }
123
124   /**
125    * Fire a trace event.
126    *
127    * @param sourceNode Current source node
128    * @param mode Template mode
129    * @param styleNode Stylesheet template node
130    */

131   public void fireTraceEvent(ElemTemplateElement styleNode)
132   {
133
134     if (hasTraceListeners())
135     {
136       int sourceNode = m_transformer.getXPathContext().getCurrentNode();
137       Node JavaDoc source = m_transformer.getXPathContext().getDTM(
138         sourceNode).getNode(sourceNode);
139
140       fireTraceEvent(new TracerEvent(m_transformer, source,
141                      m_transformer.getMode(), /*sourceNode, mode,*/
142                                      styleNode));
143     }
144   }
145
146   /**
147    * Fire a end trace event, after all children of an element have been
148    * executed.
149    *
150    * @param sourceNode Current source node
151    * @param mode Template mode
152    * @param styleNode Stylesheet template node
153    */

154   public void fireTraceEndEvent(ElemTemplateElement styleNode)
155   {
156
157     if (hasTraceListeners())
158     {
159       int sourceNode = m_transformer.getXPathContext().getCurrentNode();
160       Node JavaDoc source = m_transformer.getXPathContext().getDTM(
161         sourceNode).getNode(sourceNode);
162
163       fireTraceEndEvent(new TracerEvent(m_transformer, source,
164                      m_transformer.getMode(), /*sourceNode, mode,*/
165                                      styleNode));
166     }
167   }
168
169   /**
170    * Fire a trace event.
171    *
172    * @param te Trace event to fire
173    */

174   public void fireTraceEndEvent(TracerEvent te)
175   {
176
177     if (hasTraceListeners())
178     {
179       int nListeners = m_traceListeners.size();
180
181       for (int i = 0; i < nListeners; i++)
182       {
183         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
184         if(tl instanceof TraceListenerEx2)
185         {
186           ((TraceListenerEx2)tl).traceEnd(te);
187         }
188       }
189     }
190   }
191
192
193
194   /**
195    * Fire a trace event.
196    *
197    * @param te Trace event to fire
198    */

199   public void fireTraceEvent(TracerEvent te)
200   {
201
202     if (hasTraceListeners())
203     {
204       int nListeners = m_traceListeners.size();
205
206       for (int i = 0; i < nListeners; i++)
207       {
208         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
209
210         tl.trace(te);
211       }
212     }
213   }
214
215   /**
216    * Fire a selection event.
217    *
218    * @param sourceNode Current source node
219    * @param styleNode node in the style tree reference for the event.
220    * @param attributeName The attribute name from which the selection is made.
221    * @param xpath The XPath that executed the selection.
222    * @param selection The result of the selection.
223    *
224    * @throws javax.xml.transform.TransformerException
225    */

226   public void fireSelectedEvent(
227           int sourceNode, ElemTemplateElement styleNode, String JavaDoc attributeName,
228           XPath xpath, XObject selection)
229             throws javax.xml.transform.TransformerException JavaDoc
230   {
231
232     if (hasTraceListeners())
233     {
234       Node JavaDoc source = m_transformer.getXPathContext().getDTM(
235         sourceNode).getNode(sourceNode);
236         
237       fireSelectedEvent(new SelectionEvent(m_transformer, source, styleNode,
238                                            attributeName, xpath, selection));
239     }
240   }
241   
242   /**
243    * Fire a selection event.
244    *
245    * @param sourceNode Current source node
246    * @param styleNode node in the style tree reference for the event.
247    * @param attributeName The attribute name from which the selection is made.
248    * @param xpath The XPath that executed the selection.
249    * @param selection The result of the selection.
250    *
251    * @throws javax.xml.transform.TransformerException
252    */

253   public void fireSelectedEndEvent(
254           int sourceNode, ElemTemplateElement styleNode, String JavaDoc attributeName,
255           XPath xpath, XObject selection)
256             throws javax.xml.transform.TransformerException JavaDoc
257   {
258
259     if (hasTraceListeners())
260     {
261       Node JavaDoc source = m_transformer.getXPathContext().getDTM(
262         sourceNode).getNode(sourceNode);
263         
264       fireSelectedEndEvent(new EndSelectionEvent(m_transformer, source, styleNode,
265                                            attributeName, xpath, selection));
266     }
267   }
268   
269   /**
270    * Fire a selection event.
271    *
272    * @param se Selection event to fire
273    *
274    * @throws javax.xml.transform.TransformerException
275    */

276   public void fireSelectedEndEvent(EndSelectionEvent se)
277           throws javax.xml.transform.TransformerException JavaDoc
278   {
279
280     if (hasTraceListeners())
281     {
282       int nListeners = m_traceListeners.size();
283
284       for (int i = 0; i < nListeners; i++)
285       {
286         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
287
288         if(tl instanceof TraceListenerEx)
289           ((TraceListenerEx)tl).selectEnd(se);
290       }
291     }
292   }
293
294   /**
295    * Fire a selection event.
296    *
297    * @param se Selection event to fire
298    *
299    * @throws javax.xml.transform.TransformerException
300    */

301   public void fireSelectedEvent(SelectionEvent se)
302           throws javax.xml.transform.TransformerException JavaDoc
303   {
304
305     if (hasTraceListeners())
306     {
307       int nListeners = m_traceListeners.size();
308
309       for (int i = 0; i < nListeners; i++)
310       {
311         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
312
313         tl.selected(se);
314       }
315     }
316   }
317   
318
319   /**
320    * Fire an end extension event.
321    *
322    * @see java.lang.reflect.Method#invoke
323    *
324    * @param method The java method about to be executed
325    * @param instance The instance the method will be executed on
326    * @param arguments Parameters passed to the method.
327    */

328   public void fireExtensionEndEvent(Method JavaDoc method, Object JavaDoc instance, Object JavaDoc[] arguments)
329   {
330       ExtensionEvent ee = new ExtensionEvent(m_transformer, method, instance, arguments);
331
332     if (hasTraceListeners())
333     {
334       int nListeners = m_traceListeners.size();
335
336       for (int i = 0; i < nListeners; i++)
337       {
338         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
339         if(tl instanceof TraceListenerEx3)
340         {
341           ((TraceListenerEx3)tl).extensionEnd(ee);
342         }
343       }
344     }
345   }
346
347   /**
348    * Fire an end extension event.
349    *
350    * @see java.lang.reflect.Method#invoke
351    *
352    * @param method The java method about to be executed
353    * @param instance The instance the method will be executed on
354    * @param arguments Parameters passed to the method.
355    */

356   public void fireExtensionEvent(Method JavaDoc method, Object JavaDoc instance, Object JavaDoc[] arguments)
357   {
358     ExtensionEvent ee = new ExtensionEvent(m_transformer, method, instance, arguments);
359
360     if (hasTraceListeners())
361     {
362       int nListeners = m_traceListeners.size();
363
364       for (int i = 0; i < nListeners; i++)
365       {
366         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
367         if(tl instanceof TraceListenerEx3)
368         {
369           ((TraceListenerEx3)tl).extension(ee);
370         }
371       }
372     }
373   }
374
375   /**
376    * Fire an end extension event.
377    *
378    * @see java.lang.reflect.Method#invoke
379    *
380    * @param ee the ExtensionEvent to fire
381    */

382   public void fireExtensionEndEvent(ExtensionEvent ee)
383   {
384     if (hasTraceListeners())
385     {
386       int nListeners = m_traceListeners.size();
387
388       for (int i = 0; i < nListeners; i++)
389       {
390         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
391         if(tl instanceof TraceListenerEx3)
392         {
393           ((TraceListenerEx3)tl).extensionEnd(ee);
394         }
395       }
396     }
397   }
398
399   /**
400    * Fire an end extension event.
401    *
402    * @see java.lang.reflect.Method#invoke
403    *
404    * @param ee the ExtensionEvent to fire
405    */

406   public void fireExtensionEvent(ExtensionEvent ee)
407   {
408       
409     if (hasTraceListeners())
410     {
411       int nListeners = m_traceListeners.size();
412
413       for (int i = 0; i < nListeners; i++)
414       {
415         TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
416         if(tl instanceof TraceListenerEx3)
417         {
418           ((TraceListenerEx3)tl).extension(ee);
419         }
420       }
421     }
422   }
423 }
424
Popular Tags