• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 package org.apache.xalan.trace;
22 
23 import org.apache.xalan.templates.ElemTemplateElement;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xml.utils.QName;
26 
27 import org.w3c.dom.Attr;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 
32 /**
33  * Parent class of events generated for tracing the
34  * progress of the XSL processor.
35  * @xsl.usage advanced
36  */
37 public class TracerEvent implements java.util.EventListener
38 {
39 
40   /**
41    * The node in the style tree where the event occurs.
42    */
43   public final ElemTemplateElement m_styleNode;
44 
45   /**
46    * The XSLT processor instance.
47    */
48   public final TransformerImpl m_processor;
49 
50   /**
51    * The current context node.
52    */
53   public final Node m_sourceNode;
54 
55   /**
56    * The current mode.
57    */
58   public final QName m_mode;
59 
60   /**
61    * Create an event originating at the given node of the style tree.
62    * @param processor The XSLT TransformerFactory.
63    * @param sourceNode The current context node.
64    * @param mode The current mode.
65    * @param styleNode The stylesheet element that is executing.
66    */
TracerEvent(TransformerImpl processor, Node sourceNode, QName mode, ElemTemplateElement styleNode)67   public TracerEvent(TransformerImpl processor, Node sourceNode, QName mode,
68                      ElemTemplateElement styleNode)
69   {
70 
71     this.m_processor = processor;
72     this.m_sourceNode = sourceNode;
73     this.m_mode = mode;
74     this.m_styleNode = styleNode;
75   }
76 
77   /**
78    * Returns a string representation of the node.
79    * The string returned for elements will contain the element name
80    * and any attributes enclosed in angle brackets.
81    * The string returned for attributes will be of form, "name=value."
82    *
83    * @param n any DOM node. Must not be null.
84    *
85    * @return a string representation of the given node.
86    */
printNode(Node n)87   public static String printNode(Node n)
88   {
89 
90     String r = n.hashCode() + " ";
91 
92     if (n instanceof Element)
93     {
94       r += "<" + n.getNodeName();
95 
96       Node c = n.getFirstChild();
97 
98       while (null != c)
99       {
100         if (c instanceof Attr)
101         {
102           r += printNode(c) + " ";
103         }
104 
105         c = c.getNextSibling();
106       }
107 
108       r += ">";
109     }
110     else
111     {
112       if (n instanceof Attr)
113       {
114         r += n.getNodeName() + "=" + n.getNodeValue();
115       }
116       else
117       {
118         r += n.getNodeName();
119       }
120     }
121 
122     return r;
123   }
124 
125   /**
126    * Returns a string representation of the node list.
127    * The string will contain the list of nodes inside square braces.
128    * Elements will contain the element name
129    * and any attributes enclosed in angle brackets.
130    * Attributes will be of form, "name=value."
131    *
132    * @param l any DOM node list. Must not be null.
133    *
134    * @return a string representation of the given node list.
135    */
printNodeList(NodeList l)136   public static String printNodeList(NodeList l)
137   {
138 
139     String r = l.hashCode() + "[";
140     int len = l.getLength() - 1;
141     int i = 0;
142 
143     while (i < len)
144     {
145       Node n = l.item(i);
146 
147       if (null != n)
148       {
149         r += printNode(n) + ", ";
150       }
151 
152       ++i;
153     }
154 
155     if (i == len)
156     {
157       Node n = l.item(len);
158 
159       if (null != n)
160       {
161         r += printNode(n);
162       }
163     }
164 
165     return r + "]";
166   }
167 }
168