KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > trace > SimpleTraceListener


1 package com.icl.saxon.trace;
2
3 import com.icl.saxon.style.StyleElement;
4 import com.icl.saxon.Context;
5 import com.icl.saxon.Mode;
6 import com.icl.saxon.NodeHandler;
7 import com.icl.saxon.om.NodeInfo;
8 import com.icl.saxon.om.Navigator;
9
10 /**
11 * A Simple trace listener that writes messages to System.err
12 */

13
14 public class SimpleTraceListener implements TraceListener {
15
16   String JavaDoc indent = "";
17
18   /**
19   * Called at start
20   */

21
22   public void open() {
23     System.err.println("<trace>");
24   }
25
26   /**
27   * Called at end
28   */

29
30   public void close() {
31     System.err.println("</trace>");
32   }
33   
34   
35
36   /**
37    * Called for all top level elements
38    */

39   public void toplevel(NodeInfo element)
40   {
41     StyleElement e = (StyleElement)element;
42     System.err.println("<Top-level element=\"" + e.getDisplayName() + "\" line=\"" + e.getLineNumber() +
43        "\" file=\"" + e.getSystemId() + "\" precedence=\"" + e.getPrecedence() +"\"/>");
44   }
45
46   /**
47    * Called when a node of the source tree gets processed
48    */

49   public void enterSource(NodeHandler handler, Context context)
50   {
51     NodeInfo curr = context.getContextNodeInfo();
52     System.err.println(indent + "<Source node=\"" + Navigator.getPath(curr)
53                         + "\" line=\"" + curr.getLineNumber()
54                         + "\" mode=\"" + getModeName(context) + "\">");
55     indent += " ";
56   }
57
58   /**
59    * Called after a node of the source tree got processed
60    */

61   public void leaveSource(NodeHandler handler, Context context)
62   {
63     indent = indent.substring(0, indent.length() - 1);
64     System.err.println(indent + "</Source><!-- " +
65          Navigator.getPath(context.getContextNodeInfo()) + " -->");
66   }
67
68   /**
69    * Called when an element of the stylesheet gets processed
70    */

71   public void enter(NodeInfo element, Context context)
72   {
73     if (element.getNodeType()==NodeInfo.ELEMENT) {
74         System.err.println(indent + "<Instruction element=\"" + element.getDisplayName() + "\" line=\"" + element.getLineNumber() + "\">");
75         indent += " ";
76     }
77   }
78
79   /**
80    * Called after an element of the stylesheet got processed
81    */

82     public void leave(NodeInfo element, Context context)
83     {
84         if (element.getNodeType()==NodeInfo.ELEMENT) {
85             indent = indent.substring(0, indent.length() - 1);
86             System.err.println(indent + "</Instruction> <!-- " +
87                                  element.getDisplayName() + " -->");
88         }
89     }
90
91     String JavaDoc getModeName(Context context)
92     {
93         Mode mode = context.getMode();
94         if (mode==null) return "#none";
95         int nameCode = mode.getNameCode();
96         if (nameCode==-1) {
97             return "#default";
98         } else {
99             return context.getController().getNamePool().getDisplayName(nameCode);
100         }
101     }
102 }
103
Popular Tags