Debian Codegraph @

net/sf/saxon/trace/ContextStackIterator.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.Controller;
11 import net.sf.saxon.expr.ContextOriginator;
12 import net.sf.saxon.expr.UserFunctionCall;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.XPathContextMajor;
15 import net.sf.saxon.expr.instruct.*;
16 import net.sf.saxon.trans.rules.BuiltInRuleSet;
17
18 import java.util.Iterator;
19
20 /**
21 * This class provides a representation of the current runtime call stack, as represented by the stack
22 * of XPathContext objects.
23 */
24 public class ContextStackIterator implements Iterator<ContextStackFrame> {
25
26 // TODO: this class is no longer used by StandardErrorListener.printStackTrace(). It's potentially
27 // still useful to have a programmatic way of obtaining the stack trace, but this class is rather
28 // clumsy and probably isn't used.
29
30 private XPathContextMajor next;
31
32 /**
33 * Create an iterator over the stack of XPath dynamic context objects, starting with the top-most
34 * stackframe and working down. The objects returned by this iterator will be of class {@link ContextStackFrame}.
35 * Note that only "major" context objects are considered - those that have a stack frame of their own.
36 *
37 * @param context the current context
38 */
39
40 public ContextStackIterator(XPathContext context) {
41 if (!(context instanceof XPathContextMajor)) {
42 context = getMajorCaller(context);
43 }
44 next = (XPathContextMajor) context;
45 }
46
47 /**
48 * Returns <tt>true</tt> if the iteration has more elements. (In other
49 * words, returns <tt>true</tt> if <tt>next</tt> would return an element
50 * rather than throwing an exception.)
51 *
52 * @return <tt>true</tt> if the iterator has more elements.
53 */
54 public boolean hasNext() {
55 return next != null;
56 }
57
58 /**
59 * Returns the next element in the iteration. Calling this method
60 * repeatedly until the {@link #hasNext()} method returns false will
61 * return each element in the underlying collection exactly once.
62 *
63 * @return the next element in the iteration, which will always be an instance
64 * of {@link ContextStackFrame}
65 * @throws java.util.NoSuchElementException
66 * iteration has no more elements.
67 */
68 /*@Nullable*/
69 public ContextStackFrame next() {
70 XPathContextMajor context = next;
71 if (context == null) {
72 return null;
73 }
74 ContextOriginator origin = context.getOrigin();
75
76 if (origin instanceof Controller) {
77 next = getMajorCaller(context);
78 return new ContextStackFrame.CallingApplication();
79 } else if (origin instanceof BuiltInRuleSet) {
80 next = getMajorCaller(context);
81 return new ContextStackFrame.BuiltInTemplateRule(context);
82 } else if (origin instanceof UserFunction) {
83 ContextStackFrame.FunctionCall sf = new ContextStackFrame.FunctionCall();
84 UserFunction ufc = (UserFunction) origin;
85 sf.setLocation(ufc.getLocation());
86 sf.setFunctionName(ufc.getFunctionName());
87 sf.setContextItem(context.getContextItem());
88 sf.setContext(context);
89 next = getMajorCaller(context);
90 return sf;
91 } else if (origin instanceof UserFunctionCall) {
92 // No longer used? Bug 3671
93 ContextStackFrame.FunctionCall sf = new ContextStackFrame.FunctionCall();
94 UserFunctionCall ufc = (UserFunctionCall) origin;
95 sf.setLocation(ufc.getLocation());
96 sf.setFunctionName(ufc.getFunctionName());
97 sf.setContextItem(context.getContextItem());
98 sf.setContext(context);
99 next = getMajorCaller(context);
100 return sf;
101 } else if (origin instanceof ApplyTemplates) {
102 ContextStackFrame.ApplyTemplates sf = new ContextStackFrame.ApplyTemplates();
103 ApplyTemplates loc = (ApplyTemplates) origin;
104 sf.setLocation(loc.getLocation());
105 sf.setContextItem(context.getContextItem());
106 sf.setContext(context);
107 next = getMajorCaller(context);
108 return sf;
109 } else if (origin instanceof CallTemplate) {
110 ContextStackFrame.CallTemplate sf = new ContextStackFrame.CallTemplate();
111 CallTemplate loc = (CallTemplate) origin;
112 sf.setLocation(loc.getLocation());
113 sf.setTemplateName(loc.getObjectName());
114 sf.setContextItem(context.getContextItem());
115 sf.setContext(context);
116 next = getMajorCaller(context);
117 return sf;
118 } else if (origin instanceof GlobalVariable) {
119 ContextStackFrame.VariableEvaluation sf = new ContextStackFrame.VariableEvaluation();
120 GlobalVariable var = (GlobalVariable) origin;
121 sf.setLocation(var.getLocation());
122 sf.setContextItem(context.getContextItem());
123 sf.setVariableName(var.getVariableQName());
124 sf.setComponent(var);
125 sf.setContext(context);
126 next = getMajorCaller(context);
127 return sf;
128 } else {
129 //other context changes are not considered significant enough to report
130 //out.println(" In unidentified location " + construct);
131 next = getMajorCaller(context);
132 ContextStackFrame csf = next();
133 if (csf == null) {
134 // we can't return null, because hasNext() returned true...
135 return new ContextStackFrame.CallingApplication();
136 } else {
137 return csf;
138 }
139 }
140
141 }
142
143 private static XPathContextMajor getMajorCaller(XPathContext context) {
144 XPathContext caller = context.getCaller();
145 while (!(caller == null || caller instanceof XPathContextMajor)) {
146 caller = caller.getCaller();
147 }
148 return (XPathContextMajor) caller;
149 }
150
151 /**
152 * Removes from the underlying collection the last element returned by the
153 * iterator (optional operation).
154 *
155 * @throws UnsupportedOperationException as the <tt>remove</tt>
156 * operation is not supported by this Iterator.
157 */
158 public void remove() {
159 throw new UnsupportedOperationException();
160 }
161
162
163 }
164