1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.trace;
  58. import java.util.Vector;
  59. import java.util.TooManyListenersException;
  60. import org.w3c.dom.Node;
  61. import org.apache.xml.utils.QName;
  62. import org.apache.xalan.templates.ElemTemplateElement;
  63. import org.apache.xalan.transformer.TransformerImpl;
  64. import org.apache.xpath.objects.XObject;
  65. import org.apache.xpath.XPath;
  66. /**
  67. * This class manages trace listeners, and acts as an
  68. * interface for the tracing functionality in Xalan.
  69. */
  70. public class TraceManager
  71. {
  72. /** A transformer instance */
  73. private TransformerImpl m_transformer;
  74. /**
  75. * Constructor for the trace manager.
  76. *
  77. * @param transformer a non-null instance of a transformer
  78. */
  79. public TraceManager(TransformerImpl transformer)
  80. {
  81. m_transformer = transformer;
  82. }
  83. /**
  84. * List of listeners who are interested in tracing what's
  85. * being generated.
  86. */
  87. private Vector m_traceListeners = null;
  88. /**
  89. * Add a trace listener for the purposes of debugging and diagnosis.
  90. * @param tl Trace listener to be added.
  91. *
  92. * @throws TooManyListenersException
  93. */
  94. public void addTraceListener(TraceListener tl)
  95. throws TooManyListenersException
  96. {
  97. TransformerImpl.S_DEBUG = true;
  98. if (null == m_traceListeners)
  99. m_traceListeners = new Vector();
  100. m_traceListeners.addElement(tl);
  101. }
  102. /**
  103. * Remove a trace listener.
  104. * @param tl Trace listener to be removed.
  105. */
  106. public void removeTraceListener(TraceListener tl)
  107. {
  108. if (null != m_traceListeners)
  109. {
  110. m_traceListeners.removeElement(tl);
  111. }
  112. }
  113. /**
  114. * Fire a generate event.
  115. *
  116. * @param te Generate Event to fire
  117. */
  118. public void fireGenerateEvent(GenerateEvent te)
  119. {
  120. if (null != m_traceListeners)
  121. {
  122. int nListeners = m_traceListeners.size();
  123. for (int i = 0; i < nListeners; i++)
  124. {
  125. TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
  126. tl.generated(te);
  127. }
  128. }
  129. }
  130. /**
  131. * Tell if trace listeners are present.
  132. *
  133. * @return True if there are trace listeners
  134. */
  135. public boolean hasTraceListeners()
  136. {
  137. return (null != m_traceListeners);
  138. }
  139. /**
  140. * Fire a trace event.
  141. *
  142. * @param sourceNode Current source node
  143. * @param mode Template mode
  144. * @param styleNode Stylesheet template node
  145. */
  146. public void fireTraceEvent(ElemTemplateElement styleNode)
  147. {
  148. if (hasTraceListeners())
  149. {
  150. int sourceNode = m_transformer.getXPathContext().getCurrentNode();
  151. Node source = m_transformer.getXPathContext().getDTM(
  152. sourceNode).getNode(sourceNode);
  153. fireTraceEvent(new TracerEvent(m_transformer, source,
  154. m_transformer.getMode(), /*sourceNode, mode,*/
  155. styleNode));
  156. }
  157. }
  158. /**
  159. * Fire a end trace event, after all children of an element have been
  160. * executed.
  161. *
  162. * @param sourceNode Current source node
  163. * @param mode Template mode
  164. * @param styleNode Stylesheet template node
  165. */
  166. public void fireTraceEndEvent(ElemTemplateElement styleNode)
  167. {
  168. if (hasTraceListeners())
  169. {
  170. int sourceNode = m_transformer.getXPathContext().getCurrentNode();
  171. Node source = m_transformer.getXPathContext().getDTM(
  172. sourceNode).getNode(sourceNode);
  173. fireTraceEndEvent(new TracerEvent(m_transformer, source,
  174. m_transformer.getMode(), /*sourceNode, mode,*/
  175. styleNode));
  176. }
  177. }
  178. /**
  179. * Fire a trace event.
  180. *
  181. * @param te Trace event to fire
  182. */
  183. public void fireTraceEndEvent(TracerEvent te)
  184. {
  185. if (hasTraceListeners())
  186. {
  187. int nListeners = m_traceListeners.size();
  188. for (int i = 0; i < nListeners; i++)
  189. {
  190. TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
  191. if(tl instanceof TraceListenerEx2)
  192. {
  193. ((TraceListenerEx2)tl).traceEnd(te);
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Fire a trace event.
  200. *
  201. * @param te Trace event to fire
  202. */
  203. public void fireTraceEvent(TracerEvent te)
  204. {
  205. if (hasTraceListeners())
  206. {
  207. int nListeners = m_traceListeners.size();
  208. for (int i = 0; i < nListeners; i++)
  209. {
  210. TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
  211. tl.trace(te);
  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 attributeName,
  228. XPath xpath, XObject selection)
  229. throws javax.xml.transform.TransformerException
  230. {
  231. if (hasTraceListeners())
  232. {
  233. Node source = m_transformer.getXPathContext().getDTM(
  234. sourceNode).getNode(sourceNode);
  235. fireSelectedEvent(new SelectionEvent(m_transformer, source, styleNode,
  236. attributeName, xpath, selection));
  237. }
  238. }
  239. /**
  240. * Fire a selection event.
  241. *
  242. * @param sourceNode Current source node
  243. * @param styleNode node in the style tree reference for the event.
  244. * @param attributeName The attribute name from which the selection is made.
  245. * @param xpath The XPath that executed the selection.
  246. * @param selection The result of the selection.
  247. *
  248. * @throws javax.xml.transform.TransformerException
  249. */
  250. public void fireSelectedEndEvent(
  251. int sourceNode, ElemTemplateElement styleNode, String attributeName,
  252. XPath xpath, XObject selection)
  253. throws javax.xml.transform.TransformerException
  254. {
  255. if (hasTraceListeners())
  256. {
  257. Node source = m_transformer.getXPathContext().getDTM(
  258. sourceNode).getNode(sourceNode);
  259. fireSelectedEndEvent(new EndSelectionEvent(m_transformer, source, styleNode,
  260. attributeName, xpath, selection));
  261. }
  262. }
  263. /**
  264. * Fire a selection event.
  265. *
  266. * @param se Selection event to fire
  267. *
  268. * @throws javax.xml.transform.TransformerException
  269. */
  270. public void fireSelectedEndEvent(EndSelectionEvent se)
  271. throws javax.xml.transform.TransformerException
  272. {
  273. if (hasTraceListeners())
  274. {
  275. int nListeners = m_traceListeners.size();
  276. for (int i = 0; i < nListeners; i++)
  277. {
  278. TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
  279. if(tl instanceof TraceListenerEx)
  280. ((TraceListenerEx)tl).selectEnd(se);
  281. }
  282. }
  283. }
  284. /**
  285. * Fire a selection event.
  286. *
  287. * @param se Selection event to fire
  288. *
  289. * @throws javax.xml.transform.TransformerException
  290. */
  291. public void fireSelectedEvent(SelectionEvent se)
  292. throws javax.xml.transform.TransformerException
  293. {
  294. if (hasTraceListeners())
  295. {
  296. int nListeners = m_traceListeners.size();
  297. for (int i = 0; i < nListeners; i++)
  298. {
  299. TraceListener tl = (TraceListener) m_traceListeners.elementAt(i);
  300. tl.selected(se);
  301. }
  302. }
  303. }
  304. }