1 16 19 package org.apache.xalan.trace; 20 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.Method ; 23 24 import javax.xml.transform.SourceLocator ; 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 ; 35 36 43 public class PrintTraceListener implements TraceListenerEx3 44 { 45 46 51 public PrintTraceListener(java.io.PrintWriter pw) 52 { 53 m_pw = pw; 54 } 55 56 59 java.io.PrintWriter m_pw; 60 61 64 public boolean m_traceTemplates = false; 65 66 69 public boolean m_traceElements = false; 70 71 74 public boolean m_traceGeneration = false; 75 76 79 public boolean m_traceSelection = false; 80 81 84 public boolean m_traceExtension = false; 85 86 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 chars = new String (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 147 public void trace(TracerEvent ev) 148 { 149 _trace(ev); 157 } 158 159 165 public void traceEnd(TracerEvent ev) 166 { 167 } 176 177 178 185 public void selected(SelectionEvent ev) 186 throws javax.xml.transform.TransformerException { 187 188 if (m_traceSelection) { 189 ElemTemplateElement ete = (ElemTemplateElement) ev.m_styleNode; 190 Node sourceNode = ev.m_sourceNode; 191 192 SourceLocator 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 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 int currentPos = DTM.NULL; 260 currentPos = nl.getCurrentPos(); 261 nl.setShouldCacheNodes(true); org.apache.xml.dtm.DTMIterator clone = null; 263 265 try { 266 clone = nl.cloneWithReset(); 267 } catch (CloneNotSupportedException 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 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 nl.runTo(-1); 290 nl.setCurrentPos(currentPos); 291 293 } else { 294 m_pw.println(ev.m_selection.str()); 295 } 296 } 297 } 298 306 public void selectEnd(EndSelectionEvent ev) 307 throws javax.xml.transform.TransformerException 308 { 309 } 311 312 313 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 chars = new String (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 chars = new String (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 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 )ev.m_method).getName() + "#<init>"); 377 break; 378 case ExtensionEvent.METHOD: 379 m_pw.println("EXTENSION: " + ((Method )ev.m_method).getDeclaringClass().getName() + "#" + ((Method )ev.m_method).getName()); 380 break; 381 case ExtensionEvent.CONSTRUCTOR: 382 m_pw.println("EXTENSION: " + ((Constructor )ev.m_method).getDeclaringClass().getName() + "#<init>"); 383 break; 384 } 385 } 386 } 387 388 389 394 public void extensionEnd(ExtensionEvent ev) { 395 } 397 398 } 399 | Popular Tags |