001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: TracerEvent.java 468644 2006-10-28 06:56:42Z minchau $
020     */
021    package org.apache.xalan.trace;
022    
023    import org.apache.xalan.templates.ElemTemplateElement;
024    import org.apache.xalan.transformer.TransformerImpl;
025    import org.apache.xml.utils.QName;
026    
027    import org.w3c.dom.Attr;
028    import org.w3c.dom.Element;
029    import org.w3c.dom.Node;
030    import org.w3c.dom.NodeList;
031    
032    /**
033     * Parent class of events generated for tracing the
034     * progress of the XSL processor.
035     * @xsl.usage advanced
036     */
037    public class TracerEvent implements java.util.EventListener
038    {
039    
040      /**
041       * The node in the style tree where the event occurs.
042       */
043      public final ElemTemplateElement m_styleNode;
044    
045      /**
046       * The XSLT processor instance.
047       */
048      public final TransformerImpl m_processor;
049    
050      /**
051       * The current context node.
052       */
053      public final Node m_sourceNode;
054    
055      /**
056       * The current mode.
057       */
058      public final QName m_mode;
059    
060      /**
061       * Create an event originating at the given node of the style tree.
062       * @param processor The XSLT TransformerFactory.
063       * @param sourceNode The current context node.
064       * @param mode The current mode.
065       * @param styleNode The stylesheet element that is executing.
066       */
067      public TracerEvent(TransformerImpl processor, Node sourceNode, QName mode,
068                         ElemTemplateElement styleNode)
069      {
070    
071        this.m_processor = processor;
072        this.m_sourceNode = sourceNode;
073        this.m_mode = mode;
074        this.m_styleNode = styleNode;
075      }
076    
077      /**
078       * Returns a string representation of the node.
079       * The string returned for elements will contain the element name
080       * and any attributes enclosed in angle brackets.
081       * The string returned for attributes will be of form, "name=value."
082       *
083       * @param n any DOM node. Must not be null.
084       *
085       * @return a string representation of the given node.
086       */
087      public static String printNode(Node n)
088      {
089    
090        String r = n.hashCode() + " ";
091    
092        if (n instanceof Element)
093        {
094          r += "<" + n.getNodeName();
095    
096          Node c = n.getFirstChild();
097    
098          while (null != c)
099          {
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       */
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    }