| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.event.*; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.functions.IriToUri; |
| 16 |
import net.sf.saxon.functions.ResolveURI; |
| 17 |
import net.sf.saxon.lib.*; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.serialize.CharacterMapIndex; |
| 20 |
import net.sf.saxon.serialize.PrincipalOutputGatekeeper; |
| 21 |
import net.sf.saxon.serialize.SerializationProperties; |
| 22 |
import net.sf.saxon.style.StylesheetPackage; |
| 23 |
import net.sf.saxon.style.XSLResultDocument; |
| 24 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 25 |
import net.sf.saxon.trans.Err; |
| 26 |
import net.sf.saxon.trans.XPathException; |
| 27 |
import net.sf.saxon.trans.XsltController; |
| 28 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 29 |
import net.sf.saxon.type.ErrorType; |
| 30 |
import net.sf.saxon.type.ItemType; |
| 31 |
import net.sf.saxon.type.SchemaType; |
| 32 |
import net.sf.saxon.value.Whitespace; |
| 33 |
|
| 34 |
import javax.xml.transform.OutputKeys; |
| 35 |
import javax.xml.transform.Result; |
| 36 |
import javax.xml.transform.TransformerException; |
| 37 |
import javax.xml.transform.dom.DOMResult; |
| 38 |
import javax.xml.transform.sax.SAXResult; |
| 39 |
import javax.xml.transform.stream.StreamResult; |
| 40 |
import java.io.IOException; |
| 41 |
import java.io.StringWriter; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.HashMap; |
| 44 |
import java.util.Map; |
| 45 |
import java.util.Properties; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class ResultDocument extends Instruction |
| 60 |
implements ValidatingInstruction, InstructionWithComplexContent, ContextOriginator { |
| 61 |
|
| 62 |
private Operand hrefOp; |
| 63 |
private Operand formatOp; |
| 64 |
private Operand contentOp; |
| 65 |
private boolean async = false; |
| 66 |
protected final Properties globalProperties; |
| 67 |
protected final Properties localProperties; |
| 68 |
protected ParseOptions validationOptions; |
| 69 |
protected final Map<StructuredQName, Operand> serializationAttributes; |
| 70 |
protected boolean resolveAgainstStaticBase = false; |
| 71 |
protected final CharacterMapIndex characterMapIndex; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public ResultDocument(Properties globalProperties, |
| 87 |
Properties localProperties, |
| 88 |
Expression href, |
| 89 |
Expression formatExpression, |
| 90 |
int validationAction, |
| 91 |
SchemaType schemaType, |
| 92 |
Map<StructuredQName, Expression> serializationAttributes, |
| 93 |
CharacterMapIndex characterMapIndex) { |
| 94 |
this.globalProperties = globalProperties; |
| 95 |
this.localProperties = localProperties; |
| 96 |
if (href != null) { |
| 97 |
hrefOp = new Operand(this, href, OperandRole.SINGLE_ATOMIC); |
| 98 |
} |
| 99 |
if (formatExpression != null) { |
| 100 |
formatOp = new Operand(this, formatExpression, OperandRole.SINGLE_ATOMIC); |
| 101 |
} |
| 102 |
setValidationAction(validationAction, schemaType); |
| 103 |
this.serializationAttributes = new HashMap<>(serializationAttributes.size()); |
| 104 |
for (Map.Entry<StructuredQName, Expression> entry : serializationAttributes.entrySet()) { |
| 105 |
this.serializationAttributes.put(entry.getKey(), new Operand(this, entry.getValue(), OperandRole.SINGLE_ATOMIC)); |
| 106 |
} |
| 107 |
this.characterMapIndex = characterMapIndex; |
| 108 |
|
| 109 |
for (Expression e : serializationAttributes.values()) { |
| 110 |
adoptChildExpression(e); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void setContentExpression(Expression content) { |
| 121 |
contentOp = new Operand(this, content, OperandRole.SINGLE_ATOMIC); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void setSchemaType(SchemaType type) { |
| 132 |
if (validationOptions == null) { |
| 133 |
validationOptions = new ParseOptions(); |
| 134 |
} |
| 135 |
validationOptions.setSchemaValidationMode(Validation.BY_TYPE); |
| 136 |
validationOptions.setTopLevelType(type); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public SchemaType getSchemaType() { |
| 147 |
return validationOptions == null ? null : validationOptions.getTopLevelType(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
public boolean isResolveAgainstStaticBase() { |
| 152 |
return resolveAgainstStaticBase; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public ParseOptions getValidationOptions() { |
| 163 |
return validationOptions; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public void setValidationAction(int mode, SchemaType schemaType) { |
| 176 |
boolean preservingTypes = mode == Validation.PRESERVE && schemaType == null; |
| 177 |
if (!preservingTypes) { |
| 178 |
if (validationOptions == null) { |
| 179 |
validationOptions = new ParseOptions(); |
| 180 |
validationOptions.setSchemaValidationMode(mode); |
| 181 |
validationOptions.setTopLevelType(schemaType); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public int getValidationAction() { |
| 193 |
return validationOptions == null ? Validation.PRESERVE : validationOptions.getSchemaValidationMode(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public Expression getFormatExpression() { |
| 198 |
return formatOp == null ? null : formatOp.getChildExpression(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public void setUseStaticBaseUri(boolean staticBase) { |
| 211 |
resolveAgainstStaticBase = staticBase; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
public void setAsynchronous(boolean async) { |
| 216 |
this.async = async; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public boolean isAsynchronous() { |
| 227 |
return async; |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public boolean isMultiThreaded(Configuration config) { |
| 232 |
return isAsynchronous() && config.isLicensedFeature(Configuration.LicenseFeature.SCHEMA_VALIDATION) |
| 233 |
&& config.getBooleanProperty(Feature.ALLOW_MULTITHREADING); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 238 |
typeCheckChildren(visitor, contextInfo); |
| 239 |
String method = getStaticSerializationProperty(XSLResultDocument.METHOD); |
| 240 |
boolean contentDependentMethod = method == null && formatOp == null && |
| 241 |
!serializationAttributes.containsKey(XSLResultDocument.METHOD); |
| 242 |
boolean buildTree = "yes".equals(getStaticSerializationProperty(XSLResultDocument.BUILD_TREE)); |
| 243 |
if (buildTree || contentDependentMethod || |
| 244 |
"xml".equals(method) || "html".equals(method) || "xhtml".equals(method) || "text".equals(method)) { |
| 245 |
try { |
| 246 |
DocumentInstr.checkContentSequence(visitor.getStaticContext(), contentOp, validationOptions); |
| 247 |
} catch (XPathException err) { |
| 248 |
err.maybeSetLocation(getLocation()); |
| 249 |
throw err; |
| 250 |
} |
| 251 |
} |
| 252 |
return this; |
| 253 |
} |
| 254 |
|
| 255 |
public int getIntrinsicDependencies() { |
| 256 |
return StaticProperty.HAS_SIDE_EFFECTS; |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 261 |
optimizeChildren(visitor, contextInfo); |
| 262 |
if (isAsynchronous()) { |
| 263 |
Expression e = getParentExpression(); |
| 264 |
while (e != null) { |
| 265 |
if (e instanceof LetExpression && ExpressionTool.dependsOnVariable( |
| 266 |
getContentExpression(), new Binding[]{(LetExpression) e})) { |
| 267 |
((LetExpression) e).setNeedsEagerEvaluation(true); |
| 268 |
} |
| 269 |
e = e.getParentExpression(); |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
return this; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public Expression copy(RebindingMap rebindings) { |
| 285 |
Map<StructuredQName, Expression> map = new HashMap<>(); |
| 286 |
for (Map.Entry<StructuredQName, Operand> entry : serializationAttributes.entrySet()) { |
| 287 |
map.put(entry.getKey(), entry.getValue().getChildExpression().copy(rebindings)); |
| 288 |
} |
| 289 |
ResultDocument r = new ResultDocument( |
| 290 |
globalProperties, |
| 291 |
localProperties, |
| 292 |
getHref() == null ? null : getHref().copy(rebindings), |
| 293 |
getFormatExpression() == null ? null : getFormatExpression().copy(rebindings), |
| 294 |
getValidationAction(), |
| 295 |
getSchemaType(), |
| 296 |
map, |
| 297 |
characterMapIndex); |
| 298 |
ExpressionTool.copyLocationInfo(this, r); |
| 299 |
r.setContentExpression(getContentExpression().copy(rebindings)); |
| 300 |
r.resolveAgainstStaticBase = resolveAgainstStaticBase; |
| 301 |
r.async = async; |
| 302 |
return r; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public int getInstructionNameCode() { |
| 311 |
return StandardNames.XSL_RESULT_DOCUMENT; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public ItemType getItemType() { |
| 323 |
return ErrorType.getInstance(); |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public Iterable<Operand> operands() { |
| 328 |
ArrayList<Operand> list = new ArrayList<>(6); |
| 329 |
list.add(contentOp); |
| 330 |
if (hrefOp != null) { |
| 331 |
list.add(hrefOp); |
| 332 |
} |
| 333 |
if (formatOp != null) { |
| 334 |
list.add(formatOp); |
| 335 |
} |
| 336 |
list.addAll(serializationAttributes.values()); |
| 337 |
return list; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 361 |
PathMap.PathMapNodeSet result = super.addToPathMap(pathMap, pathMapNodeSet); |
| 362 |
result.setReturnable(false); |
| 363 |
return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 368 |
process(getContentExpression(), context); |
| 369 |
return null; |
| 370 |
} |
| 371 |
|
| 372 |
public void process(Expression content, XPathContext context) throws XPathException { |
| 373 |
checkNotTemporaryOutputState(context); |
| 374 |
context.getConfiguration().processResultDocument(this, content, context); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public void processInstruction(Expression content, XPathContext context) throws XPathException { |
| 387 |
final XsltController controller = (XsltController) context.getController(); |
| 388 |
assert controller != null; |
| 389 |
final Configuration config = controller.getConfiguration(); |
| 390 |
Receiver saved = context.getReceiver(); |
| 391 |
PipelineConfiguration pipe = saved.getPipelineConfiguration(); |
| 392 |
String savedOutputUri = context.getCurrentOutputUri(); |
| 393 |
Receiver out = processLeft(context); |
| 394 |
boolean failed = false; |
| 395 |
try { |
| 396 |
content.process(context); |
| 397 |
} catch (XPathException err) { |
| 398 |
failed = true; |
| 399 |
err.maybeSetContext(context); |
| 400 |
err.maybeSetLocation(getLocation()); |
| 401 |
throw err; |
| 402 |
} finally { |
| 403 |
try { |
| 404 |
out.close(); |
| 405 |
} catch (XPathException e) { |
| 406 |
if (!failed) { |
| 407 |
|
| 408 |
throw e; |
| 409 |
} |
| 410 |
|
| 411 |
} |
| 412 |
} |
| 413 |
context.setReceiver(saved); |
| 414 |
context.setCurrentOutputUri(savedOutputUri); |
| 415 |
|
| 416 |
} |
| 417 |
|
| 418 |
public Receiver processLeft(XPathContext context) throws XPathException { |
| 419 |
XsltController controller = (XsltController)context.getController(); |
| 420 |
Configuration config = controller.getConfiguration(); |
| 421 |
checkNotTemporaryOutputState(context); |
| 422 |
|
| 423 |
Properties computedLocalProps = gatherOutputProperties(context); |
| 424 |
if (getStaticBaseURIString() != null) { |
| 425 |
computedLocalProps.setProperty(SaxonOutputKeys.PARAMETER_DOCUMENT_BASE_URI, getStaticBaseURIString()); |
| 426 |
} |
| 427 |
SerializationProperties serParams = new SerializationProperties(computedLocalProps, characterMapIndex); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
if (validationOptions != null && validationOptions.getSchemaValidationMode() != Validation.PRESERVE) { |
| 433 |
serParams.setValidationFactory( |
| 434 |
output -> { |
| 435 |
|
| 436 |
|
| 437 |
NamespaceReducer nr = new NamespaceReducer(output); |
| 438 |
return config.getDocumentValidator( |
| 439 |
nr, output.getSystemId(), validationOptions, getLocation()); |
| 440 |
} |
| 441 |
); |
| 442 |
} |
| 443 |
|
| 444 |
Receiver out = null; |
| 445 |
ResultDocumentResolver resolver = null; |
| 446 |
String hrefValue = ""; |
| 447 |
if (getHref() != null) { |
| 448 |
hrefValue = IriToUri.iriToUri(getHref().evaluateAsString(context)).toString(); |
| 449 |
} |
| 450 |
if (hrefValue.isEmpty() || hrefValue.equals(controller.getBaseOutputURI())) { |
| 451 |
PrincipalOutputGatekeeper gateKeeper = controller.getGatekeeper(); |
| 452 |
if (gateKeeper != null) { |
| 453 |
gateKeeper.useAsSecondary(); |
| 454 |
out = gateKeeper.makeReceiver(serParams); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
if (out == null) { |
| 459 |
try { |
| 460 |
resolver = controller.getResultDocumentResolver(); |
| 461 |
out = makeReceiver(hrefValue, getStaticBaseURIString(), context, |
| 462 |
resolver, serParams, resolveAgainstStaticBase); |
| 463 |
traceDestination(context, out); |
| 464 |
} catch (XPathException e) { |
| 465 |
e.maybeSetLocation(getLocation()); |
| 466 |
e.maybeSetContext(context); |
| 467 |
throw e; |
| 468 |
} |
| 469 |
} |
| 470 |
out.getPipelineConfiguration().setController(controller); |
| 471 |
String systemId = out.getSystemId(); |
| 472 |
|
| 473 |
NamespaceReducer nr = new NamespaceReducer(out); |
| 474 |
out = new ComplexContentOutputter(nr); |
| 475 |
out.setSystemId(systemId); |
| 476 |
context.setReceiver(out); |
| 477 |
context.setCurrentOutputUri(systemId); |
| 478 |
|
| 479 |
out.open(); |
| 480 |
return out; |
| 481 |
} |
| 482 |
|
| 483 |
public CharacterMapIndex getCharacterMapIndex() { |
| 484 |
return characterMapIndex; |
| 485 |
} |
| 486 |
|
| 487 |
private void checkNotTemporaryOutputState(XPathContext context) throws XPathException { |
| 488 |
if (context.getTemporaryOutputState() != 0) { |
| 489 |
XPathException err = new XPathException("Cannot execute xsl:result-document while evaluating xsl:" + |
| 490 |
context.getNamePool().getLocalName(context.getTemporaryOutputState())); |
| 491 |
err.setErrorCode("XTDE1480"); |
| 492 |
err.setLocation(getLocation()); |
| 493 |
throw err; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
public static Receiver makeReceiver(String hrefValue, String baseURI, |
| 498 |
XPathContext context, ResultDocumentResolver resolver, |
| 499 |
SerializationProperties params, |
| 500 |
boolean resolveAgainstStaticBase) throws XPathException { |
| 501 |
String resultURI = null; |
| 502 |
Controller controller = context.getController(); |
| 503 |
try { |
| 504 |
String base; |
| 505 |
if (resolveAgainstStaticBase) { |
| 506 |
base = baseURI; |
| 507 |
} else { |
| 508 |
base = controller.getBaseOutputURI(); |
| 509 |
} |
| 510 |
|
| 511 |
try { |
| 512 |
Receiver out = resolver.resolve(context, hrefValue, base, params); |
| 513 |
String systemId = out.getSystemId(); |
| 514 |
if (systemId == null) { |
| 515 |
systemId = ResolveURI.makeAbsolute(hrefValue, base).toASCIIString(); |
| 516 |
out.setSystemId(systemId); |
| 517 |
} |
| 518 |
checkAcceptableUri(context, systemId); |
| 519 |
return out; |
| 520 |
} catch (XPathException e) { |
| 521 |
throw e; |
| 522 |
} catch (Exception err) { |
| 523 |
err.printStackTrace(); |
| 524 |
throw new XPathException("Exception thrown by output resolver", err); |
| 525 |
} |
| 526 |
} catch (TransformerException e) { |
| 527 |
throw XPathException.makeXPathException(e); |
| 528 |
} |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
public static void traceDestination(XPathContext context, Result result) { |
| 533 |
Configuration config = context.getConfiguration(); |
| 534 |
boolean timing = config.isTiming(); |
| 535 |
if (timing) { |
| 536 |
String dest = result.getSystemId(); |
| 537 |
if (dest == null) { |
| 538 |
if (result instanceof StreamResult) { |
| 539 |
dest = "anonymous output stream"; |
| 540 |
} else if (result instanceof SAXResult) { |
| 541 |
dest = "SAX2 ContentHandler"; |
| 542 |
} else if (result instanceof DOMResult) { |
| 543 |
dest = "DOM tree"; |
| 544 |
} else { |
| 545 |
dest = result.getClass().getName(); |
| 546 |
} |
| 547 |
} |
| 548 |
config.getStandardErrorOutput().println("Writing to " + dest); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
public static void checkAcceptableUri(XPathContext context, String uri) throws XPathException { |
| 553 |
XsltController controller = (XsltController) context.getController(); |
| 554 |
assert controller != null; |
| 555 |
if (uri != null) { |
| 556 |
if (controller.getDocumentPool().find(uri) != null) { |
| 557 |
XPathException err = new XPathException("Cannot write to a URI that has already been read: " + |
| 558 |
(uri.equals(Controller.ANONYMOUS_PRINCIPAL_OUTPUT_URI) ? "(implicit output URI)" : uri)); |
| 559 |
err.setXPathContext(context); |
| 560 |
err.setErrorCode("XTDE1500"); |
| 561 |
throw err; |
| 562 |
} |
| 563 |
|
| 564 |
DocumentURI documentKey = new DocumentURI(uri); |
| 565 |
|
| 566 |
synchronized(controller) { |
| 567 |
if (!controller.checkUniqueOutputDestination(documentKey)) { |
| 568 |
XPathException err = new XPathException("Cannot write more than one result document to the same URI: " + |
| 569 |
(uri.equals(Controller.ANONYMOUS_PRINCIPAL_OUTPUT_URI) ? "(implicit output URI)" : uri)); |
| 570 |
err.setXPathContext(context); |
| 571 |
err.setErrorCode("XTDE1490"); |
| 572 |
throw err; |
| 573 |
} else { |
| 574 |
controller.addUnavailableOutputDestination(documentKey); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public Properties gatherOutputProperties(XPathContext context) throws XPathException { |
| 590 |
Controller controller = context.getController(); |
| 591 |
assert controller != null; |
| 592 |
Configuration config = context.getConfiguration(); |
| 593 |
Properties computedGlobalProps = globalProperties; |
| 594 |
NamespaceResolver nsResolver = getRetainedStaticContext(); |
| 595 |
assert nsResolver != null; |
| 596 |
|
| 597 |
if (getFormatExpression() != null) { |
| 598 |
|
| 599 |
StructuredQName qName; |
| 600 |
String format = getFormatExpression().evaluateAsString(context).toString(); |
| 601 |
if (format.startsWith("Q{")) { |
| 602 |
qName = StructuredQName.fromEQName(format); |
| 603 |
} else { |
| 604 |
String[] parts; |
| 605 |
try { |
| 606 |
parts = NameChecker.getQNameParts(format); |
| 607 |
} catch (QNameException e) { |
| 608 |
XPathException err = new XPathException("The requested output format " + Err.wrap(format) + " is not a valid QName"); |
| 609 |
err.maybeSetLocation(getFormatExpression().getLocation()); |
| 610 |
err.setErrorCode("XTDE1460"); |
| 611 |
err.setXPathContext(context); |
| 612 |
throw err; |
| 613 |
} |
| 614 |
String uri = nsResolver.getURIForPrefix(parts[0], false); |
| 615 |
if (uri == null) { |
| 616 |
XPathException err = new XPathException("The namespace prefix in the format name " + format + " is undeclared"); |
| 617 |
err.maybeSetLocation(getFormatExpression().getLocation()); |
| 618 |
err.setErrorCode("XTDE1460"); |
| 619 |
err.setXPathContext(context); |
| 620 |
throw err; |
| 621 |
} |
| 622 |
qName = new StructuredQName(parts[0], uri, parts[1]); |
| 623 |
} |
| 624 |
computedGlobalProps = ((StylesheetPackage) getRetainedStaticContext().getPackageData()).getNamedOutputProperties(qName); |
| 625 |
if (computedGlobalProps == null) { |
| 626 |
XPathException err = new XPathException("There is no xsl:output format named " + format); |
| 627 |
err.setErrorCode("XTDE1460"); |
| 628 |
err.setXPathContext(context); |
| 629 |
throw err; |
| 630 |
} |
| 631 |
|
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
Properties computedLocalProps = new Properties(computedGlobalProps); |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
for (Object keyo : localProperties.keySet()) { |
| 641 |
String key = (String) keyo; |
| 642 |
StructuredQName qName = StructuredQName.fromClarkName(key); |
| 643 |
try { |
| 644 |
setSerializationProperty(computedLocalProps, qName.getURI(), qName.getLocalPart(), |
| 645 |
localProperties.getProperty(key), nsResolver, true, config); |
| 646 |
} catch (XPathException e) { |
| 647 |
e.setErrorCode("XTDE0030"); |
| 648 |
e.maybeSetLocation(getLocation()); |
| 649 |
throw e; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if (!serializationAttributes.isEmpty()) { |
| 656 |
for (Map.Entry<StructuredQName, Operand> entry : serializationAttributes.entrySet()) { |
| 657 |
String value = entry.getValue().getChildExpression().evaluateAsString(context).toString(); |
| 658 |
String lname = entry.getKey().getLocalPart(); |
| 659 |
String uri = entry.getKey().getURI(); |
| 660 |
try { |
| 661 |
setSerializationProperty(computedLocalProps, uri, lname, value, nsResolver, false, config); |
| 662 |
} catch (XPathException e) { |
| 663 |
e.setErrorCode("XTDE0030"); |
| 664 |
e.maybeSetLocation(getLocation()); |
| 665 |
e.maybeSetContext(context); |
| 666 |
if (NamespaceConstant.SAXON.equals(e.getErrorCodeNamespace()) && |
| 667 |
"SXWN".equals(e.getErrorCodeLocalPart().substring(0, 4))) { |
| 668 |
controller.getErrorListener().warning(e); |
| 669 |
} else { |
| 670 |
throw e; |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
return computedLocalProps; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public String getStaticSerializationProperty(StructuredQName name) { |
| 687 |
String clarkName = name.getClarkName(); |
| 688 |
String local = localProperties.getProperty(clarkName); |
| 689 |
if (local != null) { |
| 690 |
return local; |
| 691 |
} |
| 692 |
if (serializationAttributes.containsKey(name)) { |
| 693 |
return null; |
| 694 |
} |
| 695 |
return globalProperties.getProperty(clarkName); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public static void setSerializationProperty(Properties details, String uri, String lname, |
| 716 |
String value, NamespaceResolver nsResolver, |
| 717 |
boolean prevalidated, Configuration config) |
| 718 |
throws XPathException { |
| 719 |
SerializerFactory sf = config.getSerializerFactory(); |
| 720 |
String clarkName = lname; |
| 721 |
if (!uri.isEmpty()) { |
| 722 |
clarkName = "{" + uri + "}" + lname; |
| 723 |
} |
| 724 |
if (uri.isEmpty() || NamespaceConstant.SAXON.equals(uri)) { |
| 725 |
switch (clarkName) { |
| 726 |
case "method": |
| 727 |
value = Whitespace.trim(value); |
| 728 |
if (value.startsWith("Q{}") && value.length() > 3) { |
| 729 |
value = value.substring(3); |
| 730 |
} |
| 731 |
if (value.equals("xml") || value.equals("html") || |
| 732 |
value.equals("text") || value.equals("xhtml") || |
| 733 |
value.equals("json") || value.equals("adaptive") || |
| 734 |
prevalidated || value.startsWith("{")) { |
| 735 |
details.setProperty(OutputKeys.METHOD, value); |
| 736 |
} else if (value.startsWith("Q{")) { |
| 737 |
details.setProperty(OutputKeys.METHOD, value.substring(1)); |
| 738 |
} else { |
| 739 |
String[] parts; |
| 740 |
try { |
| 741 |
parts = NameChecker.getQNameParts(value); |
| 742 |
String prefix = parts[0]; |
| 743 |
if (prefix.isEmpty()) { |
| 744 |
XPathException err = new XPathException("method must be xml, html, xhtml, text, json, adaptive, or a prefixed name"); |
| 745 |
err.setErrorCode("SEPM0016"); |
| 746 |
err.setIsStaticError(true); |
| 747 |
throw err; |
| 748 |
} else if (nsResolver != null) { |
| 749 |
String muri = nsResolver.getURIForPrefix(prefix, false); |
| 750 |
if (muri == null) { |
| 751 |
XPathException err = new XPathException("Namespace prefix '" + prefix + "' has not been declared"); |
| 752 |
err.setErrorCode("SEPM0016"); |
| 753 |
err.setIsStaticError(true); |
| 754 |
throw err; |
| 755 |
} |
| 756 |
details.setProperty(OutputKeys.METHOD, '{' + muri + '}' + parts[1]); |
| 757 |
} else { |
| 758 |
details.setProperty(OutputKeys.METHOD, value); |
| 759 |
} |
| 760 |
} catch (QNameException e) { |
| 761 |
XPathException err = new XPathException("Invalid method name. " + e.getMessage()); |
| 762 |
err.setErrorCode("SEPM0016"); |
| 763 |
err.setIsStaticError(true); |
| 764 |
throw err; |
| 765 |
} |
| 766 |
} |
| 767 |
break; |
| 768 |
case "use-character-maps": |
| 769 |
|
| 770 |
String existing = details.getProperty(SaxonOutputKeys.USE_CHARACTER_MAPS); |
| 771 |
if (existing == null) { |
| 772 |
existing = ""; |
| 773 |
} |
| 774 |
details.setProperty(SaxonOutputKeys.USE_CHARACTER_MAPS, existing + value); |
| 775 |
break; |
| 776 |
case "cdata-section-elements": |
| 777 |
processListOfNodeNames(details, clarkName, value, nsResolver, true, prevalidated, false); |
| 778 |
break; |
| 779 |
case "suppress-indentation": |
| 780 |
processListOfNodeNames(details, clarkName, value, nsResolver, true, prevalidated, false); |
| 781 |
break; |
| 782 |
case SaxonOutputKeys.DOUBLE_SPACE: |
| 783 |
processListOfNodeNames(details, clarkName, value, nsResolver, true, prevalidated, false); |
| 784 |
break; |
| 785 |
case SaxonOutputKeys.ATTRIBUTE_ORDER: |
| 786 |
processListOfNodeNames(details, clarkName, value, nsResolver, false, prevalidated, true); |
| 787 |
break; |
| 788 |
case SaxonOutputKeys.NEXT_IN_CHAIN: |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
break; |
| 794 |
default: |
| 795 |
|
| 796 |
if (clarkName.equals("output-version")) { |
| 797 |
clarkName = "version"; |
| 798 |
} |
| 799 |
if (!prevalidated) { |
| 800 |
try { |
| 801 |
if (!SaxonOutputKeys.isUnstrippedProperty(clarkName)) { |
| 802 |
|
| 803 |
value = Whitespace.trim(value); |
| 804 |
} |
| 805 |
value = sf.checkOutputProperty(clarkName, value); |
| 806 |
} catch (XPathException err) { |
| 807 |
err.maybeSetErrorCode("SEPM0016"); |
| 808 |
throw err; |
| 809 |
} |
| 810 |
} |
| 811 |
details.setProperty(clarkName, value); |
| 812 |
break; |
| 813 |
} |
| 814 |
} else { |
| 815 |
|
| 816 |
details.setProperty('{' + uri + '}' + lname, value); |
| 817 |
} |
| 818 |
|
| 819 |
} |
| 820 |
|
| 821 |
private static void processListOfNodeNames(Properties details, String key, String value, |
| 822 |
NamespaceResolver nsResolver, boolean useDefaultNS, |
| 823 |
boolean prevalidated, boolean allowStar) throws XPathException { |
| 824 |
String existing = details.getProperty(key); |
| 825 |
if (existing == null) { |
| 826 |
existing = ""; |
| 827 |
} |
| 828 |
String s = SaxonOutputKeys.parseListOfNodeNames(value, nsResolver, useDefaultNS, prevalidated, allowStar, "SEPM0016"); |
| 829 |
details.setProperty(key, existing + s); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
public void export(ExpressionPresenter out) throws XPathException { |
| 838 |
out.startElement("resultDoc", this); |
| 839 |
out.emitAttribute("global", exportProperties(globalProperties)); |
| 840 |
out.emitAttribute("local", exportProperties(localProperties)); |
| 841 |
if (getValidationAction() != Validation.SKIP && getValidationAction() != Validation.BY_TYPE) { |
| 842 |
out.emitAttribute("validation", Validation.toString(getValidationAction())); |
| 843 |
} |
| 844 |
final SchemaType schemaType = getSchemaType(); |
| 845 |
if (schemaType != null) { |
| 846 |
out.emitAttribute("type", schemaType.getStructuredQName()); |
| 847 |
} |
| 848 |
if (getHref() != null) { |
| 849 |
out.setChildRole("href"); |
| 850 |
getHref().export(out); |
| 851 |
} |
| 852 |
if (getFormatExpression() != null) { |
| 853 |
out.setChildRole("format"); |
| 854 |
getFormatExpression().export(out); |
| 855 |
} |
| 856 |
for (Map.Entry<StructuredQName, Operand> p : serializationAttributes.entrySet()) { |
| 857 |
StructuredQName name = p.getKey(); |
| 858 |
Expression value = p.getValue().getChildExpression(); |
| 859 |
out.setChildRole(name.getEQName()); |
| 860 |
value.export(out); |
| 861 |
} |
| 862 |
out.setChildRole("content"); |
| 863 |
getContentExpression().export(out); |
| 864 |
out.endElement(); |
| 865 |
} |
| 866 |
|
| 867 |
private String exportProperties(Properties props) { |
| 868 |
try { |
| 869 |
StringWriter writer = new StringWriter(); |
| 870 |
props.store(writer, ""); |
| 871 |
return writer.toString(); |
| 872 |
} catch (IOException e) { |
| 873 |
throw new AssertionError(e); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
public static void processXslOutputElement(NodeInfo element, Properties props, XPathContext c) throws XPathException { |
| 888 |
AxisIterator iter = element.iterateAxis(AxisInfo.ATTRIBUTE); |
| 889 |
NamespaceResolver resolver = new InscopeNamespaceResolver(element); |
| 890 |
iter.forEachOrFail(att -> { |
| 891 |
String uri = att.getURI(); |
| 892 |
String local = att.getLocalPart(); |
| 893 |
String val = Whitespace.trim(att.getStringValueCS()); |
| 894 |
setSerializationProperty(props, uri, local, val, resolver, false, c.getConfiguration()); |
| 895 |
}); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
@Override |
| 905 |
public String getStreamerName() { |
| 906 |
return "ResultDocument"; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
public Expression getHref() { |
| 911 |
return hrefOp == null ? null : hrefOp.getChildExpression(); |
| 912 |
} |
| 913 |
|
| 914 |
public void setHref(Expression href) { |
| 915 |
hrefOp.setChildExpression(href); |
| 916 |
} |
| 917 |
|
| 918 |
public void setFormatExpression(Expression formatExpression) { |
| 919 |
formatOp.setChildExpression(formatExpression); |
| 920 |
} |
| 921 |
|
| 922 |
public Expression getContentExpression() { |
| 923 |
return contentOp.getChildExpression(); |
| 924 |
} |
| 925 |
|
| 926 |
} |
| 927 |
|