| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.instruct.GlobalVariable; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.expr.parser.OptimizerOptions; |
| 15 |
import net.sf.saxon.expr.parser.RetainedStaticContext; |
| 16 |
import net.sf.saxon.functions.FunctionLibrary; |
| 17 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 18 |
import net.sf.saxon.functions.SystemFunction; |
| 19 |
import net.sf.saxon.functions.registry.VendorFunctionSetHE; |
| 20 |
import net.sf.saxon.lib.Feature; |
| 21 |
import net.sf.saxon.lib.NamespaceConstant; |
| 22 |
import net.sf.saxon.om.*; |
| 23 |
import net.sf.saxon.pattern.NameTest; |
| 24 |
import net.sf.saxon.trans.*; |
| 25 |
import net.sf.saxon.tree.AttributeLocation; |
| 26 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 27 |
import net.sf.saxon.type.AnyItemType; |
| 28 |
import net.sf.saxon.type.ItemType; |
| 29 |
import net.sf.saxon.type.Type; |
| 30 |
|
| 31 |
import java.util.Set; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class ExpressionContext implements XSLTStaticContext { |
| 39 |
|
| 40 |
private StyleElement element; |
| 41 |
private StructuredQName attributeName; |
| 42 |
private Location containingLocation = null; |
| 43 |
private NamespaceResolver namespaceResolver = null; |
| 44 |
private RetainedStaticContext retainedStaticContext = null; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public ExpressionContext(StyleElement styleElement, StructuredQName attributeName) { |
| 56 |
element = styleElement; |
| 57 |
this.attributeName = attributeName; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public Configuration getConfiguration() { |
| 65 |
return element.getConfiguration(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public StylesheetPackage getPackageData() { |
| 74 |
return element.getPackageData(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public boolean isSchemaAware() { |
| 84 |
return element.isSchemaAware(); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public XPathContext makeEarlyEvaluationContext() { |
| 92 |
return new EarlyEvaluationContext(getConfiguration()); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public RetainedStaticContext makeRetainedStaticContext() { |
| 104 |
if (retainedStaticContext == null) { |
| 105 |
if (element.changesRetainedStaticContext() || !(element.getParent() instanceof StyleElement)) { |
| 106 |
retainedStaticContext = new RetainedStaticContext(this); |
| 107 |
} else { |
| 108 |
retainedStaticContext = ((StyleElement) element.getParent()).getStaticContext().makeRetainedStaticContext(); |
| 109 |
} |
| 110 |
} |
| 111 |
return retainedStaticContext; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public Location getContainingLocation() { |
| 122 |
if (containingLocation == null) { |
| 123 |
if (attributeName == null) { |
| 124 |
containingLocation = element; |
| 125 |
} else { |
| 126 |
containingLocation = new AttributeLocation(element, attributeName); |
| 127 |
} |
| 128 |
} |
| 129 |
return containingLocation; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void issueWarning(String s, Location locator) { |
| 137 |
element.issueWarning(new XPathException(s, SaxonErrorCode.SXWN9000, locator)); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public String getSystemId() { |
| 145 |
return element.getSystemId(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public String getStaticBaseURI() { |
| 155 |
return element.getBaseURI(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public NamespaceResolver getNamespaceResolver() { |
| 166 |
if (namespaceResolver == null) { |
| 167 |
namespaceResolver = element.makeNamespaceContext(); |
| 168 |
} |
| 169 |
return namespaceResolver; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public ItemType getRequiredContextItemType() { |
| 181 |
return AnyItemType.getInstance(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public DecimalFormatManager getDecimalFormatManager() { |
| 193 |
return element.getCompilation().getPrincipalStylesheetModule().getDecimalFormatManager(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
@Override |
| 205 |
public OptimizerOptions getOptimizerOptions() { |
| 206 |
return element.getCompilation().getCompilerInfo().getOptimizerOptions(); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public Expression bindVariable(StructuredQName qName) throws XPathException { |
| 218 |
SourceBinding sourceBinding = element.bindVariable(qName); |
| 219 |
if (sourceBinding == null) { |
| 220 |
if (qName.hasURI(NamespaceConstant.XSLT) && qName.getLocalPart().equals("original")) { |
| 221 |
element.getXslOriginal(StandardNames.XSL_VARIABLE); |
| 222 |
return new GlobalVariableReference(qName); |
| 223 |
} |
| 224 |
|
| 225 |
SymbolicName sn = new SymbolicName(StandardNames.XSL_VARIABLE, qName); |
| 226 |
Component comp = element.getCompilation().getPrincipalStylesheetModule().getComponent(sn); |
| 227 |
if (comp != null) { |
| 228 |
|
| 229 |
element.iterateAxis(AxisInfo.ANCESTOR_OR_SELF).forEachOrFail(parent -> { |
| 230 |
if (parent instanceof XSLGlobalVariable && ((XSLGlobalVariable) parent).getVariableQName().equals(qName)) { |
| 231 |
XPathException err = new XPathException("Variable " + qName.getDisplayName() + |
| 232 |
" cannot be used within its own declaration", "XPST0008"); |
| 233 |
err.setIsStaticError(true); |
| 234 |
throw err; |
| 235 |
} |
| 236 |
}); |
| 237 |
|
| 238 |
GlobalVariable var = (GlobalVariable) comp.getActor(); |
| 239 |
GlobalVariableReference vref = new GlobalVariableReference(var); |
| 240 |
vref.setStaticType(var.getRequiredType(), null, 0); |
| 241 |
return vref; |
| 242 |
} |
| 243 |
|
| 244 |
if (getXPathVersion() >= 30 && qName.hasURI(NamespaceConstant.ERR)) { |
| 245 |
AxisIterator catchers = element.iterateAxis(AxisInfo.ANCESTOR_OR_SELF, |
| 246 |
new NameTest(Type.ELEMENT, StandardNames.XSL_CATCH, element.getNamePool())); |
| 247 |
StyleElement catcher = (StyleElement) catchers.next(); |
| 248 |
if (catcher != null) { |
| 249 |
for (StructuredQName errorVariable : StandardNames.errorVariables) { |
| 250 |
if (errorVariable.getLocalPart().equals(qName.getLocalPart())) { |
| 251 |
SystemFunction f = VendorFunctionSetHE.getInstance().makeFunction("dynamic-error-info", 1); |
| 252 |
return f.makeFunctionCall(new StringLiteral(qName.getLocalPart())); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
XPathException err = new XPathException("Variable " + qName.getDisplayName() + |
| 259 |
" has not been declared (or its declaration is not in scope)", "XPST0008"); |
| 260 |
err.setIsStaticError(true); |
| 261 |
throw err; |
| 262 |
} |
| 263 |
|
| 264 |
VariableReference var; |
| 265 |
if (sourceBinding.hasProperty(SourceBinding.IMPLICITLY_DECLARED)) { |
| 266 |
|
| 267 |
SuppliedParameterReference supRef = new SuppliedParameterReference(0); |
| 268 |
supRef.setSuppliedType(sourceBinding.getDeclaredType()); |
| 269 |
return supRef; |
| 270 |
} |
| 271 |
if (sourceBinding.hasProperty(SourceBinding.GLOBAL)) { |
| 272 |
var = new GlobalVariableReference(qName); |
| 273 |
|
| 274 |
GlobalVariable compiledVar = ((XSLGlobalVariable) sourceBinding.getSourceElement()).getCompiledVariable(); |
| 275 |
if (compiledVar != null && element.getCompilation().getCompilerInfo().isJustInTimeCompilation()) { |
| 276 |
var.fixup(compiledVar); |
| 277 |
var.setStaticType(compiledVar.getRequiredType(), sourceBinding.getConstantValue(), 0); |
| 278 |
} else { |
| 279 |
sourceBinding.registerReference(var); |
| 280 |
} |
| 281 |
return var; |
| 282 |
} else { |
| 283 |
var = new LocalVariableReference(qName); |
| 284 |
sourceBinding.registerReference(var); |
| 285 |
return var; |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public FunctionLibrary getFunctionLibrary() { |
| 296 |
FunctionLibrary lib = element.getContainingPackage().getFunctionLibrary(); |
| 297 |
StyleElement containingOverride = element.findAncestorElement(StandardNames.XSL_OVERRIDE); |
| 298 |
if (containingOverride != null) { |
| 299 |
|
| 300 |
FunctionLibraryList libList = new FunctionLibraryList(); |
| 301 |
libList.addFunctionLibrary(lib); |
| 302 |
((XSLOverride)containingOverride).addXSLOverrideFunctionLibrary(libList); |
| 303 |
return libList; |
| 304 |
} |
| 305 |
return lib; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public boolean isElementAvailable(String qname) throws XPathException { |
| 315 |
try { |
| 316 |
String[] parts = NameChecker.getQNameParts(qname); |
| 317 |
String uri; |
| 318 |
if (parts[0].isEmpty()) { |
| 319 |
uri = getDefaultElementNamespace(); |
| 320 |
} else { |
| 321 |
uri = element.getURIForPrefix(parts[0], false); |
| 322 |
} |
| 323 |
return element.getCompilation().getStyleNodeFactory(true).isElementAvailable(uri, parts[1], true); |
| 324 |
} catch (QNameException e) { |
| 325 |
XPathException err = new XPathException("Invalid element name. " + e.getMessage()); |
| 326 |
err.setErrorCode("XTDE1440"); |
| 327 |
throw err; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public String getDefaultCollationName() { |
| 336 |
return element.getDefaultCollationName(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public String getDefaultElementNamespace() { |
| 345 |
return element.getDefaultXPathNamespace(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public String getDefaultFunctionNamespace() { |
| 353 |
return NamespaceConstant.FN; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public boolean isInBackwardsCompatibleMode() { |
| 361 |
return element.xPath10ModeIsEnabled(); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public int getXPathVersion() { |
| 374 |
return getConfiguration().getConfigurationProperty(Feature.XPATH_VERSION_FOR_XSLT); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public boolean isImportedSchema(String namespace) { |
| 385 |
|
| 386 |
return element.getPrincipalStylesheetModule().isImportedSchema(namespace); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public Set<String> getImportedSchemaNamespaces() { |
| 399 |
return element.getPrincipalStylesheetModule().getImportedSchemaTable(); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public KeyManager getKeyManager() { |
| 410 |
return element.getCompilation().getPrincipalStylesheetModule().getKeyManager(); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public StyleElement getStyleElement() { |
| 420 |
return element; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
@Override |
| 432 |
public ItemType resolveTypeAlias(StructuredQName typeName) { |
| 433 |
return getPackageData().obtainTypeAliasManager().getItemType(typeName); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
} |
| 438 |
|