KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > trace > PrintTraceListener


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: PrintTraceListener.java,v 1.21 2004/02/16 23:00:27 minchau Exp $
18  */

19 package org.apache.xalan.trace;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23
24 import javax.xml.transform.SourceLocator JavaDoc;
25
26 import org.apache.xalan.templates.Constants;
27 import org.apache.xalan.templates.ElemTemplate;
28 import org.apache.xalan.templates.ElemTemplateElement;
29 import org.apache.xalan.templates.ElemTextLiteral;
30 import org.apache.xml.dtm.DTM;
31 import org.apache.xml.dtm.ref.DTMNodeProxy;
32 import org.apache.xml.serializer.SerializerTrace;
33
34 import org.w3c.dom.Node JavaDoc;
35
36 /**
37  * Implementation of the TraceListener interface that
38  * prints each event to standard out as it occurs.
39  *
40  * @see org.apache.xalan.trace.TracerEvent
41  * @xsl.usage advanced
42  */

43 public class PrintTraceListener implements TraceListenerEx3
44 {
45
46   /**
47    * Construct a trace listener.
48    *
49    * @param pw PrintWriter to use for tracing events
50    */

51   public PrintTraceListener(java.io.PrintWriter JavaDoc pw)
52   {
53     m_pw = pw;
54   }
55
56   /**
57    * The print writer where the events should be written.
58    */

59   java.io.PrintWriter JavaDoc m_pw;
60
61   /**
62    * This needs to be set to true if the listener is to print an event whenever a template is invoked.
63    */

64   public boolean m_traceTemplates = false;
65
66   /**
67    * Set to true if the listener is to print events that occur as each node is 'executed' in the stylesheet.
68    */

69   public boolean m_traceElements = false;
70
71   /**
72    * Set to true if the listener is to print information after each result-tree generation event.
73    */

74   public boolean m_traceGeneration = false;
75
76   /**
77    * Set to true if the listener is to print information after each selection event.
78    */

79   public boolean m_traceSelection = false;
80
81   /**
82    * Set to true if the listener is to print information after each extension event.
83    */

84   public boolean m_traceExtension = false;
85
86   /**
87    * Print information about a TracerEvent.
88    *
89    * @param ev the trace event.
90    */

91   public void _trace(TracerEvent ev)
92   {
93
94     switch (ev.m_styleNode.getXSLToken())
95     {
96     case Constants.ELEMNAME_TEXTLITERALRESULT :
97       if (m_traceElements)
98       {
99         m_pw.print(ev.m_styleNode.getSystemId()+ " Line #" + ev.m_styleNode.getLineNumber() + ", "
100                    + "Column #" + ev.m_styleNode.getColumnNumber() + " -- "
101                    + ev.m_styleNode.getNodeName() + ": ");
102
103         ElemTextLiteral etl = (ElemTextLiteral) ev.m_styleNode;
104         String JavaDoc chars = new String JavaDoc(etl.getChars(), 0, etl.getChars().length);
105
106         m_pw.println(" " + chars.trim());
107       }
108       break;
109     case Constants.ELEMNAME_TEMPLATE :
110       if (m_traceTemplates || m_traceElements)
111       {
112         ElemTemplate et = (ElemTemplate) ev.m_styleNode;
113
114         m_pw.print(et.getSystemId()+ " Line #" + et.getLineNumber() + ", " + "Column #"
115                    + et.getColumnNumber() + ": " + et.getNodeName() + " ");
116
117         if (null != et.getMatch())
118         {
119           m_pw.print("match='" + et.getMatch().getPatternString() + "' ");
120         }
121
122         if (null != et.getName())
123         {
124           m_pw.print("name='" + et.getName() + "' ");
125         }
126
127         m_pw.println();
128       }
129       break;
130     default :
131       if (m_traceElements)
132       {
133         m_pw.println(ev.m_styleNode.getSystemId()+ " Line #" + ev.m_styleNode.getLineNumber() + ", "
134                      + "Column #" + ev.m_styleNode.getColumnNumber() + ": "
135                      + ev.m_styleNode.getNodeName());
136       }
137     }
138   }
139   
140   int m_indent = 0;
141   
142   /**
143    * Print information about a TracerEvent.
144    *
145    * @param ev the trace event.
146    */

147   public void trace(TracerEvent ev)
148   {
149 // m_traceElements = true;
150
// m_traceTemplates = true;
151
//
152
// for(int i = 0; i < m_indent; i++)
153
// m_pw.print(" ");
154
// m_indent = m_indent+2;
155
// m_pw.print("trace: ");
156
_trace(ev);
157   }
158   
159   /**
160    * Method that is called when the end of a trace event occurs.
161    * The method is blocking. It must return before processing continues.
162    *
163    * @param ev the trace event.
164    */

165   public void traceEnd(TracerEvent ev)
166   {
167 // m_traceElements = true;
168
// m_traceTemplates = true;
169
//
170
// m_indent = m_indent-2;
171
// for(int i = 0; i < m_indent; i++)
172
// m_pw.print(" ");
173
// m_pw.print("etrac: ");
174
// _trace(ev);
175
}
176
177
178   /**
179    * Method that is called just after a select attribute has been evaluated.
180    *
181    * @param ev the generate event.
182    *
183    * @throws javax.xml.transform.TransformerException
184    */

185 public void selected(SelectionEvent ev)
186     throws javax.xml.transform.TransformerException JavaDoc {
187
188     if (m_traceSelection) {
189         ElemTemplateElement ete = (ElemTemplateElement) ev.m_styleNode;
190         Node JavaDoc sourceNode = ev.m_sourceNode;
191
192         SourceLocator JavaDoc locator = null;
193         if (sourceNode instanceof DTMNodeProxy) {
194             int nodeHandler = ((DTMNodeProxy) sourceNode).getDTMNodeNumber();
195             locator =
196                 ((DTMNodeProxy) sourceNode).getDTM().getSourceLocatorFor(
197                     nodeHandler);
198         }
199
200         if (locator != null)
201             m_pw.println(
202                 "Selected source node '"
203                     + sourceNode.getNodeName()
204                     + "', at "
205                     + locator);
206         else
207             m_pw.println(
208                 "Selected source node '" + sourceNode.getNodeName() + "'");
209
210         if (ev.m_styleNode.getLineNumber() == 0) {
211
212             // You may not have line numbers if the selection is occuring from a
213
// default template.
214
ElemTemplateElement parent =
215                 (ElemTemplateElement) ete.getParentElem();
216
217             if (parent == ete.getStylesheetRoot().getDefaultRootRule()) {
218                 m_pw.print("(default root rule) ");
219             } else if (
220                 parent == ete.getStylesheetRoot().getDefaultTextRule()) {
221                 m_pw.print("(default text rule) ");
222             } else if (parent == ete.getStylesheetRoot().getDefaultRule()) {
223                 m_pw.print("(default rule) ");
224             }
225
226             m_pw.print(
227                 ete.getNodeName()
228                     + ", "
229                     + ev.m_attributeName
230                     + "='"
231                     + ev.m_xpath.getPatternString()
232                     + "': ");
233         } else {
234             m_pw.print(
235                 ev.m_styleNode.getSystemId()
236                     + " Line #"
237                     + ev.m_styleNode.getLineNumber()
238                     + ", "
239                     + "Column #"
240                     + ev.m_styleNode.getColumnNumber()
241                     + ": "
242                     + ete.getNodeName()
243                     + ", "
244                     + ev.m_attributeName
245                     + "='"
246                     + ev.m_xpath.getPatternString()
247                     + "': ");
248         }
249
250         if (ev.m_selection.getType() == ev.m_selection.CLASS_NODESET) {
251             m_pw.println();
252
253             org.apache.xml.dtm.DTMIterator nl = ev.m_selection.iter();
254             
255             // The following lines are added to fix bug#16222.
256
// The main cause is that the following loop change the state of iterator, which is shared
257
// with the transformer. The fix is that we record the initial state before looping, then
258
// restore the state when we finish it, which is done in the following lines added.
259
int currentPos = DTM.NULL;
260             currentPos = nl.getCurrentPos();
261             nl.setShouldCacheNodes(true); // This MUST be done before we clone the iterator!
262
org.apache.xml.dtm.DTMIterator clone = null;
263             // End of block
264

265             try {
266                 clone = nl.cloneWithReset();
267             } catch (CloneNotSupportedException JavaDoc cnse) {
268                 m_pw.println(
269                     " [Can't trace nodelist because it it threw a CloneNotSupportedException]");
270                 return;
271             }
272             int pos = clone.nextNode();
273
274             if (DTM.NULL == pos) {
275                 m_pw.println(" [empty node list]");
276             } else {
277                 while (DTM.NULL != pos) {
278                     // m_pw.println(" " + ev.m_processor.getXPathContext().getDTM(pos).getNode(pos));
279
DTM dtm = ev.m_processor.getXPathContext().getDTM(pos);
280                     m_pw.print(" ");
281                     m_pw.print(Integer.toHexString(pos));
282                     m_pw.print(": ");
283                     m_pw.println(dtm.getNodeName(pos));
284                     pos = clone.nextNode();
285                 }
286             }
287             
288             // Restore the initial state of the iterator, part of fix for bug#16222.
289
nl.runTo(-1);
290             nl.setCurrentPos(currentPos);
291             // End of fix for bug#16222
292

293         } else {
294             m_pw.println(ev.m_selection.str());
295         }
296     }
297 }
298   /**
299    * Method that is called after an xsl:apply-templates or xsl:for-each
300    * selection occurs.
301    *
302    * @param ev the generate event.
303    *
304    * @throws javax.xml.transform.TransformerException
305    */

306   public void selectEnd(EndSelectionEvent ev)
307      throws javax.xml.transform.TransformerException JavaDoc
308   {
309     // Nothing for right now.
310
}
311
312
313   /**
314    * Print information about a Generate event.
315    *
316    * @param ev the trace event.
317    */

318   public void generated(GenerateEvent ev)
319   {
320
321     if (m_traceGeneration)
322     {
323       switch (ev.m_eventtype)
324       {
325       case SerializerTrace.EVENTTYPE_STARTDOCUMENT :
326         m_pw.println("STARTDOCUMENT");
327         break;
328       case SerializerTrace.EVENTTYPE_ENDDOCUMENT :
329         m_pw.println("ENDDOCUMENT");
330         break;
331       case SerializerTrace.EVENTTYPE_STARTELEMENT :
332         m_pw.println("STARTELEMENT: " + ev.m_name);
333         break;
334       case SerializerTrace.EVENTTYPE_ENDELEMENT :
335         m_pw.println("ENDELEMENT: " + ev.m_name);
336         break;
337       case SerializerTrace.EVENTTYPE_CHARACTERS :
338       {
339         String JavaDoc chars = new String JavaDoc(ev.m_characters, ev.m_start, ev.m_length);
340
341         m_pw.println("CHARACTERS: " + chars);
342       }
343       break;
344       case SerializerTrace.EVENTTYPE_CDATA :
345       {
346         String JavaDoc chars = new String JavaDoc(ev.m_characters, ev.m_start, ev.m_length);
347
348         m_pw.println("CDATA: " + chars);
349       }
350       break;
351       case SerializerTrace.EVENTTYPE_COMMENT :
352         m_pw.println("COMMENT: " + ev.m_data);
353         break;
354       case SerializerTrace.EVENTTYPE_PI :
355         m_pw.println("PI: " + ev.m_name + ", " + ev.m_data);
356         break;
357       case SerializerTrace.EVENTTYPE_ENTITYREF :
358         m_pw.println("ENTITYREF: " + ev.m_name);
359         break;
360       case SerializerTrace.EVENTTYPE_IGNORABLEWHITESPACE :
361         m_pw.println("IGNORABLEWHITESPACE");
362         break;
363       }
364     }
365   }
366
367   /**
368    * Print information about an extension event.
369    *
370    * @param ev the extension event to print information about
371    */

372   public void extension(ExtensionEvent ev) {
373     if (m_traceExtension) {
374       switch (ev.m_callType) {
375         case ExtensionEvent.DEFAULT_CONSTRUCTOR:
376           m_pw.println("EXTENSION: " + ((Class JavaDoc)ev.m_method).getName() + "#<init>");
377           break;
378         case ExtensionEvent.METHOD:
379           m_pw.println("EXTENSION: " + ((Method JavaDoc)ev.m_method).getDeclaringClass().getName() + "#" + ((Method JavaDoc)ev.m_method).getName());
380           break;
381         case ExtensionEvent.CONSTRUCTOR:
382           m_pw.println("EXTENSION: " + ((Constructor JavaDoc)ev.m_method).getDeclaringClass().getName() + "#<init>");
383           break;
384       }
385     }
386   }
387
388
389   /**
390    * Print information about an extension event.
391    *
392    * @param ev the extension event to print information about
393    */

394   public void extensionEnd(ExtensionEvent ev) {
395     // do nothing
396
}
397
398 }
399
Popular Tags