| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.instruct.Block; |
| 12 |
import net.sf.saxon.expr.instruct.Choose; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 15 |
import net.sf.saxon.expr.parser.Token; |
| 16 |
import net.sf.saxon.expr.parser.TypeChecker; |
| 17 |
import net.sf.saxon.functions.SystemFunction; |
| 18 |
import net.sf.saxon.lib.Feature; |
| 19 |
import net.sf.saxon.lib.NamespaceConstant; |
| 20 |
import net.sf.saxon.ma.map.MapType; |
| 21 |
import net.sf.saxon.om.AttributeCollection; |
| 22 |
import net.sf.saxon.om.AxisInfo; |
| 23 |
import net.sf.saxon.om.NodeInfo; |
| 24 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 25 |
import net.sf.saxon.trans.Err; |
| 26 |
import net.sf.saxon.trans.XPathException; |
| 27 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 28 |
import net.sf.saxon.type.AnyItemType; |
| 29 |
import net.sf.saxon.type.ItemType; |
| 30 |
import net.sf.saxon.type.Type; |
| 31 |
import net.sf.saxon.value.BooleanValue; |
| 32 |
import net.sf.saxon.value.SequenceType; |
| 33 |
import net.sf.saxon.value.Whitespace; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class XSLEvaluate extends StyleElement { |
| 43 |
|
| 44 |
Expression xpath = null; |
| 45 |
SequenceType requiredType = SequenceType.ANY_SEQUENCE; |
| 46 |
Expression namespaceContext = null; |
| 47 |
Expression contextItem = null; |
| 48 |
Expression baseUri = null; |
| 49 |
Expression schemaAware = null; |
| 50 |
Expression withParams = null; |
| 51 |
Expression options = null; |
| 52 |
boolean hasFallbackChildren; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public boolean isInstruction() { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected boolean isPermittedChild(StyleElement child) { |
| 69 |
return child instanceof XSLLocalParam; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected ItemType getReturnedItemType() { |
| 80 |
return AnyItemType.getInstance(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public boolean mayContainSequenceConstructor() { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
public void prepareAttributes() throws XPathException { |
| 95 |
|
| 96 |
AttributeCollection atts = getAttributeList(); |
| 97 |
|
| 98 |
String xpathAtt = null; |
| 99 |
String asAtt = null; |
| 100 |
String contextItemAtt = null; |
| 101 |
String baseUriAtt = null; |
| 102 |
String namespaceContextAtt = null; |
| 103 |
String schemaAwareAtt = null; |
| 104 |
String withParamsAtt = null; |
| 105 |
|
| 106 |
for (int a = 0; a < atts.getLength(); a++) { |
| 107 |
String f = atts.getQName(a); |
| 108 |
switch (f) { |
| 109 |
case "xpath": |
| 110 |
xpathAtt = atts.getValue(a); |
| 111 |
xpath = makeExpression(xpathAtt, a); |
| 112 |
break; |
| 113 |
case "as": |
| 114 |
asAtt = atts.getValue(a); |
| 115 |
break; |
| 116 |
case "context-item": |
| 117 |
contextItemAtt = atts.getValue(a); |
| 118 |
contextItem = makeExpression(contextItemAtt, a); |
| 119 |
break; |
| 120 |
case "base-uri": |
| 121 |
baseUriAtt = atts.getValue(a); |
| 122 |
baseUri = makeAttributeValueTemplate(baseUriAtt, a); |
| 123 |
break; |
| 124 |
case "namespace-context": |
| 125 |
namespaceContextAtt = atts.getValue(a); |
| 126 |
namespaceContext = makeExpression(namespaceContextAtt, a); |
| 127 |
break; |
| 128 |
case "schema-aware": |
| 129 |
schemaAwareAtt = Whitespace.trim(atts.getValue(a)); |
| 130 |
schemaAware = makeAttributeValueTemplate(schemaAwareAtt, a); |
| 131 |
break; |
| 132 |
case "with-params": |
| 133 |
withParamsAtt = atts.getValue(a); |
| 134 |
withParams = makeExpression(withParamsAtt, a); |
| 135 |
break; |
| 136 |
default: |
| 137 |
if (atts.getLocalName(a).equals("options") && atts.getURI(a).equals(NamespaceConstant.SAXON)) { |
| 138 |
options = makeExpression(atts.getValue(a), a); |
| 139 |
} else { |
| 140 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 141 |
} |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (xpathAtt == null) { |
| 147 |
reportAbsence("xpath"); |
| 148 |
} |
| 149 |
|
| 150 |
if (asAtt != null) { |
| 151 |
requiredType = makeSequenceType(asAtt); |
| 152 |
try { |
| 153 |
requiredType = makeSequenceType(asAtt); |
| 154 |
} catch (XPathException e) { |
| 155 |
compileErrorInAttribute(e.getMessage(), e.getErrorCodeLocalPart(), "as"); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (contextItemAtt == null) { |
| 160 |
contextItem = Literal.makeEmptySequence(); |
| 161 |
} |
| 162 |
|
| 163 |
if (schemaAwareAtt == null) { |
| 164 |
schemaAware = new StringLiteral("no"); |
| 165 |
} else if (schemaAware instanceof StringLiteral) { |
| 166 |
checkAttributeValue("schema-aware", schemaAwareAtt, true, StyleElement.YES_NO); |
| 167 |
} |
| 168 |
|
| 169 |
if (withParamsAtt == null) { |
| 170 |
withParamsAtt = "map{}"; |
| 171 |
withParams = makeExpression(withParamsAtt, -1); |
| 172 |
} |
| 173 |
|
| 174 |
if (options == null) { |
| 175 |
options = makeExpression("map{}", -1); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 181 |
getContainingPackage().setRetainUnusedFunctions(); |
| 182 |
if (xpath == null) { |
| 183 |
xpath = new StringLiteral("''"); |
| 184 |
} |
| 185 |
xpath = typeCheck("xpath", xpath); |
| 186 |
baseUri = typeCheck("base-uri", baseUri); |
| 187 |
contextItem = typeCheck("context-item", contextItem); |
| 188 |
namespaceContext = typeCheck("namespace-context", namespaceContext); |
| 189 |
schemaAware = typeCheck("schema-aware", schemaAware); |
| 190 |
withParams = typeCheck("with-params", withParams); |
| 191 |
options = typeCheck("options", options); |
| 192 |
|
| 193 |
iterateAxis(AxisInfo.CHILD).forEachOrFail(child -> { |
| 194 |
if (child instanceof XSLWithParam) { |
| 195 |
|
| 196 |
} else if (child instanceof XSLFallback) { |
| 197 |
hasFallbackChildren = true; |
| 198 |
} else if (child.getNodeKind() == Type.TEXT) { |
| 199 |
|
| 200 |
if (!Whitespace.isWhite(child.getStringValueCS())) { |
| 201 |
compileError("No character data is allowed within xsl:evaluate", "XTSE0010"); |
| 202 |
} |
| 203 |
} else { |
| 204 |
compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) + |
| 205 |
" is not allowed as a child of xsl:evaluate", "XTSE0010"); |
| 206 |
} |
| 207 |
}); |
| 208 |
|
| 209 |
try { |
| 210 |
ExpressionVisitor visitor = makeExpressionVisitor(); |
| 211 |
TypeChecker tc = getConfiguration().getTypeChecker(false); |
| 212 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/xpath", 0); |
| 213 |
xpath = tc.staticTypeCheck(xpath, SequenceType.SINGLE_STRING, role, visitor); |
| 214 |
|
| 215 |
role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/context-item", 0); |
| 216 |
role.setErrorCode("XTTE3210"); |
| 217 |
contextItem = tc.staticTypeCheck(contextItem, SequenceType.OPTIONAL_ITEM, role, visitor); |
| 218 |
|
| 219 |
role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/namespace-context", 0); |
| 220 |
role.setErrorCode("XTTE3170"); |
| 221 |
if (namespaceContext != null) { |
| 222 |
namespaceContext = tc.staticTypeCheck(namespaceContext, SequenceType.SINGLE_NODE, role, visitor); |
| 223 |
} |
| 224 |
|
| 225 |
role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/with-params", 0); |
| 226 |
role.setErrorCode("XTTE3170"); |
| 227 |
withParams = tc.staticTypeCheck(withParams, |
| 228 |
SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE), role, visitor); |
| 229 |
|
| 230 |
role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/saxon:options", 0); |
| 231 |
options = tc.staticTypeCheck(options, |
| 232 |
SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE), role, visitor); |
| 233 |
|
| 234 |
} catch (XPathException err) { |
| 235 |
compileError(err); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
public Expression getTargetExpression() { |
| 240 |
return xpath; |
| 241 |
} |
| 242 |
|
| 243 |
public Expression getContextItemExpression() { |
| 244 |
return contextItem; |
| 245 |
} |
| 246 |
|
| 247 |
public Expression getBaseUriExpression() { |
| 248 |
return baseUri; |
| 249 |
} |
| 250 |
|
| 251 |
public Expression getNamespaceContextExpression() { |
| 252 |
return namespaceContext; |
| 253 |
} |
| 254 |
|
| 255 |
public Expression getSchemaAwareExpression() { |
| 256 |
return schemaAware; |
| 257 |
} |
| 258 |
|
| 259 |
public Expression getWithParamsExpression() { |
| 260 |
return withParams; |
| 261 |
} |
| 262 |
|
| 263 |
public Expression getOptionsExpression() { |
| 264 |
return options; |
| 265 |
} |
| 266 |
|
| 267 |
public SequenceType getRequiredType() { |
| 268 |
return requiredType; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 273 |
if (getConfiguration().getBooleanProperty(Feature.DISABLE_XSL_EVALUATE)) { |
| 274 |
|
| 275 |
validationError = new XPathException("xsl:evaluate is not available in this configuration", "XTDE3175"); |
| 276 |
return fallbackProcessing(exec, decl, this); |
| 277 |
} else { |
| 278 |
Expression evaluateExpr = getConfiguration().makeEvaluateInstruction(this, decl); |
| 279 |
if (evaluateExpr instanceof ErrorExpression) { |
| 280 |
return evaluateExpr; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
if (hasFallbackChildren) { |
| 285 |
|
| 286 |
Expression[] conditions = new Expression[2]; |
| 287 |
Expression sysProp = SystemFunction.makeCall("system-property", |
| 288 |
makeRetainedStaticContext(), |
| 289 |
new StringLiteral("Q{" + NamespaceConstant.XSLT + "}supports-dynamic-evaluation")); |
| 290 |
conditions[0] = new ValueComparison(sysProp, Token.FEQ, new StringLiteral("no")); |
| 291 |
conditions[1] = Literal.makeLiteral(BooleanValue.TRUE); |
| 292 |
Expression[] actions = new Expression[2]; |
| 293 |
List<Expression> fallbackExpressions = new ArrayList<>(); |
| 294 |
NodeInfo child; |
| 295 |
AxisIterator kids = iterateChildren(AnyNodeTest.getInstance()); |
| 296 |
while ((child = kids.next()) != null) { |
| 297 |
if (child instanceof XSLFallback) { |
| 298 |
fallbackExpressions.add(((XSLFallback)child).compileSequenceConstructor(exec, decl, false)); |
| 299 |
} |
| 300 |
} |
| 301 |
actions[0] = new Block(fallbackExpressions.toArray(new Expression[0])); |
| 302 |
actions[1] = evaluateExpr; |
| 303 |
return new Choose(conditions, actions); |
| 304 |
} else { |
| 305 |
return evaluateExpr; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
} |