| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.sxpath; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.LocalVariableReference; |
| 13 |
import net.sf.saxon.expr.PackageData; |
| 14 |
import net.sf.saxon.expr.instruct.Executable; |
| 15 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 16 |
import net.sf.saxon.expr.parser.OptimizerOptions; |
| 17 |
import net.sf.saxon.expr.parser.RetainedStaticContext; |
| 18 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 19 |
import net.sf.saxon.lib.NamespaceConstant; |
| 20 |
import net.sf.saxon.om.AxisInfo; |
| 21 |
import net.sf.saxon.om.NamespaceResolver; |
| 22 |
import net.sf.saxon.om.NodeInfo; |
| 23 |
import net.sf.saxon.om.StructuredQName; |
| 24 |
import net.sf.saxon.trans.XPathException; |
| 25 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 26 |
import net.sf.saxon.type.AnyItemType; |
| 27 |
import net.sf.saxon.type.ItemType; |
| 28 |
import net.sf.saxon.type.Type; |
| 29 |
import net.sf.saxon.value.QNameValue; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class IndependentContext extends AbstractStaticContext |
| 45 |
implements XPathStaticContext, NamespaceResolver { |
| 46 |
|
| 47 |
protected HashMap<String, String> namespaces = new HashMap<String, String>(10); |
| 48 |
protected HashMap<StructuredQName, XPathVariable> variables = new HashMap<StructuredQName, XPathVariable>(20); |
| 49 |
protected NamespaceResolver externalResolver = null; |
| 50 |
protected ItemType requiredContextItemType = AnyItemType.getInstance(); |
| 51 |
protected Set<String> importedSchemaNamespaces = new HashSet<String>(); |
| 52 |
protected boolean autoDeclare = false; |
| 53 |
protected Executable executable; |
| 54 |
protected RetainedStaticContext retainedStaticContext; |
| 55 |
protected OptimizerOptions optimizerOptions; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public IndependentContext() { |
| 63 |
this(new Configuration()); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public IndependentContext(Configuration config) { |
| 73 |
setConfiguration(config); |
| 74 |
clearNamespaces(); |
| 75 |
setDefaultFunctionLibrary(31); |
| 76 |
usingDefaultFunctionLibrary = true; |
| 77 |
setDefaultCollationName(config.getDefaultCollationName()); |
| 78 |
setOptimizerOptions(config.getOptimizerOptions()); |
| 79 |
PackageData pd = new PackageData(config); |
| 80 |
pd.setHostLanguage(Configuration.XPATH); |
| 81 |
pd.setSchemaAware(false); |
| 82 |
setPackageData(pd); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public IndependentContext(IndependentContext ic) { |
| 92 |
this(ic.getConfiguration()); |
| 93 |
setPackageData(ic.getPackageData()); |
| 94 |
setBaseURI(ic.getStaticBaseURI()); |
| 95 |
setContainingLocation(ic.getContainingLocation()); |
| 96 |
setDefaultElementNamespace(ic.getDefaultElementNamespace()); |
| 97 |
setDefaultFunctionNamespace(ic.getDefaultFunctionNamespace()); |
| 98 |
setBackwardsCompatibilityMode(ic.isInBackwardsCompatibleMode()); |
| 99 |
namespaces = new HashMap<String, String>(ic.namespaces); |
| 100 |
variables = new HashMap<StructuredQName, XPathVariable>(10); |
| 101 |
FunctionLibraryList libList = (FunctionLibraryList) ic.getFunctionLibrary(); |
| 102 |
if (libList != null) { |
| 103 |
setFunctionLibrary((FunctionLibraryList) libList.copy()); |
| 104 |
} |
| 105 |
setImportedSchemaNamespaces(ic.importedSchemaNamespaces); |
| 106 |
externalResolver = ic.externalResolver; |
| 107 |
autoDeclare = ic.autoDeclare; |
| 108 |
setXPathLanguageLevel(ic.getXPathVersion()); |
| 109 |
requiredContextItemType = ic.requiredContextItemType; |
| 110 |
setExecutable(ic.getExecutable()); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public RetainedStaticContext makeRetainedStaticContext() { |
| 127 |
if (retainedStaticContext == null) { |
| 128 |
retainedStaticContext = new RetainedStaticContext(this); |
| 129 |
} |
| 130 |
return retainedStaticContext; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void declareNamespace(String prefix, String uri) { |
| 142 |
if (prefix == null) { |
| 143 |
throw new NullPointerException("Null prefix supplied to declareNamespace()"); |
| 144 |
} |
| 145 |
if (uri == null) { |
| 146 |
throw new NullPointerException("Null namespace URI supplied to declareNamespace()"); |
| 147 |
} |
| 148 |
if ("".equals(prefix)) { |
| 149 |
setDefaultElementNamespace(uri); |
| 150 |
} else { |
| 151 |
namespaces.put(prefix, uri); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
@Override |
| 163 |
public void setDefaultElementNamespace(String uri) { |
| 164 |
if (uri == null) { |
| 165 |
uri = ""; |
| 166 |
} |
| 167 |
super.setDefaultElementNamespace(uri); |
| 168 |
namespaces.put("", uri); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void clearNamespaces() { |
| 177 |
namespaces.clear(); |
| 178 |
declareNamespace("xml", NamespaceConstant.XML); |
| 179 |
declareNamespace("xsl", NamespaceConstant.XSLT); |
| 180 |
declareNamespace("saxon", NamespaceConstant.SAXON); |
| 181 |
declareNamespace("xs", NamespaceConstant.SCHEMA); |
| 182 |
declareNamespace("", ""); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public void clearAllNamespaces() { |
| 192 |
namespaces.clear(); |
| 193 |
declareNamespace("xml", NamespaceConstant.XML); |
| 194 |
declareNamespace("", ""); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void setNamespaces(NodeInfo node) { |
| 208 |
namespaces.clear(); |
| 209 |
int kind = node.getNodeKind(); |
| 210 |
if (kind == Type.ATTRIBUTE || kind == Type.TEXT || |
| 211 |
kind == Type.COMMENT || kind == Type.PROCESSING_INSTRUCTION || |
| 212 |
kind == Type.NAMESPACE) { |
| 213 |
node = node.getParent(); |
| 214 |
} |
| 215 |
if (node == null) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
AxisIterator iter = node.iterateAxis(AxisInfo.NAMESPACE); |
| 220 |
while (true) { |
| 221 |
NodeInfo ns = iter.next(); |
| 222 |
if (ns == null) { |
| 223 |
return; |
| 224 |
} |
| 225 |
String prefix = ns.getLocalPart(); |
| 226 |
if ("".equals(prefix)) { |
| 227 |
setDefaultElementNamespace(ns.getStringValue()); |
| 228 |
} else { |
| 229 |
declareNamespace(ns.getLocalPart(), ns.getStringValue()); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public void setNamespaceResolver(NamespaceResolver resolver) { |
| 243 |
externalResolver = resolver; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setAllowUndeclaredVariables(boolean allow) { |
| 257 |
autoDeclare = allow; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public boolean isAllowUndeclaredVariables() { |
| 271 |
return autoDeclare; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public XPathVariable declareVariable(QNameValue qname) { |
| 284 |
return declareVariable(qname.getStructuredQName()); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public XPathVariable declareVariable(String namespaceURI, String localName) { |
| 299 |
StructuredQName qName = new StructuredQName("", namespaceURI, localName); |
| 300 |
return declareVariable(qName); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public XPathVariable declareVariable(StructuredQName qName) { |
| 315 |
XPathVariable var = variables.get(qName); |
| 316 |
if (var != null) { |
| 317 |
return var; |
| 318 |
} else { |
| 319 |
var = XPathVariable.make(qName); |
| 320 |
int slot = variables.size(); |
| 321 |
var.setSlotNumber(slot); |
| 322 |
variables.put(qName, var); |
| 323 |
return var; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public Iterator<XPathVariable> iterateExternalVariables() { |
| 337 |
return variables.values().iterator(); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public XPathVariable getExternalVariable(StructuredQName qName) { |
| 350 |
return variables.get(qName); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public int getSlotNumber(QNameValue qname) { |
| 361 |
StructuredQName sq = qname.getStructuredQName(); |
| 362 |
XPathVariable var = variables.get(sq); |
| 363 |
if (var == null) { |
| 364 |
return -1; |
| 365 |
} |
| 366 |
return var.getLocalSlotNumber(); |
| 367 |
} |
| 368 |
|
| 369 |
public NamespaceResolver getNamespaceResolver() { |
| 370 |
if (externalResolver != null) { |
| 371 |
return externalResolver; |
| 372 |
} else { |
| 373 |
return this; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public String getURIForPrefix(String prefix, boolean useDefault) { |
| 390 |
if (externalResolver != null) { |
| 391 |
return externalResolver.getURIForPrefix(prefix, useDefault); |
| 392 |
} |
| 393 |
if (prefix.isEmpty()) { |
| 394 |
return useDefault ? getDefaultElementNamespace() : ""; |
| 395 |
} else { |
| 396 |
return namespaces.get(prefix); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public Iterator<String> iteratePrefixes() { |
| 406 |
if (externalResolver != null) { |
| 407 |
return externalResolver.iteratePrefixes(); |
| 408 |
} else { |
| 409 |
return namespaces.keySet().iterator(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public Expression bindVariable(StructuredQName qName) throws XPathException { |
| 424 |
XPathVariable var = variables.get(qName); |
| 425 |
if (var == null) { |
| 426 |
if (autoDeclare) { |
| 427 |
return new LocalVariableReference(declareVariable(qName)); |
| 428 |
} else { |
| 429 |
throw new XPathException("Undeclared variable in XPath expression: $" + qName.getClarkName(), "XPST0008"); |
| 430 |
} |
| 431 |
} else { |
| 432 |
return new LocalVariableReference(var); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public SlotManager getStackFrameMap() { |
| 443 |
SlotManager map = getConfiguration().makeSlotManager(); |
| 444 |
XPathVariable[] va = new XPathVariable[variables.size()]; |
| 445 |
|
| 446 |
for (XPathVariable var : variables.values()) { |
| 447 |
va[var.getLocalSlotNumber()] = var; |
| 448 |
} |
| 449 |
for (XPathVariable v : va) { |
| 450 |
map.allocateSlotNumber(v.getVariableQName()); |
| 451 |
} |
| 452 |
return map; |
| 453 |
} |
| 454 |
|
| 455 |
public Collection<XPathVariable> getDeclaredVariables() { |
| 456 |
return variables.values(); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
public boolean isImportedSchema(String namespace) { |
| 461 |
return importedSchemaNamespaces.contains(namespace); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public Set<String> getImportedSchemaNamespaces() { |
| 471 |
return importedSchemaNamespaces; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public void setImportedSchemaNamespaces(Set<String> namespaces) { |
| 482 |
importedSchemaNamespaces = namespaces; |
| 483 |
if (!namespaces.isEmpty()) { |
| 484 |
setSchemaAware(true); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public void setRequiredContextItemType(ItemType type) { |
| 498 |
requiredContextItemType = type; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
public ItemType getRequiredContextItemType() { |
| 510 |
return requiredContextItemType; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public void setOptimizerOptions(OptimizerOptions options) { |
| 520 |
this.optimizerOptions = options; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public OptimizerOptions getOptimizerOptions() { |
| 531 |
return this.optimizerOptions; |
| 532 |
} |
| 533 |
|
| 534 |
public void setExecutable(Executable exec) { |
| 535 |
executable = exec; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
public Executable getExecutable() { |
| 540 |
return executable; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
public int getColumnNumber() { |
| 545 |
return -1; |
| 546 |
} |
| 547 |
|
| 548 |
public String getPublicId() { |
| 549 |
return null; |
| 550 |
} |
| 551 |
|
| 552 |
public int getLineNumber() { |
| 553 |
return -1; |
| 554 |
} |
| 555 |
|
| 556 |
} |
| 557 |
|