| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.instruct.InstructionDetails; |
| 14 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 15 |
import net.sf.saxon.expr.parser.Location; |
| 16 |
import net.sf.saxon.lib.Logger; |
| 17 |
import net.sf.saxon.lib.TraceListener; |
| 18 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 19 |
import net.sf.saxon.ma.map.MapItem; |
| 20 |
import net.sf.saxon.om.*; |
| 21 |
import net.sf.saxon.trace.LocationKind; |
| 22 |
import net.sf.saxon.trans.Err; |
| 23 |
import net.sf.saxon.trans.XPathException; |
| 24 |
import net.sf.saxon.tree.util.Navigator; |
| 25 |
import net.sf.saxon.type.Type; |
| 26 |
import net.sf.saxon.value.AtomicValue; |
| 27 |
import net.sf.saxon.value.ObjectValue; |
| 28 |
|
| 29 |
import javax.xml.transform.SourceLocator; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class Trace extends SystemFunction { |
| 39 |
|
| 40 |
Location location = ExplicitLocation.UNKNOWN_LOCATION; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public int getSpecialProperties(Expression[] arguments) { |
| 50 |
return arguments[0].getSpecialProperties(); |
| 51 |
} |
| 52 |
|
| 53 |
public int getCardinality(Expression[] arguments) { |
| 54 |
return arguments[0].getCardinality(); |
| 55 |
} |
| 56 |
|
| 57 |
public void notifyListener(String label, Sequence<?> val, SourceLocator loc, XPathContext context) { |
| 58 |
InstructionDetails info = new InstructionDetails(); |
| 59 |
info.setConstructType(LocationKind.TRACE_CALL); |
| 60 |
info.setLineNumber(loc.getLineNumber()); |
| 61 |
info.setSystemId(loc.getSystemId()); |
| 62 |
info.setProperty("label", label); |
| 63 |
info.setProperty("value", val); |
| 64 |
TraceListener listener = context.getController().getTraceListener(); |
| 65 |
listener.enter(info, context); |
| 66 |
listener.leave(info); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public Expression makeFunctionCall(Expression... arguments) { |
| 71 |
|
| 72 |
Expression e = super.makeFunctionCall(arguments); |
| 73 |
location = e.getLocation(); |
| 74 |
return e; |
| 75 |
} |
| 76 |
|
| 77 |
public static void traceItem( Item val, String label, Logger out) { |
| 78 |
if (val == null) { |
| 79 |
out.info(label); |
| 80 |
} else { |
| 81 |
if (val instanceof NodeInfo) { |
| 82 |
out.info(label + ": " + Type.displayTypeName(val) + ": " |
| 83 |
+ Navigator.getPath((NodeInfo) val)); |
| 84 |
} else if (val instanceof AtomicValue) { |
| 85 |
out.info(label + ": " + Type.displayTypeName(val) + ": " |
| 86 |
+ val.getStringValue()); |
| 87 |
} else if (val instanceof ArrayItem || val instanceof MapItem) { |
| 88 |
out.info(label + ": " + val.toShortString()); |
| 89 |
} else if (val instanceof Function) { |
| 90 |
StructuredQName name = ((Function)val).getFunctionName(); |
| 91 |
out.info(label + ": function " + (name==null ? "(anon)" : name.getDisplayName()) + "#" + ((Function)val).getArity()); |
| 92 |
} else if (val instanceof ObjectValue) { |
| 93 |
Object obj = ((ObjectValue)val).getObject(); |
| 94 |
out.info(label + ": " + obj.getClass().getName() + " = " + Err.truncate30(obj.toString())); |
| 95 |
} else { |
| 96 |
out.info(label + ": " + val.toShortString()); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 111 |
Controller controller = context.getController(); |
| 112 |
String label = arguments.length == 1 ? "*" : arguments[1].head().getStringValue(); |
| 113 |
if (controller.isTracing()) { |
| 114 |
Sequence<?> value = arguments[0].iterate().materialize(); |
| 115 |
notifyListener(label, value, location, context); |
| 116 |
return value; |
| 117 |
} else { |
| 118 |
Logger out = controller.getTraceFunctionDestination(); |
| 119 |
if (out == null) { |
| 120 |
return arguments[0]; |
| 121 |
} else { |
| 122 |
return SequenceTool.toLazySequence(new TracingIterator(arguments[0].iterate(), |
| 123 |
label, out)); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private class TracingIterator implements SequenceIterator { |
| 133 |
|
| 134 |
private SequenceIterator base; |
| 135 |
private String label; |
| 136 |
private Logger out; |
| 137 |
private boolean empty = true; |
| 138 |
private int position = 0; |
| 139 |
|
| 140 |
|
| 141 |
public TracingIterator(SequenceIterator base, String label, Logger out) { |
| 142 |
this.base = base; |
| 143 |
this.label = label; |
| 144 |
this.out = out; |
| 145 |
} |
| 146 |
|
| 147 |
public Item next() throws XPathException { |
| 148 |
Item n = base.next(); |
| 149 |
position++; |
| 150 |
if (n == null) { |
| 151 |
if (empty) { |
| 152 |
traceItem(null, label + ": empty sequence", out); |
| 153 |
} |
| 154 |
} else { |
| 155 |
traceItem(n, label + " [" + position + ']', out); |
| 156 |
empty = false; |
| 157 |
} |
| 158 |
return n; |
| 159 |
} |
| 160 |
|
| 161 |
public void close() { |
| 162 |
base.close(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public int getProperties() { |
| 176 |
return 0; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public String getStreamerName() { |
| 181 |
return "Trace"; |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|