| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.Literal; |
| 12 |
import net.sf.saxon.expr.instruct.*; |
| 13 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 14 |
import net.sf.saxon.expr.parser.Location; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.lib.Validation; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trace.LocationKind; |
| 19 |
import net.sf.saxon.trans.Err; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.linked.DocumentImpl; |
| 22 |
import net.sf.saxon.tree.linked.LinkedTreeBuilder; |
| 23 |
import net.sf.saxon.tree.util.NamespaceIterator; |
| 24 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 25 |
import net.sf.saxon.type.SchemaType; |
| 26 |
import net.sf.saxon.type.Untyped; |
| 27 |
import net.sf.saxon.z.IntHashSet; |
| 28 |
import net.sf.saxon.z.IntSet; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.Iterator; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class LiteralResultElement extends StyleElement { |
| 43 |
|
| 44 |
private NodeName resultNodeName; |
| 45 |
private NodeName[] attributeNames; |
| 46 |
private Expression[] attributeValues; |
| 47 |
private int numberOfAttributes; |
| 48 |
private boolean toplevel; |
| 49 |
private List<NamespaceBinding> namespaceCodes = new ArrayList<>(); |
| 50 |
private StructuredQName[] attributeSets; |
| 51 |
private SchemaType schemaType = null; |
| 52 |
private int validation = Validation.STRIP; |
| 53 |
private boolean inheritNamespaces = true; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public boolean mayContainSequenceConstructor() { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public boolean isInstruction() { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void prepareAttributes() throws XPathException { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
AttributeCollection atts = getAttributeList(); |
| 84 |
int num = atts.getLength(); |
| 85 |
|
| 86 |
if (num == 0) { |
| 87 |
numberOfAttributes = 0; |
| 88 |
} else { |
| 89 |
NamePool namePool = getNamePool(); |
| 90 |
attributeNames = new NodeName[num]; |
| 91 |
attributeValues = new Expression[num]; |
| 92 |
numberOfAttributes = 0; |
| 93 |
|
| 94 |
for (int i = 0; i < num; i++) { |
| 95 |
|
| 96 |
int fp = atts.getFingerprint(i); |
| 97 |
NodeName name = atts.getNodeName(i); |
| 98 |
String attURI = name.getURI(); |
| 99 |
|
| 100 |
if (attURI.equals(NamespaceConstant.XSLT)) { |
| 101 |
|
| 102 |
if (fp == StandardNames.XSL_USE_ATTRIBUTE_SETS) { |
| 103 |
|
| 104 |
} else if (fp == StandardNames.XSL_DEFAULT_COLLATION) { |
| 105 |
|
| 106 |
} else if (fp == StandardNames.XSL_DEFAULT_MODE) { |
| 107 |
|
| 108 |
} else if (fp == StandardNames.XSL_DEFAULT_VALIDATION) { |
| 109 |
|
| 110 |
} else if (fp == StandardNames.XSL_EXTENSION_ELEMENT_PREFIXES) { |
| 111 |
|
| 112 |
} else if (fp == StandardNames.XSL_EXCLUDE_RESULT_PREFIXES) { |
| 113 |
|
| 114 |
} else if (fp == StandardNames.XSL_EXPAND_TEXT) { |
| 115 |
|
| 116 |
} else if (fp == StandardNames.XSL_VERSION) { |
| 117 |
|
| 118 |
} else if (fp == StandardNames.XSL_XPATH_DEFAULT_NAMESPACE) { |
| 119 |
|
| 120 |
} else if (fp == StandardNames.XSL_TYPE) { |
| 121 |
|
| 122 |
} else if (fp == StandardNames.XSL_USE_WHEN) { |
| 123 |
|
| 124 |
} else if (fp == StandardNames.XSL_VALIDATION) { |
| 125 |
|
| 126 |
} else if (fp == StandardNames.XSL_INHERIT_NAMESPACES) { |
| 127 |
inheritNamespaces = processBooleanAttribute("xsl:inherit-namespaces", atts.getValue(i)); |
| 128 |
} else if (forwardsCompatibleModeIsEnabled()) { |
| 129 |
|
| 130 |
} else { |
| 131 |
compileError("Unknown XSLT attribute " + |
| 132 |
Err.wrap(atts.getNodeName(i).getDisplayName(), Err.ATTRIBUTE), "XTSE0805"); |
| 133 |
} |
| 134 |
} else { |
| 135 |
attributeNames[numberOfAttributes] = name; |
| 136 |
Expression exp = makeAttributeValueTemplate(atts.getValue(i), i); |
| 137 |
attributeValues[numberOfAttributes] = exp; |
| 138 |
numberOfAttributes++; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if (numberOfAttributes < attributeNames.length) { |
| 146 |
attributeNames = Arrays.copyOf(attributeNames, numberOfAttributes); |
| 147 |
attributeValues = Arrays.copyOf(attributeValues, numberOfAttributes); |
| 148 |
} |
| 149 |
} |
| 150 |
resultNodeName = getNodeName(); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 161 |
|
| 162 |
toplevel = (getParent() instanceof XSLStylesheet); |
| 163 |
|
| 164 |
resultNodeName = getNodeName(); |
| 165 |
|
| 166 |
String elementURI = getURI(); |
| 167 |
|
| 168 |
if (toplevel) { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if (elementURI.isEmpty()) { |
| 173 |
compileError("Top level elements must have a non-null namespace URI", "XTSE0130"); |
| 174 |
} |
| 175 |
} else { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
Iterator<NamespaceBinding> inscope = NamespaceIterator.iterateNamespaces(this); |
| 181 |
while (inscope.hasNext()) { |
| 182 |
namespaceCodes.add(inscope.next()); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
PrincipalStylesheetModule sheet = getPrincipalStylesheetModule(); |
| 194 |
|
| 195 |
if (sheet.hasNamespaceAliases()) { |
| 196 |
for (int i = 0; i < namespaceCodes.size(); i++) { |
| 197 |
|
| 198 |
String suri = namespaceCodes.get(i).getURI(); |
| 199 |
NamespaceBinding ncode = sheet.getNamespaceAlias(suri); |
| 200 |
if (ncode != null && !ncode.getURI().equals(suri)) { |
| 201 |
|
| 202 |
|
| 203 |
namespaceCodes.set(i, ncode); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
NamespaceBinding elementAlias = sheet.getNamespaceAlias(elementURI); |
| 210 |
if (elementAlias != null && !elementAlias.getURI().equals(elementURI)) { |
| 211 |
resultNodeName = new FingerprintedQName(elementAlias.getPrefix(), |
| 212 |
elementAlias.getURI(), |
| 213 |
getLocalPart()); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
String useAttSets = getAttributeValue(NamespaceConstant.XSLT, "use-attribute-sets"); |
| 220 |
if (useAttSets != null) { |
| 221 |
attributeSets = getUsedAttributeSets(useAttSets); |
| 222 |
} |
| 223 |
|
| 224 |
validation = getDefaultValidation(); |
| 225 |
String type = getAttributeValue(NamespaceConstant.XSLT, "type"); |
| 226 |
if (type != null) { |
| 227 |
if (!isSchemaAware()) { |
| 228 |
compileError("The xsl:type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); |
| 229 |
} |
| 230 |
schemaType = getSchemaType(type); |
| 231 |
validation = Validation.BY_TYPE; |
| 232 |
} |
| 233 |
|
| 234 |
String validate = getAttributeValue(NamespaceConstant.XSLT, "validation"); |
| 235 |
if (validate != null) { |
| 236 |
validation = validateValidationAttribute(validate); |
| 237 |
if (schemaType != null) { |
| 238 |
compileError("The attributes xsl:type and xsl:validation are mutually exclusive", "XTSE1505"); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
if (numberOfAttributes > 0) { |
| 246 |
boolean changed = false; |
| 247 |
for (int i = 0; i < numberOfAttributes; i++) { |
| 248 |
|
| 249 |
NodeName anameCode = attributeNames[i]; |
| 250 |
NodeName alias = anameCode; |
| 251 |
String attURI = anameCode.getURI(); |
| 252 |
|
| 253 |
if (!attURI.isEmpty()) { |
| 254 |
NamespaceBinding newBinding = sheet.getNamespaceAlias(attURI); |
| 255 |
if (newBinding != null && !newBinding.getURI().equals(attURI)) { |
| 256 |
alias = new FingerprintedQName( |
| 257 |
newBinding.getPrefix(), |
| 258 |
newBinding.getURI(), |
| 259 |
getAttributeList().getLocalName(i)); |
| 260 |
changed = true; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
attributeNames[i] = alias; |
| 265 |
attributeValues[i] = typeCheck(alias.getDisplayName(), attributeValues[i]); |
| 266 |
} |
| 267 |
|
| 268 |
if (changed && numberOfAttributes > 1) { |
| 269 |
|
| 270 |
IntSet names = new IntHashSet(numberOfAttributes); |
| 271 |
for (int i=0; i<numberOfAttributes; i++) { |
| 272 |
int fp = attributeNames[i].obtainFingerprint(getNamePool()); |
| 273 |
boolean absent = names.add(fp); |
| 274 |
if (!absent) { |
| 275 |
compileError("As a result of namespace aliasing, two attributes have the same expanded name", "XTSE0813"); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
for (int n = namespaceCodes.size() - 1; n >= 0; n--) { |
| 286 |
String uri = namespaceCodes.get(n).getURI(); |
| 287 |
if (isExcludedNamespace(uri) && !sheet.isAliasResultNamespace(uri)) { |
| 288 |
namespaceCodes.remove(n); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
protected void validateChildren(ComponentDeclaration decl, boolean excludeStylesheet) throws XPathException { |
| 303 |
if (!toplevel) { |
| 304 |
super.validateChildren(decl, excludeStylesheet); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 313 |
|
| 314 |
if (toplevel) { |
| 315 |
return null; |
| 316 |
} |
| 317 |
|
| 318 |
NamespaceBinding[] bindings = namespaceCodes.toArray(NamespaceBinding.EMPTY_ARRAY); |
| 319 |
FixedElement inst = new FixedElement( |
| 320 |
resultNodeName, |
| 321 |
bindings, |
| 322 |
inheritNamespaces, |
| 323 |
true, schemaType, |
| 324 |
validation); |
| 325 |
|
| 326 |
Expression content = compileSequenceConstructor(exec, decl, true); |
| 327 |
|
| 328 |
if (numberOfAttributes > 0) { |
| 329 |
for (int i = attributeNames.length - 1; i >= 0; i--) { |
| 330 |
FixedAttribute att = new FixedAttribute( |
| 331 |
attributeNames[i], |
| 332 |
Validation.STRIP, |
| 333 |
null); |
| 334 |
att.setRetainedStaticContext(makeRetainedStaticContext()); |
| 335 |
att.setSelect(attributeValues[i]); |
| 336 |
att.setLocation(allocateLocation()); |
| 337 |
Expression exp = att; |
| 338 |
if (exec.getCompilerInfo().isCompileWithTracing()) { |
| 339 |
TraceExpression trace = new TraceExpression(exp); |
| 340 |
trace.setNamespaceResolver(getNamespaceResolver()); |
| 341 |
trace.setConstructType(LocationKind.LITERAL_RESULT_ATTRIBUTE); |
| 342 |
trace.setLocation(allocateLocation()); |
| 343 |
trace.setObjectName(attributeNames[i].getStructuredQName()); |
| 344 |
exp = trace; |
| 345 |
} |
| 346 |
|
| 347 |
if (content == null) { |
| 348 |
content = exp; |
| 349 |
} else { |
| 350 |
content = Block.makeBlock(exp, content); |
| 351 |
content.setLocation(allocateLocation()); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
if (attributeSets != null) { |
| 357 |
Expression use = UseAttributeSet.makeUseAttributeSets(attributeSets, this); |
| 358 |
if (content == null) { |
| 359 |
content = use; |
| 360 |
} else { |
| 361 |
content = Block.makeBlock(use, content); |
| 362 |
content.setLocation(allocateLocation()); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if (content == null) { |
| 367 |
content = Literal.makeEmptySequence(); |
| 368 |
} |
| 369 |
inst.setContentExpression(content); |
| 370 |
inst.setRetainedStaticContext(makeRetainedStaticContext()); |
| 371 |
inst.setLocation(allocateLocation()); |
| 372 |
return inst; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public DocumentImpl makeStylesheet(boolean topLevel) throws XPathException { |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
StyleNodeFactory nodeFactory = getCompilation().getStyleNodeFactory(topLevel); |
| 389 |
if (!isInScopeNamespace(NamespaceConstant.XSLT)) { |
| 390 |
String message; |
| 391 |
if (getLocalPart().equals("stylesheet") || getLocalPart().equals("transform")) { |
| 392 |
message = "Namespace for stylesheet element should be " + NamespaceConstant.XSLT; |
| 393 |
} else { |
| 394 |
message = "The supplied file does not appear to be a stylesheet"; |
| 395 |
} |
| 396 |
XPathException err = new XPathException(message); |
| 397 |
err.setLocation(allocateLocation()); |
| 398 |
err.setErrorCode("XTSE0150"); |
| 399 |
err.setIsStaticError(true); |
| 400 |
|
| 401 |
compileError(err); |
| 402 |
throw err; |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
String version = getAttributeValue(NamespaceConstant.XSLT, "version"); |
| 410 |
if (version == null) { |
| 411 |
XPathException err = new XPathException("Simplified stylesheet: xsl:version attribute is missing"); |
| 412 |
err.setErrorCode("XTSE0150"); |
| 413 |
err.setIsStaticError(true); |
| 414 |
err.setLocation(allocateLocation()); |
| 415 |
|
| 416 |
compileError(err); |
| 417 |
throw err; |
| 418 |
} |
| 419 |
|
| 420 |
try { |
| 421 |
DocumentImpl oldRoot = (DocumentImpl) getRoot(); |
| 422 |
LinkedTreeBuilder builder = new LinkedTreeBuilder(getConfiguration().makePipelineConfiguration()); |
| 423 |
builder.setNodeFactory(nodeFactory); |
| 424 |
builder.setSystemId(this.getSystemId()); |
| 425 |
|
| 426 |
builder.open(); |
| 427 |
builder.startDocument(0); |
| 428 |
|
| 429 |
int st = StandardNames.XSL_STYLESHEET; |
| 430 |
final Location loc = ExplicitLocation.UNKNOWN_LOCATION; |
| 431 |
builder.startElement(new CodedName(st, "xsl", getNamePool()), Untyped.getInstance(), loc, 0); |
| 432 |
builder.namespace(new NamespaceBinding("xsl", NamespaceConstant.XSLT), 0); |
| 433 |
builder.attribute(new NoNamespaceName("version"), BuiltInAtomicType.UNTYPED_ATOMIC, version, loc, 0); |
| 434 |
builder.startContent(); |
| 435 |
|
| 436 |
int te = StandardNames.XSL_TEMPLATE; |
| 437 |
builder.startElement(new CodedName(te, "xsl", getNamePool()), Untyped.getInstance(), loc, 0); |
| 438 |
builder.attribute(new NoNamespaceName("match"), BuiltInAtomicType.UNTYPED_ATOMIC, "/", loc, 0); |
| 439 |
builder.startContent(); |
| 440 |
|
| 441 |
builder.graftElement(this); |
| 442 |
|
| 443 |
builder.endElement(); |
| 444 |
builder.endElement(); |
| 445 |
builder.endDocument(); |
| 446 |
builder.close(); |
| 447 |
|
| 448 |
DocumentImpl newRoot = (DocumentImpl) builder.getCurrentRoot(); |
| 449 |
newRoot.graftLocationMap(oldRoot); |
| 450 |
return newRoot; |
| 451 |
} catch (XPathException err) { |
| 452 |
|
| 453 |
err.setLocation(allocateLocation()); |
| 454 |
throw err; |
| 455 |
} |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public int getConstructType() { |
| 466 |
return LocationKind.LITERAL_RESULT_ELEMENT; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public StructuredQName getObjectName() { |
| 478 |
return new StructuredQName(getPrefix(), getURI(), getLocalPart()); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public Object getProperty(String name) { |
| 492 |
if (name.equals("name")) { |
| 493 |
return getDisplayName(); |
| 494 |
} |
| 495 |
return null; |
| 496 |
} |
| 497 |
|
| 498 |
} |