| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Version; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.expr.Callable; |
| 14 |
import net.sf.saxon.expr.StaticProperty; |
| 15 |
import net.sf.saxon.expr.XPathContext; |
| 16 |
import net.sf.saxon.lib.*; |
| 17 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 18 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 19 |
import net.sf.saxon.ma.map.HashTrieMap; |
| 20 |
import net.sf.saxon.ma.map.MapItem; |
| 21 |
import net.sf.saxon.ma.map.MapType; |
| 22 |
import net.sf.saxon.om.*; |
| 23 |
import net.sf.saxon.s9api.*; |
| 24 |
import net.sf.saxon.serialize.CharacterMap; |
| 25 |
import net.sf.saxon.serialize.CharacterMapIndex; |
| 26 |
import net.sf.saxon.serialize.SerializationProperties; |
| 27 |
import net.sf.saxon.trans.StylesheetCache; |
| 28 |
import net.sf.saxon.trans.XPathException; |
| 29 |
import net.sf.saxon.trans.XsltController; |
| 30 |
import net.sf.saxon.tree.iter.AtomicIterator; |
| 31 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 32 |
import net.sf.saxon.tree.wrapper.RebasedDocument; |
| 33 |
import net.sf.saxon.type.SpecificFunctionType; |
| 34 |
import net.sf.saxon.value.*; |
| 35 |
import net.sf.saxon.value.SequenceType; |
| 36 |
import org.xml.sax.InputSource; |
| 37 |
|
| 38 |
import javax.xml.transform.ErrorListener; |
| 39 |
import javax.xml.transform.OutputKeys; |
| 40 |
import javax.xml.transform.Source; |
| 41 |
import javax.xml.transform.TransformerException; |
| 42 |
import javax.xml.transform.sax.SAXSource; |
| 43 |
import javax.xml.transform.stream.StreamSource; |
| 44 |
import java.io.File; |
| 45 |
import java.io.StringReader; |
| 46 |
import java.io.StringWriter; |
| 47 |
import java.net.URI; |
| 48 |
import java.net.URISyntaxException; |
| 49 |
import java.util.ArrayList; |
| 50 |
import java.util.HashMap; |
| 51 |
import java.util.List; |
| 52 |
import java.util.Map; |
| 53 |
import java.util.concurrent.ConcurrentHashMap; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class TransformFn extends SystemFunction implements Callable { |
| 59 |
|
| 60 |
|
| 61 |
private static String[] transformOptionNames30 = new String[]{ |
| 62 |
"package-name", "package-version", "package-node", "package-location", "static-params", "global-context-item", |
| 63 |
"template-params", "tunnel-params", "initial-function", "function-params" |
| 64 |
}; |
| 65 |
|
| 66 |
private final static String dummyBaseOutputUriScheme = "dummy"; |
| 67 |
|
| 68 |
|
| 69 |
private boolean isTransformOptionName30(String string) { |
| 70 |
for (String s : transformOptionNames30) { |
| 71 |
if (s.equals(string)) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
public static OptionsParameter makeOptionsParameter() { |
| 80 |
OptionsParameter op = new OptionsParameter(); |
| 81 |
op.addAllowedOption("xslt-version", SequenceType.SINGLE_DECIMAL); |
| 82 |
op.addAllowedOption("stylesheet-location", SequenceType.SINGLE_STRING); |
| 83 |
op.addAllowedOption("stylesheet-node", SequenceType.SINGLE_NODE); |
| 84 |
op.addAllowedOption("stylesheet-text", SequenceType.SINGLE_STRING); |
| 85 |
op.addAllowedOption("stylesheet-base-uri", SequenceType.SINGLE_STRING); |
| 86 |
op.addAllowedOption("base-output-uri", SequenceType.SINGLE_STRING); |
| 87 |
op.addAllowedOption("stylesheet-params", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 88 |
op.addAllowedOption("source-node", SequenceType.SINGLE_NODE); |
| 89 |
op.addAllowedOption("source-location", SequenceType.SINGLE_STRING); |
| 90 |
op.addAllowedOption("initial-mode", SequenceType.SINGLE_QNAME); |
| 91 |
op.addAllowedOption("initial-match-selection", SequenceType.ANY_SEQUENCE); |
| 92 |
op.addAllowedOption("initial-template", SequenceType.SINGLE_QNAME); |
| 93 |
op.addAllowedOption("delivery-format", SequenceType.SINGLE_STRING); |
| 94 |
op.addAllowedOption("serialization-params", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 95 |
op.addAllowedOption("vendor-options", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 96 |
op.addAllowedOption("cache", SequenceType.SINGLE_BOOLEAN); |
| 97 |
op.addAllowedOption("package-name", SequenceType.SINGLE_STRING); |
| 98 |
op.addAllowedOption("package-version", SequenceType.SINGLE_STRING); |
| 99 |
op.addAllowedOption("package-node", SequenceType.SINGLE_NODE); |
| 100 |
op.addAllowedOption("package-location", SequenceType.SINGLE_STRING); |
| 101 |
op.addAllowedOption("static-params", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 102 |
op.addAllowedOption("global-context-item", SequenceType.SINGLE_ITEM); |
| 103 |
op.addAllowedOption("template-params", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 104 |
op.addAllowedOption("tunnel-params", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 105 |
op.addAllowedOption("initial-function", SequenceType.SINGLE_QNAME); |
| 106 |
op.addAllowedOption("function-params", ArrayItemType.SINGLE_ARRAY); |
| 107 |
op.addAllowedOption("requested-properties", SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE)); |
| 108 |
op.addAllowedOption("post-process", SequenceType.makeSequenceType( |
| 109 |
new SpecificFunctionType(new SequenceType[]{SequenceType.SINGLE_STRING, SequenceType.ANY_SEQUENCE}, SequenceType.ANY_SEQUENCE), |
| 110 |
StaticProperty.EXACTLY_ONE)); |
| 111 |
|
| 112 |
return op; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private void checkTransformOptions(Map<String, Sequence<?>> options, XPathContext context, boolean isXslt30Processor) throws XPathException { |
| 125 |
if (options.isEmpty()) { |
| 126 |
throw new XPathException("No transformation options supplied", "FOXT0002"); |
| 127 |
} |
| 128 |
|
| 129 |
for (String keyName : options.keySet()) { |
| 130 |
if (isTransformOptionName30(keyName) && !isXslt30Processor) { |
| 131 |
throw new XPathException("The transform option " + keyName + " is only available when using an XSLT 3.0 processor", "FOXT0002"); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
private String checkStylesheetMutualExclusion(Map<String, Sequence<?>> map) throws XPathException { |
| 137 |
return exactlyOneOf(map, "stylesheet-location", "stylesheet-node", "stylesheet-text"); |
| 138 |
} |
| 139 |
|
| 140 |
private String checkStylesheetMutualExclusion30(Map<String, Sequence<?>> map) throws XPathException { |
| 141 |
String styleOption = exactlyOneOf(map, "stylesheet-location", "stylesheet-node", "stylesheet-text", |
| 142 |
"package-name", "package-node", "package-location"); |
| 143 |
if (styleOption.equals("package-location")) { |
| 144 |
throw new XPathException("The transform option " + styleOption + " is not implemented in Saxon", "FOXT0002"); |
| 145 |
} |
| 146 |
return styleOption; |
| 147 |
} |
| 148 |
|
| 149 |
private String checkInvocationMutualExclusion(Map<String, Sequence<?>> options) throws XPathException { |
| 150 |
return oneOf(options, "initial-mode", "initial-template"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private String oneOf(Map<String, Sequence<?>> map, String... keys) throws XPathException { |
| 163 |
String found = null; |
| 164 |
for (String s : keys) { |
| 165 |
if (map.get(s) != null) { |
| 166 |
if (found != null) { |
| 167 |
throw new XPathException( |
| 168 |
"The following transform options are mutually exclusive: " + enumerate(keys), "FOXT0002"); |
| 169 |
} else { |
| 170 |
found = s; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
return found; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private String exactlyOneOf(Map<String, Sequence<?>> map, String... keys) throws XPathException { |
| 188 |
String found = oneOf(map, keys); |
| 189 |
if (found == null) { |
| 190 |
throw new XPathException("One of the following transform options must be present: " + enumerate(keys)); |
| 191 |
} |
| 192 |
return found; |
| 193 |
} |
| 194 |
|
| 195 |
private String enumerate(String... keys) { |
| 196 |
boolean first = true; |
| 197 |
FastStringBuffer buffer = new FastStringBuffer(256); |
| 198 |
for (String k : keys) { |
| 199 |
if (first) { |
| 200 |
first = false; |
| 201 |
} else { |
| 202 |
buffer.append(" | "); |
| 203 |
} |
| 204 |
buffer.append(k); |
| 205 |
} |
| 206 |
return buffer.toString(); |
| 207 |
} |
| 208 |
|
| 209 |
private String checkInvocationMutualExclusion30(Map<String, Sequence<?>> map) throws XPathException { |
| 210 |
return oneOf(map, "initial-mode", "initial-template", "initial-function"); |
| 211 |
} |
| 212 |
|
| 213 |
private void unsuitable(String option, String value) throws XPathException { |
| 214 |
throw new XPathException("No XSLT processor is available with xsl:" + option + " = " + value, "FOXT0001"); |
| 215 |
} |
| 216 |
|
| 217 |
private boolean asBoolean(AtomicValue value) throws XPathException { |
| 218 |
if (value instanceof BooleanValue) { |
| 219 |
return ((BooleanValue)value).getBooleanValue(); |
| 220 |
} else if (value instanceof StringValue) { |
| 221 |
String s = Whitespace.normalizeWhitespace(value.getStringValue()).toString(); |
| 222 |
if (s.equals("yes") || s.equals("true") || s.equals("1")) { |
| 223 |
return true; |
| 224 |
} else if (s.equals("no") || s.equals("false") || s.equals("0")) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
} |
| 228 |
throw new XPathException("Unrecognized boolean value " + value, "FOXT0002"); |
| 229 |
} |
| 230 |
|
| 231 |
private void setRequestedProperties(Map<String, Sequence<?>> options, Processor processor) throws XPathException { |
| 232 |
MapItem requestedProps = (MapItem) options.get("requested-properties").head(); |
| 233 |
AtomicIterator optionIterator = requestedProps.keys(); |
| 234 |
while (true) { |
| 235 |
AtomicValue option = optionIterator.next(); |
| 236 |
if (option != null) { |
| 237 |
StructuredQName optionName = ((QNameValue) option.head()).getStructuredQName(); |
| 238 |
AtomicValue value = (AtomicValue)requestedProps.get(option).head(); |
| 239 |
if (optionName.hasURI(NamespaceConstant.XSLT)) { |
| 240 |
String localName = optionName.getLocalPart(); |
| 241 |
switch (localName) { |
| 242 |
case "vendor-url": |
| 243 |
if (!(value.getStringValue().contains("saxonica.com") || value.getStringValue().equals("Saxonica"))) { |
| 244 |
unsuitable("vendor-url", value.getStringValue()); |
| 245 |
} |
| 246 |
break; |
| 247 |
case "product-name": |
| 248 |
if (!value.getStringValue().equals("SAXON")) { |
| 249 |
unsuitable("vendor-url", value.getStringValue()); |
| 250 |
} |
| 251 |
break; |
| 252 |
case "product-version": |
| 253 |
if (!Version.getProductVersion().startsWith(value.getStringValue())) { |
| 254 |
unsuitable("product-version", value.getStringValue()); |
| 255 |
} |
| 256 |
break; |
| 257 |
case "is-schema-aware": { |
| 258 |
boolean b = asBoolean(value); |
| 259 |
if (b) { |
| 260 |
if (processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT)) { |
| 261 |
processor.setConfigurationProperty(Feature.XSLT_SCHEMA_AWARE, true); |
| 262 |
} else { |
| 263 |
unsuitable("is-schema-aware", value.getStringValue()); |
| 264 |
} |
| 265 |
} else { |
| 266 |
if (processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT)) { |
| 267 |
unsuitable("is-schema-aware", value.getStringValue()); |
| 268 |
} |
| 269 |
} |
| 270 |
break; |
| 271 |
} |
| 272 |
case "supports-serialization": { |
| 273 |
boolean b = asBoolean(value); |
| 274 |
if (!b) { |
| 275 |
unsuitable("supports-serialization", value.getStringValue()); |
| 276 |
} |
| 277 |
break; |
| 278 |
} |
| 279 |
case "supports-backwards-compatibility": { |
| 280 |
boolean b = asBoolean(value); |
| 281 |
if (!b) { |
| 282 |
unsuitable("supports-backwards-compatibility", value.getStringValue()); |
| 283 |
} |
| 284 |
break; |
| 285 |
} |
| 286 |
case "supports-namespace-axis": { |
| 287 |
boolean b = asBoolean(value); |
| 288 |
if (!b) { |
| 289 |
unsuitable("supports-namespace-axis", value.getStringValue()); |
| 290 |
} |
| 291 |
break; |
| 292 |
} |
| 293 |
case "supports-streaming": { |
| 294 |
boolean b = asBoolean(value); |
| 295 |
if (b) { |
| 296 |
if (!processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT)) { |
| 297 |
unsuitable("supports-streaming", value.getStringValue()); |
| 298 |
} |
| 299 |
} else { |
| 300 |
if (processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT)) { |
| 301 |
processor.setConfigurationProperty(Feature.STREAMABILITY, "off"); |
| 302 |
} |
| 303 |
} |
| 304 |
break; |
| 305 |
} |
| 306 |
case "supports-dynamic-evaluation": { |
| 307 |
boolean b = asBoolean(value); |
| 308 |
if (b) { |
| 309 |
if (!processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION)) { |
| 310 |
unsuitable("supports-dynamic-evaluation", value.getStringValue()); |
| 311 |
} |
| 312 |
} else { |
| 313 |
if (processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION)) { |
| 314 |
processor.setConfigurationProperty(Feature.DISABLE_XSL_EVALUATE, true); |
| 315 |
} |
| 316 |
} |
| 317 |
break; |
| 318 |
} |
| 319 |
case "supports-higher-order-functions": { |
| 320 |
boolean b = asBoolean(value); |
| 321 |
if (b != processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION)) { |
| 322 |
unsuitable("supports-higher-order-functions", value.getStringValue()); |
| 323 |
} |
| 324 |
break; |
| 325 |
} |
| 326 |
case "xpath-version": { |
| 327 |
String v = value.getStringValue(); |
| 328 |
try { |
| 329 |
if (Double.parseDouble(v) > 3.1) { |
| 330 |
unsuitable("xpath-version", value.getStringValue()); |
| 331 |
} |
| 332 |
} catch (NumberFormatException nfe) { |
| 333 |
unsuitable("xpath-version", value.getStringValue()); |
| 334 |
} |
| 335 |
break; |
| 336 |
} |
| 337 |
case "xsd-version": { |
| 338 |
String v = value.getStringValue(); |
| 339 |
try { |
| 340 |
if (Double.parseDouble(v) > 1.1) { |
| 341 |
unsuitable("xsd-version", value.getStringValue()); |
| 342 |
} |
| 343 |
} catch (NumberFormatException nfe) { |
| 344 |
unsuitable("xsd-version", value.getStringValue()); |
| 345 |
} |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} else { |
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
private void setStaticParams(Map<String, Sequence<?>> options, XsltCompiler xsltCompiler, boolean allowTypedNodes) throws XPathException { |
| 357 |
MapItem staticParamsMap = (MapItem) options.get("static-params").head(); |
| 358 |
AtomicIterator paramIterator = staticParamsMap.keys(); |
| 359 |
while (true) { |
| 360 |
AtomicValue param = paramIterator.next(); |
| 361 |
if (param != null) { |
| 362 |
if (!(param instanceof QNameValue)) { |
| 363 |
throw new XPathException("Parameter names in static-params must be supplied as QNames", "FOXT0002"); |
| 364 |
} |
| 365 |
QName paramName = new QName(((QNameValue) param).getStructuredQName()); |
| 366 |
GroundedValue value = staticParamsMap.get(param); |
| 367 |
if (!allowTypedNodes) { |
| 368 |
checkSequenceIsUntyped(value); |
| 369 |
} |
| 370 |
XdmValue paramVal = XdmValue.wrap(value); |
| 371 |
xsltCompiler.setParameter(paramName, paramVal); |
| 372 |
} else { |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
private XsltExecutable getStylesheet(Map<String, Sequence<?>> options, XsltCompiler xsltCompiler, String styleOptionStr, XPathContext context) throws XPathException { |
| 379 |
Item styleOptionItem = options.get(styleOptionStr).head(); |
| 380 |
URI stylesheetBaseUri = null; |
| 381 |
Sequence<?> seq; |
| 382 |
if ((seq = options.get("stylesheet-base-uri")) != null) { |
| 383 |
StringValue styleBaseUri = (StringValue) seq.head(); |
| 384 |
stylesheetBaseUri = URI.create(styleBaseUri.getStringValue()); |
| 385 |
if (!stylesheetBaseUri.isAbsolute()) { |
| 386 |
URI staticBase = getRetainedStaticContext().getStaticBaseUri(); |
| 387 |
stylesheetBaseUri = staticBase.resolve(styleBaseUri.getStringValue()); |
| 388 |
} |
| 389 |
} |
| 390 |
final List<TransformerException> compileErrors = new ArrayList<>(); |
| 391 |
final ErrorListener originalListener = xsltCompiler.getErrorListener(); |
| 392 |
xsltCompiler.setErrorListener(new ErrorListener() { |
| 393 |
@Override |
| 394 |
public void warning(TransformerException exception) throws TransformerException { |
| 395 |
originalListener.warning(exception); |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public void error(TransformerException exception) throws TransformerException { |
| 400 |
compileErrors.add(exception); |
| 401 |
originalListener.error(exception); |
| 402 |
} |
| 403 |
|
| 404 |
@Override |
| 405 |
public void fatalError(TransformerException exception) throws TransformerException { |
| 406 |
compileErrors.add(exception); |
| 407 |
originalListener.fatalError(exception); |
| 408 |
} |
| 409 |
}); |
| 410 |
boolean cacheable = options.get("static-params") == null; |
| 411 |
if (options.get("cache") != null) { |
| 412 |
cacheable &= ((BooleanValue) options.get("cache").head()).getBooleanValue(); |
| 413 |
} |
| 414 |
|
| 415 |
StylesheetCache cache = context.getController().getStylesheetCache(); |
| 416 |
XsltExecutable executable = null; |
| 417 |
switch (styleOptionStr) { |
| 418 |
case "stylesheet-location": |
| 419 |
String stylesheetLocation = styleOptionItem.getStringValue(); |
| 420 |
if (cacheable) { |
| 421 |
executable = cache.getStylesheetByLocation(stylesheetLocation); |
| 422 |
} |
| 423 |
if (executable == null) { |
| 424 |
Source style; |
| 425 |
try { |
| 426 |
String base = getStaticBaseUriString(); |
| 427 |
style = xsltCompiler.getURIResolver().resolve(stylesheetLocation, base); |
| 428 |
|
| 429 |
|
| 430 |
if (style == null) { |
| 431 |
style = xsltCompiler.getProcessor().getUnderlyingConfiguration().getSystemURIResolver().resolve(stylesheetLocation, base); |
| 432 |
} |
| 433 |
} catch (TransformerException e) { |
| 434 |
throw new XPathException(e); |
| 435 |
} |
| 436 |
|
| 437 |
try { |
| 438 |
executable = xsltCompiler.compile(style); |
| 439 |
} catch (SaxonApiException e) { |
| 440 |
return reportCompileError(e, compileErrors); |
| 441 |
} |
| 442 |
if (cacheable) { |
| 443 |
cache.setStylesheetByLocation(stylesheetLocation, executable); |
| 444 |
} |
| 445 |
} |
| 446 |
break; |
| 447 |
case "stylesheet-node": |
| 448 |
case "package-node": |
| 449 |
NodeInfo stylesheetNode = (NodeInfo) styleOptionItem; |
| 450 |
|
| 451 |
if (stylesheetBaseUri != null && !stylesheetNode.getBaseURI().equals(stylesheetBaseUri.toASCIIString())) { |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
String newBaseUri = stylesheetBaseUri.toASCIIString(); |
| 458 |
RebasedDocument rebased = new RebasedDocument( |
| 459 |
stylesheetNode.getTreeInfo(), |
| 460 |
node -> newBaseUri, |
| 461 |
node -> newBaseUri); |
| 462 |
|
| 463 |
stylesheetNode = rebased.getRootNode(); |
| 464 |
} |
| 465 |
|
| 466 |
if (cacheable) { |
| 467 |
executable = cache.getStylesheetByNode(stylesheetNode); |
| 468 |
} |
| 469 |
if (executable == null) { |
| 470 |
Source source = stylesheetNode; |
| 471 |
if (stylesheetBaseUri != null) { |
| 472 |
source = AugmentedSource.makeAugmentedSource(source); |
| 473 |
source.setSystemId(stylesheetBaseUri.toASCIIString()); |
| 474 |
} |
| 475 |
try { |
| 476 |
executable = xsltCompiler.compile(source); |
| 477 |
} catch (SaxonApiException e) { |
| 478 |
reportCompileError(e, compileErrors); |
| 479 |
} |
| 480 |
if (cacheable) { |
| 481 |
cache.setStylesheetByNode(stylesheetNode, executable); |
| 482 |
} |
| 483 |
} |
| 484 |
break; |
| 485 |
case "stylesheet-text": |
| 486 |
String stylesheetText = styleOptionItem.getStringValue(); |
| 487 |
if (cacheable) { |
| 488 |
executable = cache.getStylesheetByText(stylesheetText); |
| 489 |
} |
| 490 |
if (executable == null) { |
| 491 |
StringReader sr = new StringReader(stylesheetText); |
| 492 |
SAXSource style = new SAXSource(new InputSource(sr)); |
| 493 |
if (stylesheetBaseUri != null) { |
| 494 |
style.setSystemId(stylesheetBaseUri.toASCIIString()); |
| 495 |
} |
| 496 |
try { |
| 497 |
executable = xsltCompiler.compile(style); |
| 498 |
} catch (SaxonApiException e) { |
| 499 |
reportCompileError(e, compileErrors); |
| 500 |
} |
| 501 |
if (cacheable) { |
| 502 |
cache.setStylesheetByText(stylesheetText, executable); |
| 503 |
} |
| 504 |
} |
| 505 |
break; |
| 506 |
case "package-name": |
| 507 |
String packageName = Whitespace.trim(styleOptionItem.getStringValue()); |
| 508 |
String packageVersion = null; |
| 509 |
if (options.get("package-version") != null) { |
| 510 |
packageVersion = options.get("package-version").head().getStringValue(); |
| 511 |
} |
| 512 |
try { |
| 513 |
XsltPackage pack = xsltCompiler.obtainPackage(packageName, packageVersion); |
| 514 |
if (pack == null) { |
| 515 |
throw new XPathException("Cannot locate package " + packageName + " version " + packageVersion, "FOXT0002"); |
| 516 |
} |
| 517 |
executable = pack.link(); |
| 518 |
} catch (SaxonApiException e) { |
| 519 |
if (e.getCause() instanceof XPathException) { |
| 520 |
throw (XPathException) e.getCause(); |
| 521 |
} else { |
| 522 |
throw new XPathException(e); |
| 523 |
} |
| 524 |
} |
| 525 |
break; |
| 526 |
} |
| 527 |
return executable; |
| 528 |
} |
| 529 |
|
| 530 |
private XsltExecutable reportCompileError(SaxonApiException e, List<TransformerException> compileErrors) throws XPathException { |
| 531 |
for (TransformerException te : compileErrors) { |
| 532 |
|
| 533 |
if (te instanceof XPathException) { |
| 534 |
if (((XPathException) te).getErrorCodeLocalPart() == null) { |
| 535 |
((XPathException) te).setErrorCode("FOXT0002"); |
| 536 |
} |
| 537 |
throw (XPathException)te; |
| 538 |
} |
| 539 |
} |
| 540 |
if (e.getCause() instanceof XPathException) { |
| 541 |
throw (XPathException) e.getCause(); |
| 542 |
} else { |
| 543 |
throw new XPathException(e); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 549 |
Map<String, Sequence<?>> options = getDetails().optionDetails.processSuppliedOptions((MapItem) arguments[0].head(), context); |
| 550 |
|
| 551 |
Sequence<?> vendorOptionsValue = options.get("vendor-options"); |
| 552 |
MapItem vendorOptions = vendorOptionsValue == null ? null : (MapItem) vendorOptionsValue.head(); |
| 553 |
|
| 554 |
Configuration targetConfig = context.getConfiguration(); |
| 555 |
boolean allowTypedNodes = true; |
| 556 |
int schemaValidation = Validation.DEFAULT; |
| 557 |
|
| 558 |
if (vendorOptions != null) { |
| 559 |
Sequence<?> optionValue = vendorOptions.get(new QNameValue("", NamespaceConstant.SAXON, "configuration")); |
| 560 |
if (optionValue != null) { |
| 561 |
NodeInfo configFile = (NodeInfo) optionValue.head(); |
| 562 |
targetConfig = Configuration.readConfiguration(configFile, targetConfig); |
| 563 |
allowTypedNodes = false; |
| 564 |
if (!context.getConfiguration().getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) { |
| 565 |
targetConfig.setBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS, false); |
| 566 |
} |
| 567 |
} |
| 568 |
optionValue = vendorOptions.get(new QNameValue("", NamespaceConstant.SAXON, "schema-validation")); |
| 569 |
if (optionValue != null) { |
| 570 |
String valOption = optionValue.head().getStringValue(); |
| 571 |
schemaValidation = Validation.getCode(valOption); |
| 572 |
} |
| 573 |
} |
| 574 |
Processor processor = new Processor(true); |
| 575 |
processor.setConfigurationProperty(Feature.CONFIGURATION, targetConfig); |
| 576 |
boolean isXslt30Processor = true; |
| 577 |
checkTransformOptions(options, context, isXslt30Processor); |
| 578 |
boolean useXslt30Processor = isXslt30Processor; |
| 579 |
if (options.get("xslt-version") != null) { |
| 580 |
BigDecimalValue xsltVersion = (BigDecimalValue) options.get("xslt-version").head(); |
| 581 |
if ((xsltVersion.compareTo(BigDecimalValue.THREE) >= 0 && !isXslt30Processor) || (xsltVersion.compareTo(BigDecimalValue.THREE) > 0 && isXslt30Processor)) { |
| 582 |
throw new XPathException("The transform option xslt-version is higher than the XSLT version supported by this processor", "FOXT0002"); |
| 583 |
} |
| 584 |
useXslt30Processor = xsltVersion.compareTo(BigDecimalValue.THREE) == 0; |
| 585 |
} |
| 586 |
String principalInput = oneOf(options, "source-node", "source-location", "initial-match-selection"); |
| 587 |
|
| 588 |
|
| 589 |
String invocationOption; |
| 590 |
String invocationName = "invocation"; |
| 591 |
String styleOption; |
| 592 |
|
| 593 |
invocationOption = checkInvocationMutualExclusion30(options); |
| 594 |
|
| 595 |
if (invocationOption != null) { |
| 596 |
invocationName = invocationOption; |
| 597 |
} |
| 598 |
if (!invocationName.equals("initial-template") && !invocationName.equals("initial-function") && principalInput == null) { |
| 599 |
|
| 600 |
invocationName = "initial-template"; |
| 601 |
options.put("initial-template", new QNameValue("", NamespaceConstant.XSLT, "initial-template")); |
| 602 |
} |
| 603 |
|
| 604 |
if (invocationName.equals("initial-function") && options.get("function-params") == null) { |
| 605 |
throw new XPathException("Use of the transform option initial-function requires the function parameters to be supplied using the option function-params", "FOXT0002"); |
| 606 |
} |
| 607 |
|
| 608 |
if (!invocationName.equals("initial-function") && options.get("function-params") != null) { |
| 609 |
throw new XPathException("The transform option function-params can only be used if the option initial-function is also used", "FOXT0002"); |
| 610 |
} |
| 611 |
styleOption = checkStylesheetMutualExclusion30(options); |
| 612 |
|
| 613 |
|
| 614 |
if (options.get("requested-properties") != null) { |
| 615 |
setRequestedProperties(options, processor); |
| 616 |
} |
| 617 |
|
| 618 |
XsltCompiler xsltCompiler = processor.newXsltCompiler(); |
| 619 |
xsltCompiler.setURIResolver(context.getURIResolver()); |
| 620 |
xsltCompiler.setJustInTimeCompilation(false); |
| 621 |
|
| 622 |
|
| 623 |
if (options.get("static-params") != null) { |
| 624 |
setStaticParams(options, xsltCompiler, allowTypedNodes); |
| 625 |
} |
| 626 |
|
| 627 |
XsltExecutable sheet = getStylesheet(options, xsltCompiler, styleOption, context); |
| 628 |
Xslt30Transformer transformer = sheet.load30(); |
| 629 |
|
| 630 |
|
| 631 |
String deliveryFormat = "document"; |
| 632 |
NodeInfo sourceNode = null; |
| 633 |
String sourceLocation = null; |
| 634 |
XdmValue initialMatchSelection = null; |
| 635 |
QName initialTemplate = null; |
| 636 |
QName initialMode = null; |
| 637 |
String baseOutputUri = null; |
| 638 |
Map<QName, XdmValue> stylesheetParams = new HashMap<>(); |
| 639 |
MapItem serializationParamsMap = null; |
| 640 |
StringWriter serializedResult = null; |
| 641 |
File serializedResultFile = null; |
| 642 |
XdmItem globalContextItem = null; |
| 643 |
Map<QName, XdmValue> templateParams = new HashMap<>(); |
| 644 |
Map<QName, XdmValue> tunnelParams = new HashMap<>(); |
| 645 |
QName initialFunction = null; |
| 646 |
XdmValue[] functionParams = null; |
| 647 |
Function postProcessor = null; |
| 648 |
String principalResultKey = "output"; |
| 649 |
|
| 650 |
for (String name : options.keySet()) { |
| 651 |
Sequence<?> value = options.get(name); |
| 652 |
Item head = value.head(); |
| 653 |
switch (name) { |
| 654 |
case "source-node": |
| 655 |
sourceNode = (NodeInfo) head; |
| 656 |
if (!allowTypedNodes) { |
| 657 |
checkSequenceIsUntyped(sourceNode); |
| 658 |
} |
| 659 |
break; |
| 660 |
case "source-location": |
| 661 |
sourceLocation = head.getStringValue(); |
| 662 |
break; |
| 663 |
case "initial-template": |
| 664 |
initialTemplate = new QName(((QNameValue) head).getStructuredQName()); |
| 665 |
break; |
| 666 |
case "initial-mode": |
| 667 |
initialMode = new QName(((QNameValue) head).getStructuredQName()); |
| 668 |
break; |
| 669 |
case "initial-match-selection": |
| 670 |
initialMatchSelection = XdmValue.wrap(value); |
| 671 |
if (!allowTypedNodes) { |
| 672 |
checkSequenceIsUntyped(value); |
| 673 |
} |
| 674 |
break; |
| 675 |
case "delivery-format": |
| 676 |
deliveryFormat = head.getStringValue(); |
| 677 |
if (!deliveryFormat.equals("document") && !deliveryFormat.equals("serialized") && !deliveryFormat.equals("raw")) { |
| 678 |
throw new XPathException("The transform option delivery-format should be one of: document|serialized|raw ", "FOXT0002"); |
| 679 |
} |
| 680 |
break; |
| 681 |
case "base-output-uri": |
| 682 |
baseOutputUri = head.getStringValue(); |
| 683 |
principalResultKey = baseOutputUri; |
| 684 |
|
| 685 |
break; |
| 686 |
case "serialization-params": |
| 687 |
serializationParamsMap = (MapItem) head; |
| 688 |
|
| 689 |
break; |
| 690 |
case "stylesheet-params": { |
| 691 |
MapItem params = (MapItem) head; |
| 692 |
processParams(params, stylesheetParams, allowTypedNodes); |
| 693 |
break; |
| 694 |
} |
| 695 |
case "global-context-item": |
| 696 |
if (useXslt30Processor) { |
| 697 |
if (!allowTypedNodes && head instanceof NodeInfo && ((NodeInfo) head).getTreeInfo().isTyped()) { |
| 698 |
throw new XPathException("Schema-validated nodes cannot be passed to fn:transform() when it runs under a different Saxon Configuration", "FOXT0002"); |
| 699 |
} |
| 700 |
globalContextItem = (XdmItem) XdmValue.wrap(head); |
| 701 |
} |
| 702 |
break; |
| 703 |
case "template-params": { |
| 704 |
MapItem params = (MapItem) head; |
| 705 |
processParams(params, templateParams, allowTypedNodes); |
| 706 |
break; |
| 707 |
} |
| 708 |
case "tunnel-params": { |
| 709 |
MapItem params = (MapItem) head; |
| 710 |
processParams(params, tunnelParams, allowTypedNodes); |
| 711 |
break; |
| 712 |
} |
| 713 |
case "initial-function": |
| 714 |
initialFunction = new QName(((QNameValue) head).getStructuredQName()); |
| 715 |
break; |
| 716 |
case "function-params": |
| 717 |
ArrayItem functionParamsArray = (ArrayItem) head; |
| 718 |
functionParams = new XdmValue[functionParamsArray.arrayLength()]; |
| 719 |
for (int i = 0; i < functionParams.length; i++) { |
| 720 |
Sequence<?> argVal = functionParamsArray.get(i); |
| 721 |
if (!allowTypedNodes) { |
| 722 |
checkSequenceIsUntyped(argVal); |
| 723 |
} |
| 724 |
functionParams[i] = XdmValue.wrap(argVal); |
| 725 |
} |
| 726 |
break; |
| 727 |
case "post-process": |
| 728 |
postProcessor = (Function) head; |
| 729 |
break; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
if (baseOutputUri == null) { |
| 734 |
baseOutputUri = getStaticBaseUriString(); |
| 735 |
} else { |
| 736 |
try { |
| 737 |
URI base = new URI(baseOutputUri); |
| 738 |
if (!base.isAbsolute()) { |
| 739 |
base = getRetainedStaticContext().getStaticBaseUri().resolve(baseOutputUri); |
| 740 |
baseOutputUri = base.toASCIIString(); |
| 741 |
} |
| 742 |
} catch (URISyntaxException err) { |
| 743 |
throw new XPathException("Invalid base output URI " + baseOutputUri, "FOXT0002"); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
Deliverer deliverer = Deliverer.makeDeliverer(deliveryFormat); |
| 748 |
deliverer.setTransformer(transformer); |
| 749 |
deliverer.setBaseOutputUri(baseOutputUri); |
| 750 |
deliverer.setPrincipalResultKey(principalResultKey); |
| 751 |
deliverer.setPostProcessor(postProcessor, context); |
| 752 |
|
| 753 |
XsltController controller = transformer.getUnderlyingController(); |
| 754 |
controller.setResultDocumentResolver(deliverer); |
| 755 |
|
| 756 |
Destination destination = deliverer.getPrimaryDestination(serializationParamsMap); |
| 757 |
Sequence<?> result; |
| 758 |
try { |
| 759 |
transformer.setStylesheetParameters(stylesheetParams); |
| 760 |
transformer.setBaseOutputURI(baseOutputUri); |
| 761 |
transformer.setInitialTemplateParameters(templateParams, false); |
| 762 |
transformer.setInitialTemplateParameters(tunnelParams, true); |
| 763 |
|
| 764 |
if (schemaValidation == Validation.STRICT || schemaValidation == Validation.LAX) { |
| 765 |
if (sourceNode != null) { |
| 766 |
sourceNode = validate(sourceNode, targetConfig, schemaValidation); |
| 767 |
} else if (sourceLocation != null) { |
| 768 |
StreamSource ss = new StreamSource(sourceLocation); |
| 769 |
ParseOptions parseOptions = new ParseOptions(targetConfig.getParseOptions()); |
| 770 |
parseOptions.setSchemaValidationMode(schemaValidation); |
| 771 |
TreeInfo tree = targetConfig.buildDocumentTree(ss, parseOptions); |
| 772 |
sourceNode = tree.getRootNode(); |
| 773 |
sourceLocation = null; |
| 774 |
} |
| 775 |
if (globalContextItem instanceof XdmNode) { |
| 776 |
NodeInfo v = validate(((XdmNode)globalContextItem).getUnderlyingNode(), targetConfig, schemaValidation); |
| 777 |
globalContextItem = (XdmNode)XdmValue.wrap(v); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
if (sourceNode != null && globalContextItem == null) { |
| 782 |
transformer.setGlobalContextItem(new XdmNode(sourceNode.getRoot())); |
| 783 |
} |
| 784 |
if (globalContextItem != null) { |
| 785 |
transformer.setGlobalContextItem(globalContextItem); |
| 786 |
} |
| 787 |
if (initialTemplate != null) { |
| 788 |
transformer.callTemplate(initialTemplate, destination); |
| 789 |
result = deliverer.getPrimaryResult(); |
| 790 |
} else if (initialFunction != null) { |
| 791 |
transformer.callFunction(initialFunction, functionParams, destination); |
| 792 |
result = deliverer.getPrimaryResult(); |
| 793 |
} else { |
| 794 |
if (initialMode != null) { |
| 795 |
transformer.setInitialMode(initialMode); |
| 796 |
} |
| 797 |
if (initialMatchSelection == null && sourceNode != null) { |
| 798 |
initialMatchSelection = XdmValue.wrap(sourceNode); |
| 799 |
} |
| 800 |
if (initialMatchSelection == null && sourceLocation != null) { |
| 801 |
StreamSource stream = new StreamSource(sourceLocation); |
| 802 |
if (transformer.getUnderlyingController().getInitialMode().isDeclaredStreamable()) { |
| 803 |
transformer.applyTemplates(stream, destination); |
| 804 |
} else { |
| 805 |
transformer.transform(stream,destination); |
| 806 |
} |
| 807 |
result = deliverer.getPrimaryResult(); |
| 808 |
} else { |
| 809 |
transformer.applyTemplates(initialMatchSelection, destination); |
| 810 |
result = deliverer.getPrimaryResult(); |
| 811 |
} |
| 812 |
} |
| 813 |
} catch (SaxonApiException e) { |
| 814 |
XPathException e2; |
| 815 |
if (e.getCause() instanceof XPathException) { |
| 816 |
e2 = (XPathException) e.getCause(); |
| 817 |
e2.setIsGlobalError(false); |
| 818 |
throw e2; |
| 819 |
} else { |
| 820 |
throw new XPathException(e); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
HashTrieMap resultMap = new HashTrieMap(); |
| 827 |
resultMap = deliverer.populateResultMap(resultMap); |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (result != null) { |
| 832 |
AtomicValue resultKey = new StringValue(principalResultKey); |
| 833 |
resultMap = resultMap.addEntry(resultKey, result.materialize()); |
| 834 |
} |
| 835 |
return resultMap; |
| 836 |
|
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private void processParams(MapItem suppliedParams, Map<QName, XdmValue> checkedParams, boolean allowTypedNodes) throws XPathException { |
| 850 |
AtomicIterator paramIterator = suppliedParams.keys(); |
| 851 |
while (true) { |
| 852 |
AtomicValue param = paramIterator.next(); |
| 853 |
if (param != null) { |
| 854 |
if (!(param instanceof QNameValue)) { |
| 855 |
throw new XPathException("The names of parameters must be supplied as QNames", "FOXT0002"); |
| 856 |
} |
| 857 |
QName paramName = new QName(((QNameValue) param).getStructuredQName()); |
| 858 |
Sequence<?> value = suppliedParams.get(param); |
| 859 |
if (!allowTypedNodes) { |
| 860 |
checkSequenceIsUntyped(value); |
| 861 |
} |
| 862 |
XdmValue paramVal = XdmValue.wrap(value); |
| 863 |
checkedParams.put(paramName, paramVal); |
| 864 |
} else { |
| 865 |
break; |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
private void checkSequenceIsUntyped(Sequence value) throws XPathException { |
| 871 |
SequenceIterator iter = value.iterate(); |
| 872 |
Item item; |
| 873 |
while ((item = iter.next()) != null) { |
| 874 |
if (item instanceof NodeInfo && ((NodeInfo) item).getTreeInfo().isTyped()) { |
| 875 |
throw new XPathException("Schema-validated nodes cannot be passed to fn:transform() when it runs under a different Saxon Configuration", "FOXT0002"); |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
private static NodeInfo validate(NodeInfo node, Configuration config, int validation) throws XPathException { |
| 881 |
ParseOptions options = new ParseOptions(config.getParseOptions()); |
| 882 |
options.setSchemaValidationMode(validation); |
| 883 |
return config.buildDocumentTree(node, options).getRootNode(); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
private static abstract class Deliverer implements ResultDocumentResolver { |
| 891 |
|
| 892 |
protected Xslt30Transformer transformer; |
| 893 |
protected String baseOutputUri; |
| 894 |
protected String principalResultKey; |
| 895 |
protected Function postProcessor; |
| 896 |
protected XPathContext context; |
| 897 |
protected HashTrieMap resultMap = new HashTrieMap(); |
| 898 |
|
| 899 |
public static Deliverer makeDeliverer(String deliveryFormat) { |
| 900 |
switch (deliveryFormat) { |
| 901 |
case "document": |
| 902 |
return new DocumentDeliverer(); |
| 903 |
case "serialized": |
| 904 |
return new SerializedDeliverer(); |
| 905 |
case "raw": |
| 906 |
return new RawDeliverer(); |
| 907 |
default: |
| 908 |
throw new IllegalArgumentException("delivery-format"); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
public final void setTransformer(Xslt30Transformer transformer) { |
| 913 |
this.transformer = transformer; |
| 914 |
} |
| 915 |
|
| 916 |
public final void setPrincipalResultKey(String key) { |
| 917 |
this.principalResultKey = key; |
| 918 |
} |
| 919 |
|
| 920 |
public final void setBaseOutputUri(String uri) { |
| 921 |
this.baseOutputUri = uri; |
| 922 |
} |
| 923 |
|
| 924 |
public void setPostProcessor(Function postProcessor, XPathContext context) { |
| 925 |
this.postProcessor = postProcessor; |
| 926 |
this.context = context; |
| 927 |
} |
| 928 |
|
| 929 |
protected URI getAbsoluteUri(String href, String baseUri) throws XPathException { |
| 930 |
URI absolute; |
| 931 |
try { |
| 932 |
absolute = ResolveURI.makeAbsolute(href, baseUri); |
| 933 |
} catch (URISyntaxException e) { |
| 934 |
throw XPathException.makeXPathException(e); |
| 935 |
} |
| 936 |
return absolute; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
public abstract HashTrieMap populateResultMap(HashTrieMap resultMap) throws XPathException; |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
public abstract Destination getPrimaryDestination(MapItem serializationParamsMap) throws XPathException; |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
protected Serializer makeSerializer(MapItem serializationParamsMap) throws XPathException { |
| 967 |
Serializer serializer = transformer.newSerializer(); |
| 968 |
if (serializationParamsMap != null) { |
| 969 |
AtomicIterator paramIterator = serializationParamsMap.keys(); |
| 970 |
AtomicValue param; |
| 971 |
while ((param = paramIterator.next()) != null) { |
| 972 |
|
| 973 |
QName paramName; |
| 974 |
if (param instanceof StringValue) { |
| 975 |
paramName = new QName(param.getStringValue()); |
| 976 |
} else if (param instanceof QNameValue) { |
| 977 |
paramName = new QName(((QNameValue) param.head()).getStructuredQName()); |
| 978 |
} else { |
| 979 |
throw new XPathException("Serialization parameters must be strings or QNames", "XPTY0004"); |
| 980 |
} |
| 981 |
String paramValue = null; |
| 982 |
GroundedValue supplied = serializationParamsMap.get(param); |
| 983 |
if (supplied.getLength() > 0) { |
| 984 |
if (supplied.getLength() == 1) { |
| 985 |
Item val = supplied.itemAt(0); |
| 986 |
if (val instanceof StringValue) { |
| 987 |
paramValue = val.getStringValue(); |
| 988 |
} else if (val instanceof BooleanValue) { |
| 989 |
paramValue = ((BooleanValue) val).getBooleanValue() ? "yes" : "no"; |
| 990 |
} else if (val instanceof DecimalValue) { |
| 991 |
paramValue = val.getStringValue(); |
| 992 |
} else if (val instanceof QNameValue) { |
| 993 |
paramValue = ((QNameValue)val).getClarkName(); |
| 994 |
} else if (val instanceof MapItem && paramName.getClarkName().equals(SaxonOutputKeys.USE_CHARACTER_MAPS)) { |
| 995 |
CharacterMap charMap = Serialize.toCharacterMap((MapItem)val); |
| 996 |
CharacterMapIndex charMapIndex = new CharacterMapIndex(); |
| 997 |
charMapIndex.putCharacterMap(charMap.getName(), charMap); |
| 998 |
serializer.setCharacterMap(charMapIndex); |
| 999 |
String existing = serializer.getOutputProperty(Serializer.Property.USE_CHARACTER_MAPS); |
| 1000 |
if (existing == null) { |
| 1001 |
serializer.setOutputProperty(Serializer.Property.USE_CHARACTER_MAPS, |
| 1002 |
charMap.getName().getClarkName()); |
| 1003 |
} else { |
| 1004 |
serializer.setOutputProperty(Serializer.Property.USE_CHARACTER_MAPS, |
| 1005 |
existing + " " + charMap.getName().getClarkName()); |
| 1006 |
} |
| 1007 |
continue; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
if (paramValue == null) { |
| 1011 |
|
| 1012 |
SequenceIterator iter = supplied.iterate(); |
| 1013 |
Item it; |
| 1014 |
paramValue = ""; |
| 1015 |
while ((it = iter.next()) != null) { |
| 1016 |
if (it instanceof QNameValue) { |
| 1017 |
paramValue += " " + ((QNameValue)it).getClarkName(); |
| 1018 |
} else { |
| 1019 |
throw new XPathException("Value of serialization parameter " + paramName.getEQName() + " not recognized", "XPTY0004"); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
} |
| 1023 |
Serializer.Property prop = Serializer.getProperty(paramName); |
| 1024 |
if (paramName.getClarkName().equals(OutputKeys.CDATA_SECTION_ELEMENTS) |
| 1025 |
|| paramName.getClarkName().equals(SaxonOutputKeys.SUPPRESS_INDENTATION)) { |
| 1026 |
String existing = serializer.getOutputProperty(paramName); |
| 1027 |
if (existing == null) { |
| 1028 |
serializer.setOutputProperty(prop, paramValue); |
| 1029 |
} else { |
| 1030 |
serializer.setOutputProperty(prop, existing + paramValue); |
| 1031 |
} |
| 1032 |
} else { |
| 1033 |
serializer.setOutputProperty(prop, paramValue); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
} |
| 1038 |
} |
| 1039 |
return serializer; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public abstract Sequence<?> getPrimaryResult() throws XPathException; |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
public GroundedValue<?> postProcess(String uri, Sequence<?> result) throws XPathException { |
| 1056 |
if (postProcessor != null) { |
| 1057 |
result = postProcessor.call(context.newCleanContext(), new Sequence[]{new StringValue(uri), result}); |
| 1058 |
} |
| 1059 |
return result.materialize(); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
private static class DocumentDeliverer extends Deliverer { |
| 1068 |
private Map<String, TreeInfo> results = new ConcurrentHashMap<>(); |
| 1069 |
private XdmDestination destination = new XdmDestination(); |
| 1070 |
|
| 1071 |
public DocumentDeliverer() { |
| 1072 |
} |
| 1073 |
|
| 1074 |
@Override |
| 1075 |
public Destination getPrimaryDestination(MapItem serializationParamsMap) { |
| 1076 |
return destination; |
| 1077 |
} |
| 1078 |
|
| 1079 |
@Override |
| 1080 |
public Sequence<?> getPrimaryResult() throws XPathException { |
| 1081 |
XdmNode node = destination.getXdmNode(); |
| 1082 |
return node == null ? null : postProcess(baseOutputUri, node.getUnderlyingNode()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
@Override |
| 1086 |
public Receiver resolve(XPathContext context, String href, String baseUri, SerializationProperties properties) throws XPathException { |
| 1087 |
URI absolute = getAbsoluteUri(href, baseUri); |
| 1088 |
XdmDestination destination = new XdmDestination(); |
| 1089 |
destination.setDestinationBaseURI(absolute); |
| 1090 |
destination.onClose(() -> { |
| 1091 |
XdmNode root = destination.getXdmNode(); |
| 1092 |
results.put(absolute.toASCIIString(), root.getUnderlyingValue().getTreeInfo()); |
| 1093 |
}); |
| 1094 |
return destination.getReceiver(context.getReceiver().getPipelineConfiguration(), properties); |
| 1095 |
} |
| 1096 |
|
| 1097 |
public HashTrieMap populateResultMap(HashTrieMap resultMap) { |
| 1098 |
for (Map.Entry<String, TreeInfo> entry : results.entrySet()) { |
| 1099 |
String uri = entry.getKey(); |
| 1100 |
resultMap = resultMap.addEntry(new StringValue(uri), |
| 1101 |
entry.getValue().getRootNode()); |
| 1102 |
} |
| 1103 |
return resultMap; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
private static class SerializedDeliverer extends Deliverer { |
| 1112 |
private Map<String, String> results = new ConcurrentHashMap<>(); |
| 1113 |
private Map<String, StringWriter> workInProgress = new ConcurrentHashMap<>(); |
| 1114 |
private StringWriter primaryWriter; |
| 1115 |
|
| 1116 |
public SerializedDeliverer() { |
| 1117 |
} |
| 1118 |
|
| 1119 |
@Override |
| 1120 |
public Destination getPrimaryDestination(MapItem serializationParamsMap) throws XPathException { |
| 1121 |
Serializer serializer = makeSerializer(serializationParamsMap); |
| 1122 |
primaryWriter = new StringWriter(); |
| 1123 |
serializer.setOutputWriter(primaryWriter); |
| 1124 |
return serializer; |
| 1125 |
} |
| 1126 |
|
| 1127 |
@Override |
| 1128 |
public Sequence<?> getPrimaryResult() throws XPathException { |
| 1129 |
String str = primaryWriter.toString(); |
| 1130 |
if (str.isEmpty()) { |
| 1131 |
return null; |
| 1132 |
} |
| 1133 |
return postProcess(baseOutputUri, new StringValue(str)); |
| 1134 |
} |
| 1135 |
|
| 1136 |
@Override |
| 1137 |
public Receiver resolve(XPathContext context, String href, String baseUri, SerializationProperties properties) throws XPathException { |
| 1138 |
URI absolute = getAbsoluteUri(href, baseUri); |
| 1139 |
if (absolute.getScheme().equals(dummyBaseOutputUriScheme)) { |
| 1140 |
throw new XPathException("The location of output documents is undefined: use the transform option base-output-uri", "FOXT0002"); |
| 1141 |
} |
| 1142 |
StringWriter writer = new StringWriter(); |
| 1143 |
Serializer serializer = makeSerializer(null); |
| 1144 |
serializer.setCharacterMap(properties.getCharacterMapIndex()); |
| 1145 |
serializer.setOutputWriter(writer); |
| 1146 |
serializer.onClose(() -> results.put(absolute.toASCIIString(), writer.toString())); |
| 1147 |
try { |
| 1148 |
Receiver out = serializer.getReceiver(context.getReceiver().getPipelineConfiguration(), properties); |
| 1149 |
out.setSystemId(absolute.toASCIIString()); |
| 1150 |
return out; |
| 1151 |
} catch (SaxonApiException e) { |
| 1152 |
throw XPathException.makeXPathException(e); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
@Override |
| 1157 |
public HashTrieMap populateResultMap(HashTrieMap resultMap) { |
| 1158 |
for (Map.Entry<String, String> entry : results.entrySet()) { |
| 1159 |
String uri = entry.getKey(); |
| 1160 |
resultMap = resultMap.addEntry(new StringValue(uri), |
| 1161 |
new StringValue(entry.getValue())); |
| 1162 |
} |
| 1163 |
return resultMap; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
private static class RawDeliverer extends Deliverer { |
| 1168 |
private Map<String, XdmValue> results = new ConcurrentHashMap<>(); |
| 1169 |
private RawDestination primaryDestination = new RawDestination(); |
| 1170 |
|
| 1171 |
public RawDeliverer() {} |
| 1172 |
|
| 1173 |
@Override |
| 1174 |
public Destination getPrimaryDestination(MapItem serializationParamsMap) { |
| 1175 |
return primaryDestination; |
| 1176 |
} |
| 1177 |
|
| 1178 |
@Override |
| 1179 |
public Sequence<?> getPrimaryResult() throws XPathException { |
| 1180 |
Sequence<?> actualResult = primaryDestination.getXdmValue().getUnderlyingValue(); |
| 1181 |
return postProcess(baseOutputUri, actualResult); |
| 1182 |
} |
| 1183 |
|
| 1184 |
@Override |
| 1185 |
public Receiver resolve(XPathContext context, String href, String baseUri, SerializationProperties properties) throws XPathException { |
| 1186 |
URI absolute = getAbsoluteUri(href, baseUri); |
| 1187 |
RawDestination destination = new RawDestination(); |
| 1188 |
destination.onClose(() -> results.put(absolute.toASCIIString(), destination.getXdmValue())); |
| 1189 |
return destination.getReceiver(context.getReceiver().getPipelineConfiguration(), properties); |
| 1190 |
} |
| 1191 |
|
| 1192 |
@Override |
| 1193 |
public HashTrieMap populateResultMap(HashTrieMap resultMap) { |
| 1194 |
for (Map.Entry<String, XdmValue> entry : results.entrySet()) { |
| 1195 |
String uri = entry.getKey(); |
| 1196 |
resultMap = resultMap.addEntry(new StringValue(uri), |
| 1197 |
entry.getValue().getUnderlyingValue()); |
| 1198 |
} |
| 1199 |
return resultMap; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
} |