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 org.w3c.dom.*;
  59. import org.apache.xalan.templates.ElemTemplateElement;
  60. import org.apache.xalan.transformer.TransformerImpl;
  61. import org.apache.xml.utils.QName;
  62. /**
  63. * <meta name="usage" content="advanced"/>
  64. * Parent class of events generated for tracing the
  65. * progress of the XSL processor.
  66. */
  67. public class TracerEvent implements java.util.EventListener
  68. {
  69. /**
  70. * The node in the style tree where the event occurs.
  71. */
  72. public final ElemTemplateElement m_styleNode;
  73. /**
  74. * The XSLT processor instance.
  75. */
  76. public final TransformerImpl m_processor;
  77. /**
  78. * The current context node.
  79. */
  80. public final Node m_sourceNode;
  81. /**
  82. * The current mode.
  83. */
  84. public final QName m_mode;
  85. /**
  86. * Create an event originating at the given node of the style tree.
  87. * @param processor The XSLT TransformerFactory.
  88. * @param sourceNode The current context node.
  89. * @param mode The current mode.
  90. * @param m_styleNode node in the style tree reference for the event.
  91. * Should not be null. That is not enforced.
  92. * @param styleNode The stylesheet element that is executing.
  93. */
  94. public TracerEvent(TransformerImpl processor, Node sourceNode, QName mode,
  95. ElemTemplateElement styleNode)
  96. {
  97. this.m_processor = processor;
  98. this.m_sourceNode = sourceNode;
  99. this.m_mode = mode;
  100. this.m_styleNode = styleNode;
  101. }
  102. /**
  103. * Returns a string representation of the node.
  104. * The string returned for elements will contain the element name
  105. * and any attributes enclosed in angle brackets.
  106. * The string returned for attributes will be of form, "name=value."
  107. *
  108. * @param n any DOM node. Must not be null.
  109. *
  110. * @return a string representation of the given node.
  111. */
  112. public static String printNode(Node n)
  113. {
  114. String r = n.hashCode() + " ";
  115. if (n instanceof Element)
  116. {
  117. r += "<" + n.getNodeName();
  118. Node c = n.getFirstChild();
  119. while (null != c)
  120. {
  121. if (c instanceof Attr)
  122. {
  123. r += printNode(c) + " ";
  124. }
  125. c = c.getNextSibling();
  126. }
  127. r += ">";
  128. }
  129. else
  130. {
  131. if (n instanceof Attr)
  132. {
  133. r += n.getNodeName() + "=" + n.getNodeValue();
  134. }
  135. else
  136. {
  137. r += n.getNodeName();
  138. }
  139. }
  140. return r;
  141. }
  142. /**
  143. * Returns a string representation of the node list.
  144. * The string will contain the list of nodes inside square braces.
  145. * Elements will contain the element name
  146. * and any attributes enclosed in angle brackets.
  147. * Attributes will be of form, "name=value."
  148. *
  149. * @param l any DOM node list. Must not be null.
  150. *
  151. * @return a string representation of the given node list.
  152. */
  153. public static String printNodeList(NodeList l)
  154. {
  155. String r = l.hashCode() + "[";
  156. int len = l.getLength() - 1;
  157. int i = 0;
  158. while (i < len)
  159. {
  160. Node n = l.item(i);
  161. if (null != n)
  162. {
  163. r += printNode(n) + ", ";
  164. }
  165. ++i;
  166. }
  167. if (i == len)
  168. {
  169. Node n = l.item(len);
  170. if (null != n)
  171. {
  172. r += printNode(n);
  173. }
  174. }
  175. return r + "]";
  176. }
  177. }