1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.ExpressionTool; 4 import net.sf.saxon.expr.StaticContext; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.instruct.InstructionDetails; 7 import net.sf.saxon.om.*; 8 import net.sf.saxon.trace.Location; 9 import net.sf.saxon.trace.TraceListener; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.type.Type; 12 import net.sf.saxon.value.Value; 13 14 19 20 21 public class Trace extends SystemFunction { 22 23 NamespaceResolver resolver; 24 26 27 31 32 public Expression simplify(StaticContext env) throws XPathException { 33 resolver = env.getNamespaceResolver(); 34 return super.simplify(env); 35 } 36 37 40 41 public Expression preEvaluate(StaticContext env) { 42 return this; 43 } 44 45 50 51 public int computeSpecialProperties() { 52 return argument[0].getSpecialProperties(); 53 } 54 55 58 59 public int computeCardinality() { 60 return argument[0].getCardinality(); 61 } 62 63 66 67 public Item evaluateItem(XPathContext context) throws XPathException { 68 Item val = argument[0].evaluateItem(context); 69 String label = argument[1].evaluateAsString(context); 70 if (context.getController().isTracing()) { 71 notifyListener(label, Value.asValue(val), context); 72 } else { 73 traceItem(val, label); 74 } 75 return val; 76 } 77 78 private void notifyListener(String label, Value val, XPathContext context) { 79 InstructionDetails info = (InstructionDetails)getInstructionInfo(); 80 info.setConstructType(Location.TRACE_CALL); 81 info.setNamespaceResolver(resolver); 82 info.setProperty("label", label); 83 info.setProperty("value", val); 84 TraceListener listener = context.getController().getTraceListener(); 85 listener.enter(info, context); 86 listener.leave(info); 87 } 88 89 private void traceItem(Item val, String label) { 90 if (val==null) { 91 System.err.println(label + ": empty sequence"); 92 } else { 93 if (val instanceof NodeInfo) { 94 System.err.println(label + ": " + Type.displayTypeName(val) + ": " 95 + Navigator.getPath((NodeInfo)val)); 96 } else { 97 System.err.println(label + ": " + Type.displayTypeName(val) + ": " 98 + val.getStringValue()); 99 } 100 } 101 } 102 103 106 107 public SequenceIterator iterate(XPathContext context) throws XPathException { 108 if (context.getController().isTracing()) { 109 String label = argument[1].evaluateAsString(context); 110 Value value = ExpressionTool.eagerEvaluate(argument[0], context); 111 notifyListener(label, value, context); 112 return value.iterate(context); 113 } else { 114 return new TracingIterator(argument[0].iterate(context), argument[1].evaluateAsString(context)); 115 } 116 } 117 118 119 122 123 public class TracingIterator implements SequenceIterator { 124 125 SequenceIterator base; 126 String label; 127 boolean empty = true; 128 129 130 public TracingIterator(SequenceIterator base, String label) { 131 this.base = base; 132 this.label = label; 133 } 134 135 public Item next() throws XPathException { 136 Item n = base.next(); 137 if (n==null) { 138 if (empty) { 139 traceItem(null, label); 140 } 141 } else { 142 traceItem(n, label + " [" + position() + ']'); 143 empty = false; 144 } 145 return n; 146 } 147 148 public Item current() { 149 return base.current(); 150 } 151 152 public int position() { 153 return base.position(); 154 } 155 156 public SequenceIterator getAnother() throws XPathException { 157 return new TracingIterator(base.getAnother(), label); 158 } 159 160 169 170 public int getProperties() { 171 return 0; 172 } 173 } 174 175 } 176 177 | Popular Tags |