| 1 |
package net.sf.saxon.trans; |
| 2 |
|
| 3 |
import net.sf.saxon.Configuration; |
| 4 |
import net.sf.saxon.event.*; |
| 5 |
import net.sf.saxon.expr.*; |
| 6 |
import net.sf.saxon.expr.accum.Accumulator; |
| 7 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 8 |
import net.sf.saxon.expr.accum.AccumulatorRule; |
| 9 |
import net.sf.saxon.expr.compat.ArithmeticExpression10; |
| 10 |
import net.sf.saxon.expr.compat.GeneralComparison10; |
| 11 |
import net.sf.saxon.expr.flwor.LocalVariableBinding; |
| 12 |
import net.sf.saxon.expr.instruct.*; |
| 13 |
import net.sf.saxon.expr.number.NumberFormatter; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.expr.sort.*; |
| 16 |
import net.sf.saxon.functions.*; |
| 17 |
import net.sf.saxon.functions.registry.ConstructorFunctionLibrary; |
| 18 |
import net.sf.saxon.lib.*; |
| 19 |
import net.sf.saxon.ma.arrays.ArrayFunctionSet; |
| 20 |
import net.sf.saxon.ma.arrays.SimpleArrayItem; |
| 21 |
import net.sf.saxon.ma.arrays.SquareArrayConstructor; |
| 22 |
import net.sf.saxon.ma.map.HashTrieMap; |
| 23 |
import net.sf.saxon.ma.map.MapFunctionSet; |
| 24 |
import net.sf.saxon.om.*; |
| 25 |
import net.sf.saxon.pattern.*; |
| 26 |
import net.sf.saxon.query.XQueryFunctionLibrary; |
| 27 |
import net.sf.saxon.serialize.CharacterMap; |
| 28 |
import net.sf.saxon.serialize.CharacterMapIndex; |
| 29 |
import net.sf.saxon.style.PackageVersion; |
| 30 |
import net.sf.saxon.style.StylesheetFunctionLibrary; |
| 31 |
import net.sf.saxon.style.StylesheetPackage; |
| 32 |
import net.sf.saxon.sxpath.IndependentContext; |
| 33 |
import net.sf.saxon.trans.packages.IPackageLoader; |
| 34 |
import net.sf.saxon.trans.rules.BuiltInRuleSet; |
| 35 |
import net.sf.saxon.trans.rules.Rule; |
| 36 |
import net.sf.saxon.trans.rules.RuleManager; |
| 37 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 38 |
import net.sf.saxon.tree.util.Navigator; |
| 39 |
import net.sf.saxon.tree.util.Orphan; |
| 40 |
import net.sf.saxon.tree.wrapper.VirtualCopy; |
| 41 |
import net.sf.saxon.type.*; |
| 42 |
import net.sf.saxon.value.*; |
| 43 |
import net.sf.saxon.z.IntHashMap; |
| 44 |
|
| 45 |
import javax.xml.transform.Source; |
| 46 |
import javax.xml.transform.stream.StreamSource; |
| 47 |
import java.io.IOException; |
| 48 |
import java.io.StringReader; |
| 49 |
import java.math.BigInteger; |
| 50 |
import java.util.*; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class PackageLoaderHE implements IPackageLoader { |
| 57 |
|
| 58 |
private final static NestedIntegerValue SAXON9911 = new NestedIntegerValue(new int[]{9,9,1,1}); |
| 59 |
|
| 60 |
private Configuration config; |
| 61 |
protected final Stack<StylesheetPackage> packStack = new Stack<>(); |
| 62 |
private XPathParser parser; |
| 63 |
public final Stack<List<ComponentInvocation>> fixups = new Stack<>(); |
| 64 |
public final List<Action> completionActions = new ArrayList<>(); |
| 65 |
private List<Component> unnamedComponents = new ArrayList<>(); |
| 66 |
public final Map<String, StylesheetPackage> allPackages = new HashMap<>(); |
| 67 |
private Stack<LocalBinding> localBindings; |
| 68 |
private ExecutableFunctionLibrary overriding; |
| 69 |
private ExecutableFunctionLibrary underriding; |
| 70 |
private final Stack<RetainedStaticContext> contextStack = new Stack<>(); |
| 71 |
public final Map<SymbolicName, UserFunction> userFunctions = new HashMap<>(); |
| 72 |
private final Map<String, IntHashMap<Location>> locationMap = new HashMap<>(); |
| 73 |
private final Map<Integer, Component> componentIdMap = new HashMap<>(); |
| 74 |
private final Map<Component, String> externalReferences = new HashMap<>(); |
| 75 |
private String relocatableBase = null; |
| 76 |
private NestedIntegerValue originalVersion = null; |
| 77 |
|
| 78 |
public PackageLoaderHE(Configuration config) { |
| 79 |
this.config = config; |
| 80 |
overriding = new ExecutableFunctionLibrary(config); |
| 81 |
underriding = new ExecutableFunctionLibrary(config); |
| 82 |
try { |
| 83 |
parser = config.newExpressionParser("XP", false, 31); |
| 84 |
QNameParser qNameParser = new QNameParser(null); |
| 85 |
qNameParser.setAcceptEQName(true); |
| 86 |
parser.setQNameParser(qNameParser); |
| 87 |
} catch (XPathException e) { |
| 88 |
throw new AssertionError(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public static void processAccumulatorList(PackageLoaderHE loader, SourceDocument inst, String accumulatorNames) { |
| 93 |
if (accumulatorNames != null) { |
| 94 |
final List<StructuredQName> accNameList = new ArrayList<>(); |
| 95 |
StringTokenizer tokenizer = new StringTokenizer(accumulatorNames); |
| 96 |
while (tokenizer.hasMoreTokens()) { |
| 97 |
String token = tokenizer.nextToken(); |
| 98 |
StructuredQName name = StructuredQName.fromEQName(token); |
| 99 |
accNameList.add(name); |
| 100 |
} |
| 101 |
final StylesheetPackage pack = loader.getPackStack().peek(); |
| 102 |
loader.addCompletionAction(() -> { |
| 103 |
Set<Accumulator> list = new HashSet<>(); |
| 104 |
for (StructuredQName sn : accNameList) { |
| 105 |
for (Accumulator test : pack.getAccumulatorRegistry().getAllAccumulators()) { |
| 106 |
if (test.getAccumulatorName().equals(sn)) { |
| 107 |
list.add(test); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
inst.setUsedAccumulators(list); |
| 112 |
}); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public Configuration getConfiguration() { |
| 117 |
return config; |
| 118 |
} |
| 119 |
|
| 120 |
public StylesheetPackage getPackage() { |
| 121 |
return packStack.get(0); |
| 122 |
} |
| 123 |
|
| 124 |
public Stack<StylesheetPackage> getPackStack() { |
| 125 |
return packStack; |
| 126 |
} |
| 127 |
|
| 128 |
public void addCompletionAction(Action action) { |
| 129 |
completionActions.add(action); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public StylesheetPackage loadPackage(Source source) throws XPathException { |
| 134 |
|
| 135 |
ParseOptions options = new ParseOptions(); |
| 136 |
options.setSpaceStrippingRule(AllElementsSpaceStrippingRule.getInstance()); |
| 137 |
options.setSchemaValidationMode(Validation.SKIP); |
| 138 |
|
| 139 |
final List<ProxyReceiver> filters = new ArrayList<>(1); |
| 140 |
FilterFactory checksumFactory = new FilterFactory() { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public ProxyReceiver makeFilter(Receiver next) { |
| 148 |
CheckSumFilter filter = new CheckSumFilter(next); |
| 149 |
filter.setCheckExistingChecksum(true); |
| 150 |
filters.add(filter); |
| 151 |
return filter; |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
options.addFilter(checksumFactory); |
| 156 |
|
| 157 |
NodeInfo doc = config.buildDocumentTree(source, options).getRootNode(); |
| 158 |
|
| 159 |
CheckSumFilter csf = (CheckSumFilter) filters.get(0); |
| 160 |
if (!csf.isChecksumCorrect()) { |
| 161 |
throw new XPathException("Package cannot be loaded: incorrect checksum", SaxonErrorCode.SXPK0002); |
| 162 |
} |
| 163 |
return loadPackageDoc(doc); |
| 164 |
} |
| 165 |
|
| 166 |
public StylesheetPackage loadPackageDoc(NodeInfo doc) throws XPathException { |
| 167 |
|
| 168 |
StylesheetPackage pack = config.makeStylesheetPackage(); |
| 169 |
pack.setRuleManager(new RuleManager(pack)); |
| 170 |
pack.setCharacterMapIndex(new CharacterMapIndex()); |
| 171 |
pack.setJustInTimeCompilation(false); |
| 172 |
packStack.push(pack); |
| 173 |
NodeInfo packageElement = doc.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 174 |
if (!packageElement.getURI().equals(NamespaceConstant.SAXON_XSLT_EXPORT)) { |
| 175 |
throw new XPathException("Incorrect namespace for XSLT export file", SaxonErrorCode.SXPK0002); |
| 176 |
} |
| 177 |
if (!packageElement.getLocalPart().equals("package")) { |
| 178 |
throw new XPathException("Outermost element of XSLT export file must be 'package'", SaxonErrorCode.SXPK0002); |
| 179 |
} |
| 180 |
String saxonVersionAtt = packageElement.getAttributeValue("", "saxonVersion"); |
| 181 |
if (saxonVersionAtt == null) { |
| 182 |
saxonVersionAtt = "9.8.0.1"; |
| 183 |
} |
| 184 |
originalVersion = NestedIntegerValue.parse(saxonVersionAtt); |
| 185 |
String dmk = packageElement.getAttributeValue("", "dmk"); |
| 186 |
if (dmk != null) { |
| 187 |
int licenseId = config.registerLocalLicense(dmk); |
| 188 |
pack.setLocalLicenseId(licenseId); |
| 189 |
} |
| 190 |
|
| 191 |
loadPackageElement(packageElement, pack); |
| 192 |
|
| 193 |
for (Map.Entry<Component, String> entry : externalReferences.entrySet()) { |
| 194 |
Component comp = entry.getKey(); |
| 195 |
StringTokenizer tokenizer = new StringTokenizer(entry.getValue()); |
| 196 |
while (tokenizer.hasMoreTokens()) { |
| 197 |
String token = tokenizer.nextToken(); |
| 198 |
int target = Integer.parseInt(token); |
| 199 |
Component targetComponent = componentIdMap.get(target); |
| 200 |
if (targetComponent == null) { |
| 201 |
throw new XPathException("Unresolved external reference to component " + target); |
| 202 |
} |
| 203 |
comp.getComponentBindings().add(new ComponentBinding(targetComponent.getActor().getSymbolicName(), targetComponent)); |
| 204 |
} |
| 205 |
} |
| 206 |
return pack; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
public void needsPELicense(String name) { |
| 212 |
int localLicenseId = getPackage().getLocalLicenseId(); |
| 213 |
config.checkLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION, name, localLicenseId); |
| 214 |
} |
| 215 |
|
| 216 |
public void needsEELicense(String name) { |
| 217 |
int localLicenseId = getPackage().getLocalLicenseId(); |
| 218 |
config.checkLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT, name, localLicenseId); |
| 219 |
} |
| 220 |
|
| 221 |
public void loadPackageElement(NodeInfo packageElement, StylesheetPackage pack) throws XPathException { |
| 222 |
|
| 223 |
fixups.push(new ArrayList<>()); |
| 224 |
String packageName = packageElement.getAttributeValue("", "name"); |
| 225 |
String packageId = packageElement.getAttributeValue("", "id"); |
| 226 |
String packageKey = packageId == null ? packageName : packageId; |
| 227 |
boolean relocatable = "true".equals(packageElement.getAttributeValue("", "relocatable")); |
| 228 |
if (packageName != null) { |
| 229 |
pack.setPackageName(packageName); |
| 230 |
allPackages.put(packageKey, pack); |
| 231 |
} |
| 232 |
String implicitAtt = packageElement.getAttributeValue("", "implicit"); |
| 233 |
if (implicitAtt != null) { |
| 234 |
pack.setImplicitPackage(implicitAtt.equals("true")); |
| 235 |
} else { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
pack.setImplicitPackage(originalVersion.compareTo(SAXON9911) <= 0); |
| 240 |
} |
| 241 |
pack.setPackageVersion( |
| 242 |
new PackageVersion(packageElement.getAttributeValue("", "packageVersion"))); |
| 243 |
pack.setVersion(getIntegerAttribute(packageElement, "version")); |
| 244 |
pack.setSchemaAware("1".equals(packageElement.getAttributeValue("", "schemaAware"))); |
| 245 |
if (pack.isSchemaAware()) { |
| 246 |
needsEELicense("schema-awareness"); |
| 247 |
} |
| 248 |
pack.setStripsTypeAnnotations("1".equals(packageElement.getAttributeValue("", "stripType"))); |
| 249 |
pack.setKeyManager(new KeyManager(pack.getConfiguration(), pack)); |
| 250 |
pack.setDeclaredModes("1".equals(packageElement.getAttributeValue("", "declaredModes"))); |
| 251 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 252 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "package", config.getNamePool())); |
| 253 |
iterator.forEachOrFail(usePack -> { |
| 254 |
StylesheetPackage subPack = config.makeStylesheetPackage(); |
| 255 |
subPack.setRuleManager(new RuleManager(pack)); |
| 256 |
subPack.setCharacterMapIndex(new CharacterMapIndex()); |
| 257 |
subPack.setJustInTimeCompilation(false); |
| 258 |
packStack.push(subPack); |
| 259 |
loadPackageElement(usePack, subPack); |
| 260 |
packStack.pop(); |
| 261 |
pack.addUsedPackage(subPack); |
| 262 |
}); |
| 263 |
|
| 264 |
FunctionLibraryList functionLibrary = new FunctionLibraryList(); |
| 265 |
functionLibrary.addFunctionLibrary(config.getXSLT30FunctionSet()); |
| 266 |
functionLibrary.addFunctionLibrary(MapFunctionSet.getInstance()); |
| 267 |
functionLibrary.addFunctionLibrary(ArrayFunctionSet.getInstance()); |
| 268 |
functionLibrary.addFunctionLibrary(MathFunctionSet.getInstance()); |
| 269 |
|
| 270 |
functionLibrary.addFunctionLibrary(new StylesheetFunctionLibrary(pack, true)); |
| 271 |
|
| 272 |
functionLibrary.addFunctionLibrary(new ConstructorFunctionLibrary(config)); |
| 273 |
|
| 274 |
XQueryFunctionLibrary queryFunctions = new XQueryFunctionLibrary(config); |
| 275 |
functionLibrary.addFunctionLibrary(queryFunctions); |
| 276 |
functionLibrary.addFunctionLibrary(config.getIntegratedFunctionLibrary()); |
| 277 |
config.addExtensionBinders(functionLibrary); |
| 278 |
|
| 279 |
functionLibrary.addFunctionLibrary(new StylesheetFunctionLibrary(pack, false)); |
| 280 |
|
| 281 |
pack.setFunctionLibraryDetails(functionLibrary, overriding, underriding); |
| 282 |
|
| 283 |
RetainedStaticContext rsc = new RetainedStaticContext(config); |
| 284 |
if (relocatable) { |
| 285 |
|
| 286 |
relocatableBase = packageElement.getBaseURI(); |
| 287 |
rsc.setStaticBaseUriString(relocatableBase); |
| 288 |
} |
| 289 |
rsc.setPackageData(pack); |
| 290 |
contextStack.push(rsc); |
| 291 |
localBindings = new Stack<>(); |
| 292 |
|
| 293 |
readGlobalContext(packageElement); |
| 294 |
readSchemaNamespaces(packageElement); |
| 295 |
readKeys(packageElement); |
| 296 |
readComponents(packageElement, false); |
| 297 |
NodeInfo overridden = packageElement.iterateAxis(AxisInfo.CHILD, |
| 298 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "overridden", config.getNamePool())).next(); |
| 299 |
if (overridden != null) { |
| 300 |
readComponents(overridden, true); |
| 301 |
} |
| 302 |
readAccumulators(packageElement); |
| 303 |
readOutputProperties(packageElement); |
| 304 |
readCharacterMaps(packageElement); |
| 305 |
readSpaceStrippingRules(packageElement); |
| 306 |
readDecimalFormats(packageElement); |
| 307 |
resolveFixups(); |
| 308 |
fixups.pop(); |
| 309 |
for (Action a : completionActions) { |
| 310 |
a.doAction(); |
| 311 |
} |
| 312 |
|
| 313 |
StructuredQName defaultModeName = getQNameAttribute(packageElement, "defaultMode"); |
| 314 |
if (defaultModeName == null) { |
| 315 |
pack.setDefaultMode(Mode.UNNAMED_MODE_NAME); |
| 316 |
} else { |
| 317 |
pack.setDefaultMode(defaultModeName); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private void readGlobalContext(NodeInfo packageElement) throws XPathException { |
| 322 |
final GlobalContextRequirement req = new GlobalContextRequirement(); |
| 323 |
packStack.peek().setContextItemRequirements(req); |
| 324 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 325 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "glob", config.getNamePool())); |
| 326 |
iterator.forEachOrFail(varElement -> { |
| 327 |
String use = varElement.getAttributeValue("", "use"); |
| 328 |
if ("opt".equals(use)) { |
| 329 |
req.setMayBeOmitted(true); |
| 330 |
req.setAbsentFocus(false); |
| 331 |
} else if ("pro".equals(use)) { |
| 332 |
req.setMayBeOmitted(true); |
| 333 |
req.setAbsentFocus(true); |
| 334 |
} else if ("req".equals(use)) { |
| 335 |
req.setMayBeOmitted(false); |
| 336 |
req.setAbsentFocus(false); |
| 337 |
} |
| 338 |
ItemType requiredType = parseItemTypeAttribute(varElement, "type"); |
| 339 |
if (requiredType != null) { |
| 340 |
req.addRequiredItemType(requiredType); |
| 341 |
} |
| 342 |
}); |
| 343 |
} |
| 344 |
|
| 345 |
protected void readSchemaNamespaces(NodeInfo packageElement) throws XPathException { |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
private void readKeys(NodeInfo packageElement) throws XPathException { |
| 350 |
StylesheetPackage pack = packStack.peek(); |
| 351 |
NodeInfo keyElement; |
| 352 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 353 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "key", config.getNamePool())); |
| 354 |
while ((keyElement = iterator.next()) != null) { |
| 355 |
StructuredQName keyName = getQNameAttribute(keyElement, "name"); |
| 356 |
SymbolicName symbol = new SymbolicName(StandardNames.XSL_KEY, keyName); |
| 357 |
|
| 358 |
String flags = keyElement.getAttributeValue("", "flags"); |
| 359 |
boolean backwards = flags != null && flags.contains("b"); |
| 360 |
boolean range = flags != null && flags.contains("r"); |
| 361 |
boolean reusable = flags != null && flags.contains("u"); |
| 362 |
boolean composite = flags != null && flags.contains("c"); |
| 363 |
Pattern match = getFirstChildPattern(keyElement); |
| 364 |
Expression use = getSecondChildExpression(keyElement); |
| 365 |
String collationName = keyElement.getAttributeValue("", "collation"); |
| 366 |
if (collationName == null) { |
| 367 |
collationName = NamespaceConstant.CODEPOINT_COLLATION_URI; |
| 368 |
} |
| 369 |
StringCollator collation = config.getCollation(collationName); |
| 370 |
KeyDefinition keyDefinition = new KeyDefinition(symbol, match, use, collationName, collation); |
| 371 |
int slots = getIntegerAttribute(keyElement, "slots"); |
| 372 |
if (slots != Integer.MIN_VALUE) { |
| 373 |
keyDefinition.setStackFrameMap(new SlotManager(slots)); |
| 374 |
} |
| 375 |
String binds = keyElement.getAttributeValue("", "binds"); |
| 376 |
Component keyComponent = keyDefinition.makeDeclaringComponent(Visibility.PRIVATE, pack); |
| 377 |
externalReferences.put(keyComponent, binds); |
| 378 |
if (backwards) { |
| 379 |
keyDefinition.setBackwardsCompatible(true); |
| 380 |
} |
| 381 |
if (range) { |
| 382 |
keyDefinition.setRangeKey(true); |
| 383 |
} |
| 384 |
if (composite) { |
| 385 |
keyDefinition.setComposite(true); |
| 386 |
} |
| 387 |
pack.getKeyManager().addKeyDefinition(keyName, keyDefinition, reusable, pack.getConfiguration()); |
| 388 |
pack.addComponent(keyComponent); |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
private void readComponents(NodeInfo packageElement, boolean overridden) throws XPathException { |
| 394 |
StylesheetPackage pack = packStack.peek(); |
| 395 |
NodeInfo child; |
| 396 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 397 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "co", config.getNamePool())); |
| 398 |
while ((child = iterator.next()) != null) { |
| 399 |
int id = getIntegerAttribute(child, "id"); |
| 400 |
String visAtt = child.getAttributeValue("", "vis"); |
| 401 |
Visibility vis = visAtt == null ? Visibility.PRIVATE : Visibility.valueOf(visAtt); |
| 402 |
String binds = child.getAttributeValue("", "binds"); |
| 403 |
String dPackKey = child.getAttributeValue("", "dpack"); |
| 404 |
StylesheetPackage declaringPackage; |
| 405 |
if (dPackKey == null) { |
| 406 |
declaringPackage = pack; |
| 407 |
} else if (allPackages.containsKey(dPackKey)) { |
| 408 |
declaringPackage = allPackages.get(dPackKey); |
| 409 |
} else { |
| 410 |
declaringPackage = config.makeStylesheetPackage(); |
| 411 |
declaringPackage.setPackageName(dPackKey); |
| 412 |
declaringPackage.setTargetEdition(config.getEditionCode()); |
| 413 |
declaringPackage.setJustInTimeCompilation(false); |
| 414 |
} |
| 415 |
Component component; |
| 416 |
int base = getIntegerAttribute(child, "base"); |
| 417 |
if (base != Integer.MIN_VALUE) { |
| 418 |
|
| 419 |
Component baseComponent = componentIdMap.get(base); |
| 420 |
if (baseComponent == null) { |
| 421 |
throw new AssertionError(base+""); |
| 422 |
} |
| 423 |
component = Component.makeComponent(baseComponent.getActor(), vis, VisibilityProvenance.EXPLICIT, pack, declaringPackage); |
| 424 |
component.setBaseComponent(baseComponent); |
| 425 |
if (component instanceof Component.M) { |
| 426 |
|
| 427 |
pack.getRuleManager().obtainMode(baseComponent.getActor().getObjectName(), true); |
| 428 |
} |
| 429 |
} else { |
| 430 |
NodeInfo grandchild = child.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 431 |
Actor cc = null; |
| 432 |
String kind = grandchild.getLocalPart(); |
| 433 |
boolean codeGen = false; |
| 434 |
switch (kind) { |
| 435 |
case "template": |
| 436 |
cc = readNamedTemplate(grandchild); |
| 437 |
codeGen = true; |
| 438 |
break; |
| 439 |
case "globalVariable": |
| 440 |
cc = readGlobalVariable(grandchild); |
| 441 |
codeGen = true; |
| 442 |
break; |
| 443 |
case "globalParam": |
| 444 |
cc = readGlobalParam(grandchild); |
| 445 |
break; |
| 446 |
case "function": |
| 447 |
cc = readGlobalFunction(grandchild); |
| 448 |
codeGen = ((UserFunction)cc).getDeclaredStreamability() == FunctionStreamability.UNCLASSIFIED; |
| 449 |
break; |
| 450 |
case "mode": |
| 451 |
cc = readMode(grandchild); |
| 452 |
break; |
| 453 |
case "attributeSet": |
| 454 |
cc = readAttributeSet(grandchild); |
| 455 |
break; |
| 456 |
default: |
| 457 |
throw new XPathException("unknown component kind " + kind); |
| 458 |
} |
| 459 |
VisibilityProvenance provenance = visAtt == null ? VisibilityProvenance.DEFAULTED : VisibilityProvenance.EXPLICIT; |
| 460 |
component = Component.makeComponent(cc, vis, provenance, pack, declaringPackage); |
| 461 |
cc.setDeclaringComponent(component); |
| 462 |
cc.setDeclaredVisibility(vis); |
| 463 |
Optimizer optimizer = config.obtainOptimizer(); |
| 464 |
StructuredQName name = cc.getObjectName(); |
| 465 |
int evaluationModes = Expression.ITERATE_METHOD | Expression.PROCESS_METHOD; |
| 466 |
if (codeGen) { |
| 467 |
String objectName = name == null ? ("h" + component.hashCode()) : name.getLocalPart(); |
| 468 |
cc.setBody(optimizer.makeByteCodeCandidate(cc, cc.getBody(), objectName, evaluationModes)); |
| 469 |
optimizer.injectByteCodeCandidates(cc.getBody()); |
| 470 |
} else if (cc instanceof Mode) { |
| 471 |
((Mode)cc).processRules(rule -> { |
| 472 |
TemplateRule tr = (TemplateRule)rule.getAction(); |
| 473 |
String objectName = "match=\"" + tr.getMatchPattern() + '"'; |
| 474 |
tr.setBody(optimizer.makeByteCodeCandidate(tr, tr.getBody(), objectName, evaluationModes)); |
| 475 |
optimizer.injectByteCodeCandidates(tr.getBody()); |
| 476 |
}); |
| 477 |
} |
| 478 |
} |
| 479 |
externalReferences.put(component, binds); |
| 480 |
componentIdMap.put(id, component); |
| 481 |
if (overridden) { |
| 482 |
pack.addOverriddenComponent(component); |
| 483 |
} else { |
| 484 |
if (component.getVisibility() == Visibility.HIDDEN) { |
| 485 |
pack.addHiddenComponent(component); |
| 486 |
} else { |
| 487 |
pack.addComponent(component); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
private GlobalVariable readGlobalVariable(NodeInfo varElement) throws XPathException { |
| 494 |
StylesheetPackage pack = packStack.peek(); |
| 495 |
StructuredQName variableName = getQNameAttribute(varElement, "name"); |
| 496 |
GlobalVariable var = new GlobalVariable(); |
| 497 |
var.setVariableQName(variableName); |
| 498 |
var.setPackageData(pack); |
| 499 |
var.setRequiredType(parseSequenceType(varElement, "type")); |
| 500 |
String flags = varElement.getAttributeValue("", "flags"); |
| 501 |
if (flags != null) { |
| 502 |
if (flags.contains("a")) { |
| 503 |
var.setAssignable(true); |
| 504 |
} |
| 505 |
if (flags.contains("x")) { |
| 506 |
var.setIndexedVariable(); |
| 507 |
} |
| 508 |
if (flags.contains("r")) { |
| 509 |
var.setRequiredParam(true); |
| 510 |
} |
| 511 |
} |
| 512 |
int slots = getIntegerAttribute(varElement, "slots"); |
| 513 |
if (slots > 0) { |
| 514 |
var.setContainsLocals(new SlotManager(slots)); |
| 515 |
} |
| 516 |
NodeInfo bodyElement = varElement.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 517 |
if (bodyElement == null) { |
| 518 |
var.setSelectExpression(Literal.makeEmptySequence()); |
| 519 |
} else { |
| 520 |
Expression body = loadExpression(bodyElement); |
| 521 |
var.setSelectExpression(body); |
| 522 |
RetainedStaticContext rsc = body.getRetainedStaticContext(); |
| 523 |
body.setRetainedStaticContext(rsc); |
| 524 |
} |
| 525 |
|
| 526 |
pack.addGlobalVariable(var); |
| 527 |
return var; |
| 528 |
} |
| 529 |
|
| 530 |
private GlobalParam readGlobalParam(NodeInfo varElement) throws XPathException { |
| 531 |
StylesheetPackage pack = packStack.peek(); |
| 532 |
StructuredQName variableName = getQNameAttribute(varElement, "name"); |
| 533 |
|
| 534 |
localBindings = new Stack<>(); |
| 535 |
GlobalParam var = new GlobalParam(); |
| 536 |
var.setVariableQName(variableName); |
| 537 |
var.setPackageData(pack); |
| 538 |
var.setRequiredType(parseSequenceType(varElement, "type")); |
| 539 |
String flags = varElement.getAttributeValue("", "flags"); |
| 540 |
if (flags != null) { |
| 541 |
if (flags.contains("a")) { |
| 542 |
var.setAssignable(true); |
| 543 |
} |
| 544 |
if (flags.contains("x")) { |
| 545 |
var.setIndexedVariable(); |
| 546 |
} |
| 547 |
if (flags.contains("r")) { |
| 548 |
var.setRequiredParam(true); |
| 549 |
} |
| 550 |
if (flags.contains("i")) { |
| 551 |
var.setImplicitlyRequiredParam(true); |
| 552 |
} |
| 553 |
} |
| 554 |
int slots = getIntegerAttribute(varElement, "slots"); |
| 555 |
if (slots > 0) { |
| 556 |
var.setContainsLocals(new SlotManager(slots)); |
| 557 |
} |
| 558 |
NodeInfo bodyElement = varElement.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 559 |
if (bodyElement == null) { |
| 560 |
var.setSelectExpression(Literal.makeEmptySequence()); |
| 561 |
} else { |
| 562 |
Expression body = loadExpression(bodyElement); |
| 563 |
var.setSelectExpression(body); |
| 564 |
RetainedStaticContext rsc = body.getRetainedStaticContext(); |
| 565 |
body.setRetainedStaticContext(rsc); |
| 566 |
} |
| 567 |
return var; |
| 568 |
} |
| 569 |
|
| 570 |
private NamedTemplate readNamedTemplate(NodeInfo templateElement) throws XPathException { |
| 571 |
StylesheetPackage pack = packStack.peek(); |
| 572 |
localBindings = new Stack<>(); |
| 573 |
StructuredQName templateName = getQNameAttribute(templateElement, "name"); |
| 574 |
String flags = templateElement.getAttributeValue("", "flags"); |
| 575 |
int slots = getIntegerAttribute(templateElement, "slots"); |
| 576 |
SequenceType contextType = parseSequenceType(templateElement, "cxt"); |
| 577 |
ItemType contextItemType = contextType == null ? AnyItemType.getInstance() : contextType.getPrimaryType(); |
| 578 |
|
| 579 |
NamedTemplate template = new NamedTemplate(templateName); |
| 580 |
template.setStackFrameMap(new SlotManager(slots)); |
| 581 |
template.setPackageData(pack); |
| 582 |
template.setRequiredType(parseSequenceType(templateElement, "as")); |
| 583 |
template.setContextItemRequirements(contextItemType, flags.contains("o"), !flags.contains("s")); |
| 584 |
NodeInfo bodyElement = getChildWithRole(templateElement, "body"); |
| 585 |
if (bodyElement == null) { |
| 586 |
template.setBody(Literal.makeEmptySequence()); |
| 587 |
} else { |
| 588 |
Expression body = loadExpression(bodyElement); |
| 589 |
template.setBody(body); |
| 590 |
RetainedStaticContext rsc = body.getRetainedStaticContext(); |
| 591 |
body.setRetainedStaticContext(rsc); |
| 592 |
} |
| 593 |
return template; |
| 594 |
} |
| 595 |
|
| 596 |
private UserFunction readGlobalFunction(NodeInfo functionElement) throws XPathException { |
| 597 |
localBindings = new Stack<>(); |
| 598 |
UserFunction function = readFunction(functionElement); |
| 599 |
userFunctions.put(function.getSymbolicName(), function); |
| 600 |
underriding.addFunction(function); |
| 601 |
return function; |
| 602 |
} |
| 603 |
|
| 604 |
private UserFunction currentFunction; |
| 605 |
|
| 606 |
public UserFunction readFunction(NodeInfo functionElement) throws XPathException { |
| 607 |
StylesheetPackage pack = packStack.peek(); |
| 608 |
StructuredQName functionName = getQNameAttribute(functionElement, "name"); |
| 609 |
int slots = getIntegerAttribute(functionElement, "slots"); |
| 610 |
String flags = functionElement.getAttributeValue("", "flags"); |
| 611 |
if (flags == null) { |
| 612 |
flags = ""; |
| 613 |
} |
| 614 |
final UserFunction function = makeFunction(flags); |
| 615 |
function.setFunctionName(functionName); |
| 616 |
function.setStackFrameMap(new SlotManager(slots)); |
| 617 |
function.setPackageData(pack); |
| 618 |
function.setRetainedStaticContext(makeRetainedStaticContext(functionElement)); |
| 619 |
function.setResultType(parseSequenceType(functionElement, "as")); |
| 620 |
function.setDeclaredStreamability(FunctionStreamability.UNCLASSIFIED); |
| 621 |
function.incrementReferenceCount(); |
| 622 |
int evalMode = getIntegerAttribute(functionElement, "eval"); |
| 623 |
if (evalMode == Integer.MIN_VALUE) { |
| 624 |
evalMode = ExpressionTool.UNDECIDED; |
| 625 |
} |
| 626 |
|
| 627 |
if (flags.contains("p")) { |
| 628 |
function.setDeterminism(UserFunction.Determinism.PROACTIVE); |
| 629 |
} else if (flags.contains("e")) { |
| 630 |
function.setDeterminism(UserFunction.Determinism.ELIDABLE); |
| 631 |
} else if (flags.contains("d")) { |
| 632 |
function.setDeterminism(UserFunction.Determinism.DETERMINISTIC); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
boolean streaming = false; |
| 637 |
if (flags.contains("U")) { |
| 638 |
function.setDeclaredStreamability(FunctionStreamability.UNCLASSIFIED); |
| 639 |
} else if (flags.contains("A")) { |
| 640 |
function.setDeclaredStreamability(FunctionStreamability.ABSORBING); |
| 641 |
streaming = true; |
| 642 |
} else if (flags.contains("I")) { |
| 643 |
function.setDeclaredStreamability(FunctionStreamability.INSPECTION); |
| 644 |
streaming = true; |
| 645 |
} else if (flags.contains("F")) { |
| 646 |
function.setDeclaredStreamability(FunctionStreamability.FILTER); |
| 647 |
streaming = true; |
| 648 |
} else if (flags.contains("S")) { |
| 649 |
function.setDeclaredStreamability(FunctionStreamability.SHALLOW_DESCENT); |
| 650 |
streaming = true; |
| 651 |
} else if (flags.contains("D")) { |
| 652 |
function.setDeclaredStreamability(FunctionStreamability.DEEP_DESCENT); |
| 653 |
streaming = true; |
| 654 |
} else if (flags.contains("C")) { |
| 655 |
function.setDeclaredStreamability(FunctionStreamability.ASCENT); |
| 656 |
streaming = true; |
| 657 |
} |
| 658 |
|
| 659 |
function.setEvaluationMode(evalMode); |
| 660 |
|
| 661 |
currentFunction = function; |
| 662 |
List<UserFunctionParameter> params = new ArrayList<>(); |
| 663 |
AxisIterator argIterator = functionElement.iterateAxis(AxisInfo.CHILD, |
| 664 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "arg", config.getNamePool())); |
| 665 |
NodeInfo argElement; |
| 666 |
int slot = 0; |
| 667 |
while ((argElement = argIterator.next()) != null) { |
| 668 |
UserFunctionParameter arg = new UserFunctionParameter(); |
| 669 |
arg.setVariableQName(getQNameAttribute(argElement, "name")); |
| 670 |
arg.setRequiredType(parseSequenceType(argElement, "as")); |
| 671 |
arg.setSlotNumber(slot++); |
| 672 |
params.add(arg); |
| 673 |
localBindings.push(arg); |
| 674 |
} |
| 675 |
function.setParameterDefinitions(params.toArray(new UserFunctionParameter[0])); |
| 676 |
if (streaming) { |
| 677 |
params.get(0).setFunctionStreamability(function.getDeclaredStreamability()); |
| 678 |
} |
| 679 |
NodeInfo bodyElement = getChildWithRole(functionElement, "body"); |
| 680 |
if (bodyElement == null) { |
| 681 |
function.setBody(Literal.makeEmptySequence()); |
| 682 |
} else { |
| 683 |
Expression body = loadExpression(bodyElement); |
| 684 |
function.setBody(body); |
| 685 |
RetainedStaticContext rsc = body.getRetainedStaticContext(); |
| 686 |
body.setRetainedStaticContext(rsc); |
| 687 |
} |
| 688 |
|
| 689 |
for (int i = 0; i < function.getArity(); i++) { |
| 690 |
localBindings.pop(); |
| 691 |
} |
| 692 |
if (function.getDeclaredStreamability() != FunctionStreamability.UNCLASSIFIED) { |
| 693 |
addCompletionAction(function::prepareForStreaming); |
| 694 |
} |
| 695 |
return function; |
| 696 |
} |
| 697 |
|
| 698 |
protected UserFunction makeFunction(String flags) { |
| 699 |
return new UserFunction(); |
| 700 |
} |
| 701 |
|
| 702 |
private AttributeSet readAttributeSet(NodeInfo aSetElement) throws XPathException { |
| 703 |
StylesheetPackage pack = packStack.peek(); |
| 704 |
localBindings = new Stack<>(); |
| 705 |
StructuredQName aSetName = getQNameAttribute(aSetElement, "name"); |
| 706 |
int slots = getIntegerAttribute(aSetElement, "slots"); |
| 707 |
|
| 708 |
|
| 709 |
AttributeSet aSet = new AttributeSet(); |
| 710 |
aSet.setName(aSetName); |
| 711 |
aSet.setStackFrameMap(new SlotManager(slots)); |
| 712 |
aSet.setPackageData(pack); |
| 713 |
aSet.setBody(getFirstChildExpression(aSetElement)); |
| 714 |
aSet.setDeclaredStreamable("s".equals(aSetElement.getAttributeValue("", "flags"))); |
| 715 |
|
| 716 |
return aSet; |
| 717 |
|
| 718 |
} |
| 719 |
|
| 720 |
private Mode readMode(NodeInfo modeElement) throws XPathException { |
| 721 |
final StylesheetPackage pack = packStack.peek(); |
| 722 |
StructuredQName modeName = getQNameAttribute(modeElement, "name"); |
| 723 |
if (modeName == null) { |
| 724 |
modeName = Mode.UNNAMED_MODE_NAME; |
| 725 |
} |
| 726 |
final SimpleMode mode = (SimpleMode) pack.getRuleManager().obtainMode(modeName, true); |
| 727 |
|
| 728 |
int patternSlots = getIntegerAttribute(modeElement, "patternSlots"); |
| 729 |
mode.allocatePatternSlots(patternSlots); |
| 730 |
|
| 731 |
String onNoMatch = modeElement.getAttributeValue("", "onNo"); |
| 732 |
BuiltInRuleSet base; |
| 733 |
if (onNoMatch != null) { |
| 734 |
base = mode.getBuiltInRuleSetForCode(onNoMatch); |
| 735 |
mode.setBuiltInRuleSet(base); |
| 736 |
} |
| 737 |
|
| 738 |
String flags = modeElement.getAttributeValue("", "flags"); |
| 739 |
if (flags != null) { |
| 740 |
mode.setStreamable(flags.contains("s")); |
| 741 |
if (flags.contains("t")) { |
| 742 |
mode.setExplicitProperty("typed", "yes", 1); |
| 743 |
} |
| 744 |
if (flags.contains("u")) { |
| 745 |
mode.setExplicitProperty("typed", "no", 1); |
| 746 |
} |
| 747 |
if (flags.contains("F")) { |
| 748 |
mode.setRecoveryPolicy(Configuration.DO_NOT_RECOVER); |
| 749 |
} |
| 750 |
if (flags.contains("W")) { |
| 751 |
mode.setRecoveryPolicy(Configuration.RECOVER_WITH_WARNINGS); |
| 752 |
} |
| 753 |
if (flags.contains("e")) { |
| 754 |
mode.setHasRules(false); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
final List<StructuredQName> accNames = getListOfQNameAttribute(modeElement, "useAcc"); |
| 759 |
addCompletionAction(() -> { |
| 760 |
AccumulatorRegistry registry = pack.getAccumulatorRegistry(); |
| 761 |
Set<Accumulator> accumulators = new HashSet<>(); |
| 762 |
for (StructuredQName qn : accNames) { |
| 763 |
Accumulator acc = registry.getAccumulator(qn); |
| 764 |
accumulators.add(acc); |
| 765 |
|
| 766 |
} |
| 767 |
mode.setAccumulators(accumulators); |
| 768 |
}); |
| 769 |
|
| 770 |
AxisIterator iterator2 = modeElement.iterateAxis(AxisInfo.DESCENDANT, |
| 771 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "templateRule", config.getNamePool())); |
| 772 |
NodeInfo templateRuleElement0; |
| 773 |
LinkedList<NodeInfo> ruleStack = new LinkedList<>(); |
| 774 |
while ((templateRuleElement0 = iterator2.next()) != null) { |
| 775 |
|
| 776 |
ruleStack.addFirst(templateRuleElement0); |
| 777 |
} |
| 778 |
for (NodeInfo templateRuleElement : ruleStack) { |
| 779 |
int precedence = getIntegerAttribute(templateRuleElement, "prec"); |
| 780 |
int rank = getIntegerAttribute(templateRuleElement, "rank"); |
| 781 |
String priorityAtt = templateRuleElement.getAttributeValue("", "prio"); |
| 782 |
double priority = Double.parseDouble(priorityAtt); |
| 783 |
int sequence = getIntegerAttribute(templateRuleElement, "seq"); |
| 784 |
int part = getIntegerAttribute(templateRuleElement, "part"); |
| 785 |
if (part == Integer.MIN_VALUE) { |
| 786 |
part = 0; |
| 787 |
} |
| 788 |
int minImportPrecedence = getIntegerAttribute(templateRuleElement, "minImp"); |
| 789 |
int slots = getIntegerAttribute(templateRuleElement, "slots"); |
| 790 |
boolean streamable = "1".equals(templateRuleElement.getAttributeValue("", "streamable")); |
| 791 |
String tflags = templateRuleElement.getAttributeValue("", "flags"); |
| 792 |
SequenceType contextType = parseSequenceType(templateRuleElement, "cxt"); |
| 793 |
ItemType contextItemType = contextType == null ? AnyItemType.getInstance() : contextType.getPrimaryType(); |
| 794 |
|
| 795 |
NodeInfo matchElement = getChildWithRole(templateRuleElement, "match"); |
| 796 |
Pattern match = loadPattern(matchElement); |
| 797 |
|
| 798 |
localBindings = new Stack<>(); |
| 799 |
TemplateRule template = config.makeTemplateRule(); |
| 800 |
template.setMatchPattern(match); |
| 801 |
template.setStackFrameMap(new SlotManager(slots)); |
| 802 |
template.setPackageData(pack); |
| 803 |
template.setRequiredType(parseSequenceType(templateRuleElement, "as")); |
| 804 |
template.setDeclaredStreamable(streamable); |
| 805 |
template.setContextItemRequirements(contextItemType, !tflags.contains("s")); |
| 806 |
NodeInfo bodyElement = getChildWithRole(templateRuleElement, "action"); |
| 807 |
if (bodyElement == null) { |
| 808 |
template.setBody(Literal.makeEmptySequence()); |
| 809 |
} else { |
| 810 |
Expression body = loadExpression(bodyElement); |
| 811 |
template.setBody(body); |
| 812 |
RetainedStaticContext rsc = body.getRetainedStaticContext(); |
| 813 |
body.setRetainedStaticContext(rsc); |
| 814 |
} |
| 815 |
Rule rule = mode.makeRule(match, template, precedence, minImportPrecedence, priority, sequence, part); |
| 816 |
rule.setRank(rank); |
| 817 |
mode.addRule(match, rule); |
| 818 |
mode.setHasRules(true); |
| 819 |
} |
| 820 |
|
| 821 |
addCompletionAction(mode::prepareStreamability); |
| 822 |
|
| 823 |
|
| 824 |
return mode; |
| 825 |
|
| 826 |
} |
| 827 |
|
| 828 |
private void readAccumulators(NodeInfo packageElement) throws XPathException { |
| 829 |
StylesheetPackage pack = packStack.peek(); |
| 830 |
NodeInfo accElement; |
| 831 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 832 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "accumulator", config.getNamePool())); |
| 833 |
while ((accElement = iterator.next()) != null) { |
| 834 |
StructuredQName accName = getQNameAttribute(accElement, "name"); |
| 835 |
Accumulator acc = new Accumulator(); |
| 836 |
Component component = Component.makeComponent(acc, Visibility.PRIVATE, VisibilityProvenance.DEFAULTED, pack, pack); |
| 837 |
acc.setDeclaringComponent(component); |
| 838 |
int iniSlots = getIntegerAttribute(accElement, "slots"); |
| 839 |
acc.setSlotManagerForInitialValueExpression(new SlotManager(iniSlots)); |
| 840 |
acc.setAccumulatorName(accName); |
| 841 |
String binds = accElement.getAttributeValue("", "binds"); |
| 842 |
externalReferences.put(component, binds); |
| 843 |
boolean streamable = "1".equals(accElement.getAttributeValue("", "streamable")); |
| 844 |
String flags = accElement.getAttributeValue("", "flags"); |
| 845 |
boolean universal = flags != null && flags.contains("u"); |
| 846 |
acc.setDeclaredStreamable(streamable); |
| 847 |
acc.setUniversallyApplicable(universal); |
| 848 |
Expression init = getExpressionWithRole(accElement, "init"); |
| 849 |
acc.setInitialValueExpression(init); |
| 850 |
NodeInfo pre = getChild(accElement, 1); |
| 851 |
readAccumulatorRules(acc, pre); |
| 852 |
NodeInfo post = getChild(accElement, 2); |
| 853 |
readAccumulatorRules(acc, post); |
| 854 |
pack.getAccumulatorRegistry().addAccumulator(acc); |
| 855 |
} |
| 856 |
|
| 857 |
} |
| 858 |
|
| 859 |
private void readAccumulatorRules(Accumulator acc, NodeInfo owner) throws XPathException { |
| 860 |
AxisIterator iterator = owner.iterateAxis(AxisInfo.CHILD, |
| 861 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "accRule", config.getNamePool())); |
| 862 |
NodeInfo accRuleElement; |
| 863 |
boolean preDescent = owner.getLocalPart().equals("pre"); |
| 864 |
SimpleMode mode = preDescent ? acc.getPreDescentRules() : acc.getPostDescentRules(); |
| 865 |
int patternSlots = getIntegerAttribute(owner, "slots"); |
| 866 |
mode.setStackFrameSlotsNeeded(patternSlots); |
| 867 |
while ((accRuleElement = iterator.next()) != null) { |
| 868 |
int slots = getIntegerAttribute(accRuleElement, "slots"); |
| 869 |
int rank = getIntegerAttribute(accRuleElement, "rank"); |
| 870 |
String flags = accRuleElement.getAttributeValue("", "flags"); |
| 871 |
SlotManager sm = new SlotManager(slots); |
| 872 |
Pattern pattern = getFirstChildPattern(accRuleElement); |
| 873 |
Expression select = getSecondChildExpression(accRuleElement); |
| 874 |
AccumulatorRule rule = new AccumulatorRule(select, sm, !preDescent); |
| 875 |
if (flags != null && flags.contains("c")) { |
| 876 |
rule.setCapturing(true); |
| 877 |
} |
| 878 |
mode.addRule(pattern, mode.makeRule(pattern, rule, rank, 0, rank, 0, 0)); |
| 879 |
} |
| 880 |
mode.computeRankings(1); |
| 881 |
} |
| 882 |
|
| 883 |
private void readOutputProperties(NodeInfo packageElement) { |
| 884 |
StylesheetPackage pack = packStack.peek(); |
| 885 |
NodeInfo outputElement; |
| 886 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 887 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "output", config.getNamePool())); |
| 888 |
while ((outputElement = iterator.next()) != null) { |
| 889 |
StructuredQName outputName = getQNameAttribute(outputElement, "name"); |
| 890 |
Properties props = new Properties(); |
| 891 |
NodeInfo propertyElement; |
| 892 |
AxisIterator iterator1 = outputElement.iterateAxis(AxisInfo.CHILD, |
| 893 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "property", config.getNamePool())); |
| 894 |
while ((propertyElement = iterator1.next()) != null) { |
| 895 |
String name = propertyElement.getAttributeValue("", "name"); |
| 896 |
String value = propertyElement.getAttributeValue("", "value"); |
| 897 |
if (name.startsWith("{http://saxon.sf.net/}") && !name.equals(SaxonOutputKeys.STYLESHEET_VERSION)) { |
| 898 |
needsPELicense("Saxon output properties"); |
| 899 |
} |
| 900 |
props.setProperty(name, value); |
| 901 |
} |
| 902 |
if (outputName == null) { |
| 903 |
pack.setDefaultOutputProperties(props); |
| 904 |
} else { |
| 905 |
pack.setNamedOutputProperties(outputName, props); |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
private void readCharacterMaps(NodeInfo packageElement) throws XPathException { |
| 911 |
StylesheetPackage pack = packStack.peek(); |
| 912 |
NodeInfo charMapElement; |
| 913 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 914 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "charMap", config.getNamePool())); |
| 915 |
while ((charMapElement = iterator.next()) != null) { |
| 916 |
StructuredQName mapName = getQNameAttribute(charMapElement, "name"); |
| 917 |
NodeInfo mappingElement; |
| 918 |
AxisIterator iterator1 = charMapElement.iterateAxis(AxisInfo.CHILD, |
| 919 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "m", config.getNamePool())); |
| 920 |
IntHashMap<String> map = new IntHashMap<>(); |
| 921 |
while ((mappingElement = iterator1.next()) != null) { |
| 922 |
int c = getIntegerAttribute(mappingElement, "c"); |
| 923 |
String s = mappingElement.getAttributeValue("", "s"); |
| 924 |
map.put(c, s); |
| 925 |
} |
| 926 |
CharacterMap characterMap = new CharacterMap(mapName, map); |
| 927 |
pack.getCharacterMapIndex().putCharacterMap(mapName, characterMap); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
private void readSpaceStrippingRules(NodeInfo packageElement) throws XPathException { |
| 932 |
StylesheetPackage pack = packStack.peek(); |
| 933 |
NodeInfo element; |
| 934 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 935 |
while ((element = iterator.next()) != null) { |
| 936 |
String s = element.getLocalPart(); |
| 937 |
switch (s) { |
| 938 |
case "strip.all": |
| 939 |
pack.setStripperRules(new AllElementsSpaceStrippingRule()); |
| 940 |
pack.setStripsWhitespace(true); |
| 941 |
break; |
| 942 |
case "strip.none": |
| 943 |
pack.setStripperRules(new NoElementsSpaceStrippingRule()); |
| 944 |
break; |
| 945 |
case "strip": |
| 946 |
AxisIterator iterator2 = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 947 |
NodeInfo element2; |
| 948 |
SelectedElementsSpaceStrippingRule rules = new SelectedElementsSpaceStrippingRule(false); |
| 949 |
while ((element2 = iterator2.next()) != null) { |
| 950 |
Stripper.StripRuleTarget which = element2.getLocalPart().equals("s") ? Stripper.STRIP : Stripper.PRESERVE; |
| 951 |
String value = element2.getAttributeValue("", "test"); |
| 952 |
NodeTest t; |
| 953 |
if (value.equals("*")) { |
| 954 |
t = NodeKindTest.ELEMENT; |
| 955 |
} else { |
| 956 |
|
| 957 |
t = (NodeTest) parseItemTypeAttribute(element2, "test"); |
| 958 |
} |
| 959 |
int prec = getIntegerAttribute(element2, "prec"); |
| 960 |
NodeTestPattern pat = new NodeTestPattern(t); |
| 961 |
rules.addRule(pat, which, prec, prec); |
| 962 |
} |
| 963 |
pack.setStripperRules(rules); |
| 964 |
pack.setStripsWhitespace(true); |
| 965 |
break; |
| 966 |
} |
| 967 |
|
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
private void readDecimalFormats(NodeInfo packageElement) throws XPathException { |
| 972 |
NodeInfo formatElement; |
| 973 |
DecimalFormatManager decimalFormatManager = packStack.peek().getDecimalFormatManager(); |
| 974 |
AxisIterator iterator = packageElement.iterateAxis(AxisInfo.CHILD, |
| 975 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "decimalFormat", config.getNamePool())); |
| 976 |
|
| 977 |
String[] propertyNames = DecimalSymbols.propertyNames; |
| 978 |
while ((formatElement = iterator.next()) != null) { |
| 979 |
StructuredQName name = getQNameAttribute(formatElement, "name"); |
| 980 |
DecimalSymbols symbols; |
| 981 |
if (name == null) { |
| 982 |
symbols = decimalFormatManager.getDefaultDecimalFormat(); |
| 983 |
} else { |
| 984 |
symbols = decimalFormatManager.obtainNamedDecimalFormat(name); |
| 985 |
} |
| 986 |
symbols.setHostLanguage(Configuration.XSLT, 31); |
| 987 |
for (String p : propertyNames) { |
| 988 |
if (formatElement.getAttributeValue("", p) != null) { |
| 989 |
switch (p) { |
| 990 |
case "NaN": |
| 991 |
symbols.setNaN(formatElement.getAttributeValue("", "NaN")); |
| 992 |
break; |
| 993 |
case "infinity": |
| 994 |
symbols.setInfinity(formatElement.getAttributeValue("", "infinity")); |
| 995 |
break; |
| 996 |
case "name": |
| 997 |
|
| 998 |
break; |
| 999 |
default: |
| 1000 |
symbols.setIntProperty(p, getIntegerAttribute(formatElement, p)); |
| 1001 |
break; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
public NodeInfo getChild(NodeInfo parent, int n) { |
| 1017 |
AxisIterator iter = parent.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 1018 |
NodeInfo node = iter.next(); |
| 1019 |
for (int i = 0; i < n; i++) { |
| 1020 |
node = iter.next(); |
| 1021 |
} |
| 1022 |
return node; |
| 1023 |
} |
| 1024 |
|
| 1025 |
public NodeInfo getChildWithRole(NodeInfo parent, String role) { |
| 1026 |
AxisIterator iter = parent.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 1027 |
NodeInfo node; |
| 1028 |
while ((node = iter.next()) != null) { |
| 1029 |
String roleAtt = node.getAttributeValue("", "role"); |
| 1030 |
if (role.equals(roleAtt)) { |
| 1031 |
return node; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
return null; |
| 1035 |
} |
| 1036 |
|
| 1037 |
public Expression getFirstChildExpression(NodeInfo parent) throws XPathException { |
| 1038 |
NodeInfo node = parent.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 1039 |
return loadExpression(node); |
| 1040 |
} |
| 1041 |
|
| 1042 |
public Expression getSecondChildExpression(NodeInfo parent) throws XPathException { |
| 1043 |
NodeInfo node = getChild(parent, 1); |
| 1044 |
return loadExpression(node); |
| 1045 |
} |
| 1046 |
|
| 1047 |
public Expression getNthChildExpression(NodeInfo parent, int n) throws XPathException { |
| 1048 |
NodeInfo node = getChild(parent, n); |
| 1049 |
return loadExpression(node); |
| 1050 |
} |
| 1051 |
|
| 1052 |
public Expression getExpressionWithRole(NodeInfo parent, String role) throws XPathException { |
| 1053 |
NodeInfo node = getChildWithRole(parent, role); |
| 1054 |
return node == null ? null : loadExpression(node); |
| 1055 |
} |
| 1056 |
|
| 1057 |
public Expression loadExpression(NodeInfo element) throws XPathException { |
| 1058 |
if (element == null) { |
| 1059 |
return null; |
| 1060 |
} |
| 1061 |
String tag = element.getLocalPart(); |
| 1062 |
ExpressionLoader loader = eMap.get(tag); |
| 1063 |
if (loader == null) { |
| 1064 |
String message = "Cannot load expression with tag " + tag; |
| 1065 |
String req = licensableConstructs.get(tag); |
| 1066 |
if (req != null) { |
| 1067 |
message += ". The stylesheet uses Saxon-" + req + " features"; |
| 1068 |
} |
| 1069 |
throw new XPathException(message, SaxonErrorCode.SXPK0002); |
| 1070 |
} else { |
| 1071 |
RetainedStaticContext rsc = makeRetainedStaticContext(element); |
| 1072 |
contextStack.push(rsc); |
| 1073 |
Expression exp = loader.loadFrom(this, element); |
| 1074 |
exp.setRetainedStaticContextLocally(rsc); |
| 1075 |
contextStack.pop(); |
| 1076 |
|
| 1077 |
exp.setLocation(makeLocation(element)); |
| 1078 |
return exp; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
private Location makeLocation(NodeInfo element) { |
| 1083 |
String lineAtt = getInheritedAttribute(element, "line"); |
| 1084 |
String moduleAtt = getInheritedAttribute(element, "module"); |
| 1085 |
if (lineAtt != null && moduleAtt != null) { |
| 1086 |
int line = Integer.parseInt(lineAtt); |
| 1087 |
return allocateLocation(moduleAtt, line); |
| 1088 |
} else { |
| 1089 |
return ExplicitLocation.UNKNOWN_LOCATION; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
public RetainedStaticContext makeRetainedStaticContext(NodeInfo element) { |
| 1094 |
StylesheetPackage pack = packStack.peek(); |
| 1095 |
String baseURIAtt = element.getAttributeValue("", "baseUri"); |
| 1096 |
String defaultCollAtt = element.getAttributeValue("", "defaultCollation"); |
| 1097 |
String defaultElementNS = element.getAttributeValue("", "defaultElementNS"); |
| 1098 |
String nsAtt = element.getAttributeValue("", "ns"); |
| 1099 |
String versionAtt = element.getAttributeValue("", "vn"); |
| 1100 |
if (baseURIAtt != null || defaultCollAtt != null || nsAtt != null || |
| 1101 |
versionAtt != null || defaultElementNS != null || |
| 1102 |
contextStack.peek().getDecimalFormatManager() == null |
| 1103 |
) { |
| 1104 |
RetainedStaticContext rsc = new RetainedStaticContext(config); |
| 1105 |
rsc.setPackageData(pack); |
| 1106 |
if (defaultCollAtt != null) { |
| 1107 |
rsc.setDefaultCollationName(defaultCollAtt); |
| 1108 |
} else { |
| 1109 |
rsc.setDefaultCollationName(NamespaceConstant.CODEPOINT_COLLATION_URI); |
| 1110 |
} |
| 1111 |
if (baseURIAtt != null) { |
| 1112 |
rsc.setStaticBaseUriString(baseURIAtt); |
| 1113 |
} else if (relocatableBase != null) { |
| 1114 |
rsc.setStaticBaseUriString(relocatableBase); |
| 1115 |
} else { |
| 1116 |
String base = Navigator.getInheritedAttributeValue(element, "", "baseUri"); |
| 1117 |
if (base != null) { |
| 1118 |
rsc.setStaticBaseUriString(base); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
if (nsAtt == null) { |
| 1122 |
nsAtt = Navigator.getInheritedAttributeValue(element, "","ns"); |
| 1123 |
} |
| 1124 |
if (nsAtt != null && !nsAtt.isEmpty()) { |
| 1125 |
String[] namespaces = nsAtt.split(" "); |
| 1126 |
for (String ns : namespaces) { |
| 1127 |
int eq = ns.indexOf('='); |
| 1128 |
if (eq < 0) { |
| 1129 |
throw new IllegalStateException("ns=" + nsAtt); |
| 1130 |
} |
| 1131 |
String prefix = ns.substring(0, eq); |
| 1132 |
String uri = ns.substring(eq + 1); |
| 1133 |
if (uri.equals("~")) { |
| 1134 |
uri = NamespaceConstant.getUriForConventionalPrefix(prefix); |
| 1135 |
} |
| 1136 |
rsc.declareNamespace(prefix, uri); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
if (defaultElementNS == null) { |
| 1140 |
defaultElementNS = Navigator.getInheritedAttributeValue(element, "", "defaultElementNS"); |
| 1141 |
} |
| 1142 |
if (defaultElementNS != null) { |
| 1143 |
rsc.setDefaultElementNamespace(defaultElementNS); |
| 1144 |
} |
| 1145 |
rsc.setDecimalFormatManager(packStack.peek().getDecimalFormatManager()); |
| 1146 |
return rsc; |
| 1147 |
} else { |
| 1148 |
return contextStack.peek(); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
private String getDefaultCollationName(NodeInfo element) { |
| 1153 |
while (element != null) { |
| 1154 |
String coll = element.getAttributeValue("", "defaultCollation"); |
| 1155 |
if (coll != null) { |
| 1156 |
return coll; |
| 1157 |
} |
| 1158 |
element = element.getParent(); |
| 1159 |
} |
| 1160 |
return NamespaceConstant.CODEPOINT_COLLATION_URI; |
| 1161 |
} |
| 1162 |
|
| 1163 |
private Pattern getFirstChildPattern(NodeInfo parent) throws XPathException { |
| 1164 |
NodeInfo node = parent.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next(); |
| 1165 |
return loadPattern(node); |
| 1166 |
} |
| 1167 |
|
| 1168 |
private Pattern getSecondChildPattern(NodeInfo parent) throws XPathException { |
| 1169 |
NodeInfo node = getChild(parent, 1); |
| 1170 |
return loadPattern(node); |
| 1171 |
} |
| 1172 |
|
| 1173 |
public Pattern getPatternWithRole(NodeInfo parent, String role) throws XPathException { |
| 1174 |
NodeInfo node = getChildWithRole(parent, role); |
| 1175 |
return node == null ? null : loadPattern(node); |
| 1176 |
} |
| 1177 |
|
| 1178 |
private Pattern loadPattern(NodeInfo element) throws XPathException { |
| 1179 |
String tag = element.getLocalPart(); |
| 1180 |
PatternLoader loader = pMap.get(tag); |
| 1181 |
if (loader == null) { |
| 1182 |
|
| 1183 |
throw new XPathException("Cannot load pattern with tag " + tag, SaxonErrorCode.SXPK0002); |
| 1184 |
} else { |
| 1185 |
Pattern pat = loader.loadFrom(this, element); |
| 1186 |
pat.setLocation(makeLocation(element)); |
| 1187 |
pat.setRetainedStaticContext(makeRetainedStaticContext(element)); |
| 1188 |
return pat; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
public SchemaType getTypeAttribute(NodeInfo element, String attName) throws XPathException { |
| 1193 |
String val = element.getAttributeValue("", attName); |
| 1194 |
if (val == null) { |
| 1195 |
return null; |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
if (val.startsWith("xs:")) { |
| 1203 |
return config.getSchemaType(new StructuredQName("xs", NamespaceConstant.SCHEMA, val.substring(3))); |
| 1204 |
} else { |
| 1205 |
StructuredQName name = getQNameAttribute(element, attName); |
| 1206 |
return config.getSchemaType(name); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
public StructuredQName getQNameAttribute(NodeInfo element, String localName) { |
| 1211 |
String val = element.getAttributeValue("", localName); |
| 1212 |
if (val == null) { |
| 1213 |
return null; |
| 1214 |
} |
| 1215 |
return StructuredQName.fromEQName(val); |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
} |
| 1231 |
|
| 1232 |
public List<StructuredQName> getListOfQNameAttribute(NodeInfo element, String localName) throws XPathException { |
| 1233 |
String val = element.getAttributeValue("", localName); |
| 1234 |
if (val == null) { |
| 1235 |
return Collections.emptyList(); |
| 1236 |
} |
| 1237 |
List<StructuredQName> result = new ArrayList<>(); |
| 1238 |
for (String s : val.split(" ")) { |
| 1239 |
StructuredQName sq = resolveQName(s, element); |
| 1240 |
result.add(sq); |
| 1241 |
} |
| 1242 |
return result; |
| 1243 |
} |
| 1244 |
|
| 1245 |
private StructuredQName resolveQName(String val, NodeInfo element) throws XPathException { |
| 1246 |
if (val.startsWith("Q{")) { |
| 1247 |
return StructuredQName.fromEQName(val); |
| 1248 |
} else if (val.contains(":")) { |
| 1249 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1250 |
return StructuredQName.fromLexicalQName(val, true, true, resolver); |
| 1251 |
} else { |
| 1252 |
return new StructuredQName("", "", val); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
public int getIntegerAttribute(NodeInfo element, String localName) throws XPathException { |
| 1266 |
String val = element.getAttributeValue("", localName); |
| 1267 |
if (val == null) { |
| 1268 |
return Integer.MIN_VALUE; |
| 1269 |
} |
| 1270 |
try { |
| 1271 |
return Integer.parseInt(val); |
| 1272 |
} catch (NumberFormatException e) { |
| 1273 |
throw new XPathException("Expected integer value for " + |
| 1274 |
element.getDisplayName() + "/" + localName + |
| 1275 |
", found '" + val + "'", SaxonErrorCode.SXPK0002); |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
public String getInheritedAttribute(NodeInfo element, String localName) { |
| 1280 |
while (element != null) { |
| 1281 |
String val = element.getAttributeValue("", localName); |
| 1282 |
if (val != null) { |
| 1283 |
return val; |
| 1284 |
} |
| 1285 |
element = element.getParent(); |
| 1286 |
} |
| 1287 |
return null; |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
public SequenceType parseSequenceType(NodeInfo element, String name) throws XPathException { |
| 1300 |
IndependentContext env = makeStaticContext(element); |
| 1301 |
String attValue = element.getAttributeValue("", name); |
| 1302 |
if (attValue == null) { |
| 1303 |
return SequenceType.ANY_SEQUENCE; |
| 1304 |
} else { |
| 1305 |
return parser.parseExtendedSequenceType(attValue, env); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
private IndependentContext makeStaticContext(NodeInfo element) { |
| 1310 |
StylesheetPackage pack = packStack.peek(); |
| 1311 |
IndependentContext env = new IndependentContext(config); |
| 1312 |
final InscopeNamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1313 |
env.setNamespaceResolver(resolver); |
| 1314 |
env.setImportedSchemaNamespaces(pack.getSchemaNamespaces()); |
| 1315 |
env.getImportedSchemaNamespaces().add(NamespaceConstant.ANONYMOUS); |
| 1316 |
parser.getQNameParser().setNamespaceResolver(resolver); |
| 1317 |
return env; |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
public ItemType parseItemTypeAttribute(NodeInfo element, String attName) throws XPathException { |
| 1330 |
String attValue = element.getAttributeValue("", attName); |
| 1331 |
if (attValue == null) { |
| 1332 |
return AnyItemType.getInstance(); |
| 1333 |
} |
| 1334 |
return parseItemType(element, attValue); |
| 1335 |
} |
| 1336 |
|
| 1337 |
private ItemType parseItemType(NodeInfo element, String attValue) throws XPathException { |
| 1338 |
IndependentContext env = makeStaticContext(element); |
| 1339 |
return parser.parseExtendedItemType(attValue, env); |
| 1340 |
} |
| 1341 |
|
| 1342 |
public AtomicComparer makeAtomicComparer(String name, NodeInfo element) throws XPathException { |
| 1343 |
if (name.equals("CCC")) { |
| 1344 |
return CodepointCollatingComparer.getInstance(); |
| 1345 |
} else if (name.equals("CAVC")) { |
| 1346 |
return ComparableAtomicValueComparer.getInstance(); |
| 1347 |
} else if (name.startsWith("GAC|")) { |
| 1348 |
StringCollator collator = config.getCollation(name.substring(4)); |
| 1349 |
return new GenericAtomicComparer(collator, null); |
| 1350 |
} else if (name.equals("CalVC")) { |
| 1351 |
return new CalendarValueComparer(null); |
| 1352 |
} else if (name.equals("EQC")) { |
| 1353 |
return EqualityComparer.getInstance(); |
| 1354 |
} else if (name.equals("NC")) { |
| 1355 |
return NumericComparer.getInstance(); |
| 1356 |
} else if (name.equals("NC11")) { |
| 1357 |
return NumericComparer11.getInstance(); |
| 1358 |
} else if (name.equals("QUNC")) { |
| 1359 |
return new UntypedNumericComparer(); |
| 1360 |
} else if (name.equals("DblSC")) { |
| 1361 |
return DoubleSortComparer.getInstance(); |
| 1362 |
} else if (name.equals("DecSC")) { |
| 1363 |
return DecimalSortComparer.getInstance(); |
| 1364 |
} else if (name.startsWith("CAC|")) { |
| 1365 |
StringCollator collator = config.getCollation(name.substring(4)); |
| 1366 |
return new CollatingAtomicComparer(collator); |
| 1367 |
} else if (name.startsWith("AtSC|")) { |
| 1368 |
int nextBar = name.indexOf('|', 5); |
| 1369 |
String fps = name.substring(5, nextBar); |
| 1370 |
int fp = Integer.parseInt(fps); |
| 1371 |
String collName = name.substring(nextBar + 1); |
| 1372 |
return AtomicSortComparer.makeSortComparer(config.getCollation(collName), fp, new EarlyEvaluationContext(config)); |
| 1373 |
} else if (name.startsWith("DESC|")) { |
| 1374 |
AtomicComparer base = makeAtomicComparer(name.substring(5), element); |
| 1375 |
return new DescendingComparer(base); |
| 1376 |
} else if (name.startsWith("TEXT|")) { |
| 1377 |
AtomicComparer base = makeAtomicComparer(name.substring(5), element); |
| 1378 |
return new TextComparer(base); |
| 1379 |
} else { |
| 1380 |
throw new XPathException("Unknown comparer " + name, SaxonErrorCode.SXPK0002); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
private SortKeyDefinitionList loadSortKeyDefinitions(NodeInfo element) throws XPathException { |
| 1392 |
List<SortKeyDefinition> skdl = new ArrayList<>(4); |
| 1393 |
NodeInfo sortKeyElement; |
| 1394 |
AxisIterator iterator = element.iterateAxis(AxisInfo.CHILD, |
| 1395 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "sortKey", config.getNamePool())); |
| 1396 |
while ((sortKeyElement = iterator.next()) != null) { |
| 1397 |
SortKeyDefinition skd = new SortKeyDefinition(); |
| 1398 |
String compAtt = sortKeyElement.getAttributeValue("", "comp"); |
| 1399 |
if (compAtt != null) { |
| 1400 |
AtomicComparer ac = makeAtomicComparer(compAtt, sortKeyElement); |
| 1401 |
skd.setFinalComparator(ac); |
| 1402 |
} |
| 1403 |
skd.setSortKey(getExpressionWithRole(sortKeyElement, "select"), true); |
| 1404 |
skd.setOrder(getExpressionWithRole(sortKeyElement, "order")); |
| 1405 |
skd.setLanguage(getExpressionWithRole(sortKeyElement, "lang")); |
| 1406 |
skd.setCollationNameExpression(getExpressionWithRole(sortKeyElement, "collation")); |
| 1407 |
skd.setCaseOrder(getExpressionWithRole(sortKeyElement, "caseOrder")); |
| 1408 |
skd.setStable(getExpressionWithRole(sortKeyElement, "stable")); |
| 1409 |
skd.setDataTypeExpression(getExpressionWithRole(sortKeyElement, "dataType")); |
| 1410 |
skdl.add(skd); |
| 1411 |
} |
| 1412 |
return new SortKeyDefinitionList(skdl.toArray(new SortKeyDefinition[0])); |
| 1413 |
} |
| 1414 |
|
| 1415 |
private WithParam[] loadWithParams(NodeInfo element, Expression parent, boolean needTunnel) throws XPathException { |
| 1416 |
List<WithParam> wps = new ArrayList<>(4); |
| 1417 |
NodeInfo wpElement; |
| 1418 |
AxisIterator iterator = element.iterateAxis(AxisInfo.CHILD, |
| 1419 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "withParam", config.getNamePool())); |
| 1420 |
while ((wpElement = iterator.next()) != null) { |
| 1421 |
String flags = wpElement.getAttributeValue("", "flags"); |
| 1422 |
boolean isTunnel = flags != null && flags.contains("t"); |
| 1423 |
if (needTunnel == isTunnel) { |
| 1424 |
WithParam wp = new WithParam(); |
| 1425 |
wp.setVariableQName(getQNameAttribute(wpElement, "name")); |
| 1426 |
wp.setSelectExpression(parent, getFirstChildExpression(wpElement)); |
| 1427 |
wp.setRequiredType(parseSequenceType(wpElement, "as")); |
| 1428 |
wp.setTypeChecked(flags != null && flags.contains("c")); |
| 1429 |
wps.add(wp); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
return wps.toArray(new WithParam[0]); |
| 1433 |
} |
| 1434 |
|
| 1435 |
private Properties importProperties(String value) { |
| 1436 |
try { |
| 1437 |
StringReader reader = new StringReader(value); |
| 1438 |
Properties props = new Properties(); |
| 1439 |
props.load(reader); |
| 1440 |
return props; |
| 1441 |
} catch (IOException e) { |
| 1442 |
throw new AssertionError(e); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
public interface ExpressionLoader { |
| 1447 |
Expression loadFrom(PackageLoaderHE loader, NodeInfo element) throws XPathException; |
| 1448 |
} |
| 1449 |
|
| 1450 |
public interface PatternLoader { |
| 1451 |
Pattern loadFrom(PackageLoaderHE loader, NodeInfo element) throws XPathException; |
| 1452 |
} |
| 1453 |
|
| 1454 |
private static Expression notInHE(String tag) throws XPathException { |
| 1455 |
throw new XPathException("SEF file contains instruction " + tag + " which is not available in Saxon-HE"); |
| 1456 |
} |
| 1457 |
|
| 1458 |
protected static final Map<String, ExpressionLoader> eMap = new HashMap<>(200); |
| 1459 |
|
| 1460 |
protected static final Map<String, String> licensableConstructs = new HashMap<>(30); |
| 1461 |
|
| 1462 |
static { |
| 1463 |
licensableConstructs.put("gcEE", "EE"); |
| 1464 |
licensableConstructs.put("indexedFilter", "EE"); |
| 1465 |
licensableConstructs.put("indexedFilter2", "EE"); |
| 1466 |
licensableConstructs.put("indexedLookup", "EE"); |
| 1467 |
licensableConstructs.put("stream", "EE"); |
| 1468 |
licensableConstructs.put("switch", "EE"); |
| 1469 |
|
| 1470 |
licensableConstructs.put("acFnRef", "PE"); |
| 1471 |
licensableConstructs.put("assign", "PE"); |
| 1472 |
licensableConstructs.put("coercedFn", "PE"); |
| 1473 |
licensableConstructs.put("curriedFunc", "PE"); |
| 1474 |
licensableConstructs.put("do", "PE"); |
| 1475 |
licensableConstructs.put("evaluate", "PE"); |
| 1476 |
licensableConstructs.put("fnCoercer", "PE"); |
| 1477 |
licensableConstructs.put("fnRef", "PE"); |
| 1478 |
licensableConstructs.put("inlineFn", "PE"); |
| 1479 |
licensableConstructs.put("javaCall", "PE"); |
| 1480 |
licensableConstructs.put("origF", "PE"); |
| 1481 |
licensableConstructs.put("partialApply", "PE"); |
| 1482 |
licensableConstructs.put("ufRef", "PE"); |
| 1483 |
licensableConstructs.put("while", "PE"); |
| 1484 |
} |
| 1485 |
|
| 1486 |
static { |
| 1487 |
|
| 1488 |
eMap.put("among", (loader, element) -> { |
| 1489 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1490 |
Expression rhs = loader.getSecondChildExpression(element); |
| 1491 |
return new SingletonIntersectExpression(lhs, Token.INTERSECT, rhs); |
| 1492 |
}); |
| 1493 |
|
| 1494 |
eMap.put("analyzeString", (loader, element) -> { |
| 1495 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 1496 |
Expression regex = loader.getExpressionWithRole(element, "regex"); |
| 1497 |
Expression flags = loader.getExpressionWithRole(element, "flags"); |
| 1498 |
Expression matching = loader.getExpressionWithRole(element, "matching"); |
| 1499 |
Expression nonMatching = loader.getExpressionWithRole(element, "nonMatching"); |
| 1500 |
AnalyzeString instr = new AnalyzeString(select, regex, flags, matching, nonMatching, null); |
| 1501 |
instr.precomputeRegex(loader.getConfiguration(), null); |
| 1502 |
return instr; |
| 1503 |
}); |
| 1504 |
|
| 1505 |
eMap.put("and", (loader, element) -> { |
| 1506 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1507 |
Expression rhs = loader.getSecondChildExpression(element); |
| 1508 |
return new AndExpression(lhs, rhs); |
| 1509 |
}); |
| 1510 |
|
| 1511 |
eMap.put("applyImports", (loader, element) -> { |
| 1512 |
ApplyImports inst = new ApplyImports(); |
| 1513 |
|
| 1514 |
WithParam[] actuals = loader.loadWithParams(element, inst, false); |
| 1515 |
WithParam[] tunnels = loader.loadWithParams(element, inst, true); |
| 1516 |
inst.setActualParams(actuals); |
| 1517 |
inst.setTunnelParams(tunnels); |
| 1518 |
return inst; |
| 1519 |
}); |
| 1520 |
|
| 1521 |
|
| 1522 |
eMap.put("applyT", (loader, element) -> { |
| 1523 |
StylesheetPackage pack = loader.packStack.peek(); |
| 1524 |
Expression select = loader.getFirstChildExpression(element); |
| 1525 |
StructuredQName modeAtt = loader.getQNameAttribute(element, "mode"); |
| 1526 |
SimpleMode mode; |
| 1527 |
if (modeAtt != null) { |
| 1528 |
mode = (SimpleMode) pack.getRuleManager().obtainMode(modeAtt, true); |
| 1529 |
} else { |
| 1530 |
mode = (SimpleMode) pack.getRuleManager().obtainMode(null, true); |
| 1531 |
} |
| 1532 |
String flags = element.getAttributeValue("", "flags"); |
| 1533 |
if (flags == null) { |
| 1534 |
flags = ""; |
| 1535 |
} |
| 1536 |
|
| 1537 |
boolean useCurrentMode = flags.contains("c"); |
| 1538 |
boolean useTailRecursion = flags.contains("t"); |
| 1539 |
boolean implicitSelect = flags.contains("i"); |
| 1540 |
boolean inStreamableConstruct = flags.contains("d"); |
| 1541 |
|
| 1542 |
ApplyTemplates inst = new ApplyTemplates( |
| 1543 |
select, useCurrentMode, useTailRecursion, implicitSelect, inStreamableConstruct, mode, loader.packStack.peek().getRuleManager()); |
| 1544 |
WithParam[] actuals = loader.loadWithParams(element, inst, false); |
| 1545 |
WithParam[] tunnels = loader.loadWithParams(element, inst, true); |
| 1546 |
inst.setActualParams(actuals); |
| 1547 |
inst.setTunnelParams(tunnels); |
| 1548 |
|
| 1549 |
int bindingSlot = loader.getIntegerAttribute(element, "bSlot"); |
| 1550 |
inst.setBindingSlot(bindingSlot); |
| 1551 |
|
| 1552 |
return inst; |
| 1553 |
}); |
| 1554 |
|
| 1555 |
eMap.put("arith", (loader, element) -> { |
| 1556 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1557 |
Expression rhs = loader.getSecondChildExpression(element); |
| 1558 |
final String code = element.getAttributeValue("", "calc"); |
| 1559 |
Calculator calc = Calculator.reconstructCalculator(code); |
| 1560 |
int operator = Calculator.operatorFromCode(code.charAt(1)); |
| 1561 |
int token = Calculator.getTokenFromOperator(operator); |
| 1562 |
ArithmeticExpression exp = new ArithmeticExpression(lhs, token, rhs); |
| 1563 |
exp.setCalculator(calc); |
| 1564 |
return exp; |
| 1565 |
}); |
| 1566 |
|
| 1567 |
eMap.put("arith10", (loader, element) -> { |
| 1568 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1569 |
Expression rhs = loader.getSecondChildExpression(element); |
| 1570 |
final String code = element.getAttributeValue("", "calc"); |
| 1571 |
Calculator calc = Calculator.reconstructCalculator(code); |
| 1572 |
int operator = Calculator.operatorFromCode(code.charAt(1)); |
| 1573 |
int token = Calculator.getTokenFromOperator(operator); |
| 1574 |
ArithmeticExpression10 exp = new ArithmeticExpression10(lhs, token, rhs); |
| 1575 |
exp.setCalculator(calc); |
| 1576 |
return exp; |
| 1577 |
}); |
| 1578 |
|
| 1579 |
eMap.put("array", (loader, element) -> { |
| 1580 |
List<Expression> children = getChildExpressionList(loader, element); |
| 1581 |
List<GroundedValue<?>> values = new ArrayList<>(children.size()); |
| 1582 |
for (Expression child : children) { |
| 1583 |
values.add(((Literal) child).getValue()); |
| 1584 |
} |
| 1585 |
return Literal.makeLiteral(new SimpleArrayItem(values)); |
| 1586 |
}); |
| 1587 |
|
| 1588 |
eMap.put("arrayBlock", (loader, element) -> { |
| 1589 |
List<Expression> children = getChildExpressionList(loader, element); |
| 1590 |
return new SquareArrayConstructor(children); |
| 1591 |
}); |
| 1592 |
|
| 1593 |
eMap.put("atomic", (loader, element) -> { |
| 1594 |
String valAtt = element.getAttributeValue("", "val"); |
| 1595 |
AtomicType type = (AtomicType)loader.getTypeAttribute(element, "type"); |
| 1596 |
AtomicValue val = type.getStringConverter(loader.config.getConversionRules()) |
| 1597 |
.convertString(valAtt).asAtomic(); |
| 1598 |
return Literal.makeLiteral(val); |
| 1599 |
}); |
| 1600 |
|
| 1601 |
eMap.put("atomSing", (loader, element) -> { |
| 1602 |
Expression body = loader.getFirstChildExpression(element); |
| 1603 |
RoleDiagnostic role = RoleDiagnostic.reconstruct(element.getAttributeValue("", "diag")); |
| 1604 |
String cardAtt = element.getAttributeValue("", "card"); |
| 1605 |
boolean allowEmpty = "?".equals(cardAtt); |
| 1606 |
return new SingletonAtomizer(body, role, allowEmpty); |
| 1607 |
}); |
| 1608 |
|
| 1609 |
eMap.put("att", (loader, element) -> { |
| 1610 |
String displayName = element.getAttributeValue("", "name"); |
| 1611 |
String[] parts; |
| 1612 |
try { |
| 1613 |
parts = NameChecker.getQNameParts(displayName); |
| 1614 |
} catch (QNameException err) { |
| 1615 |
throw new XPathException(err); |
| 1616 |
} |
| 1617 |
String uri = element.getAttributeValue("", "nsuri"); |
| 1618 |
if (uri == null) { |
| 1619 |
uri = ""; |
| 1620 |
} |
| 1621 |
StructuredQName name = new StructuredQName(parts[0], uri, parts[1]); |
| 1622 |
NodeName attName = new FingerprintedQName(name, loader.config.getNamePool()); |
| 1623 |
int validation = Validation.SKIP; |
| 1624 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1625 |
if (valAtt != null) { |
| 1626 |
validation = Validation.getCode(valAtt); |
| 1627 |
} |
| 1628 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1629 |
if (schemaType != null) { |
| 1630 |
validation = Validation.BY_TYPE; |
| 1631 |
} |
| 1632 |
Expression content = loader.getFirstChildExpression(element); |
| 1633 |
FixedAttribute att = new FixedAttribute(attName, validation, (SimpleType) schemaType); |
| 1634 |
att.setSelect(content); |
| 1635 |
return att; |
| 1636 |
}); |
| 1637 |
|
| 1638 |
eMap.put("attVal", (loader, element) -> { |
| 1639 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 1640 |
FingerprintedQName attName = new FingerprintedQName(name, loader.config.getNamePool()); |
| 1641 |
AttributeGetter getter = new AttributeGetter(attName); |
| 1642 |
getter.setRequiredChecks(loader.getIntegerAttribute(element, "chk")); |
| 1643 |
return getter; |
| 1644 |
}); |
| 1645 |
|
| 1646 |
eMap.put("axis", (loader, element) -> { |
| 1647 |
String axisName = element.getAttributeValue("", "name"); |
| 1648 |
byte axis = AxisInfo.getAxisNumber(axisName); |
| 1649 |
NodeTest nt = (NodeTest) loader.parseItemTypeAttribute(element, "nodeTest"); |
| 1650 |
return new AxisExpression(axis, nt); |
| 1651 |
}); |
| 1652 |
|
| 1653 |
eMap.put("break", (loader, element) -> new BreakInstr()); |
| 1654 |
|
| 1655 |
eMap.put("callT", (loader, element) -> { |
| 1656 |
StylesheetPackage pack = loader.packStack.peek(); |
| 1657 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 1658 |
SymbolicName symbol = new SymbolicName(StandardNames.XSL_TEMPLATE, name); |
| 1659 |
Component target = pack.getComponent(symbol); |
| 1660 |
NamedTemplate t; |
| 1661 |
if (target == null) { |
| 1662 |
t = new NamedTemplate(name); |
| 1663 |
} else { |
| 1664 |
t = (NamedTemplate) target.getActor(); |
| 1665 |
} |
| 1666 |
String flags = element.getAttributeValue("", "flags"); |
| 1667 |
boolean useTailRecursion = flags != null && flags.contains("t"); |
| 1668 |
boolean inStreamableConstruct = flags != null && flags.contains("d"); |
| 1669 |
CallTemplate inst = new CallTemplate(t, name, useTailRecursion, inStreamableConstruct); |
| 1670 |
WithParam[] actuals = loader.loadWithParams(element, inst, false); |
| 1671 |
WithParam[] tunnels = loader.loadWithParams(element, inst, true); |
| 1672 |
inst.setActualParameters(actuals, tunnels); |
| 1673 |
int bindingSlot = loader.getIntegerAttribute(element, "bSlot"); |
| 1674 |
inst.setBindingSlot(bindingSlot); |
| 1675 |
loader.fixups.peek().add(inst); |
| 1676 |
return inst; |
| 1677 |
}); |
| 1678 |
|
| 1679 |
eMap.put("cast", (loader, element) -> { |
| 1680 |
Expression body = loader.getFirstChildExpression(element); |
| 1681 |
SchemaType st = loader.getTypeAttribute(element, "as"); |
| 1682 |
boolean allowEmpty = element.getAttributeValue("", "emptiable").equals("1"); |
| 1683 |
if (st == null) { |
| 1684 |
throw new AssertionError("Unknown simple type " + element.getAttributeValue("", "as")); |
| 1685 |
} else if (st instanceof AtomicType) { |
| 1686 |
return new CastExpression(body, (AtomicType) st, allowEmpty); |
| 1687 |
} else if (st instanceof ListType) { |
| 1688 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1689 |
ListConstructorFunction ucf = new ListConstructorFunction((ListType) st, resolver, allowEmpty); |
| 1690 |
return new StaticFunctionCall(ucf, new Expression[]{body}); |
| 1691 |
} else if (st instanceof UnionType) { |
| 1692 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1693 |
UnionConstructorFunction ucf = new UnionConstructorFunction((UnionType) st, resolver, allowEmpty); |
| 1694 |
return new StaticFunctionCall(ucf, new Expression[]{body}); |
| 1695 |
} else { |
| 1696 |
throw new AssertionError("Unknown simple type variety " + st.getClass()); |
| 1697 |
} |
| 1698 |
}); |
| 1699 |
|
| 1700 |
eMap.put("castable", (loader, element) -> { |
| 1701 |
Expression body = loader.getFirstChildExpression(element); |
| 1702 |
SchemaType st = loader.getTypeAttribute(element, "as"); |
| 1703 |
boolean allowEmpty = element.getAttributeValue("", "emptiable").equals("1"); |
| 1704 |
if (st == null) { |
| 1705 |
throw new AssertionError("Unknown simple type " + element.getAttributeValue("", "as")); |
| 1706 |
} else if (st instanceof AtomicType) { |
| 1707 |
return new CastableExpression(body, (AtomicType) st, allowEmpty); |
| 1708 |
} else if (st instanceof ListType) { |
| 1709 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1710 |
ListCastableFunction ucf = new ListCastableFunction((ListType) st, resolver, allowEmpty); |
| 1711 |
return new StaticFunctionCall(ucf, new Expression[]{body}); |
| 1712 |
} else if (st instanceof UnionType) { |
| 1713 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 1714 |
UnionCastableFunction ucf = new UnionCastableFunction((UnionType) st, resolver, allowEmpty); |
| 1715 |
return new StaticFunctionCall(ucf, new Expression[]{body}); |
| 1716 |
} else { |
| 1717 |
throw new AssertionError("Unknown simple type variety " + st.getClass()); |
| 1718 |
} |
| 1719 |
}); |
| 1720 |
|
| 1721 |
eMap.put("check", (loader, element) -> { |
| 1722 |
Expression body = loader.getFirstChildExpression(element); |
| 1723 |
String cardAtt = element.getAttributeValue("", "card"); |
| 1724 |
int c; |
| 1725 |
switch (cardAtt) { |
| 1726 |
case "?": |
| 1727 |
c = StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 1728 |
break; |
| 1729 |
case "*": |
| 1730 |
c = StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 1731 |
break; |
| 1732 |
case "+": |
| 1733 |
c = StaticProperty.ALLOWS_ONE_OR_MORE; |
| 1734 |
break; |
| 1735 |
case "\u00B0": |
| 1736 |
c = StaticProperty.ALLOWS_ZERO; |
| 1737 |
break; |
| 1738 |
case "1": |
| 1739 |
c = StaticProperty.EXACTLY_ONE; |
| 1740 |
break; |
| 1741 |
default: |
| 1742 |
throw new IllegalStateException("Occurrence indicator: '" + cardAtt + "'"); |
| 1743 |
} |
| 1744 |
RoleDiagnostic role = RoleDiagnostic.reconstruct(element.getAttributeValue("", "diag")); |
| 1745 |
return CardinalityChecker.makeCardinalityChecker(body, c, role); |
| 1746 |
}); |
| 1747 |
|
| 1748 |
eMap.put("choose", (loader, element) -> { |
| 1749 |
List<Expression> conditions = new ArrayList<>(); |
| 1750 |
List<Expression> actions = new ArrayList<>(); |
| 1751 |
AxisIterator iter = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 1752 |
NodeInfo child; |
| 1753 |
boolean odd = true; |
| 1754 |
while ((child = iter.next()) != null) { |
| 1755 |
if (odd) { |
| 1756 |
conditions.add(loader.loadExpression(child)); |
| 1757 |
} else { |
| 1758 |
actions.add(loader.loadExpression(child)); |
| 1759 |
} |
| 1760 |
odd = !odd; |
| 1761 |
} |
| 1762 |
return new Choose(conditions.toArray(new Expression[0]), |
| 1763 |
actions.toArray(new Expression[0])); |
| 1764 |
}); |
| 1765 |
|
| 1766 |
eMap.put("comment", (loader, element) -> { |
| 1767 |
Expression select = loader.getFirstChildExpression(element); |
| 1768 |
Comment inst = new Comment(); |
| 1769 |
inst.setSelect(select); |
| 1770 |
return inst; |
| 1771 |
}); |
| 1772 |
|
| 1773 |
eMap.put("compareToInt", (loader, element) -> { |
| 1774 |
BigInteger i = new BigInteger(element.getAttributeValue("", "val")); |
| 1775 |
String opAtt = element.getAttributeValue("", "op"); |
| 1776 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1777 |
return new CompareToIntegerConstant(lhs, parseValueComparisonOperator(opAtt), i.longValue()); |
| 1778 |
}); |
| 1779 |
|
| 1780 |
eMap.put("compAtt", (loader, element) -> { |
| 1781 |
Expression name = loader.getExpressionWithRole(element, "name"); |
| 1782 |
Expression namespace = loader.getExpressionWithRole(element, "namespace"); |
| 1783 |
Expression content = loader.getExpressionWithRole(element, "select"); |
| 1784 |
int validation = Validation.SKIP; |
| 1785 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1786 |
if (valAtt != null) { |
| 1787 |
validation = Validation.getCode(valAtt); |
| 1788 |
} |
| 1789 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1790 |
if (schemaType != null) { |
| 1791 |
validation = Validation.BY_TYPE; |
| 1792 |
} |
| 1793 |
ComputedAttribute att = new ComputedAttribute(name, namespace, null, validation, (SimpleType) schemaType, false); |
| 1794 |
att.setSelect(content); |
| 1795 |
return att; |
| 1796 |
}); |
| 1797 |
|
| 1798 |
|
| 1799 |
eMap.put("compElem", (loader, element) -> { |
| 1800 |
Expression name = loader.getExpressionWithRole(element, "name"); |
| 1801 |
Expression namespace = loader.getExpressionWithRole(element, "namespace"); |
| 1802 |
Expression content = loader.getExpressionWithRole(element, "content"); |
| 1803 |
int validation = Validation.SKIP; |
| 1804 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1805 |
if (valAtt != null) { |
| 1806 |
validation = Validation.getCode(valAtt); |
| 1807 |
} |
| 1808 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1809 |
if (schemaType != null) { |
| 1810 |
validation = Validation.BY_TYPE; |
| 1811 |
} |
| 1812 |
String flags = element.getAttributeValue("", "flags"); |
| 1813 |
ComputedElement inst = new ComputedElement(name, namespace, schemaType, validation, true, false); |
| 1814 |
if (flags != null) { |
| 1815 |
inst.setInheritanceFlags(flags); |
| 1816 |
} |
| 1817 |
inst.setContentExpression(content); |
| 1818 |
return inst; |
| 1819 |
}); |
| 1820 |
|
| 1821 |
|
| 1822 |
eMap.put("compiledExpression", PackageLoaderHE::getFirstChildExpression); |
| 1823 |
|
| 1824 |
eMap.put("conditionalSort", (loader, element) -> { |
| 1825 |
Expression lhs = loader.getFirstChildExpression(element); |
| 1826 |
Expression rhs = loader.getSecondChildExpression(element); |
| 1827 |
return new ConditionalSorter(lhs, (DocumentSorter) rhs); |
| 1828 |
}); |
| 1829 |
|
| 1830 |
eMap.put("condCont", (loader, element) -> { |
| 1831 |
Expression base = loader.getFirstChildExpression(element); |
| 1832 |
return new WherePopulated(base); |
| 1833 |
}); |
| 1834 |
|
| 1835 |
eMap.put("condSeq", (loader, element) -> { |
| 1836 |
Expression[] args = getChildExpressionArray(loader, element); |
| 1837 |
return new ConditionalBlock(args); |
| 1838 |
}); |
| 1839 |
|
| 1840 |
eMap.put("consume", (loader, element) -> { |
| 1841 |
Expression arg = loader.getFirstChildExpression(element); |
| 1842 |
return new ConsumingOperand(arg); |
| 1843 |
}); |
| 1844 |
|
| 1845 |
eMap.put("convert", (loader, element) -> { |
| 1846 |
Expression body = loader.getFirstChildExpression(element); |
| 1847 |
ItemType fromType = loader.parseItemTypeAttribute(element, "from"); |
| 1848 |
ItemType toType = loader.parseItemTypeAttribute(element, "to"); |
| 1849 |
AtomicSequenceConverter asc = new AtomicSequenceConverter(body, (PlainType) toType); |
| 1850 |
if ("p".equals(element.getAttributeValue("", "flags"))) { |
| 1851 |
Converter c = toType.equals(BuiltInAtomicType.DOUBLE) ? new Converter.PromoterToDouble() : new Converter.PromoterToFloat(); |
| 1852 |
asc.setConverter(c); |
| 1853 |
} else { |
| 1854 |
Converter c = asc.allocateConverter(loader.config, false, fromType); |
| 1855 |
asc.setConverter(c); |
| 1856 |
} |
| 1857 |
String diag = element.getAttributeValue("", "diag"); |
| 1858 |
if (diag != null) { |
| 1859 |
asc.setRoleDiagnostic(RoleDiagnostic.reconstruct(diag)); |
| 1860 |
} |
| 1861 |
return asc; |
| 1862 |
}); |
| 1863 |
|
| 1864 |
eMap.put("copy", (loader, element) -> { |
| 1865 |
int validation = Validation.SKIP; |
| 1866 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1867 |
if (valAtt != null) { |
| 1868 |
validation = Validation.getCode(valAtt); |
| 1869 |
} |
| 1870 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1871 |
if (schemaType != null) { |
| 1872 |
validation = Validation.BY_TYPE; |
| 1873 |
} |
| 1874 |
Copy inst = new Copy(false, false, schemaType, validation); |
| 1875 |
inst.setContentExpression(loader.getFirstChildExpression(element)); |
| 1876 |
String flags = element.getAttributeValue("", "flags"); |
| 1877 |
inst.setCopyNamespaces(flags.contains("c")); |
| 1878 |
inst.setInheritNamespacesToChildren(flags.contains("i")); |
| 1879 |
inst.setInheritNamespacesFromParent(flags.contains("n")); |
| 1880 |
return inst; |
| 1881 |
}); |
| 1882 |
|
| 1883 |
eMap.put("copyOf", (loader, element) -> { |
| 1884 |
Expression select = loader.getFirstChildExpression(element); |
| 1885 |
String flags = element.getAttributeValue("", "flags"); |
| 1886 |
boolean copyNamespaces = flags.contains("c"); |
| 1887 |
boolean rejectDups = flags.contains("d"); |
| 1888 |
int validation = Validation.SKIP; |
| 1889 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1890 |
if (valAtt != null) { |
| 1891 |
validation = Validation.getCode(valAtt); |
| 1892 |
} |
| 1893 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1894 |
if (schemaType != null) { |
| 1895 |
validation = Validation.BY_TYPE; |
| 1896 |
} |
| 1897 |
CopyOf inst = new CopyOf(select, copyNamespaces, validation, schemaType, rejectDups); |
| 1898 |
inst.setCopyAccumulators(flags.contains("m")); |
| 1899 |
inst.setCopyLineNumbers(flags.contains("l")); |
| 1900 |
inst.setSchemaAware(flags.contains("s")); |
| 1901 |
return inst; |
| 1902 |
}); |
| 1903 |
|
| 1904 |
eMap.put("currentGroup", (loader, element) -> new CurrentGroupCall()); |
| 1905 |
|
| 1906 |
eMap.put("currentGroupingKey", (loader, element) -> new CurrentGroupingKeyCall()); |
| 1907 |
|
| 1908 |
eMap.put("cvUntyped", (loader, element) -> { |
| 1909 |
Expression body = loader.getFirstChildExpression(element); |
| 1910 |
ItemType toType = loader.parseItemTypeAttribute(element, "to"); |
| 1911 |
if (((SimpleType) toType).isNamespaceSensitive()) { |
| 1912 |
return UntypedSequenceConverter.makeUntypedSequenceRejector(loader.config, body, (PlainType) toType); |
| 1913 |
} else { |
| 1914 |
UntypedSequenceConverter cv = UntypedSequenceConverter.makeUntypedSequenceConverter(loader.config, body, (PlainType) toType); |
| 1915 |
String diag = element.getAttributeValue("", "diag"); |
| 1916 |
if (diag != null) { |
| 1917 |
cv.setRoleDiagnostic(RoleDiagnostic.reconstruct(diag)); |
| 1918 |
} |
| 1919 |
return cv; |
| 1920 |
} |
| 1921 |
}); |
| 1922 |
|
| 1923 |
eMap.put("data", (loader, element) -> { |
| 1924 |
Expression body = loader.getFirstChildExpression(element); |
| 1925 |
return new Atomizer(body, null); |
| 1926 |
}); |
| 1927 |
|
| 1928 |
eMap.put("dbl", (loader, element) -> { |
| 1929 |
String val = element.getAttributeValue("", "val"); |
| 1930 |
double d = StringToDouble.getInstance().stringToNumber(val); |
| 1931 |
return Literal.makeLiteral(new DoubleValue(d)); |
| 1932 |
}); |
| 1933 |
|
| 1934 |
eMap.put("dec", (loader, element) -> { |
| 1935 |
String val = element.getAttributeValue("", "val"); |
| 1936 |
return Literal.makeLiteral(BigDecimalValue.makeDecimalValue(val, false).asAtomic()); |
| 1937 |
}); |
| 1938 |
|
| 1939 |
eMap.put("doc", (loader, element) -> { |
| 1940 |
int validation = Validation.SKIP; |
| 1941 |
String valAtt = element.getAttributeValue("", "validation"); |
| 1942 |
if (valAtt != null) { |
| 1943 |
validation = Validation.getCode(valAtt); |
| 1944 |
} |
| 1945 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 1946 |
if (schemaType != null) { |
| 1947 |
validation = Validation.BY_TYPE; |
| 1948 |
} |
| 1949 |
String flags = element.getAttributeValue("", "flags"); |
| 1950 |
boolean textOnly = flags != null && flags.contains("t"); |
| 1951 |
String base = element.getAttributeValue("", "base"); |
| 1952 |
String constantText = element.getAttributeValue("", "text"); |
| 1953 |
Expression body = loader.getFirstChildExpression(element); |
| 1954 |
DocumentInstr inst = new DocumentInstr(textOnly, constantText); |
| 1955 |
inst.setContentExpression(body); |
| 1956 |
inst.setValidationAction(validation, schemaType); |
| 1957 |
return inst; |
| 1958 |
}); |
| 1959 |
|
| 1960 |
eMap.put("docOrder", (loader, element) -> { |
| 1961 |
Expression select = loader.getFirstChildExpression(element); |
| 1962 |
boolean intra = element.getAttributeValue("", "intra").equals("1"); |
| 1963 |
return new DocumentSorter(select, intra); |
| 1964 |
}); |
| 1965 |
|
| 1966 |
eMap.put("dot", (loader, element) -> { |
| 1967 |
ContextItemExpression cie = new ContextItemExpression(); |
| 1968 |
ItemType type = loader.parseItemTypeAttribute(element, "type"); |
| 1969 |
boolean maybeAbsent = false; |
| 1970 |
if ("a".equals(element.getAttributeValue("", "flags"))) { |
| 1971 |
maybeAbsent = true; |
| 1972 |
} |
| 1973 |
ContextItemStaticInfo info = loader.getConfiguration().makeContextItemStaticInfo(type, maybeAbsent); |
| 1974 |
cie.setStaticInfo(info); |
| 1975 |
return cie; |
| 1976 |
}); |
| 1977 |
|
| 1978 |
eMap.put("elem", (loader, element) -> { |
| 1979 |
String displayName = element.getAttributeValue("", "name"); |
| 1980 |
String[] parts; |
| 1981 |
try { |
| 1982 |
parts = NameChecker.getQNameParts(displayName); |
| 1983 |
} catch (QNameException err) { |
| 1984 |
throw new XPathException(err); |
| 1985 |
} |
| 1986 |
String nsuri = element.getAttributeValue("", "nsuri"); |
| 1987 |
StructuredQName name = new StructuredQName(parts[0], nsuri, parts[1]); |
| 1988 |
|
| 1989 |
NodeName elemName = new FingerprintedQName(name, loader.config.getNamePool()); |
| 1990 |
String ns = element.getAttributeValue("", "namespaces"); |
| 1991 |
NamespaceBinding[] bindings = NamespaceBinding.EMPTY_ARRAY; |
| 1992 |
if (ns != null && !ns.isEmpty()) { |
| 1993 |
String[] pairs = ns.split(" "); |
| 1994 |
bindings = new NamespaceBinding[pairs.length]; |
| 1995 |
int i = 0; |
| 1996 |
for (String pair : pairs) { |
| 1997 |
int eq = pair.indexOf('='); |
| 1998 |
if (eq >= 0) { |
| 1999 |
String prefix = pair.substring(0, eq); |
| 2000 |
if (prefix.equals("#")) { |
| 2001 |
prefix = ""; |
| 2002 |
} |
| 2003 |
String uri = pair.substring(eq + 1); |
| 2004 |
if (uri.equals("~")) { |
| 2005 |
uri = NamespaceConstant.getUriForConventionalPrefix(prefix); |
| 2006 |
} |
| 2007 |
bindings[i++] = new NamespaceBinding(prefix, uri); |
| 2008 |
} else { |
| 2009 |
RetainedStaticContext rsc = loader.contextStack.peek(); |
| 2010 |
String prefix = pair; |
| 2011 |
if (prefix.equals("#")) { |
| 2012 |
prefix = ""; |
| 2013 |
} |
| 2014 |
String uri = rsc.getURIForPrefix(prefix, true); |
| 2015 |
assert uri != null; |
| 2016 |
bindings[i++] = new NamespaceBinding(prefix, uri); |
| 2017 |
} |
| 2018 |
} |
| 2019 |
} |
| 2020 |
int validation = Validation.SKIP; |
| 2021 |
String valAtt = element.getAttributeValue("", "validation"); |
| 2022 |
if (valAtt != null) { |
| 2023 |
validation = Validation.getCode(valAtt); |
| 2024 |
} |
| 2025 |
SchemaType schemaType = loader.getTypeAttribute(element, "type"); |
| 2026 |
if (schemaType != null) { |
| 2027 |
validation = Validation.BY_TYPE; |
| 2028 |
} |
| 2029 |
|
| 2030 |
Expression content = loader.getFirstChildExpression(element); |
| 2031 |
FixedElement elem = new FixedElement(elemName, bindings, true, true, schemaType, validation); |
| 2032 |
String flags = element.getAttributeValue("", "flags"); |
| 2033 |
if (flags != null) { |
| 2034 |
elem.setInheritanceFlags(flags); |
| 2035 |
} |
| 2036 |
elem.setContentExpression(content); |
| 2037 |
return elem; |
| 2038 |
}); |
| 2039 |
|
| 2040 |
|
| 2041 |
eMap.put("empty", (loader, element) -> Literal.makeLiteral(EmptySequence.getInstance())); |
| 2042 |
|
| 2043 |
eMap.put("emptyTextNodeRemover", (loader, element) -> { |
| 2044 |
Expression body = loader.getFirstChildExpression(element); |
| 2045 |
return new EmptyTextNodeRemover(body); |
| 2046 |
}); |
| 2047 |
|
| 2048 |
eMap.put("error", (loader, element) -> { |
| 2049 |
String message = element.getAttributeValue("", "message"); |
| 2050 |
String code = element.getAttributeValue("", "code"); |
| 2051 |
boolean isTypeErr = "1".equals(element.getAttributeValue("", "isTypeErr")); |
| 2052 |
return new ErrorExpression(message, code, isTypeErr); |
| 2053 |
}); |
| 2054 |
|
| 2055 |
eMap.put("every", (loader, element) -> { |
| 2056 |
Expression select = loader.getFirstChildExpression(element); |
| 2057 |
|
| 2058 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2059 |
StructuredQName name = loader.getQNameAttribute(element, "var"); |
| 2060 |
SequenceType requiredType = loader.parseSequenceType(element, "as"); |
| 2061 |
QuantifiedExpression qEx = new QuantifiedExpression(); |
| 2062 |
qEx.setOperator(Token.EVERY); |
| 2063 |
qEx.setSequence(select); |
| 2064 |
qEx.setRequiredType(requiredType); |
| 2065 |
qEx.setSlotNumber(slot); |
| 2066 |
qEx.setVariableQName(name); |
| 2067 |
|
| 2068 |
loader.localBindings.push(qEx); |
| 2069 |
Expression action = loader.getSecondChildExpression(element); |
| 2070 |
loader.localBindings.pop(); |
| 2071 |
qEx.setAction(action); |
| 2072 |
|
| 2073 |
return qEx; |
| 2074 |
}); |
| 2075 |
|
| 2076 |
|
| 2077 |
eMap.put("except", (loader, element) -> { |
| 2078 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2079 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2080 |
return new VennExpression(lhs, Token.EXCEPT, rhs); |
| 2081 |
}); |
| 2082 |
|
| 2083 |
eMap.put("false", (loader, element) -> Literal.makeLiteral(BooleanValue.FALSE)); |
| 2084 |
|
| 2085 |
eMap.put("filter", (loader, element) -> { |
| 2086 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2087 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2088 |
String flags = element.getAttributeValue("", "flags"); |
| 2089 |
FilterExpression fe = new FilterExpression(lhs, rhs); |
| 2090 |
fe.setFlags(flags); |
| 2091 |
return fe; |
| 2092 |
}); |
| 2093 |
|
| 2094 |
eMap.put("first", (loader, element) -> { |
| 2095 |
Expression base = loader.getFirstChildExpression(element); |
| 2096 |
return FirstItemExpression.makeFirstItemExpression(base); |
| 2097 |
}); |
| 2098 |
|
| 2099 |
eMap.put("fn", (loader, element) -> { |
| 2100 |
RetainedStaticContext rsc = loader.makeRetainedStaticContext(element); |
| 2101 |
loader.contextStack.push(rsc); |
| 2102 |
final Expression[] args = getChildExpressionArray(loader, element); |
| 2103 |
String name = element.getAttributeValue("", "name"); |
| 2104 |
if (name.equals("_STRING-JOIN_2.0")) { |
| 2105 |
|
| 2106 |
name = "string-join"; |
| 2107 |
} |
| 2108 |
Expression e = SystemFunction.makeCall(name, rsc, args); |
| 2109 |
if (e == null) { |
| 2110 |
throw new XPathException("Unknown system function " + name + "#" + args.length); |
| 2111 |
} |
| 2112 |
|
| 2113 |
if (e instanceof SystemFunctionCall) { |
| 2114 |
final SystemFunction fn = ((SystemFunctionCall) e).getTargetFunction(); |
| 2115 |
fn.setRetainedStaticContext(rsc); |
| 2116 |
SequenceIterator iter = element.iterateAxis(AxisInfo.ATTRIBUTE); |
| 2117 |
NodeInfo att; |
| 2118 |
Properties props = new Properties(); |
| 2119 |
while ((att = (NodeInfo) iter.next()) != null) { |
| 2120 |
props.setProperty(att.getLocalPart(), att.getStringValue()); |
| 2121 |
} |
| 2122 |
fn.importAttributes(props); |
| 2123 |
loader.addCompletionAction(() -> fn.fixArguments(args)); |
| 2124 |
} |
| 2125 |
loader.contextStack.pop(); |
| 2126 |
return e; |
| 2127 |
}); |
| 2128 |
|
| 2129 |
eMap.put("follows", (loader, element) -> { |
| 2130 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2131 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2132 |
return new IdentityComparison(lhs, Token.FOLLOWS, rhs); |
| 2133 |
}); |
| 2134 |
|
| 2135 |
eMap.put("for", (loader, element) -> { |
| 2136 |
Expression select = loader.getFirstChildExpression(element); |
| 2137 |
|
| 2138 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2139 |
StructuredQName name = loader.getQNameAttribute(element, "var"); |
| 2140 |
SequenceType requiredType = loader.parseSequenceType(element, "as"); |
| 2141 |
ForExpression forEx = new ForExpression(); |
| 2142 |
forEx.setSequence(select); |
| 2143 |
forEx.setRequiredType(requiredType); |
| 2144 |
forEx.setSlotNumber(slot); |
| 2145 |
forEx.setVariableQName(name); |
| 2146 |
|
| 2147 |
loader.localBindings.push(forEx); |
| 2148 |
Expression action = loader.getSecondChildExpression(element); |
| 2149 |
loader.localBindings.pop(); |
| 2150 |
forEx.setAction(action); |
| 2151 |
|
| 2152 |
return forEx; |
| 2153 |
}); |
| 2154 |
|
| 2155 |
eMap.put("forEach", (loader, element) -> { |
| 2156 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2157 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2158 |
|
| 2159 |
Expression threads = loader.getExpressionWithRole(element, "threads"); |
| 2160 |
if (threads == null) { |
| 2161 |
return new ForEach(lhs, rhs); |
| 2162 |
} else { |
| 2163 |
Expression forEach = new ForEach(lhs, rhs, false, threads); |
| 2164 |
return loader.getConfiguration().obtainOptimizer().generateMultithreadedInstruction(forEach); |
| 2165 |
} |
| 2166 |
}); |
| 2167 |
|
| 2168 |
eMap.put("forEachGroup", (loader, element) -> { |
| 2169 |
String algorithmAtt = element.getAttributeValue("", "algorithm"); |
| 2170 |
byte algo; |
| 2171 |
if ("by".equals(algorithmAtt)) { |
| 2172 |
algo = ForEachGroup.GROUP_BY; |
| 2173 |
} else if ("adjacent".equals(algorithmAtt)) { |
| 2174 |
algo = ForEachGroup.GROUP_ADJACENT; |
| 2175 |
} else if ("starting".equals(algorithmAtt)) { |
| 2176 |
algo = ForEachGroup.GROUP_STARTING; |
| 2177 |
} else if ("ending".equals(algorithmAtt)) { |
| 2178 |
algo = ForEachGroup.GROUP_ENDING; |
| 2179 |
} else { |
| 2180 |
throw new AssertionError(); |
| 2181 |
} |
| 2182 |
String flags = element.getAttributeValue("", "flags"); |
| 2183 |
boolean composite = flags != null && flags.contains("c"); |
| 2184 |
boolean inFork = flags != null && flags.contains("k"); |
| 2185 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 2186 |
Expression key; |
| 2187 |
if (algo == ForEachGroup.GROUP_BY || algo == ForEachGroup.GROUP_ADJACENT) { |
| 2188 |
key = loader.getExpressionWithRole(element, "key"); |
| 2189 |
} else { |
| 2190 |
key = loader.getPatternWithRole(element, "match"); |
| 2191 |
} |
| 2192 |
SortKeyDefinitionList sortKeys = loader.loadSortKeyDefinitions(element); |
| 2193 |
if (sortKeys.size() == 0) { |
| 2194 |
sortKeys = null; |
| 2195 |
} |
| 2196 |
Expression collationNameExp = loader.getExpressionWithRole(element, "collation"); |
| 2197 |
Expression content = loader.getExpressionWithRole(element, "content"); |
| 2198 |
StringCollator collator = null; |
| 2199 |
if (collationNameExp instanceof StringLiteral) { |
| 2200 |
String collationName = ((StringLiteral) collationNameExp).getStringValue(); |
| 2201 |
collator = loader.config.getCollation(collationName); |
| 2202 |
} |
| 2203 |
ForEachGroup feg = new ForEachGroup( |
| 2204 |
select, content, algo, key, collator, collationNameExp, sortKeys); |
| 2205 |
feg.setComposite(composite); |
| 2206 |
feg.setIsInFork(inFork); |
| 2207 |
return feg; |
| 2208 |
}); |
| 2209 |
|
| 2210 |
eMap.put("fork", (loader, element) -> { |
| 2211 |
Expression[] args = getChildExpressionArray(loader, element); |
| 2212 |
return new Fork(args); |
| 2213 |
}); |
| 2214 |
|
| 2215 |
eMap.put("gc", (loader, element) -> { |
| 2216 |
String opAtt = element.getAttributeValue("", "op"); |
| 2217 |
int op = getOperator(opAtt); |
| 2218 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2219 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2220 |
String compAtt = element.getAttributeValue("", "comp"); |
| 2221 |
AtomicComparer comp = loader.makeAtomicComparer(compAtt, element); |
| 2222 |
GeneralComparison gc = new GeneralComparison20(lhs, op, rhs); |
| 2223 |
gc.setAtomicComparer(comp); |
| 2224 |
return gc; |
| 2225 |
}); |
| 2226 |
|
| 2227 |
|
| 2228 |
eMap.put("gc10", (loader, element) -> { |
| 2229 |
String opAtt = element.getAttributeValue("", "op"); |
| 2230 |
int op = getOperator(opAtt); |
| 2231 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2232 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2233 |
String compAtt = element.getAttributeValue("", "comp"); |
| 2234 |
GeneralComparison10 gc = new GeneralComparison10(lhs, op, rhs); |
| 2235 |
AtomicComparer comp = loader.makeAtomicComparer(compAtt, element); |
| 2236 |
gc.setAtomicComparer(comp); |
| 2237 |
return gc; |
| 2238 |
}); |
| 2239 |
|
| 2240 |
eMap.put("gVarRef", (loader, element) -> { |
| 2241 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2242 |
GlobalVariableReference ref = new GlobalVariableReference(name); |
| 2243 |
int bindingSlot = loader.getIntegerAttribute(element, "bSlot"); |
| 2244 |
ref.setBindingSlot(bindingSlot); |
| 2245 |
loader.fixups.peek().add(ref); |
| 2246 |
return ref; |
| 2247 |
}); |
| 2248 |
|
| 2249 |
eMap.put("homCheck", (loader, element) -> { |
| 2250 |
Expression body = loader.getFirstChildExpression(element); |
| 2251 |
return new HomogeneityChecker(body); |
| 2252 |
}); |
| 2253 |
|
| 2254 |
eMap.put("ifCall", (loader, element) -> { |
| 2255 |
Expression[] args = getChildExpressionArray(loader, element); |
| 2256 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2257 |
Expression exp = null; |
| 2258 |
if (name.hasURI(NamespaceConstant.MATH)) { |
| 2259 |
exp = MathFunctionSet.getInstance().makeFunction(name.getLocalPart(), args.length).makeFunctionCall(args); |
| 2260 |
} else if (name.hasURI(NamespaceConstant.MAP_FUNCTIONS)) { |
| 2261 |
exp = MapFunctionSet.getInstance().makeFunction(name.getLocalPart(), args.length).makeFunctionCall(args); |
| 2262 |
} else if (name.hasURI(NamespaceConstant.ARRAY_FUNCTIONS)) { |
| 2263 |
exp = ArrayFunctionSet.getInstance().makeFunction(name.getLocalPart(), args.length).makeFunctionCall(args); |
| 2264 |
} else if (name.hasURI(NamespaceConstant.SAXON)) { |
| 2265 |
loader.needsPELicense("Saxon extension functions"); |
| 2266 |
exp = null; |
| 2267 |
} |
| 2268 |
if (exp == null) { |
| 2269 |
SymbolicName.F sName = new SymbolicName.F(name, args.length); |
| 2270 |
SequenceType type = loader.parseSequenceType(element, "type"); |
| 2271 |
IndependentContext ic = new IndependentContext(loader.config); |
| 2272 |
RetainedStaticContext rsc = loader.makeRetainedStaticContext(element); |
| 2273 |
ic.setBaseURI(rsc.getStaticBaseUriString()); |
| 2274 |
ic.setPackageData(rsc.getPackageData()); |
| 2275 |
ic.setXPathLanguageLevel(31); |
| 2276 |
ic.setDefaultElementNamespace(rsc.getDefaultElementNamespace()); |
| 2277 |
ic.setNamespaceResolver(rsc); |
| 2278 |
ic.setBackwardsCompatibilityMode(rsc.isBackwardsCompatibility()); |
| 2279 |
ic.setDefaultCollationName(rsc.getDefaultCollationName()); |
| 2280 |
ic.setDefaultFunctionNamespace(rsc.getDefaultFunctionNamespace()); |
| 2281 |
ic.setDecimalFormatManager(rsc.getDecimalFormatManager()); |
| 2282 |
List<String> reasons = new ArrayList<>(); |
| 2283 |
exp = loader.config.getIntegratedFunctionLibrary().bind(sName, args, ic, reasons); |
| 2284 |
if (exp == null) { |
| 2285 |
exp = loader.config.getBuiltInExtensionLibraryList().bind(sName, args, ic, reasons); |
| 2286 |
} |
| 2287 |
if (exp instanceof SystemFunctionCall) { |
| 2288 |
SystemFunction fn = ((SystemFunctionCall) exp).getTargetFunction(); |
| 2289 |
fn.setRetainedStaticContext(loader.makeRetainedStaticContext(element)); |
| 2290 |
SequenceIterator iter = element.iterateAxis(AxisInfo.ATTRIBUTE); |
| 2291 |
NodeInfo att; |
| 2292 |
Properties props = new Properties(); |
| 2293 |
while ((att = (NodeInfo) iter.next()) != null) { |
| 2294 |
props.setProperty(att.getLocalPart(), att.getStringValue()); |
| 2295 |
} |
| 2296 |
fn.importAttributes(props); |
| 2297 |
} |
| 2298 |
if (exp == null) { |
| 2299 |
StringBuilder msg = new StringBuilder("IntegratedFunctionCall to " + sName + " not found"); |
| 2300 |
for (String reason : reasons) { |
| 2301 |
msg.append(". ").append(reason); |
| 2302 |
} |
| 2303 |
throw new XPathException(msg.toString()); |
| 2304 |
} |
| 2305 |
if (exp instanceof IntegratedFunctionCall) { |
| 2306 |
((IntegratedFunctionCall) exp).getFunction().supplyStaticContext(ic, -1, args); |
| 2307 |
((IntegratedFunctionCall) exp).setResultType(type); |
| 2308 |
} |
| 2309 |
} |
| 2310 |
return exp; |
| 2311 |
}); |
| 2312 |
|
| 2313 |
eMap.put("instance", (loader, element) -> { |
| 2314 |
Expression body = loader.getFirstChildExpression(element); |
| 2315 |
SequenceType type = loader.parseSequenceType(element, "of"); |
| 2316 |
return new InstanceOfExpression(body, type); |
| 2317 |
}); |
| 2318 |
|
| 2319 |
eMap.put("int", (loader, element) -> { |
| 2320 |
BigInteger i = new BigInteger(element.getAttributeValue("", "val")); |
| 2321 |
return Literal.makeLiteral(IntegerValue.makeIntegerValue(i)); |
| 2322 |
}); |
| 2323 |
|
| 2324 |
eMap.put("intersect", (loader, element) -> { |
| 2325 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2326 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2327 |
return new VennExpression(lhs, Token.INTERSECT, rhs); |
| 2328 |
}); |
| 2329 |
|
| 2330 |
eMap.put("intRangeTest", (loader, element) -> { |
| 2331 |
Expression val = loader.getFirstChildExpression(element); |
| 2332 |
Expression min = loader.getSecondChildExpression(element); |
| 2333 |
Expression max = loader.getNthChildExpression(element, 2); |
| 2334 |
return new IntegerRangeTest(val, min, max); |
| 2335 |
}); |
| 2336 |
|
| 2337 |
eMap.put("is", (loader, element) -> { |
| 2338 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2339 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2340 |
return new IdentityComparison(lhs, Token.IS, rhs); |
| 2341 |
}); |
| 2342 |
|
| 2343 |
eMap.put("isLast", (loader, element) -> { |
| 2344 |
boolean cond = element.getAttributeValue("", "test").equals("1"); |
| 2345 |
return new IsLastExpression(cond); |
| 2346 |
}); |
| 2347 |
|
| 2348 |
eMap.put("iterate", (loader, element) -> { |
| 2349 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 2350 |
LocalParamBlock params = (LocalParamBlock) loader.getExpressionWithRole(element, "params"); |
| 2351 |
Expression onCompletion = loader.getExpressionWithRole(element, "on-completion"); |
| 2352 |
Expression action = loader.getExpressionWithRole(element, "action"); |
| 2353 |
return new IterateInstr(select, params, action, onCompletion); |
| 2354 |
}); |
| 2355 |
|
| 2356 |
eMap.put("lastOf", (loader, element) -> { |
| 2357 |
Expression base = loader.getFirstChildExpression(element); |
| 2358 |
return new LastItemExpression(base); |
| 2359 |
}); |
| 2360 |
|
| 2361 |
eMap.put("let", (loader, element) -> { |
| 2362 |
Expression select = loader.getFirstChildExpression(element); |
| 2363 |
|
| 2364 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2365 |
int evalMode = loader.getIntegerAttribute(element, "eval"); |
| 2366 |
StructuredQName name = loader.getQNameAttribute(element, "var"); |
| 2367 |
SequenceType requiredType = loader.parseSequenceType(element, "as"); |
| 2368 |
LetExpression let = new LetExpression(); |
| 2369 |
let.setSequence(select); |
| 2370 |
let.setRequiredType(requiredType); |
| 2371 |
let.setSlotNumber(slot); |
| 2372 |
let.setVariableQName(name); |
| 2373 |
let.setEvaluationMode(evalMode); |
| 2374 |
|
| 2375 |
loader.localBindings.push(let); |
| 2376 |
Expression action = loader.getSecondChildExpression(element); |
| 2377 |
loader.localBindings.pop(); |
| 2378 |
let.setAction(action); |
| 2379 |
|
| 2380 |
return let; |
| 2381 |
}); |
| 2382 |
|
| 2383 |
eMap.put("literal", (loader, element) -> { |
| 2384 |
List<Item> children = new ArrayList<>(); |
| 2385 |
AxisIterator iter = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 2386 |
NodeInfo child; |
| 2387 |
while ((child = iter.next()) != null) { |
| 2388 |
Expression e = loader.loadExpression(child); |
| 2389 |
children.add(((Literal) e).getValue().head()); |
| 2390 |
} |
| 2391 |
return Literal.makeLiteral(SequenceExtent.makeSequenceExtent(children)); |
| 2392 |
}); |
| 2393 |
|
| 2394 |
eMap.put("lookup", (loader, element) -> { |
| 2395 |
Expression select = loader.getFirstChildExpression(element); |
| 2396 |
Expression key = loader.getSecondChildExpression(element); |
| 2397 |
return new LookupExpression(select, key); |
| 2398 |
}); |
| 2399 |
|
| 2400 |
|
| 2401 |
eMap.put("lookupAll", (loader, element) -> { |
| 2402 |
Expression select = loader.getFirstChildExpression(element); |
| 2403 |
return new LookupAllExpression(select); |
| 2404 |
}); |
| 2405 |
|
| 2406 |
eMap.put("map", (loader, element) -> { |
| 2407 |
List<Expression> children = getChildExpressionList(loader, element); |
| 2408 |
AtomicValue key = null; |
| 2409 |
HashTrieMap map = new HashTrieMap(); |
| 2410 |
for (Expression child : children) { |
| 2411 |
if (key == null) { |
| 2412 |
key = (AtomicValue)((Literal)child).getValue(); |
| 2413 |
} else { |
| 2414 |
GroundedValue<?> value = ((Literal) child).getValue(); |
| 2415 |
map.initialPut(key, value); |
| 2416 |
key = null; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
return Literal.makeLiteral(map); |
| 2420 |
}); |
| 2421 |
|
| 2422 |
eMap.put("merge", (loader, element) -> { |
| 2423 |
final MergeInstr inst = new MergeInstr(); |
| 2424 |
AxisIterator kids = element.iterateAxis(AxisInfo.CHILD, |
| 2425 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "mergeSrc", loader.config.getNamePool())); |
| 2426 |
NodeInfo msElem; |
| 2427 |
List<MergeInstr.MergeSource> list = new ArrayList<>(); |
| 2428 |
while ((msElem = kids.next()) != null) { |
| 2429 |
final MergeInstr.MergeSource ms = new MergeInstr.MergeSource(inst); |
| 2430 |
String mergeSourceName = msElem.getAttributeValue("", "name"); |
| 2431 |
if (mergeSourceName != null) { |
| 2432 |
ms.sourceName = mergeSourceName; |
| 2433 |
} |
| 2434 |
String valAtt = msElem.getAttributeValue("", "validation"); |
| 2435 |
if (valAtt != null) { |
| 2436 |
ms.validation = Validation.getCode(valAtt); |
| 2437 |
} |
| 2438 |
SchemaType schemaType = loader.getTypeAttribute(msElem, "type"); |
| 2439 |
if (schemaType != null) { |
| 2440 |
ms.schemaType = schemaType; |
| 2441 |
ms.validation = Validation.BY_TYPE; |
| 2442 |
} |
| 2443 |
String flagsAtt = msElem.getAttributeValue("", "flags"); |
| 2444 |
ms.streamable = "s".equals(flagsAtt); |
| 2445 |
if (ms.streamable) { |
| 2446 |
loader.addCompletionAction(ms::prepareForStreaming); |
| 2447 |
} |
| 2448 |
RetainedStaticContext rsc = loader.makeRetainedStaticContext(element); |
| 2449 |
ms.baseURI = rsc.getStaticBaseUriString(); |
| 2450 |
|
| 2451 |
String accumulatorNames = msElem.getAttributeValue("", "accum"); |
| 2452 |
if (accumulatorNames == null) { |
| 2453 |
accumulatorNames = ""; |
| 2454 |
} |
| 2455 |
final List<StructuredQName> accNameList = new ArrayList<>(); |
| 2456 |
StringTokenizer tokenizer = new StringTokenizer(accumulatorNames); |
| 2457 |
while (tokenizer.hasMoreTokens()) { |
| 2458 |
String token = tokenizer.nextToken(); |
| 2459 |
StructuredQName name = StructuredQName.fromEQName(token); |
| 2460 |
accNameList.add(name); |
| 2461 |
} |
| 2462 |
loader.addCompletionAction(new Action() { |
| 2463 |
final StylesheetPackage pack = loader.getPackStack().peek(); |
| 2464 |
|
| 2465 |
public void doAction() { |
| 2466 |
Set<Accumulator> list = new HashSet<>(); |
| 2467 |
for (StructuredQName sn : accNameList) { |
| 2468 |
for (Accumulator test : pack.getAccumulatorRegistry().getAllAccumulators()) { |
| 2469 |
if (test.getAccumulatorName().equals(sn)) { |
| 2470 |
list.add(test); |
| 2471 |
} |
| 2472 |
} |
| 2473 |
} |
| 2474 |
ms.accumulators = list; |
| 2475 |
} |
| 2476 |
}); |
| 2477 |
Expression forEachItem = loader.getExpressionWithRole(msElem, "forEachItem"); |
| 2478 |
if (forEachItem != null) { |
| 2479 |
ms.initForEachItem(inst, forEachItem); |
| 2480 |
} |
| 2481 |
Expression forEachStream = loader.getExpressionWithRole(msElem, "forEachStream"); |
| 2482 |
if (forEachStream != null) { |
| 2483 |
ms.initForEachStream(inst, forEachStream); |
| 2484 |
} |
| 2485 |
Expression selectRows = loader.getExpressionWithRole(msElem, "selectRows"); |
| 2486 |
if (selectRows != null) { |
| 2487 |
ms.initRowSelect(inst, selectRows); |
| 2488 |
} |
| 2489 |
SortKeyDefinitionList keys = loader.loadSortKeyDefinitions(msElem); |
| 2490 |
ms.setMergeKeyDefinitionSet(keys); |
| 2491 |
list.add(ms); |
| 2492 |
} |
| 2493 |
Expression mergeAction = loader.getExpressionWithRole(element, "action"); |
| 2494 |
MergeInstr.MergeSource[] mergeSources = list.toArray(new MergeInstr.MergeSource[0]); |
| 2495 |
inst.init(mergeSources, mergeAction); |
| 2496 |
loader.completionActions.add(inst::fixupGroupReferences); |
| 2497 |
return inst; |
| 2498 |
}); |
| 2499 |
|
| 2500 |
eMap.put("mergeAdj", (loader, element) -> { |
| 2501 |
Expression body = loader.getFirstChildExpression(element); |
| 2502 |
return new AdjacentTextNodeMerger(body); |
| 2503 |
}); |
| 2504 |
|
| 2505 |
eMap.put("message", (loader, element) -> { |
| 2506 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 2507 |
Expression terminate = loader.getExpressionWithRole(element, "terminate"); |
| 2508 |
Expression error = loader.getExpressionWithRole(element, "error"); |
| 2509 |
return new Message(select, terminate, error); |
| 2510 |
}); |
| 2511 |
|
| 2512 |
eMap.put("minus", (loader, element) -> { |
| 2513 |
Expression body = loader.getFirstChildExpression(element); |
| 2514 |
return new NegateExpression(body); |
| 2515 |
}); |
| 2516 |
|
| 2517 |
eMap.put("namespace", (loader, element) -> { |
| 2518 |
Expression name = loader.getFirstChildExpression(element); |
| 2519 |
Expression select = loader.getSecondChildExpression(element); |
| 2520 |
NamespaceConstructor inst = new NamespaceConstructor(name); |
| 2521 |
inst.setSelect(select); |
| 2522 |
return inst; |
| 2523 |
}); |
| 2524 |
|
| 2525 |
eMap.put("nextIteration", (loader, element) -> { |
| 2526 |
NextIteration inst = new NextIteration(); |
| 2527 |
AxisIterator kids = element.iterateAxis(AxisInfo.CHILD, |
| 2528 |
new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "withParam", loader.config.getNamePool())); |
| 2529 |
NodeInfo wp; |
| 2530 |
List<WithParam> params = new ArrayList<>(); |
| 2531 |
while ((wp = kids.next()) != null) { |
| 2532 |
WithParam withParam = new WithParam(); |
| 2533 |
String flags = wp.getAttributeValue("", "flags"); |
| 2534 |
StructuredQName paramName = loader.getQNameAttribute(wp, "name"); |
| 2535 |
withParam.setVariableQName(paramName); |
| 2536 |
int slot = loader.getIntegerAttribute(wp, "slot"); |
| 2537 |
withParam.setSlotNumber(slot); |
| 2538 |
withParam.setRequiredType(SequenceType.ANY_SEQUENCE); |
| 2539 |
withParam.setSelectExpression(inst, loader.getFirstChildExpression(wp)); |
| 2540 |
withParam.setRequiredType(loader.parseSequenceType(wp, "as")); |
| 2541 |
withParam.setTypeChecked(flags != null && flags.contains("c")); |
| 2542 |
params.add(withParam); |
| 2543 |
} |
| 2544 |
inst.setParameters(params.toArray(new WithParam[0])); |
| 2545 |
return inst; |
| 2546 |
}); |
| 2547 |
|
| 2548 |
eMap.put("nextMatch", (loader, element) -> { |
| 2549 |
String flags = element.getAttributeValue("", "flags"); |
| 2550 |
boolean useTailRecursion = false; |
| 2551 |
if (flags != null && flags.contains("t")) { |
| 2552 |
useTailRecursion = true; |
| 2553 |
} |
| 2554 |
NextMatch inst = new NextMatch(useTailRecursion); |
| 2555 |
|
| 2556 |
WithParam[] actuals = loader.loadWithParams(element, inst, false); |
| 2557 |
WithParam[] tunnels = loader.loadWithParams(element, inst, true); |
| 2558 |
inst.setActualParams(actuals); |
| 2559 |
inst.setTunnelParams(tunnels); |
| 2560 |
return inst; |
| 2561 |
}); |
| 2562 |
|
| 2563 |
|
| 2564 |
eMap.put("node", (loader, element) -> { |
| 2565 |
int kind = loader.getIntegerAttribute(element, "kind"); |
| 2566 |
String content = element.getAttributeValue("", "content"); |
| 2567 |
String baseURI = element.getAttributeValue("", "baseURI"); |
| 2568 |
NodeInfo node = null; |
| 2569 |
switch (kind) { |
| 2570 |
case Type.DOCUMENT: |
| 2571 |
case Type.ELEMENT: { |
| 2572 |
StreamSource source = new StreamSource(new StringReader(content), baseURI); |
| 2573 |
node = loader.config.buildDocumentTree(source).getRootNode(); |
| 2574 |
if (kind == Type.ELEMENT) { |
| 2575 |
node = VirtualCopy.makeVirtualCopy(node.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT).next()); |
| 2576 |
} |
| 2577 |
break; |
| 2578 |
} |
| 2579 |
case Type.TEXT: |
| 2580 |
case Type.COMMENT: { |
| 2581 |
Orphan o = new Orphan(loader.getConfiguration()); |
| 2582 |
o.setNodeKind((short) kind); |
| 2583 |
o.setStringValue(content); |
| 2584 |
node = o; |
| 2585 |
break; |
| 2586 |
} |
| 2587 |
default: { |
| 2588 |
Orphan o = new Orphan(loader.getConfiguration()); |
| 2589 |
o.setNodeKind((short) kind); |
| 2590 |
o.setStringValue(content); |
| 2591 |
String prefix = element.getAttributeValue("", "prefix"); |
| 2592 |
String ns = element.getAttributeValue("", "ns"); |
| 2593 |
String local = element.getAttributeValue("", "localName"); |
| 2594 |
if (local != null) { |
| 2595 |
FingerprintedQName name = new FingerprintedQName(prefix == null ? "" : prefix, ns == null ? "" : ns, local); |
| 2596 |
o.setNodeName(name); |
| 2597 |
} |
| 2598 |
node = o; |
| 2599 |
break; |
| 2600 |
} |
| 2601 |
} |
| 2602 |
|
| 2603 |
return Literal.makeLiteral(new One<>(node)); |
| 2604 |
}); |
| 2605 |
|
| 2606 |
eMap.put("nodeNum", (loader, element) -> { |
| 2607 |
String levelAtt = element.getAttributeValue("", "level"); |
| 2608 |
int level = getLevelCode(levelAtt); |
| 2609 |
|
| 2610 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 2611 |
Pattern count = loader.getPatternWithRole(element, "count"); |
| 2612 |
Pattern from = loader.getPatternWithRole(element, "from"); |
| 2613 |
return new NumberInstruction(select, level, count, from); |
| 2614 |
}); |
| 2615 |
|
| 2616 |
eMap.put("numSeqFmt", (loader, element) -> { |
| 2617 |
Expression value = loader.getExpressionWithRole(element, "value"); |
| 2618 |
Expression format = loader.getExpressionWithRole(element, "format"); |
| 2619 |
if (format == null) { |
| 2620 |
format = new StringLiteral("1"); |
| 2621 |
} |
| 2622 |
Expression groupSize = loader.getExpressionWithRole(element, "gpSize"); |
| 2623 |
Expression groupSeparator = loader.getExpressionWithRole(element, "gpSep"); |
| 2624 |
Expression letterValue = loader.getExpressionWithRole(element, "letterValue"); |
| 2625 |
Expression ordinal = loader.getExpressionWithRole(element, "ordinal"); |
| 2626 |
Expression startAt = loader.getExpressionWithRole(element, "startAt"); |
| 2627 |
Expression lang = loader.getExpressionWithRole(element, "lang"); |
| 2628 |
String flags = element.getAttributeValue("", "flags"); |
| 2629 |
boolean backwardsCompatible = flags != null && flags.contains("1"); |
| 2630 |
NumberFormatter formatter = null; |
| 2631 |
|
| 2632 |
NumberSequenceFormatter ni = new NumberSequenceFormatter(value, format, groupSize, groupSeparator, |
| 2633 |
letterValue, ordinal, startAt, lang, formatter, backwardsCompatible); |
| 2634 |
ni.preallocateNumberer(loader.config); |
| 2635 |
return ni; |
| 2636 |
}); |
| 2637 |
|
| 2638 |
eMap.put("onEmpty", (loader, element) -> { |
| 2639 |
Expression base = loader.getFirstChildExpression(element); |
| 2640 |
return new OnEmptyExpr(base); |
| 2641 |
}); |
| 2642 |
|
| 2643 |
eMap.put("onNonEmpty", (loader, element) -> { |
| 2644 |
Expression base = loader.getFirstChildExpression(element); |
| 2645 |
return new OnNonEmptyExpr(base); |
| 2646 |
}); |
| 2647 |
|
| 2648 |
eMap.put("or", (loader, element) -> { |
| 2649 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2650 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2651 |
return new OrExpression(lhs, rhs); |
| 2652 |
}); |
| 2653 |
|
| 2654 |
eMap.put("origFC", (loader, element) -> { |
| 2655 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2656 |
String packKey = element.getAttributeValue("", "pack"); |
| 2657 |
StylesheetPackage declPack = loader.allPackages.get(packKey); |
| 2658 |
if (declPack == null) { |
| 2659 |
throw new XPathException("Unknown package key " + packKey); |
| 2660 |
} |
| 2661 |
|
| 2662 |
Expression[] args = getChildExpressionArray(loader, element); |
| 2663 |
int arity = args.length; |
| 2664 |
SymbolicName sn = new SymbolicName.F(name, arity); |
| 2665 |
Component target = declPack.getComponent(sn); |
| 2666 |
OriginalFunction orig = new OriginalFunction(target); |
| 2667 |
return new StaticFunctionCall(orig, args); |
| 2668 |
}); |
| 2669 |
|
| 2670 |
eMap.put("param", (loader, element) -> { |
| 2671 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2672 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2673 |
LocalParam param = new LocalParam(); |
| 2674 |
param.setVariableQName(name); |
| 2675 |
param.setSlotNumber(slot); |
| 2676 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 2677 |
if (select != null) { |
| 2678 |
param.setSelectExpression(select); |
| 2679 |
param.computeEvaluationMode(); |
| 2680 |
} |
| 2681 |
Expression convert = loader.getExpressionWithRole(element, "conversion"); |
| 2682 |
if (convert != null) { |
| 2683 |
param.setConversion(convert); |
| 2684 |
} |
| 2685 |
param.setRequiredType(loader.parseSequenceType(element, "as")); |
| 2686 |
String flags = element.getAttributeValue("", "flags"); |
| 2687 |
if (flags != null) { |
| 2688 |
param.setTunnel(flags.contains("t")); |
| 2689 |
param.setRequiredParam(flags.contains("r")); |
| 2690 |
param.setImplicitlyRequiredParam(flags.contains("i")); |
| 2691 |
} |
| 2692 |
loader.localBindings.add(param); |
| 2693 |
return param; |
| 2694 |
}); |
| 2695 |
|
| 2696 |
eMap.put("params", (loader, element) -> { |
| 2697 |
List<LocalParam> children = new ArrayList<>(); |
| 2698 |
AxisIterator iter = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 2699 |
NodeInfo child; |
| 2700 |
while ((child = iter.next()) != null) { |
| 2701 |
children.add((LocalParam) loader.loadExpression(child)); |
| 2702 |
} |
| 2703 |
return new LocalParamBlock(children.toArray(new LocalParam[0])); |
| 2704 |
}); |
| 2705 |
|
| 2706 |
eMap.put("precedes", (loader, element) -> { |
| 2707 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2708 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2709 |
return new IdentityComparison(lhs, Token.PRECEDES, rhs); |
| 2710 |
}); |
| 2711 |
|
| 2712 |
eMap.put("procInst", (loader, element) -> { |
| 2713 |
Expression name = loader.getFirstChildExpression(element); |
| 2714 |
Expression select = loader.getSecondChildExpression(element); |
| 2715 |
ProcessingInstruction inst = new ProcessingInstruction(name); |
| 2716 |
inst.setSelect(select); |
| 2717 |
return inst; |
| 2718 |
}); |
| 2719 |
|
| 2720 |
eMap.put("qName", (loader, element) -> { |
| 2721 |
String preAtt = element.getAttributeValue("", "pre"); |
| 2722 |
String uriAtt = element.getAttributeValue("", "uri"); |
| 2723 |
String locAtt = element.getAttributeValue("", "loc"); |
| 2724 |
AtomicType type = BuiltInAtomicType.QNAME; |
| 2725 |
if (element.getAttributeValue("", "type") != null) { |
| 2726 |
type = (AtomicType) loader.parseItemTypeAttribute(element, "type"); |
| 2727 |
} |
| 2728 |
QualifiedNameValue val; |
| 2729 |
if (type.getPrimitiveType() == StandardNames.XS_QNAME) { |
| 2730 |
val = new QNameValue(preAtt, uriAtt, locAtt, type, false); |
| 2731 |
} else { |
| 2732 |
val = new NotationValue(preAtt, uriAtt, locAtt, null); |
| 2733 |
val.setTypeLabel(type); |
| 2734 |
} |
| 2735 |
return Literal.makeLiteral(val); |
| 2736 |
}); |
| 2737 |
|
| 2738 |
eMap.put("range", (loader, element) -> { |
| 2739 |
int from = loader.getIntegerAttribute(element, "from"); |
| 2740 |
int to = loader.getIntegerAttribute(element, "to"); |
| 2741 |
return Literal.makeLiteral(new IntegerRange(from, to)); |
| 2742 |
}); |
| 2743 |
|
| 2744 |
eMap.put("resultDoc", (loader, element) -> { |
| 2745 |
loader.packStack.peek().setCreatesSecondaryResultDocuments(true); |
| 2746 |
Expression href = null; |
| 2747 |
Expression format = null; |
| 2748 |
Expression content = null; |
| 2749 |
String globalProps = element.getAttributeValue("", "global"); |
| 2750 |
String localProps = element.getAttributeValue("", "local"); |
| 2751 |
Properties globals = globalProps == null ? new Properties() : loader.importProperties(globalProps); |
| 2752 |
Properties locals = localProps == null ? new Properties() : loader.importProperties(localProps); |
| 2753 |
Map<StructuredQName, Expression> dynamicProperties = new HashMap<>(); |
| 2754 |
NodeInfo child; |
| 2755 |
AxisIterator iter = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 2756 |
while ((child = iter.next()) != null) { |
| 2757 |
Expression exp = loader.loadExpression(child); |
| 2758 |
String role = child.getAttributeValue("", "role"); |
| 2759 |
if ("href".equals(role)) { |
| 2760 |
href = exp; |
| 2761 |
} else if ("format".equals(role)) { |
| 2762 |
format = exp; |
| 2763 |
} else if ("content".equals(role)) { |
| 2764 |
content = exp; |
| 2765 |
} else { |
| 2766 |
StructuredQName name = StructuredQName.fromEQName(role); |
| 2767 |
dynamicProperties.put(name, exp); |
| 2768 |
} |
| 2769 |
} |
| 2770 |
int validation = Validation.SKIP; |
| 2771 |
String valAtt = element.getAttributeValue("", "validation"); |
| 2772 |
if (valAtt != null) { |
| 2773 |
validation = Validation.getCode(valAtt); |
| 2774 |
} |
| 2775 |
SchemaType schemaType = null; |
| 2776 |
StructuredQName typeAtt = loader.getQNameAttribute(element, "type"); |
| 2777 |
if (typeAtt != null) { |
| 2778 |
schemaType = loader.config.getSchemaType(typeAtt); |
| 2779 |
validation = Validation.BY_TYPE; |
| 2780 |
} |
| 2781 |
ResultDocument instr = new ResultDocument(globals, locals, href, format, validation, schemaType, |
| 2782 |
dynamicProperties, loader.packStack.peek().getCharacterMapIndex()); |
| 2783 |
instr.setContentExpression(content); |
| 2784 |
return instr; |
| 2785 |
}); |
| 2786 |
|
| 2787 |
eMap.put("root", (loader, element) -> new RootExpression()); |
| 2788 |
|
| 2789 |
eMap.put("saxonDoctype", (loader, element) -> { |
| 2790 |
Expression arg = loader.getFirstChildExpression(element); |
| 2791 |
return new Doctype(arg); |
| 2792 |
}); |
| 2793 |
|
| 2794 |
eMap.put("sequence", (loader, element) -> { |
| 2795 |
Expression[] args = getChildExpressionArray(loader, element); |
| 2796 |
return new Block(args); |
| 2797 |
}); |
| 2798 |
|
| 2799 |
eMap.put("slash", (loader, element) -> { |
| 2800 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2801 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2802 |
String simpleAtt = element.getAttributeValue("", "simple"); |
| 2803 |
if ("1".equals(simpleAtt)) { |
| 2804 |
return new SimpleStepExpression(lhs, rhs); |
| 2805 |
} else { |
| 2806 |
SlashExpression se = new SlashExpression(lhs, rhs); |
| 2807 |
if ("2".equals(simpleAtt)) { |
| 2808 |
se.setContextFree(true); |
| 2809 |
} |
| 2810 |
return se; |
| 2811 |
} |
| 2812 |
}); |
| 2813 |
|
| 2814 |
eMap.put("some", (loader, element) -> { |
| 2815 |
Expression select = loader.getFirstChildExpression(element); |
| 2816 |
|
| 2817 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2818 |
StructuredQName name = loader.getQNameAttribute(element, "var"); |
| 2819 |
SequenceType requiredType = loader.parseSequenceType(element, "as"); |
| 2820 |
QuantifiedExpression qEx = new QuantifiedExpression(); |
| 2821 |
qEx.setOperator(Token.SOME); |
| 2822 |
qEx.setSequence(select); |
| 2823 |
qEx.setRequiredType(requiredType); |
| 2824 |
qEx.setSlotNumber(slot); |
| 2825 |
qEx.setVariableQName(name); |
| 2826 |
|
| 2827 |
loader.localBindings.push(qEx); |
| 2828 |
Expression action = loader.getSecondChildExpression(element); |
| 2829 |
loader.localBindings.pop(); |
| 2830 |
qEx.setAction(action); |
| 2831 |
|
| 2832 |
return qEx; |
| 2833 |
}); |
| 2834 |
|
| 2835 |
eMap.put("sort", (loader, element) -> { |
| 2836 |
Expression body = loader.getFirstChildExpression(element); |
| 2837 |
SortKeyDefinitionList sortKeys = loader.loadSortKeyDefinitions(element); |
| 2838 |
return new SortExpression(body, sortKeys); |
| 2839 |
}); |
| 2840 |
|
| 2841 |
eMap.put("sourceDoc", (loader, element) -> { |
| 2842 |
int valSpecified = loader.getIntegerAttribute(element, "validation"); |
| 2843 |
int validation = valSpecified == Integer.MIN_VALUE ? Validation.SKIP : valSpecified; |
| 2844 |
SchemaType schemaType = null; |
| 2845 |
StructuredQName typeAtt = loader.getQNameAttribute(element, "schemaType"); |
| 2846 |
if (typeAtt != null) { |
| 2847 |
schemaType = loader.getConfiguration().getSchemaType(typeAtt); |
| 2848 |
validation = Validation.BY_TYPE; |
| 2849 |
} |
| 2850 |
final ParseOptions options = new ParseOptions(loader.getConfiguration().getParseOptions()); |
| 2851 |
options.setSchemaValidationMode(validation); |
| 2852 |
options.setTopLevelType(schemaType); |
| 2853 |
String flags = element.getAttributeValue("", "flags"); |
| 2854 |
if (flags != null && flags.contains("s")) { |
| 2855 |
loader.addCompletionAction(() -> options.setSpaceStrippingRule(loader.getPackage().getSpaceStrippingRule())); |
| 2856 |
} |
| 2857 |
Expression body = loader.getExpressionWithRole(element, "body"); |
| 2858 |
Expression href = loader.getExpressionWithRole(element, "href"); |
| 2859 |
|
| 2860 |
final SourceDocument inst = new SourceDocument(href, body, options); |
| 2861 |
|
| 2862 |
String accumulatorNames = element.getAttributeValue("", "accum"); |
| 2863 |
processAccumulatorList(loader, inst, accumulatorNames); |
| 2864 |
return inst; |
| 2865 |
}); |
| 2866 |
|
| 2867 |
eMap.put("str", (loader, element) -> StringLiteral.makeLiteral( |
| 2868 |
new StringValue(element.getAttributeValue("", "val")) |
| 2869 |
)); |
| 2870 |
|
| 2871 |
|
| 2872 |
eMap.put("subscript", (loader, element) -> { |
| 2873 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2874 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2875 |
return new SubscriptExpression(lhs, rhs); |
| 2876 |
}); |
| 2877 |
|
| 2878 |
eMap.put("supplied", (loader, element) -> { |
| 2879 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2880 |
return new SuppliedParameterReference(slot); |
| 2881 |
}); |
| 2882 |
|
| 2883 |
|
| 2884 |
eMap.put("tail", (loader, element) -> { |
| 2885 |
Expression select = loader.getFirstChildExpression(element); |
| 2886 |
int start = loader.getIntegerAttribute(element, "start"); |
| 2887 |
return new TailExpression(select, start); |
| 2888 |
}); |
| 2889 |
|
| 2890 |
eMap.put("tailCallLoop", (loader, element) -> { |
| 2891 |
Expression body = loader.getFirstChildExpression(element); |
| 2892 |
return new TailCallLoop(loader.currentFunction, body); |
| 2893 |
}); |
| 2894 |
|
| 2895 |
eMap.put("to", (loader, element) -> { |
| 2896 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2897 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2898 |
return new RangeExpression(lhs, rhs); |
| 2899 |
}); |
| 2900 |
|
| 2901 |
eMap.put("treat", (loader, element) -> { |
| 2902 |
Expression body = loader.getFirstChildExpression(element); |
| 2903 |
ItemType type = loader.parseItemTypeAttribute(element, "as"); |
| 2904 |
RoleDiagnostic role = RoleDiagnostic.reconstruct(element.getAttributeValue("", "diag")); |
| 2905 |
return new ItemChecker(body, type, role); |
| 2906 |
}); |
| 2907 |
|
| 2908 |
eMap.put("true", (loader, element) -> Literal.makeLiteral(BooleanValue.TRUE)); |
| 2909 |
|
| 2910 |
eMap.put("try", (loader, element) -> { |
| 2911 |
Expression tryExp = loader.getFirstChildExpression(element); |
| 2912 |
TryCatch tryCatch = new TryCatch(tryExp); |
| 2913 |
if ("r".equals(element.getAttributeValue("", "flags"))) { |
| 2914 |
tryCatch.setRollbackOutput(true); |
| 2915 |
} |
| 2916 |
AxisIterator iter = element.iterateAxis( |
| 2917 |
AxisInfo.CHILD, new NameTest(Type.ELEMENT, NamespaceConstant.SAXON_XSLT_EXPORT, "catch", loader.config.getNamePool())); |
| 2918 |
NodeInfo catchElement; |
| 2919 |
while ((catchElement = iter.next()) != null) { |
| 2920 |
String errAtt = catchElement.getAttributeValue("", "err"); |
| 2921 |
String[] tests = errAtt.split("\\|"); |
| 2922 |
QNameTest test; |
| 2923 |
if (tests.length == 1) { |
| 2924 |
test = (QNameTest) loader.parseItemTypeAttribute(catchElement, "err"); |
| 2925 |
} else { |
| 2926 |
List<QNameTest> list = new ArrayList<>(); |
| 2927 |
for (String s : tests) { |
| 2928 |
QNameTest qt = (QNameTest) loader.parseItemType(catchElement, s); |
| 2929 |
list.add(qt); |
| 2930 |
} |
| 2931 |
test = new UnionQNameTest(list); |
| 2932 |
} |
| 2933 |
Expression catchExpr = loader.getFirstChildExpression(catchElement); |
| 2934 |
tryCatch.addCatchExpression(test, catchExpr); |
| 2935 |
} |
| 2936 |
return tryCatch; |
| 2937 |
}); |
| 2938 |
|
| 2939 |
eMap.put("ufCall", (loader, element) -> { |
| 2940 |
Expression[] args = getChildExpressionArray(loader, element); |
| 2941 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2942 |
UserFunctionCall call = new UserFunctionCall(); |
| 2943 |
call.setFunctionName(name); |
| 2944 |
call.setArguments(args); |
| 2945 |
int bindingSlot = loader.getIntegerAttribute(element, "bSlot"); |
| 2946 |
call.setBindingSlot(bindingSlot); |
| 2947 |
String eval = element.getAttributeValue("", "eval"); |
| 2948 |
if (eval != null) { |
| 2949 |
String[] evals = eval.split(" "); |
| 2950 |
int[] evalModes = new int[evals.length]; |
| 2951 |
for (int i = 0; i < evals.length; i++) { |
| 2952 |
evalModes[i] = Integer.parseInt(evals[i]); |
| 2953 |
} |
| 2954 |
call.setArgumentEvaluationModes(evalModes); |
| 2955 |
} |
| 2956 |
loader.fixups.peek().add(call); |
| 2957 |
return call; |
| 2958 |
}); |
| 2959 |
|
| 2960 |
eMap.put("union", (loader, element) -> { |
| 2961 |
Expression lhs = loader.getFirstChildExpression(element); |
| 2962 |
Expression rhs = loader.getSecondChildExpression(element); |
| 2963 |
return new VennExpression(lhs, Token.UNION, rhs); |
| 2964 |
}); |
| 2965 |
|
| 2966 |
eMap.put("useAS", (loader, element) -> { |
| 2967 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2968 |
boolean streamable = "s".equals(element.getAttributeValue("", "flags")); |
| 2969 |
UseAttributeSet use = new UseAttributeSet(name, streamable); |
| 2970 |
int bindingSlot = loader.getIntegerAttribute(element, "bSlot"); |
| 2971 |
use.setBindingSlot(bindingSlot); |
| 2972 |
loader.fixups.peek().add(use); |
| 2973 |
return use; |
| 2974 |
}); |
| 2975 |
|
| 2976 |
eMap.put("valueOf", (loader, element) -> { |
| 2977 |
Expression select = loader.getFirstChildExpression(element); |
| 2978 |
String flags = element.getAttributeValue("", "flags"); |
| 2979 |
boolean doe = flags != null && flags.contains("d"); |
| 2980 |
boolean notIfEmpty = flags != null && flags.contains("e"); |
| 2981 |
return new ValueOf(select, doe, notIfEmpty); |
| 2982 |
}); |
| 2983 |
|
| 2984 |
eMap.put("varRef", (loader, element) -> { |
| 2985 |
StructuredQName name = loader.getQNameAttribute(element, "name"); |
| 2986 |
Stack<LocalBinding> locals = loader.localBindings; |
| 2987 |
LocalBinding binding = null; |
| 2988 |
for (int i = locals.size() - 1; i >= 0; i--) { |
| 2989 |
LocalBinding b = locals.get(i); |
| 2990 |
if (b.getVariableQName().equals(name)) { |
| 2991 |
binding = b; |
| 2992 |
break; |
| 2993 |
} |
| 2994 |
} |
| 2995 |
if (binding == null) { |
| 2996 |
throw new XPathException("No binding found for local variable " + name); |
| 2997 |
} |
| 2998 |
int slot = loader.getIntegerAttribute(element, "slot"); |
| 2999 |
LocalVariableReference ref = new LocalVariableReference(binding); |
| 3000 |
ref.setSlotNumber(slot); |
| 3001 |
return ref; |
| 3002 |
}); |
| 3003 |
|
| 3004 |
eMap.put("vc", (loader, element) -> { |
| 3005 |
String opAtt = element.getAttributeValue("", "op"); |
| 3006 |
int op; |
| 3007 |
op = parseValueComparisonOperator(opAtt); |
| 3008 |
Expression lhs = loader.getFirstChildExpression(element); |
| 3009 |
Expression rhs = loader.getSecondChildExpression(element); |
| 3010 |
ValueComparison vc = new ValueComparison(lhs, op, rhs); |
| 3011 |
String compAtt = element.getAttributeValue("", "comp"); |
| 3012 |
AtomicComparer comp = loader.makeAtomicComparer(compAtt, element); |
| 3013 |
vc.setAtomicComparer(comp); |
| 3014 |
String onEmptyAtt = element.getAttributeValue("", "onEmpty"); |
| 3015 |
if (onEmptyAtt != null) { |
| 3016 |
vc.setResultWhenEmpty(BooleanValue.get("1".equals(onEmptyAtt))); |
| 3017 |
} |
| 3018 |
return vc; |
| 3019 |
}); |
| 3020 |
|
| 3021 |
eMap.put("xslNumber", (loader, element) -> { |
| 3022 |
|
| 3023 |
String levelAtt = element.getAttributeValue("", "level"); |
| 3024 |
int level = getLevelCode(levelAtt); |
| 3025 |
|
| 3026 |
Expression select = loader.getExpressionWithRole(element, "select"); |
| 3027 |
Expression value = loader.getExpressionWithRole(element, "value"); |
| 3028 |
Expression format = loader.getExpressionWithRole(element, "format"); |
| 3029 |
if (format == null) { |
| 3030 |
format = new StringLiteral("1"); |
| 3031 |
} |
| 3032 |
Expression groupSize = loader.getExpressionWithRole(element, "gpSize"); |
| 3033 |
Expression groupSeparator = loader.getExpressionWithRole(element, "gpSep"); |
| 3034 |
Expression letterValue = loader.getExpressionWithRole(element, "letterValue"); |
| 3035 |
Expression ordinal = loader.getExpressionWithRole(element, "ordinal"); |
| 3036 |
Expression startAt = loader.getExpressionWithRole(element, "startAt"); |
| 3037 |
Expression lang = loader.getExpressionWithRole(element, "lang"); |
| 3038 |
String flags = element.getAttributeValue("", "flags"); |
| 3039 |
boolean backwardsCompatible = flags != null && flags.contains("1"); |
| 3040 |
NumberFormatter formatter = null; |
| 3041 |
|
| 3042 |
Pattern count = loader.getPatternWithRole(element, "count"); |
| 3043 |
Pattern from = loader.getPatternWithRole(element, "from"); |
| 3044 |
if (value == null) { |
| 3045 |
value = new NumberInstruction( |
| 3046 |
select, |
| 3047 |
level, |
| 3048 |
count, |
| 3049 |
from); |
| 3050 |
} |
| 3051 |
NumberSequenceFormatter ni = new NumberSequenceFormatter( |
| 3052 |
value, |
| 3053 |
format, |
| 3054 |
groupSize, |
| 3055 |
groupSeparator, |
| 3056 |
letterValue, |
| 3057 |
ordinal, |
| 3058 |
startAt, |
| 3059 |
lang, |
| 3060 |
formatter, |
| 3061 |
backwardsCompatible); |
| 3062 |
ni.preallocateNumberer(loader.config); |
| 3063 |
return ni; |
| 3064 |
}); |
| 3065 |
} |
| 3066 |
|
| 3067 |
private static int getLevelCode(String levelAtt) { |
| 3068 |
int level; |
| 3069 |
if (levelAtt == null) { |
| 3070 |
level = NumberInstruction.SINGLE; |
| 3071 |
} else if (levelAtt.equals("single")) { |
| 3072 |
level = NumberInstruction.SINGLE; |
| 3073 |
} else if (levelAtt.equals("multi")) { |
| 3074 |
level = NumberInstruction.MULTI; |
| 3075 |
} else if (levelAtt.equals("any")) { |
| 3076 |
level = NumberInstruction.ANY; |
| 3077 |
} else if (levelAtt.equals("simple")) { |
| 3078 |
level = NumberInstruction.SIMPLE; |
| 3079 |
} else { |
| 3080 |
throw new AssertionError(); |
| 3081 |
} |
| 3082 |
return level; |
| 3083 |
} |
| 3084 |
|
| 3085 |
protected static List<Expression> getChildExpressionList(PackageLoaderHE loader, NodeInfo element) throws XPathException { |
| 3086 |
List<Expression> children = new ArrayList<>(); |
| 3087 |
AxisIterator iter = element.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 3088 |
NodeInfo child; |
| 3089 |
while ((child = iter.next()) != null) { |
| 3090 |
children.add(loader.loadExpression(child)); |
| 3091 |
} |
| 3092 |
return children; |
| 3093 |
} |
| 3094 |
|
| 3095 |
protected static Expression[] getChildExpressionArray(PackageLoaderHE loader, NodeInfo element) throws XPathException { |
| 3096 |
List<Expression> children = getChildExpressionList(loader, element); |
| 3097 |
return children.toArray(new Expression[0]); |
| 3098 |
} |
| 3099 |
|
| 3100 |
protected static int getOperator(String opAtt) { |
| 3101 |
int op; |
| 3102 |
switch (opAtt) { |
| 3103 |
case "=": |
| 3104 |
op = Token.EQUALS; |
| 3105 |
break; |
| 3106 |
case "!=": |
| 3107 |
op = Token.NE; |
| 3108 |
break; |
| 3109 |
case "<=": |
| 3110 |
op = Token.LE; |
| 3111 |
break; |
| 3112 |
case ">=": |
| 3113 |
op = Token.GE; |
| 3114 |
break; |
| 3115 |
case "<": |
| 3116 |
op = Token.LT; |
| 3117 |
break; |
| 3118 |
case ">": |
| 3119 |
op = Token.GT; |
| 3120 |
break; |
| 3121 |
default: |
| 3122 |
throw new IllegalStateException(); |
| 3123 |
} |
| 3124 |
return op; |
| 3125 |
} |
| 3126 |
|
| 3127 |
private static int parseValueComparisonOperator(String opAtt) { |
| 3128 |
int op; |
| 3129 |
switch (opAtt) { |
| 3130 |
case "eq": |
| 3131 |
op = Token.FEQ; |
| 3132 |
break; |
| 3133 |
case "ne": |
| 3134 |
op = Token.FNE; |
| 3135 |
break; |
| 3136 |
case "le": |
| 3137 |
op = Token.FLE; |
| 3138 |
break; |
| 3139 |
case "ge": |
| 3140 |
op = Token.FGE; |
| 3141 |
break; |
| 3142 |
case "lt": |
| 3143 |
op = Token.FLT; |
| 3144 |
break; |
| 3145 |
case "gt": |
| 3146 |
op = Token.FGT; |
| 3147 |
break; |
| 3148 |
default: |
| 3149 |
throw new IllegalStateException(); |
| 3150 |
} |
| 3151 |
return op; |
| 3152 |
} |
| 3153 |
|
| 3154 |
static final Map<String, PatternLoader> pMap = new HashMap<>(200); |
| 3155 |
|
| 3156 |
static { |
| 3157 |
|
| 3158 |
pMap.put("p.anchor", (loader, element) -> AnchorPattern.getInstance()); |
| 3159 |
|
| 3160 |
pMap.put("p.any", (loader, element) -> new UniversalPattern()); |
| 3161 |
|
| 3162 |
pMap.put("p.booleanExp", (loader, element) -> { |
| 3163 |
Expression exp = loader.getFirstChildExpression(element); |
| 3164 |
return new BooleanExpressionPattern(exp); |
| 3165 |
}); |
| 3166 |
|
| 3167 |
pMap.put("p.genNode", (loader, element) -> { |
| 3168 |
NodeTest type = (NodeTest) loader.parseItemTypeAttribute(element, "type"); |
| 3169 |
Expression exp = loader.getFirstChildExpression(element); |
| 3170 |
return new GeneralNodePattern(exp, type); |
| 3171 |
}); |
| 3172 |
|
| 3173 |
pMap.put("p.genPos", (loader, element) -> { |
| 3174 |
NodeTest type = (NodeTest) loader.parseItemTypeAttribute(element, "type"); |
| 3175 |
Expression exp = loader.getFirstChildExpression(element); |
| 3176 |
String flags = element.getAttributeValue("", "flags"); |
| 3177 |
GeneralPositionalPattern gpp = new GeneralPositionalPattern(type, exp); |
| 3178 |
gpp.setUsesPosition(!"P".equals(flags)); |
| 3179 |
return gpp; |
| 3180 |
}); |
| 3181 |
|
| 3182 |
pMap.put("p.nodeSet", (loader, element) -> { |
| 3183 |
ItemType type = loader.parseItemTypeAttribute(element, "type"); |
| 3184 |
Expression select = loader.getFirstChildExpression(element); |
| 3185 |
NodeSetPattern pat = new NodeSetPattern(select); |
| 3186 |
pat.setItemType(type); |
| 3187 |
return pat; |
| 3188 |
}); |
| 3189 |
|
| 3190 |
pMap.put("p.nodeTest", (loader, element) -> { |
| 3191 |
NodeTest test = (NodeTest) loader.parseItemTypeAttribute(element, "test"); |
| 3192 |
return new NodeTestPattern(test); |
| 3193 |
}); |
| 3194 |
|
| 3195 |
pMap.put("p.venn", (loader, element) -> { |
| 3196 |
Pattern p0 = loader.getFirstChildPattern(element); |
| 3197 |
Pattern p1 = loader.getSecondChildPattern(element); |
| 3198 |
String operator = element.getAttributeValue("", "op"); |
| 3199 |
switch (operator) { |
| 3200 |
case "union": |
| 3201 |
return new UnionPattern(p0, p1); |
| 3202 |
case "intersect": |
| 3203 |
return new IntersectPattern(p0, p1); |
| 3204 |
case "except": |
| 3205 |
return new ExceptPattern(p0, p1); |
| 3206 |
} |
| 3207 |
return null; |
| 3208 |
}); |
| 3209 |
|
| 3210 |
pMap.put("p.simPos", (loader, element) -> { |
| 3211 |
NodeTest test = (NodeTest) loader.parseItemTypeAttribute(element, "test"); |
| 3212 |
int pos = loader.getIntegerAttribute(element, "pos"); |
| 3213 |
return new SimplePositionalPattern(test, pos); |
| 3214 |
}); |
| 3215 |
|
| 3216 |
pMap.put("p.withCurrent", (loader, element) -> { |
| 3217 |
LocalVariableBinding let = new LocalVariableBinding(Current.FN_CURRENT, SequenceType.SINGLE_ITEM); |
| 3218 |
let.setSlotNumber(0); |
| 3219 |
loader.localBindings.push(let); |
| 3220 |
Pattern p0 = loader.getFirstChildPattern(element); |
| 3221 |
loader.localBindings.pop(); |
| 3222 |
return new PatternThatSetsCurrent(p0, let); |
| 3223 |
}); |
| 3224 |
|
| 3225 |
pMap.put("p.withUpper", (loader, element) -> { |
| 3226 |
String axisName = element.getAttributeValue("", "axis"); |
| 3227 |
byte axis = AxisInfo.getAxisNumber(axisName); |
| 3228 |
Pattern basePattern = loader.getFirstChildPattern(element); |
| 3229 |
Pattern upperPattern = loader.getSecondChildPattern(element); |
| 3230 |
return new AncestorQualifiedPattern(basePattern, upperPattern, axis); |
| 3231 |
}); |
| 3232 |
|
| 3233 |
pMap.put("p.withPredicate", (loader, element) -> { |
| 3234 |
Pattern basePattern = loader.getFirstChildPattern(element); |
| 3235 |
Expression predicate = loader.getSecondChildExpression(element); |
| 3236 |
return new BasePatternWithPredicate(basePattern, predicate); |
| 3237 |
}); |
| 3238 |
|
| 3239 |
} |
| 3240 |
|
| 3241 |
private void resolveFixups() throws XPathException { |
| 3242 |
StylesheetPackage pack = packStack.peek(); |
| 3243 |
for (ComponentInvocation call : fixups.peek()) { |
| 3244 |
if (processComponentReference(pack, call)) { |
| 3245 |
break; |
| 3246 |
} |
| 3247 |
} |
| 3248 |
pack.allocateBinderySlots(); |
| 3249 |
} |
| 3250 |
|
| 3251 |
protected boolean processComponentReference(StylesheetPackage pack, ComponentInvocation call) throws XPathException { |
| 3252 |
SymbolicName sn = call.getSymbolicName(); |
| 3253 |
Component c = pack.getComponent(sn); |
| 3254 |
if (c == null) { |
| 3255 |
if (sn.getComponentName().hasURI(NamespaceConstant.XSLT) && sn.getComponentName().getLocalPart().equals("original")) { |
| 3256 |
return true; |
| 3257 |
} else { |
| 3258 |
throw new XPathException("Loading compiled package: unresolved component reference to " + sn); |
| 3259 |
} |
| 3260 |
} |
| 3261 |
if (call instanceof GlobalVariableReference) { |
| 3262 |
((GlobalVariableReference) call).setTarget(c); |
| 3263 |
} else if (call instanceof UserFunctionCall) { |
| 3264 |
((UserFunctionCall) call).setFunction((UserFunction) c.getActor()); |
| 3265 |
((UserFunctionCall) call).setStaticType(((UserFunction) c.getActor()).getResultType()); |
| 3266 |
|
| 3267 |
|
| 3268 |
} else if (call instanceof CallTemplate) { |
| 3269 |
((CallTemplate) call).setTargetTemplate((NamedTemplate) c.getActor()); |
| 3270 |
} else if (call instanceof UseAttributeSet) { |
| 3271 |
((UseAttributeSet) call).setTarget((AttributeSet) c.getActor()); |
| 3272 |
} else if (call instanceof ApplyTemplates) { |
| 3273 |
((ApplyTemplates) call).setMode((SimpleMode) c.getActor()); |
| 3274 |
} else { |
| 3275 |
throw new XPathException("Unknown component reference " + call.getClass()); |
| 3276 |
} |
| 3277 |
return false; |
| 3278 |
} |
| 3279 |
|
| 3280 |
private Location allocateLocation(String module, int lineNumber) { |
| 3281 |
IntHashMap<Location> lineMap = locationMap.get(module); |
| 3282 |
if (lineMap == null) { |
| 3283 |
lineMap = new IntHashMap<>(); |
| 3284 |
locationMap.put(module, lineMap); |
| 3285 |
} |
| 3286 |
Location loc = lineMap.get(lineNumber); |
| 3287 |
if (loc == null) { |
| 3288 |
loc = new ExplicitLocation(module, lineNumber, -1); |
| 3289 |
lineMap.put(lineNumber, loc); |
| 3290 |
} |
| 3291 |
return loc; |
| 3292 |
} |
| 3293 |
|
| 3294 |
} |
| 3295 |
|
| 3296 |
|
| 3297 |
|