| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.sxpath; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.XPathContextMajor; |
| 12 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 13 |
import net.sf.saxon.expr.parser.TypeChecker; |
| 14 |
import net.sf.saxon.lib.CollectionFinder; |
| 15 |
import net.sf.saxon.lib.CollectionURIResolver; |
| 16 |
import net.sf.saxon.lib.UnfailingErrorListener; |
| 17 |
import net.sf.saxon.lib.UnparsedTextURIResolver; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 22 |
import net.sf.saxon.type.ItemType; |
| 23 |
import net.sf.saxon.type.TypeHierarchy; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
|
| 26 |
import javax.xml.transform.ErrorListener; |
| 27 |
import javax.xml.transform.URIResolver; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class XPathDynamicContext { |
| 36 |
|
| 37 |
private ItemType contextItemType; |
| 38 |
private XPathContextMajor contextObject; |
| 39 |
private SlotManager stackFrameMap; |
| 40 |
|
| 41 |
protected XPathDynamicContext(ItemType contextItemType, XPathContextMajor contextObject, SlotManager stackFrameMap) { |
| 42 |
this.contextItemType = contextItemType; |
| 43 |
this.contextObject = contextObject; |
| 44 |
this.stackFrameMap = stackFrameMap; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public void setContextItem(Item<?> item) throws XPathException { |
| 55 |
if (item instanceof NodeInfo) { |
| 56 |
if (!((NodeInfo) item).getConfiguration().isCompatible(contextObject.getConfiguration())) { |
| 57 |
throw new XPathException( |
| 58 |
"Supplied node must be built using the same or a compatible Configuration", |
| 59 |
SaxonErrorCode.SXXP0004); |
| 60 |
} |
| 61 |
} |
| 62 |
TypeHierarchy th = contextObject.getConfiguration().getTypeHierarchy(); |
| 63 |
if (!contextItemType.matches(item, th)) { |
| 64 |
throw new XPathException("Supplied context item does not match required context item type " + |
| 65 |
contextItemType); |
| 66 |
} |
| 67 |
ManualIterator<Item<?>> iter = new ManualIterator<>(item); |
| 68 |
contextObject.setCurrentIterator(iter); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Item getContextItem() { |
| 78 |
return contextObject.getContextItem(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void setVariable(XPathVariable variable, Sequence<?> value) throws XPathException { |
| 94 |
SequenceType requiredType = variable.getRequiredType(); |
| 95 |
if (requiredType != SequenceType.ANY_SEQUENCE) { |
| 96 |
XPathException err = TypeChecker.testConformance(value, requiredType, contextObject); |
| 97 |
if (err != null) { |
| 98 |
throw err; |
| 99 |
} |
| 100 |
} |
| 101 |
SequenceIterator<?> iter = value.iterate(); |
| 102 |
Item<?> item; |
| 103 |
while ((item = iter.next()) != null) { |
| 104 |
if (item instanceof NodeInfo && !((NodeInfo) item).getConfiguration().isCompatible(contextObject.getConfiguration())) { |
| 105 |
throw new XPathException( |
| 106 |
"Supplied node must be built using the same or a compatible Configuration", |
| 107 |
SaxonErrorCode.SXXP0004); |
| 108 |
} |
| 109 |
} |
| 110 |
int slot = variable.getLocalSlotNumber(); |
| 111 |
StructuredQName expectedName = slot >= stackFrameMap.getNumberOfVariables() ? null : |
| 112 |
stackFrameMap.getVariableMap().get(slot); |
| 113 |
if (!variable.getVariableQName().equals(expectedName)) { |
| 114 |
throw new XPathException( |
| 115 |
"Supplied XPathVariable is bound to the wrong slot: perhaps it was created using a different static context"); |
| 116 |
} |
| 117 |
contextObject.setLocalVariable(slot, value); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public void setURIResolver(URIResolver resolver) { |
| 131 |
contextObject.setURIResolver(resolver); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public URIResolver getURIResolver() { |
| 144 |
return contextObject.getURIResolver(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setCollectionURIResolver(CollectionURIResolver resolver) { |
| 157 |
contextObject.getController().setCollectionURIResolver(resolver); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public CollectionURIResolver getCollectionURIResolver() { |
| 171 |
return contextObject.getController().getCollectionURIResolver(); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public CollectionFinder getCollectionFinder() { |
| 184 |
return contextObject.getController().getCollectionFinder(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void setCollectionFinder(CollectionFinder cf) { |
| 196 |
contextObject.getController().setCollectionFinder(cf); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public void setErrorListener(ErrorListener listener) { |
| 209 |
contextObject.setErrorListener(listener); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public UnfailingErrorListener getErrorListener() { |
| 223 |
return contextObject.getErrorListener(); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public XPathContext getXPathContextObject() { |
| 234 |
return contextObject; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public void setUnparsedTextURIResolver(UnparsedTextURIResolver resolver) { |
| 247 |
contextObject.getController().setUnparsedTextURIResolver(resolver); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public UnparsedTextURIResolver getUnparsedTextURIResolver() { |
| 259 |
return contextObject.getController().getUnparsedTextURIResolver(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
protected void checkExternalVariables( SlotManager stackFrameMap, int numberOfExternals) throws XPathException { |
| 271 |
Sequence[] stack = contextObject.getStackFrame().getStackFrameValues(); |
| 272 |
for (int i = 0; i < numberOfExternals; i++) { |
| 273 |
if (stack[i] == null) { |
| 274 |
StructuredQName qname = stackFrameMap.getVariableMap().get(i); |
| 275 |
throw new XPathException("No value has been supplied for variable $" + qname.getDisplayName()); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|