| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ContextStackIterator implements Iterator<ContextStackFrame> { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private XPathContextMajor next; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public boolean hasNext() { |
| 55 |
return next != null; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 130 |
|
| 131 |
next = getMajorCaller(context); |
| 132 |
ContextStackFrame csf = next(); |
| 133 |
if (csf == null) { |
| 134 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void remove() { |
| 159 |
throw new UnsupportedOperationException(); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
} |
| 164 |
|