| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.instruct.*; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.functions.FunctionLibrary; |
| 15 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 16 |
import net.sf.saxon.functions.ResolveURI; |
| 17 |
import net.sf.saxon.functions.registry.BuiltInFunctionSet; |
| 18 |
import net.sf.saxon.functions.registry.ConstructorFunctionLibrary; |
| 19 |
import net.sf.saxon.lib.NamespaceConstant; |
| 20 |
import net.sf.saxon.lib.Validation; |
| 21 |
import net.sf.saxon.om.NamespaceBinding; |
| 22 |
import net.sf.saxon.om.NamespaceResolver; |
| 23 |
import net.sf.saxon.om.QNameParser; |
| 24 |
import net.sf.saxon.om.StructuredQName; |
| 25 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 26 |
import net.sf.saxon.trace.TraceCodeInjector; |
| 27 |
import net.sf.saxon.trans.DecimalFormatManager; |
| 28 |
import net.sf.saxon.trans.KeyManager; |
| 29 |
import net.sf.saxon.trans.XPathException; |
| 30 |
import net.sf.saxon.type.AnyItemType; |
| 31 |
import net.sf.saxon.type.ItemType; |
| 32 |
import net.sf.saxon.type.SchemaType; |
| 33 |
|
| 34 |
import java.net.URI; |
| 35 |
import java.net.URISyntaxException; |
| 36 |
import java.util.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class QueryModule implements StaticContext { |
| 45 |
private boolean isMainModule; |
| 46 |
private final Configuration config; |
| 47 |
private StaticQueryContext userQueryContext; |
| 48 |
private final QueryModule topModule; |
| 49 |
private URI locationURI; |
| 50 |
private String baseURI; |
| 51 |
private String moduleNamespace; |
| 52 |
private HashMap<String, String> explicitPrologNamespaces; |
| 53 |
private Stack<ActiveNamespace> activeNamespaces; |
| 54 |
private HashMap<StructuredQName, GlobalVariable> variables; |
| 55 |
|
| 56 |
private HashMap<StructuredQName, GlobalVariable> libraryVariables; |
| 57 |
|
| 58 |
|
| 59 |
private HashMap<StructuredQName, UndeclaredVariable> undeclaredVariables; |
| 60 |
private HashSet<String> importedSchemata; |
| 61 |
private HashMap<String, HashSet<String>> loadedSchemata; |
| 62 |
|
| 63 |
|
| 64 |
private Executable executable; |
| 65 |
private List<QueryModule> importers; |
| 66 |
|
| 67 |
|
| 68 |
private FunctionLibraryList functionLibraryList; |
| 69 |
private XQueryFunctionLibrary globalFunctionLibrary; |
| 70 |
private int localFunctionLibraryNr; |
| 71 |
private int importedFunctionLibraryNr; |
| 72 |
private int unboundFunctionLibraryNr; |
| 73 |
private Set<String> importedModuleNamespaces; |
| 74 |
private boolean inheritNamespaces = true; |
| 75 |
private boolean preserveNamespaces = true; |
| 76 |
private int constructionMode = Validation.PRESERVE; |
| 77 |
private String defaultFunctionNamespace; |
| 78 |
private String defaultElementNamespace; |
| 79 |
private boolean preserveSpace = false; |
| 80 |
private boolean defaultEmptyLeast = true; |
| 81 |
private String defaultCollationName; |
| 82 |
private int revalidationMode = Validation.SKIP; |
| 83 |
private boolean isUpdating = false; |
| 84 |
private ItemType requiredContextItemType = AnyItemType.getInstance(); |
| 85 |
private DecimalFormatManager decimalFormatManager = null; |
| 86 |
private CodeInjector codeInjector; |
| 87 |
private PackageData packageData; |
| 88 |
private RetainedStaticContext moduleStaticContext = null; |
| 89 |
private Location moduleLocation; |
| 90 |
private OptimizerOptions optimizerOptions; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public QueryModule( StaticQueryContext sqc) throws XPathException { |
| 101 |
config = sqc.getConfiguration(); |
| 102 |
isMainModule = true; |
| 103 |
topModule = this; |
| 104 |
activeNamespaces = new Stack<>(); |
| 105 |
baseURI = sqc.getBaseURI(); |
| 106 |
defaultCollationName = sqc.getDefaultCollationName(); |
| 107 |
try { |
| 108 |
locationURI = baseURI == null ? null : new URI(baseURI); |
| 109 |
} catch (URISyntaxException err) { |
| 110 |
throw new XPathException("Invalid location URI: " + baseURI); |
| 111 |
} |
| 112 |
executable = sqc.makeExecutable(); |
| 113 |
importers = null; |
| 114 |
init(sqc); |
| 115 |
|
| 116 |
PackageData pd = new PackageData(config); |
| 117 |
pd.setHostLanguage(Configuration.XQUERY); |
| 118 |
pd.setSchemaAware(isSchemaAware()); |
| 119 |
packageData = pd; |
| 120 |
|
| 121 |
for (Iterator<GlobalVariable> vars = sqc.iterateDeclaredGlobalVariables(); vars.hasNext(); ) { |
| 122 |
GlobalVariable var = vars.next(); |
| 123 |
declareVariable(var); |
| 124 |
pd.addGlobalVariable(var); |
| 125 |
var.setPackageData(pd); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
executable.setTopLevelPackage(pd); |
| 130 |
executable.addPackage(pd); |
| 131 |
|
| 132 |
if (sqc.getModuleLocation() == null) { |
| 133 |
moduleLocation = new ExplicitLocation(sqc.getSystemId(), 1, -1); |
| 134 |
} else { |
| 135 |
moduleLocation = sqc.getModuleLocation(); |
| 136 |
} |
| 137 |
optimizerOptions = sqc.getOptimizerOptions(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public QueryModule(Configuration config, QueryModule importer) { |
| 149 |
this.config = config; |
| 150 |
importers = null; |
| 151 |
if (importer == null) { |
| 152 |
topModule = this; |
| 153 |
} else { |
| 154 |
topModule = importer.topModule; |
| 155 |
userQueryContext = importer.userQueryContext; |
| 156 |
importers = new ArrayList<>(2); |
| 157 |
importers.add(importer); |
| 158 |
} |
| 159 |
init(userQueryContext); |
| 160 |
packageData = importer.getPackageData(); |
| 161 |
activeNamespaces = new Stack<>(); |
| 162 |
executable = null; |
| 163 |
optimizerOptions = importer.optimizerOptions; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private void init( StaticQueryContext sqc) { |
| 174 |
|
| 175 |
userQueryContext = sqc; |
| 176 |
variables = new HashMap<>(10); |
| 177 |
undeclaredVariables = new HashMap<>(5); |
| 178 |
if (isTopLevelModule()) { |
| 179 |
libraryVariables = new HashMap<>(10); |
| 180 |
} |
| 181 |
importedSchemata = new HashSet<>(5); |
| 182 |
|
| 183 |
importedModuleNamespaces = new HashSet<>(5); |
| 184 |
moduleNamespace = null; |
| 185 |
activeNamespaces = new Stack<>(); |
| 186 |
|
| 187 |
explicitPrologNamespaces = new HashMap<>(10); |
| 188 |
if (sqc != null) { |
| 189 |
|
| 190 |
inheritNamespaces = sqc.isInheritNamespaces(); |
| 191 |
preserveNamespaces = sqc.isPreserveNamespaces(); |
| 192 |
preserveSpace = sqc.isPreserveBoundarySpace(); |
| 193 |
defaultEmptyLeast = sqc.isEmptyLeast(); |
| 194 |
defaultFunctionNamespace = sqc.getDefaultFunctionNamespace(); |
| 195 |
defaultElementNamespace = sqc.getDefaultElementNamespace(); |
| 196 |
defaultCollationName = sqc.getDefaultCollationName(); |
| 197 |
constructionMode = sqc.getConstructionMode(); |
| 198 |
if (constructionMode == Validation.PRESERVE && !sqc.isSchemaAware()) { |
| 199 |
|
| 200 |
constructionMode = Validation.STRIP; |
| 201 |
} |
| 202 |
requiredContextItemType = sqc.getRequiredContextItemType(); |
| 203 |
isUpdating = sqc.isUpdatingEnabled(); |
| 204 |
codeInjector = sqc.getCodeInjector(); |
| 205 |
optimizerOptions = sqc.getOptimizerOptions(); |
| 206 |
} |
| 207 |
initializeFunctionLibraries(sqc); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static QueryModule makeQueryModule( |
| 227 |
String baseURI, Executable executable, QueryModule importer, |
| 228 |
String query, String namespaceURI) throws XPathException { |
| 229 |
Configuration config = executable.getConfiguration(); |
| 230 |
QueryModule module = new QueryModule(config, importer); |
| 231 |
try { |
| 232 |
module.setLocationURI(new URI(baseURI)); |
| 233 |
} catch (URISyntaxException e) { |
| 234 |
throw new XPathException("Invalid location URI " + baseURI, e); |
| 235 |
} |
| 236 |
module.setBaseURI(baseURI); |
| 237 |
module.setExecutable(executable); |
| 238 |
module.setModuleNamespace(namespaceURI); |
| 239 |
|
| 240 |
executable.addQueryLibraryModule(module); |
| 241 |
XQueryParser qp = (XQueryParser) config.newExpressionParser( |
| 242 |
"XQ", importer.isUpdating(), 31); |
| 243 |
if (importer.getCodeInjector() != null) { |
| 244 |
qp.setCodeInjector(importer.getCodeInjector()); |
| 245 |
} else if (config.isCompileWithTracing()) { |
| 246 |
qp.setCodeInjector(new TraceCodeInjector()); |
| 247 |
} |
| 248 |
QNameParser qnp = new QNameParser(module.getLiveNamespaceResolver()); |
| 249 |
qnp.setAcceptEQName(importer.getXPathVersion() >= 30); |
| 250 |
qnp.setUnescaper(new XQueryParser.Unescaper(config.getValidCharacterChecker())); |
| 251 |
qp.setQNameParser(qnp); |
| 252 |
|
| 253 |
qp.parseLibraryModule(query, module); |
| 254 |
|
| 255 |
String namespace = module.getModuleNamespace(); |
| 256 |
if (namespace == null) { |
| 257 |
XPathException err = new XPathException("Imported module must be a library module"); |
| 258 |
err.setErrorCode("XQST0059"); |
| 259 |
err.setIsStaticError(true); |
| 260 |
throw err; |
| 261 |
} |
| 262 |
if (!namespace.equals(namespaceURI)) { |
| 263 |
XPathException err = new XPathException("Imported module's namespace does not match requested namespace"); |
| 264 |
err.setErrorCode("XQST0059"); |
| 265 |
err.setIsStaticError(true); |
| 266 |
throw err; |
| 267 |
} |
| 268 |
|
| 269 |
return module; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private void initializeFunctionLibraries( StaticQueryContext sqc) { |
| 279 |
Configuration config = getConfiguration(); |
| 280 |
if (isTopLevelModule()) { |
| 281 |
globalFunctionLibrary = new XQueryFunctionLibrary(config); |
| 282 |
} |
| 283 |
|
| 284 |
functionLibraryList = new FunctionLibraryList(); |
| 285 |
functionLibraryList.addFunctionLibrary(getBuiltInFunctionSet()); |
| 286 |
functionLibraryList.addFunctionLibrary(config.getBuiltInExtensionLibraryList()); |
| 287 |
functionLibraryList.addFunctionLibrary(new ConstructorFunctionLibrary(config)); |
| 288 |
|
| 289 |
localFunctionLibraryNr = functionLibraryList.addFunctionLibrary( |
| 290 |
new XQueryFunctionLibrary(config)); |
| 291 |
|
| 292 |
importedFunctionLibraryNr = functionLibraryList.addFunctionLibrary( |
| 293 |
new ImportedFunctionLibrary(this, getTopLevelModule().getGlobalFunctionLibrary())); |
| 294 |
|
| 295 |
if (sqc != null && sqc.getExtensionFunctionLibrary() != null) { |
| 296 |
functionLibraryList.addFunctionLibrary(sqc.getExtensionFunctionLibrary()); |
| 297 |
} |
| 298 |
|
| 299 |
functionLibraryList.addFunctionLibrary(config.getIntegratedFunctionLibrary()); |
| 300 |
config.addExtensionBinders(functionLibraryList); |
| 301 |
|
| 302 |
unboundFunctionLibraryNr = functionLibraryList.addFunctionLibrary( |
| 303 |
new UnboundFunctionLibrary()); |
| 304 |
} |
| 305 |
|
| 306 |
public BuiltInFunctionSet getBuiltInFunctionSet() { |
| 307 |
if (isUpdating()) { |
| 308 |
return config.getXQueryUpdateFunctionSet(); |
| 309 |
} else { |
| 310 |
return config.getXPath31FunctionSet(); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public Configuration getConfiguration() { |
| 321 |
return config; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public PackageData getPackageData() { |
| 332 |
return packageData; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public void setPackageData(PackageData packageData) { |
| 343 |
this.packageData = packageData; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public boolean isTopLevelModule() { |
| 355 |
return this == topModule; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public void setIsMainModule(boolean main) { |
| 365 |
isMainModule = main; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public boolean isMainModule() { |
| 375 |
return isMainModule; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
public boolean mayImportModule( String namespace) { |
| 389 |
if (namespace.equals(moduleNamespace)) { |
| 390 |
return false; |
| 391 |
} |
| 392 |
if (importers == null) { |
| 393 |
return true; |
| 394 |
} |
| 395 |
for (QueryModule importer : importers) { |
| 396 |
if (!importer.mayImportModule(namespace)) { |
| 397 |
return false; |
| 398 |
} |
| 399 |
} |
| 400 |
return true; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public boolean isSchemaAware() { |
| 410 |
return executable.isSchemaAware(); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
@Override |
| 421 |
public OptimizerOptions getOptimizerOptions() { |
| 422 |
return optimizerOptions; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public RetainedStaticContext makeRetainedStaticContext() { |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if (activeNamespaces.empty()) { |
| 438 |
if (moduleStaticContext == null) { |
| 439 |
moduleStaticContext = new RetainedStaticContext(this); |
| 440 |
} |
| 441 |
return moduleStaticContext; |
| 442 |
} else { |
| 443 |
return new RetainedStaticContext(this); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public void setInheritNamespaces(boolean inherit) { |
| 456 |
inheritNamespaces = inherit; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public boolean isInheritNamespaces() { |
| 467 |
return inheritNamespaces; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public void setPreserveNamespaces(boolean inherit) { |
| 477 |
preserveNamespaces = inherit; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public boolean isPreserveNamespaces() { |
| 487 |
return preserveNamespaces; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public void setConstructionMode(int mode) { |
| 497 |
constructionMode = mode; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
public int getConstructionMode() { |
| 507 |
return constructionMode; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public void setPreserveBoundarySpace(boolean preserve) { |
| 517 |
preserveSpace = preserve; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public boolean isPreserveBoundarySpace() { |
| 527 |
return preserveSpace; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public void setEmptyLeast(boolean least) { |
| 539 |
defaultEmptyLeast = least; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
public boolean isEmptyLeast() { |
| 551 |
return defaultEmptyLeast; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public XQueryFunctionLibrary getGlobalFunctionLibrary() { |
| 562 |
return globalFunctionLibrary; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public ImportedFunctionLibrary getImportedFunctionLibrary() { |
| 573 |
return (ImportedFunctionLibrary) functionLibraryList.get(importedFunctionLibraryNr); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public void addImportedNamespace(String uri) { |
| 584 |
if (importedModuleNamespaces == null) { |
| 585 |
importedModuleNamespaces = new HashSet<>(5); |
| 586 |
} |
| 587 |
importedModuleNamespaces.add(uri); |
| 588 |
getImportedFunctionLibrary().addImportedNamespace(uri); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
public boolean importsNamespace(String uri) { |
| 600 |
return importedModuleNamespaces != null && |
| 601 |
importedModuleNamespaces.contains(uri); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
public QueryModule getTopLevelModule() { |
| 613 |
return topModule; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public Executable getExecutable() { |
| 625 |
return executable; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public void setExecutable(Executable executable) { |
| 636 |
this.executable = executable; |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public StaticQueryContext getUserQueryContext() { |
| 650 |
return userQueryContext; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public Location getContainingLocation() { |
| 662 |
return moduleLocation; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
public void setModuleNamespace( String uri) { |
| 674 |
moduleNamespace = uri; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public String getModuleNamespace() { |
| 686 |
return moduleNamespace; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public void setLocationURI(URI uri) { |
| 696 |
locationURI = uri; |
| 697 |
moduleLocation = new ExplicitLocation(locationURI.toString(), 1, -1); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public URI getLocationURI() { |
| 708 |
return locationURI; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public String getSystemId() { |
| 719 |
return locationURI == null ? null : locationURI.toString(); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
public void setBaseURI(String uri) { |
| 729 |
baseURI = uri; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
public String getStaticBaseURI() { |
| 739 |
return baseURI; |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public SlotManager getGlobalStackFrameMap() { |
| 751 |
return getPackageData().getGlobalSlotManager(); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
public void declareVariable( GlobalVariable var) throws XPathException { |
| 772 |
StructuredQName key = var.getVariableQName(); |
| 773 |
if (variables.get(key) != null) { |
| 774 |
GlobalVariable old = variables.get(key); |
| 775 |
if (old == var || old.getUltimateOriginalVariable() == var.getUltimateOriginalVariable()) { |
| 776 |
|
| 777 |
} else { |
| 778 |
String oldloc = " (see line " + old.getLineNumber(); |
| 779 |
String oldSysId = old.getSystemId(); |
| 780 |
if (oldSysId != null && |
| 781 |
!oldSysId.equals(var.getSystemId())) { |
| 782 |
oldloc += " in module " + old.getSystemId(); |
| 783 |
} |
| 784 |
oldloc += ")"; |
| 785 |
XPathException err = new XPathException("Duplicate definition of global variable " |
| 786 |
+ var.getVariableQName().getDisplayName() |
| 787 |
+ oldloc); |
| 788 |
err.setErrorCode("XQST0049"); |
| 789 |
err.setIsStaticError(true); |
| 790 |
err.setLocation(var); |
| 791 |
throw err; |
| 792 |
} |
| 793 |
} |
| 794 |
variables.put(key, var); |
| 795 |
getPackageData().addGlobalVariable(var); |
| 796 |
|
| 797 |
final HashMap<StructuredQName, GlobalVariable> libVars = getTopLevelModule().libraryVariables; |
| 798 |
GlobalVariable old = libVars.get(key); |
| 799 |
if (old == null || old == var) { |
| 800 |
|
| 801 |
} else { |
| 802 |
XPathException err = new XPathException("Duplicate definition of global variable " |
| 803 |
+ var.getVariableQName().getDisplayName() |
| 804 |
+ " (see line " + old.getLineNumber() + " in module " + old.getSystemId() + ')'); |
| 805 |
err.setErrorCode("XQST0049"); |
| 806 |
err.setIsStaticError(true); |
| 807 |
err.setLocation(var); |
| 808 |
throw err; |
| 809 |
} |
| 810 |
|
| 811 |
if (!isMainModule()) { |
| 812 |
libVars.put(key, var); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
public Iterable<GlobalVariable> getGlobalVariables() { |
| 826 |
return libraryVariables.values(); |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
public List<GlobalVariable> fixupGlobalVariables(SlotManager globalVariableMap) throws XPathException { |
| 839 |
List<GlobalVariable> varDefinitions = new ArrayList<>(20); |
| 840 |
List<Iterator<GlobalVariable>> iters = new ArrayList<>(); |
| 841 |
iters.add(variables.values().iterator()); |
| 842 |
iters.add(libraryVariables.values().iterator()); |
| 843 |
|
| 844 |
|
| 845 |
for (Iterator<GlobalVariable> iter : iters) { |
| 846 |
while (iter.hasNext()) { |
| 847 |
GlobalVariable var = iter.next(); |
| 848 |
if (!varDefinitions.contains(var)) { |
| 849 |
int slot = globalVariableMap.allocateSlotNumber(var.getVariableQName()); |
| 850 |
var.compile(getExecutable(), slot); |
| 851 |
varDefinitions.add(var); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
return varDefinitions; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
public void lookForModuleCycles( Stack<QueryModule> referees, int lineNumber) throws XPathException { |
| 875 |
if (referees.contains(this)) { |
| 876 |
int s = referees.indexOf(this); |
| 877 |
referees.push(this); |
| 878 |
StringBuilder message = new StringBuilder("Circular dependency between modules. "); |
| 879 |
for (int i = s; i < referees.size() - 1; i++) { |
| 880 |
QueryModule next = referees.get(i + 1); |
| 881 |
if (i == s) { |
| 882 |
message.append("Module ").append(getSystemId()).append(" references module ").append(next.getSystemId()); |
| 883 |
} else { |
| 884 |
message.append(", which references module ").append(next.getSystemId()); |
| 885 |
} |
| 886 |
} |
| 887 |
message.append('.'); |
| 888 |
XPathException err = new XPathException(message.toString()); |
| 889 |
err.setErrorCode("XQST0093"); |
| 890 |
err.setIsStaticError(true); |
| 891 |
ExplicitLocation loc = new ExplicitLocation(getSystemId(), lineNumber, -1); |
| 892 |
err.setLocator(loc); |
| 893 |
throw err; |
| 894 |
} else { |
| 895 |
referees.push(this); |
| 896 |
Iterator<GlobalVariable> viter = getModuleVariables(); |
| 897 |
while (viter.hasNext()) { |
| 898 |
GlobalVariable gv = viter.next(); |
| 899 |
|
| 900 |
Expression select = gv.getSelectExpression(); |
| 901 |
if (select != null) { |
| 902 |
List<Binding> list = new ArrayList<>(10); |
| 903 |
ExpressionTool.gatherReferencedVariables(select, list); |
| 904 |
for (Binding b : list) { |
| 905 |
if (b instanceof GlobalVariable) { |
| 906 |
String uri = ((GlobalVariable) b).getSystemId(); |
| 907 |
StructuredQName qName = b.getVariableQName(); |
| 908 |
boolean synthetic = qName.hasURI(NamespaceConstant.SAXON_GENERATED_VARIABLE); |
| 909 |
if (!synthetic && uri != null && !uri.equals(getSystemId())) { |
| 910 |
QueryModule sqc = executable.getQueryModuleWithSystemId(uri, topModule); |
| 911 |
if (sqc != null) { |
| 912 |
sqc.lookForModuleCycles(referees, ((GlobalVariable) b).getLineNumber()); |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
List<UserFunction> fList = new ArrayList<>(5); |
| 918 |
ExpressionTool.gatherCalledFunctions(select, fList); |
| 919 |
for (UserFunction f : fList) { |
| 920 |
String uri = f.getSystemId(); |
| 921 |
if (uri != null && !uri.equals(getSystemId())) { |
| 922 |
QueryModule sqc = executable.getQueryModuleWithSystemId(uri, topModule); |
| 923 |
if (sqc != null) { |
| 924 |
sqc.lookForModuleCycles(referees, f.getLineNumber()); |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
Iterator<XQueryFunction> fiter = getLocalFunctionLibrary().getFunctionDefinitions(); |
| 931 |
while (fiter.hasNext()) { |
| 932 |
XQueryFunction gf = fiter.next(); |
| 933 |
|
| 934 |
Expression body = gf.getUserFunction().getBody(); |
| 935 |
if (body != null) { |
| 936 |
List<Binding> vList = new ArrayList<>(10); |
| 937 |
ExpressionTool.gatherReferencedVariables(body, vList); |
| 938 |
for (Binding b : vList) { |
| 939 |
if (b instanceof GlobalVariable) { |
| 940 |
String uri = ((GlobalVariable) b).getSystemId(); |
| 941 |
StructuredQName qName = b.getVariableQName(); |
| 942 |
boolean synthetic = qName.hasURI(NamespaceConstant.SAXON) && "gg".equals(qName.getPrefix()); |
| 943 |
if (!synthetic && uri != null && !uri.equals(getSystemId())) { |
| 944 |
QueryModule sqc = executable.getQueryModuleWithSystemId(uri, topModule); |
| 945 |
if (sqc != null) { |
| 946 |
sqc.lookForModuleCycles(referees, ((GlobalVariable) b).getLineNumber()); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
List<UserFunction> fList = new ArrayList<>(10); |
| 952 |
ExpressionTool.gatherCalledFunctions(body, fList); |
| 953 |
for (UserFunction f : fList) { |
| 954 |
String uri = f.getSystemId(); |
| 955 |
if (uri != null && !uri.equals(getSystemId())) { |
| 956 |
QueryModule sqc = executable.getQueryModuleWithSystemId(uri, topModule); |
| 957 |
if (sqc != null) { |
| 958 |
sqc.lookForModuleCycles(referees, f.getLineNumber()); |
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
referees.pop(); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
public Iterator<GlobalVariable> getModuleVariables() { |
| 975 |
return variables.values().iterator(); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
public void checkForCircularities( List<GlobalVariable> compiledVars, XQueryFunctionLibrary globalFunctionLibrary) throws XPathException { |
| 989 |
Iterator<GlobalVariable> iter = compiledVars.iterator(); |
| 990 |
Stack<Object> stack = null; |
| 991 |
while (iter.hasNext()) { |
| 992 |
if (stack == null) { |
| 993 |
stack = new Stack<>(); |
| 994 |
} |
| 995 |
GlobalVariable gv = iter.next(); |
| 996 |
if (gv != null) { |
| 997 |
gv.lookForCycles(stack, globalFunctionLibrary); |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
public void typeCheckGlobalVariables( List<GlobalVariable> compiledVars) throws XPathException { |
| 1013 |
ExpressionVisitor visitor = ExpressionVisitor.make(this); |
| 1014 |
for (GlobalVariable compiledVar : compiledVars) { |
| 1015 |
compiledVar.typeCheck(visitor); |
| 1016 |
} |
| 1017 |
if (isMainModule()) { |
| 1018 |
GlobalContextRequirement gcr = executable.getGlobalContextRequirement(); |
| 1019 |
if (gcr != null && gcr.getDefaultValue() != null) { |
| 1020 |
ContextItemStaticInfo info = getConfiguration().makeContextItemStaticInfo(AnyItemType.getInstance(), true); |
| 1021 |
gcr.setDefaultValue(gcr.getDefaultValue().typeCheck(visitor, info)); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
public Expression bindVariable( StructuredQName qName) throws XPathException { |
| 1039 |
GlobalVariable var = variables.get(qName); |
| 1040 |
if (var == null) { |
| 1041 |
String uri = qName.getURI(); |
| 1042 |
if ((uri.equals("") && isMainModule()) || uri.equals(moduleNamespace) || importsNamespace(uri)) { |
| 1043 |
QueryModule main = getTopLevelModule(); |
| 1044 |
var = main.libraryVariables.get(qName); |
| 1045 |
if (var == null) { |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
UndeclaredVariable uvar = undeclaredVariables.get(qName); |
| 1051 |
if (uvar != null) { |
| 1052 |
|
| 1053 |
GlobalVariableReference ref = new GlobalVariableReference(qName); |
| 1054 |
uvar.registerReference(ref); |
| 1055 |
return ref; |
| 1056 |
} else { |
| 1057 |
|
| 1058 |
uvar = new UndeclaredVariable(); |
| 1059 |
uvar.setPackageData(main.getPackageData()); |
| 1060 |
uvar.setVariableQName(qName); |
| 1061 |
GlobalVariableReference ref = new GlobalVariableReference(qName); |
| 1062 |
uvar.registerReference(ref); |
| 1063 |
undeclaredVariables.put(qName, uvar); |
| 1064 |
return ref; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
} else { |
| 1073 |
if (var.isPrivate()) { |
| 1074 |
XPathException err = new XPathException("Variable $" + qName.getDisplayName() + " is private"); |
| 1075 |
err.setErrorCode("XPST0008"); |
| 1076 |
err.setIsStaticError(true); |
| 1077 |
throw err; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} else { |
| 1081 |
|
| 1082 |
XPathException err = new XPathException("Variable $" + qName.getDisplayName() + " has not been declared"); |
| 1083 |
err.setErrorCode("XPST0008"); |
| 1084 |
err.setIsStaticError(true); |
| 1085 |
throw err; |
| 1086 |
} |
| 1087 |
} else { |
| 1088 |
if (var.isPrivate() && (var.getSystemId() == null || !var.getSystemId().equals(getSystemId()))) { |
| 1089 |
String message = "Variable $" + qName.getDisplayName() + " is private"; |
| 1090 |
if (var.getSystemId() == null) { |
| 1091 |
message += " (no base URI known)"; |
| 1092 |
} |
| 1093 |
XPathException err = new XPathException(message, "XPST0008"); |
| 1094 |
err.setIsStaticError(true); |
| 1095 |
throw err; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
GlobalVariableReference vref = new GlobalVariableReference(qName); |
| 1099 |
var.registerReference(vref); |
| 1100 |
return vref; |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
public FunctionLibrary getFunctionLibrary() { |
| 1114 |
return functionLibraryList; |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
public XQueryFunctionLibrary getLocalFunctionLibrary() { |
| 1125 |
return (XQueryFunctionLibrary) functionLibraryList.get(localFunctionLibraryNr); |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
public void declareFunction( XQueryFunction function) throws XPathException { |
| 1139 |
Configuration config = getConfiguration(); |
| 1140 |
if (function.getNumberOfArguments() == 1) { |
| 1141 |
StructuredQName name = function.getFunctionName(); |
| 1142 |
SchemaType t = config.getSchemaType(name); |
| 1143 |
if (t != null && t.isAtomicType()) { |
| 1144 |
XPathException err = new XPathException("Function name " + function.getDisplayName() + |
| 1145 |
" clashes with the name of the constructor function for an atomic type"); |
| 1146 |
err.setErrorCode("XQST0034"); |
| 1147 |
err.setIsStaticError(true); |
| 1148 |
throw err; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
XQueryFunctionLibrary local = getLocalFunctionLibrary(); |
| 1152 |
local.declareFunction(function); |
| 1153 |
|
| 1154 |
QueryModule main = getTopLevelModule(); |
| 1155 |
main.globalFunctionLibrary.declareFunction(function); |
| 1156 |
|
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
public void bindUnboundFunctionCalls() throws XPathException { |
| 1171 |
UnboundFunctionLibrary lib = (UnboundFunctionLibrary) functionLibraryList.get(unboundFunctionLibraryNr); |
| 1172 |
lib.bindUnboundFunctionReferences(functionLibraryList, getConfiguration()); |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
public void fixupGlobalFunctions() throws XPathException { |
| 1188 |
globalFunctionLibrary.fixupGlobalFunctions(this); |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
public void optimizeGlobalFunctions() throws XPathException { |
| 1201 |
globalFunctionLibrary.optimizeGlobalFunctions(this); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
public void explainGlobalFunctions(ExpressionPresenter out) throws XPathException{ |
| 1213 |
globalFunctionLibrary.explainGlobalFunctions(out); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
public UserFunction getUserDefinedFunction(String uri, String localName, int arity) { |
| 1230 |
return globalFunctionLibrary.getUserDefinedFunction(uri, localName, arity); |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
public void bindUnboundVariables() throws XPathException { |
| 1243 |
for (UndeclaredVariable uv : undeclaredVariables.values()) { |
| 1244 |
StructuredQName qName = uv.getVariableQName(); |
| 1245 |
GlobalVariable var = variables.get(qName); |
| 1246 |
if (var == null) { |
| 1247 |
String uri = qName.getURI(); |
| 1248 |
if (importsNamespace(uri)) { |
| 1249 |
QueryModule main = getTopLevelModule(); |
| 1250 |
var = main.libraryVariables.get(qName); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
if (var == null) { |
| 1254 |
XPathException err = new XPathException("Unresolved reference to variable $" + |
| 1255 |
uv.getVariableQName().getDisplayName()); |
| 1256 |
err.setErrorCode("XPST0008"); |
| 1257 |
err.setIsStaticError(true); |
| 1258 |
throw err; |
| 1259 |
} else if (var.isPrivate() && !var.getSystemId().equals(getSystemId())) { |
| 1260 |
XPathException err = new XPathException("Cannot reference a private variable in a different module"); |
| 1261 |
err.setErrorCode("XPST0008"); |
| 1262 |
err.setIsStaticError(true); |
| 1263 |
throw err; |
| 1264 |
} else { |
| 1265 |
uv.transferReferences(var); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
public void addImportedSchema(String targetNamespace, String baseURI, List<String> locationURIs) { |
| 1285 |
if (importedSchemata == null) { |
| 1286 |
importedSchemata = new HashSet<>(5); |
| 1287 |
} |
| 1288 |
importedSchemata.add(targetNamespace); |
| 1289 |
HashMap<String, HashSet<String>> loadedSchemata = getTopLevelModule().loadedSchemata; |
| 1290 |
if (loadedSchemata == null) { |
| 1291 |
loadedSchemata = new HashMap<>(5); |
| 1292 |
getTopLevelModule().loadedSchemata = loadedSchemata; |
| 1293 |
} |
| 1294 |
HashSet<String> entries = loadedSchemata.get(targetNamespace); |
| 1295 |
if (entries == null) { |
| 1296 |
entries = new HashSet<>(locationURIs.size()); |
| 1297 |
loadedSchemata.put(targetNamespace, entries); |
| 1298 |
} |
| 1299 |
for (String relative : locationURIs) { |
| 1300 |
try { |
| 1301 |
URI abs = ResolveURI.makeAbsolute(relative, baseURI); |
| 1302 |
entries.add(abs.toString()); |
| 1303 |
} catch (URISyntaxException e) { |
| 1304 |
|
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
public boolean isImportedSchema(String namespace) { |
| 1319 |
return importedSchemata != null && importedSchemata.contains(namespace); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
public Set<String> getImportedSchemaNamespaces() { |
| 1330 |
if (importedSchemata == null) { |
| 1331 |
return Collections.emptySet(); |
| 1332 |
} else { |
| 1333 |
return importedSchemata; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
public void reportStaticError( XPathException err) { |
| 1344 |
if (!err.hasBeenReported()) { |
| 1345 |
if (userQueryContext == null) { |
| 1346 |
config.getErrorListener().fatalError(err); |
| 1347 |
} else { |
| 1348 |
userQueryContext.getErrorListener().fatalError(err); |
| 1349 |
} |
| 1350 |
err.setHasBeenReported(true); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
public XPathContext makeEarlyEvaluationContext() { |
| 1362 |
return new EarlyEvaluationContext(getConfiguration()); |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
public String getDefaultCollationName() { |
| 1375 |
if (defaultCollationName == null) { |
| 1376 |
defaultCollationName = NamespaceConstant.CODEPOINT_COLLATION_URI; |
| 1377 |
} |
| 1378 |
return defaultCollationName; |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
public void setDefaultCollationName(String collation) { |
| 1387 |
defaultCollationName = collation; |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
public void declarePrologNamespace( String prefix, String uri) throws XPathException { |
| 1402 |
if (prefix == null) { |
| 1403 |
throw new NullPointerException("Null prefix supplied to declarePrologNamespace()"); |
| 1404 |
} |
| 1405 |
if (uri == null) { |
| 1406 |
throw new NullPointerException("Null namespace URI supplied to declarePrologNamespace()"); |
| 1407 |
} |
| 1408 |
if (prefix.equals("xml") != uri.equals(NamespaceConstant.XML)) { |
| 1409 |
XPathException err = new XPathException("Invalid declaration of the XML namespace"); |
| 1410 |
err.setErrorCode("XQST0070"); |
| 1411 |
err.setIsStaticError(true); |
| 1412 |
throw err; |
| 1413 |
} |
| 1414 |
if (explicitPrologNamespaces.get(prefix) != null) { |
| 1415 |
XPathException err = new XPathException("Duplicate declaration of namespace prefix \"" + prefix + '"'); |
| 1416 |
err.setErrorCode("XQST0033"); |
| 1417 |
err.setIsStaticError(true); |
| 1418 |
throw err; |
| 1419 |
} else { |
| 1420 |
explicitPrologNamespaces.put(prefix, uri); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
public void declareActiveNamespace( String prefix, String uri) { |
| 1437 |
if (prefix == null) { |
| 1438 |
throw new NullPointerException("Null prefix supplied to declareActiveNamespace()"); |
| 1439 |
} |
| 1440 |
if (uri == null) { |
| 1441 |
throw new NullPointerException("Null namespace URI supplied to declareActiveNamespace()"); |
| 1442 |
} |
| 1443 |
|
| 1444 |
ActiveNamespace entry = new ActiveNamespace(); |
| 1445 |
entry.prefix = prefix; |
| 1446 |
entry.uri = uri; |
| 1447 |
activeNamespaces.push(entry); |
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
public void undeclareNamespace() { |
| 1465 |
activeNamespaces.pop(); |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
public NamespaceResolver getLiveNamespaceResolver() { |
| 1475 |
return new NamespaceResolver() { |
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
public String getURIForPrefix(String prefix, boolean useDefault) { |
| 1491 |
return checkURIForPrefix(prefix); |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
public Iterator<String> iteratePrefixes() { |
| 1502 |
return getNamespaceResolver().iteratePrefixes(); |
| 1503 |
} |
| 1504 |
}; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
public String checkURIForPrefix( String prefix) { |
| 1521 |
|
| 1522 |
if (activeNamespaces != null) { |
| 1523 |
for (int i = activeNamespaces.size() - 1; i >= 0; i--) { |
| 1524 |
if (activeNamespaces.get(i).prefix.equals(prefix)) { |
| 1525 |
String uri = activeNamespaces.get(i).uri; |
| 1526 |
if (uri.equals("") && !prefix.equals("")) { |
| 1527 |
|
| 1528 |
return null; |
| 1529 |
} |
| 1530 |
return uri; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
} |
| 1534 |
if (prefix.isEmpty()) { |
| 1535 |
return defaultElementNamespace; |
| 1536 |
} |
| 1537 |
String uri = explicitPrologNamespaces.get(prefix); |
| 1538 |
if (uri != null) { |
| 1539 |
|
| 1540 |
return uri.isEmpty() ? null : uri; |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (userQueryContext != null) { |
| 1544 |
uri = userQueryContext.getNamespaceForPrefix(prefix); |
| 1545 |
if (uri != null) { |
| 1546 |
return uri; |
| 1547 |
} |
| 1548 |
NamespaceResolver externalResolver = userQueryContext.getExternalNamespaceResolver(); |
| 1549 |
if (externalResolver != null) { |
| 1550 |
return externalResolver.getURIForPrefix(prefix, true); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
return null; |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
public String getDefaultElementNamespace() { |
| 1567 |
return checkURIForPrefix(""); |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
public void setDefaultElementNamespace(String uri) { |
| 1577 |
defaultElementNamespace = uri; |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
public String getDefaultFunctionNamespace() { |
| 1587 |
return defaultFunctionNamespace; |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
public void setDefaultFunctionNamespace(String uri) { |
| 1597 |
defaultFunctionNamespace = uri; |
| 1598 |
} |
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
public void setRevalidationMode(int mode) { |
| 1609 |
if (mode == Validation.STRICT || mode == Validation.LAX || mode == Validation.SKIP) { |
| 1610 |
revalidationMode = mode; |
| 1611 |
} else { |
| 1612 |
throw new IllegalArgumentException("Invalid mode " + mode); |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
public int getRevalidationMode() { |
| 1625 |
return revalidationMode; |
| 1626 |
} |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
public NamespaceBinding[] getActiveNamespaceCodes() { |
| 1637 |
if (activeNamespaces == null) { |
| 1638 |
return NamespaceBinding.EMPTY_ARRAY; |
| 1639 |
} |
| 1640 |
NamespaceBinding[] nsBindings = new NamespaceBinding[activeNamespaces.size()]; |
| 1641 |
int used = 0; |
| 1642 |
HashSet<String> prefixes = new HashSet<>(10); |
| 1643 |
for (int n = activeNamespaces.size() - 1; n >= 0; n--) { |
| 1644 |
ActiveNamespace an = activeNamespaces.get(n); |
| 1645 |
if (!prefixes.contains(an.prefix)) { |
| 1646 |
prefixes.add(an.prefix); |
| 1647 |
nsBindings[used++] = new NamespaceBinding(an.prefix, an.uri); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
if (used < nsBindings.length) { |
| 1651 |
nsBindings = Arrays.copyOf(nsBindings, used); |
| 1652 |
} |
| 1653 |
return nsBindings; |
| 1654 |
} |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
public NamespaceResolver getNamespaceResolver() { |
| 1667 |
List<NamespaceBinding> externalNamespaceCodes = null; |
| 1668 |
NamespaceResolver externalResolver = userQueryContext.getExternalNamespaceResolver(); |
| 1669 |
if (externalResolver != null) { |
| 1670 |
externalNamespaceCodes = new ArrayList<>(); |
| 1671 |
Iterator iter = externalResolver.iteratePrefixes(); |
| 1672 |
while (iter.hasNext()) { |
| 1673 |
String prefix = (String) iter.next(); |
| 1674 |
String uri = externalResolver.getURIForPrefix(prefix, true); |
| 1675 |
NamespaceBinding nscode = new NamespaceBinding(prefix, uri); |
| 1676 |
externalNamespaceCodes.add(nscode); |
| 1677 |
} |
| 1678 |
} |
| 1679 |
HashMap<String, String> userDeclaredNamespaces = userQueryContext.getUserDeclaredNamespaces(); |
| 1680 |
List<NamespaceBinding> nsBindings = new ArrayList<>(); |
| 1681 |
|
| 1682 |
for (Map.Entry<String, String> e : userDeclaredNamespaces.entrySet()) { |
| 1683 |
nsBindings.add(new NamespaceBinding(e.getKey(), e.getValue())); |
| 1684 |
} |
| 1685 |
for (Map.Entry<String, String> e : explicitPrologNamespaces.entrySet()) { |
| 1686 |
nsBindings.add(new NamespaceBinding(e.getKey(), e.getValue())); |
| 1687 |
} |
| 1688 |
if (!defaultElementNamespace.isEmpty()) { |
| 1689 |
nsBindings.add(new NamespaceBinding("", defaultElementNamespace)); |
| 1690 |
} |
| 1691 |
nsBindings.addAll(Arrays.asList(getActiveNamespaceCodes())); |
| 1692 |
|
| 1693 |
if (externalNamespaceCodes != null) { |
| 1694 |
nsBindings.addAll(externalNamespaceCodes); |
| 1695 |
} |
| 1696 |
|
| 1697 |
return new SavedNamespaceContext(nsBindings); |
| 1698 |
} |
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
public ItemType getRequiredContextItemType() { |
| 1709 |
return requiredContextItemType; |
| 1710 |
} |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
public DecimalFormatManager getDecimalFormatManager() { |
| 1722 |
if (decimalFormatManager == null) { |
| 1723 |
decimalFormatManager = new DecimalFormatManager(Configuration.XQUERY, getXPathVersion()); |
| 1724 |
} |
| 1725 |
return decimalFormatManager; |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
public void issueWarning(String s, Location locator) { |
| 1735 |
XPathException err = new XPathException(s); |
| 1736 |
err.setLocator(locator); |
| 1737 |
if (userQueryContext != null) { |
| 1738 |
userQueryContext.getErrorListener().warning(err); |
| 1739 |
} else { |
| 1740 |
getConfiguration().getErrorListener().warning(err); |
| 1741 |
} |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
public boolean isInBackwardsCompatibleMode() { |
| 1752 |
return false; |
| 1753 |
} |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
public boolean isUpdating() { |
| 1763 |
return isUpdating; |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
public int getXPathVersion() { |
| 1775 |
return 31; |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
public CodeInjector getCodeInjector() { |
| 1785 |
return codeInjector; |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
public KeyManager getKeyManager() { |
| 1796 |
return packageData.getKeyManager(); |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
@Override |
| 1809 |
public ItemType resolveTypeAlias(StructuredQName typeName) { |
| 1810 |
return getPackageData().obtainTypeAliasManager().getItemType(typeName); |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
private static class ActiveNamespace { |
| 1818 |
public String prefix; |
| 1819 |
public String uri; |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|