Debian Codegraph @

net/sf/saxon/trace/ContextStackFrame.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.trace;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.expr.instruct.*;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.lib.Logger;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.ma.arrays.ArrayItem;
16 import net.sf.saxon.ma.map.MapItem;
17 import net.sf.saxon.om.Function;
18 import net.sf.saxon.om.Item;
19 import net.sf.saxon.om.NodeInfo;
20 import net.sf.saxon.om.StructuredQName;
21 import net.sf.saxon.trans.KeyDefinition;
22 import net.sf.saxon.tree.util.Navigator;
23 import net.sf.saxon.value.AtomicValue;
24
25 /**
26 * An entry on the context stack. A new entry is created every time the context changes. This is a
27 * representation of the stack created on request; it does not hold live data.
28 */
29 public abstract class ContextStackFrame {
30
31 private XPathContext context;
32 private Location location;
33 private Item contextItem;
34 private Object container;
35
36 /**
37 * Set the location of the instruction that caused this new context
38 * to be created
39 *
40 * @param loc the location of the instruction
41 */
42
43 public void setLocation(Location loc) {
44 this.location = loc;
45 }
46
47 /**
48 * Get the system ID representing the location of the instruction that caused this new context
49 * to be created
50 *
51 * @return the system ID (base URI/module URI) of the module containing the instruction
52 */
53
54 public String getSystemId() {
55 return location.getSystemId();
56 }
57
58 /**
59 * Get the line number of the location of the instruction that caused this new context
60 * to be created
61 *
62 * @return the line number of the instruction within its containing module
63 */
64
65 public int getLineNumber() {
66 return location.getLineNumber();
67 }
68
69 /**
70 * Set the container of the instruction that caused this new context to be created. This will
71 * generally be an object such as an XSLT Template or a user-defined function
72 *
73 * @param container the container of the instruction
74 */
75
76 public void setComponent(Object container) {
77 this.container = container;
78 }
79
80 /**
81 * Get the container of the instruction that caused this new context to be created. This will
82 * generally be an object such as an XSLT Template or a user-defined function
83 *
84 * @return the container of the instruction in the expression tree
85 */
86
87 public Object getContainer() {
88 return container;
89 }
90
91 /**
92 * Set the value of the context at this level of the context stack
93 *
94 * @param context the context as it was when this new context was created
95 */
96
97 public void setContext(XPathContext context) {
98 this.context = context;
99 }
100
101 /**
102 * Get the value of the context at this level of the context stack
103 *
104 * @return the context as it was when this new context was created
105 */
106
107 public XPathContext getContext() {
108 return context;
109 }
110
111 /**
112 * Set the value of the context item at this level of the context stack
113 *
114 * @param contextItem the context item as it was when this new context was created
115 */
116
117 public void setContextItem(Item contextItem) {
118 this.contextItem = contextItem;
119 }
120
121 /**
122 * Get the value of the context item at this level of the context stack
123 *
124 * @return the context item as it was when this new context was created
125 */
126
127 public Item getContextItem() {
128 return contextItem;
129 }
130
131 /**
132 * Display a representation of the stack frame on the specified output stream
133 *
134 * @param out the output stream
135 */
136
137 public abstract void print(Logger out);
138
139 /**
140 * Show the location of a call (for use by subclasses)
141 *
142 * @return typically "(" + systemId() + "#" + lineNumber() + ")"
143 */
144
145 protected String showLocation() {
146 if (getSystemId() == null) {
147 return "";
148 }
149 int line = getLineNumber();
150 if (line == -1 || line == 0xfffff) {
151 return "(" + getSystemId() + ")";
152 } else {
153 return "(" + getSystemId() + "#" + getLineNumber() + ")";
154 }
155 }
156
157
158
159 /**
160 * Subclass of ContextStackFrame representing the outermost stack frame,
161 * for the calling application
162 */
163
164 public static class CallingApplication extends ContextStackFrame {
165 public void print(Logger out) {
166 //out.println(" (called from external application)");
167 }
168 }
169
170 /**
171 * Subclass of ContextStackFrame representing a built-in template rule in XSLT
172 */
173
174 public static class BuiltInTemplateRule extends ContextStackFrame {
175 private XPathContext context;
176 public BuiltInTemplateRule(XPathContext context) {
177 this.context = context;
178 }
179 public void print(Logger out) {
180 Item contextItem = context.getContextItem();
181 String diag;
182 if (contextItem instanceof NodeInfo) {
183 diag = Navigator.getPath((NodeInfo)contextItem);
184 } else if (contextItem instanceof AtomicValue) {
185 diag = "value " + contextItem.toString();
186 } else if (contextItem instanceof MapItem) {
187 diag = "map";
188 } else if (contextItem instanceof ArrayItem) {
189 diag = "array";
190 } else if (contextItem instanceof Function) {
191 diag = "function";
192 } else {
193 diag = "item";
194 }
195 out.error(" in built-in template rule for " + diag + " in " +
196 context.getCurrentMode().getActor().getModeTitle().toLowerCase());
197 }
198 }
199
200 /**
201 * Subclass of ContextStackFrame representing a call to a user-defined function
202 * either in XSLT or XQuery
203 */
204
205 public static class FunctionCall extends ContextStackFrame {
206
207 StructuredQName functionName;
208
209 /**
210 * Get the name of the function being called
211 *
212 * @return the name of the function being called
213 */
214 public StructuredQName getFunctionName() {
215 return functionName;
216 }
217
218 /**
219 * Set the name of the function being called
220 *
221 * @param functionName the name of the function being called
222 */
223 public void setFunctionName(StructuredQName functionName) {
224 this.functionName = functionName;
225 }
226
227 /**
228 * Display a representation of the stack frame on the specified output stream
229 *
230 * @param out the output stream
231 */
232
233 public void print(Logger out) {
234 out.error(" at " + (functionName == null ? "(anonymous)" : functionName.getDisplayName()) + "() " + showLocation());
235 }
236
237 }
238
239 /**
240 * Subclass of ContextStackFrame representing an xsl:apply-templates call in XSLT
241 */
242
243 public static class ApplyTemplates extends ContextStackFrame {
244
245 /**
246 * Display a representation of the stack frame on the specified output stream
247 *
248 * @param out the output stream
249 */
250
251 public void print(Logger out) {
252 out.error(" at xsl:apply-templates " + showLocation());
253 Item node = getContextItem();
254 if (node instanceof NodeInfo) {
255 out.error(" processing " + Navigator.getPath((NodeInfo) node));
256 }
257 }
258 }
259
260 /**
261 * Subclass of ContextStackFrame representing an xsl:call-template instruction in XSLT
262 */
263
264 public static class CallTemplate extends ContextStackFrame {
265
266 /**
267 * Get the name of the template being called
268 *
269 * @return the name of the template being called. Note this may be null in the case of the
270 * extension instruction saxon:call-template
271 */
272 public StructuredQName getTemplateName() {
273 return templateName;
274 }
275
276 /**
277 * Set the name of the template being called
278 *
279 * @param templateName the name of the template being called.
280 */
281 public void setTemplateName(StructuredQName templateName) {
282 this.templateName = templateName;
283 }
284
285 StructuredQName templateName;
286
287 /**
288 * Display a representation of the stack frame on the specified output stream
289 *
290 * @param out the output stream
291 */
292
293 public void print(Logger out) {
294 String name = templateName == null ? "??" : templateName.getDisplayName();
295 out.error(" at xsl:call-template name=\"" + name + "\" " + showLocation());
296 }
297 }
298
299 /**
300 * Subclass of ContextStackFrame representing the evaluation of a variable (typically a global variable)
301 */
302
303 public static class VariableEvaluation extends ContextStackFrame {
304
305 /**
306 * Get the name of the variable
307 *
308 * @return the name of the variable
309 */
310 public StructuredQName getVariableName() {
311 return variableName;
312 }
313
314 /**
315 * Set the name of the variable
316 *
317 * @param variableName the name of the variable
318 */
319 public void setVariableName(StructuredQName variableName) {
320 this.variableName = variableName;
321 }
322
323 StructuredQName variableName;
324
325 /**
326 * Display a representation of the stack frame on the specified output stream
327 *
328 * @param out the output stream
329 */
330
331 public void print(Logger out) {
332 out.error(" in " + displayContainer(getContainer()) + " " + showLocation());
333 }
334 }
335
336 private static String displayContainer(/*@NotNull*/ Object container) {
337 if (container instanceof Actor) {
338 StructuredQName name = ((Actor) container).getObjectName();
339 String objectName = name == null ? "" : name.getDisplayName();
340 if (container instanceof NamedTemplate) {
341 return "template name=\"" + objectName + "\"";
342 } else if (container instanceof UserFunction) {
343 return "function " + objectName + "()";
344 } else if (container instanceof AttributeSet) {
345 return "attribute-set " + objectName;
346 } else if (container instanceof KeyDefinition) {
347 return "key " + objectName;
348 } else if (container instanceof GlobalVariable) {
349 StructuredQName qName = ((GlobalVariable) container).getVariableQName();
350 if (qName.hasURI(NamespaceConstant.SAXON_GENERATED_VARIABLE)) {
351 return "optimizer-created global variable";
352 } else {
353 return "global variable $" + qName.getDisplayName();
354 }
355 }
356 } else if (container instanceof TemplateRule) {
357 return "template match=\"" + ((TemplateRule) container).getMatchPattern().toString() + "\"";
358 }
359 return "";
360 }
361
362
363 }
364