Debian Codegraph @

net/sf/saxon/functions/Trace.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class supports the XPath 2.0 function trace().
33 * The value is traced to the registered output stream (defaulting to System.err),
34 * unless a TraceListener is in use, in which case the information is sent to the TraceListener
35 */
36
37
38 public class Trace extends SystemFunction {
39
40 Location location = ExplicitLocation.UNKNOWN_LOCATION;
41
42 /**
43 * Get the static properties of this expression (other than its type). The result is
44 * bit-significant. These properties are used for optimizations. In general, if
45 * property bit is set, it is true, but if it is unset, the value is unknown.
46 * @param arguments the actual arguments
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 // Fix bug 2597
72 Expression e = super.makeFunctionCall(arguments);
73 location = e.getLocation();
74 return e;
75 }
76
77 public static void traceItem(/*@Nullable*/ 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 * Evaluate the expression
103 *
104 * @param context the dynamic evaluation context
105 * @param arguments the values of the arguments, supplied as SequenceIterators
106 * @return the result of the evaluation, in the form of a SequenceIterator
107 * @throws net.sf.saxon.trans.XPathException
108 * if a dynamic error occurs during the evaluation of the expression
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 * Tracing Iterator class
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 * Get properties of this iterator, as a bit-significant integer.
167 *
168 * @return the properties of this iterator. This will be some combination of
169 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
170 * and {@link #LOOKAHEAD}. It is always
171 * acceptable to return the value zero, indicating that there are no known special properties.
172 * It is acceptable for the properties of the iterator to change depending on its state.
173 */
174
175 public int getProperties() {
176 return 0;
177 }
178 }
179
180 public String getStreamerName() {
181 return "Trace";
182 }
183
184 }
185