| 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.Controller; |
| 12 |
import net.sf.saxon.event.Builder; |
| 13 |
import net.sf.saxon.event.PipelineConfiguration; |
| 14 |
import net.sf.saxon.event.Receiver; |
| 15 |
import net.sf.saxon.event.Sender; |
| 16 |
import net.sf.saxon.expr.*; |
| 17 |
import net.sf.saxon.expr.parser.Location; |
| 18 |
import net.sf.saxon.expr.parser.PathMap; |
| 19 |
import net.sf.saxon.expr.sort.DocumentOrderIterator; |
| 20 |
import net.sf.saxon.expr.sort.GlobalOrderComparer; |
| 21 |
import net.sf.saxon.lib.Feature; |
| 22 |
import net.sf.saxon.lib.ParseOptions; |
| 23 |
import net.sf.saxon.lib.RelativeURIResolver; |
| 24 |
import net.sf.saxon.lib.StandardErrorHandler; |
| 25 |
import net.sf.saxon.om.*; |
| 26 |
import net.sf.saxon.style.StylesheetPackage; |
| 27 |
import net.sf.saxon.trans.Err; |
| 28 |
import net.sf.saxon.trans.NonDelegatingURIResolver; |
| 29 |
import net.sf.saxon.trans.XPathException; |
| 30 |
import net.sf.saxon.trans.XsltController; |
| 31 |
import net.sf.saxon.tree.tiny.TinyBuilder; |
| 32 |
import net.sf.saxon.value.Cardinality; |
| 33 |
|
| 34 |
import javax.xml.transform.Source; |
| 35 |
import javax.xml.transform.SourceLocator; |
| 36 |
import javax.xml.transform.TransformerException; |
| 37 |
import javax.xml.transform.URIResolver; |
| 38 |
import javax.xml.transform.dom.DOMSource; |
| 39 |
import javax.xml.transform.stream.StreamSource; |
| 40 |
import java.net.URI; |
| 41 |
import java.net.URISyntaxException; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class DocumentFn extends SystemFunction implements Callable { |
| 49 |
|
| 50 |
private Location location; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public int getCardinality(Expression[] arguments) { |
| 57 |
Expression expression = arguments[0]; |
| 58 |
if (Cardinality.allowsMany(expression.getCardinality())) { |
| 59 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 60 |
} else { |
| 61 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public int getSpecialProperties(Expression[] arguments) { |
| 74 |
return StaticProperty.ORDERED_NODESET | |
| 75 |
StaticProperty.PEER_NODESET | |
| 76 |
StaticProperty.NO_NODES_NEWLY_CREATED; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public Expression makeFunctionCall(Expression... arguments) { |
| 85 |
location = arguments[0].getLocation(); |
| 86 |
Expression expr = Doc.maybePreEvaluate(this, arguments); |
| 87 |
return expr == null ? super.makeFunctionCall(arguments) : expr; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 100 |
int numArgs = getArity(); |
| 101 |
|
| 102 |
SequenceIterator hrefSequence = arguments[0].iterate(); |
| 103 |
String baseURI = null; |
| 104 |
if (numArgs == 2) { |
| 105 |
|
| 106 |
NodeInfo base = (NodeInfo) arguments[1].head(); |
| 107 |
baseURI = base.getBaseURI(); |
| 108 |
if (baseURI == null) { |
| 109 |
throw new XPathException("The second argument to document() is a node with no base URI", "XTDE1162"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
DocumentMappingFunction map = new DocumentMappingFunction(context); |
| 114 |
map.baseURI = baseURI; |
| 115 |
map.stylesheetURI = getStaticBaseUriString(); |
| 116 |
map.packageData = getRetainedStaticContext().getPackageData(); |
| 117 |
map.locator = location; |
| 118 |
|
| 119 |
ItemMappingIterator iter = new ItemMappingIterator(hrefSequence, map); |
| 120 |
|
| 121 |
|
| 122 |
return SequenceTool.toLazySequence(new DocumentOrderIterator(iter, GlobalOrderComparer.getInstance())); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
private static class DocumentMappingFunction implements ItemMappingFunction { |
| 130 |
|
| 131 |
public String baseURI; |
| 132 |
public String stylesheetURI; |
| 133 |
public Location locator; |
| 134 |
public PackageData packageData; |
| 135 |
public XPathContext context; |
| 136 |
|
| 137 |
public DocumentMappingFunction(XPathContext context) { |
| 138 |
this.context = context; |
| 139 |
} |
| 140 |
|
| 141 |
public Item mapItem(Item item) throws XPathException { |
| 142 |
String b = baseURI; |
| 143 |
if (b == null) { |
| 144 |
if (item instanceof NodeInfo) { |
| 145 |
b = ((NodeInfo) item).getBaseURI(); |
| 146 |
} else { |
| 147 |
b = stylesheetURI; |
| 148 |
} |
| 149 |
} |
| 150 |
try { |
| 151 |
return makeDoc(item.getStringValue(), b, packageData, null, context, locator, false); |
| 152 |
} catch (XPathException xerr) { |
| 153 |
switch (xerr.getErrorCodeLocalPart()) { |
| 154 |
case "XTRE1160": |
| 155 |
|
| 156 |
xerr.setErrorCode("XTDE1160"); |
| 157 |
|
| 158 |
try { |
| 159 |
context.getController().recoverableError(xerr); |
| 160 |
} catch (XPathException err2) { |
| 161 |
throw xerr; |
| 162 |
} |
| 163 |
|
| 164 |
String href = item.getStringValue(); |
| 165 |
int hash = href.indexOf('#'); |
| 166 |
href = href.substring(0, hash); |
| 167 |
return makeDoc(href, b, packageData, null, context, locator, false); |
| 168 |
case "XTDE1162": |
| 169 |
|
| 170 |
throw xerr; |
| 171 |
default: |
| 172 |
|
| 173 |
if (!xerr.hasBeenReported()) { |
| 174 |
xerr.maybeSetLocation(locator); |
| 175 |
String code = (xerr.getException() instanceof URISyntaxException) ? "FODC0005" : "FODC0002"; |
| 176 |
xerr.maybeSetErrorCode(code); |
| 177 |
try { |
| 178 |
context.getController().recoverableError(xerr); |
| 179 |
} catch (XPathException err2) { |
| 180 |
throw xerr; |
| 181 |
} |
| 182 |
} else { |
| 183 |
throw xerr; |
| 184 |
} |
| 185 |
break; |
| 186 |
} |
| 187 |
return null; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static NodeInfo makeDoc(String href, String baseURI, PackageData packageData, ParseOptions options, XPathContext c, Location locator, boolean silent) |
| 209 |
throws XPathException { |
| 210 |
|
| 211 |
Configuration config = c.getConfiguration(); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
int hash = href.indexOf('#'); |
| 216 |
|
| 217 |
String fragmentId = null; |
| 218 |
if (hash >= 0) { |
| 219 |
if (hash == href.length() - 1) { |
| 220 |
|
| 221 |
href = href.substring(0, hash); |
| 222 |
} else { |
| 223 |
fragmentId = href.substring(hash + 1); |
| 224 |
href = href.substring(0, hash); |
| 225 |
if (!NameChecker.isValidNCName(fragmentId)) { |
| 226 |
XPathException de = new XPathException("The fragment identifier " + Err.wrap(fragmentId) + " is not a valid NCName"); |
| 227 |
de.setErrorCode("XTRE1160"); |
| 228 |
de.setXPathContext(c); |
| 229 |
de.setLocator(locator); |
| 230 |
throw de; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
Controller controller = c.getController(); |
| 236 |
if (controller == null) { |
| 237 |
throw new XPathException("doc() function is not available in this environment"); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
DocumentURI documentKey = computeDocumentKey(href, baseURI, packageData, c); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
TreeInfo doc = config.getGlobalDocumentPool().find(documentKey); |
| 246 |
if (doc != null) { |
| 247 |
return doc.getRootNode(); |
| 248 |
} |
| 249 |
|
| 250 |
DocumentPool pool = controller.getDocumentPool(); |
| 251 |
|
| 252 |
|
| 253 |
synchronized (controller) { |
| 254 |
doc = pool.find(documentKey); |
| 255 |
if (doc != null) { |
| 256 |
return getFragment(doc, fragmentId, c, locator); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if (controller instanceof XsltController && |
| 262 |
!((XsltController)controller).checkUniqueOutputDestination(documentKey)) { |
| 263 |
pool.markUnavailable(documentKey); |
| 264 |
XPathException err = new XPathException( |
| 265 |
"Cannot read a document that was written during the same transformation: " + documentKey); |
| 266 |
err.setXPathContext(c); |
| 267 |
err.setErrorCode("XTRE1500"); |
| 268 |
err.setLocator(locator); |
| 269 |
throw err; |
| 270 |
} |
| 271 |
|
| 272 |
if (pool.isMarkedUnavailable(documentKey)) { |
| 273 |
XPathException err = new XPathException( |
| 274 |
"Document has been marked not available: " + documentKey); |
| 275 |
err.setXPathContext(c); |
| 276 |
err.setErrorCode("FODC0002"); |
| 277 |
err.setLocator(locator); |
| 278 |
throw err; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
try { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
Source source = resolveURI(href, baseURI, documentKey.toString(), c); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
source = config.getSourceResolver().resolveSource(source, config); |
| 291 |
|
| 292 |
|
| 293 |
TreeInfo newdoc; |
| 294 |
if (source instanceof NodeInfo || source instanceof DOMSource) { |
| 295 |
NodeInfo startNode = controller.prepareInputTree(source); |
| 296 |
newdoc = startNode.getTreeInfo(); |
| 297 |
} else { |
| 298 |
Builder b = controller.makeBuilder(); |
| 299 |
b.setUseEventLocation(true); |
| 300 |
if (b instanceof TinyBuilder) { |
| 301 |
((TinyBuilder) b).setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 302 |
} |
| 303 |
|
| 304 |
Receiver s = b; |
| 305 |
if (options == null) { |
| 306 |
options = new ParseOptions(b.getPipelineConfiguration().getParseOptions()); |
| 307 |
if (packageData instanceof StylesheetPackage) { |
| 308 |
SpaceStrippingRule rule = ((StylesheetPackage)packageData).getSpaceStrippingRule(); |
| 309 |
if (rule != NoElementsSpaceStrippingRule.getInstance()) { |
| 310 |
options.setSpaceStrippingRule(rule); |
| 311 |
} |
| 312 |
} |
| 313 |
options.setSchemaValidationMode(controller.getSchemaValidationMode()); |
| 314 |
} |
| 315 |
b.getPipelineConfiguration().setParseOptions(options); |
| 316 |
if (options.isLineNumbering()) { |
| 317 |
b.setLineNumbering(true); |
| 318 |
} |
| 319 |
|
| 320 |
if (silent) { |
| 321 |
StandardErrorHandler eh = new StandardErrorHandler(controller.getErrorListener()); |
| 322 |
eh.setSilent(true); |
| 323 |
options.setErrorHandler(eh); |
| 324 |
} |
| 325 |
if (packageData instanceof StylesheetPackage && ((StylesheetPackage)packageData).isStripsTypeAnnotations()) { |
| 326 |
s = config.getAnnotationStripper(s); |
| 327 |
} |
| 328 |
|
| 329 |
PathMap map = controller.getPathMapForDocumentProjection(); |
| 330 |
if (map != null) { |
| 331 |
PathMap.PathMapRoot pathRoot = map.getRootForDocument(documentKey.toString()); |
| 332 |
if (pathRoot != null && !pathRoot.isReturnable() && !pathRoot.hasUnknownDependencies()) { |
| 333 |
options.addFilter(config.makeDocumentProjector(pathRoot)); |
| 334 |
} |
| 335 |
} |
| 336 |
s.setPipelineConfiguration(b.getPipelineConfiguration()); |
| 337 |
try { |
| 338 |
Sender.send(source, s, options); |
| 339 |
newdoc = b.getCurrentRoot().getTreeInfo(); |
| 340 |
b.reset(); |
| 341 |
} catch (XPathException err) { |
| 342 |
if (err.getErrorCodeLocalPart() == null || err.getErrorCodeLocalPart().equals("SXXP0003")) { |
| 343 |
err.setErrorCode("FODC0002"); |
| 344 |
} |
| 345 |
throw err; |
| 346 |
} finally { |
| 347 |
if (options.isPleaseCloseAfterUse()) { |
| 348 |
ParseOptions.close(source); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
synchronized (controller) { |
| 359 |
doc = pool.find(documentKey); |
| 360 |
if (doc != null) { |
| 361 |
return getFragment(doc, fragmentId, c, locator); |
| 362 |
} |
| 363 |
controller.registerDocument(newdoc, documentKey); |
| 364 |
if (controller instanceof XsltController) { |
| 365 |
((XsltController) controller).addUnavailableOutputDestination(documentKey); |
| 366 |
} |
| 367 |
} |
| 368 |
return getFragment(newdoc, fragmentId, c, locator); |
| 369 |
|
| 370 |
} catch (TransformerException err) { |
| 371 |
pool.markUnavailable(documentKey); |
| 372 |
XPathException xerr = XPathException.makeXPathException(err); |
| 373 |
xerr.maybeSetLocation(locator); |
| 374 |
String code = (err.getException() instanceof URISyntaxException) ? "FODC0005" : "FODC0002"; |
| 375 |
xerr.maybeSetErrorCode(code); |
| 376 |
throw xerr; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public static Source resolveURI(String href, String baseURI, String documentKey, XPathContext context) |
| 392 |
throws XPathException { |
| 393 |
URIResolver resolver = context.getURIResolver(); |
| 394 |
Source source; |
| 395 |
|
| 396 |
if (baseURI == null) { |
| 397 |
try { |
| 398 |
URI uri = new URI(href); |
| 399 |
if (!uri.isAbsolute()) { |
| 400 |
throw new XPathException("Relative URI passed to document() function (" + href + |
| 401 |
"); but no base URI is available", "XTDE1162"); |
| 402 |
} |
| 403 |
} catch (URISyntaxException e) { |
| 404 |
throw new XPathException("Invalid URI passed to document() function: " + href, "FODC0005"); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
try { |
| 409 |
if (resolver instanceof RelativeURIResolver && documentKey != null) { |
| 410 |
source = ((RelativeURIResolver) resolver).dereference(documentKey); |
| 411 |
} else { |
| 412 |
source = resolver.resolve(href, baseURI); |
| 413 |
} |
| 414 |
} catch (Exception ex) { |
| 415 |
|
| 416 |
XPathException de = new XPathException("Exception thrown by URIResolver", ex); |
| 417 |
if (context.getConfiguration().getBooleanProperty(Feature.TRACE_EXTERNAL_FUNCTIONS)) { |
| 418 |
ex.printStackTrace(); |
| 419 |
} |
| 420 |
throw de; |
| 421 |
} |
| 422 |
|
| 423 |
if(source instanceof StreamSource && ((StreamSource)source).getInputStream() == null && ((StreamSource)source).getReader() == null) { |
| 424 |
String uri = source.getSystemId(); |
| 425 |
resolver = context.getController().getStandardURIResolver(); |
| 426 |
try { |
| 427 |
source = resolver.resolve(uri, ""); |
| 428 |
} catch (TransformerException ex) { |
| 429 |
throw XPathException.makeXPathException(ex); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
if (source == null && !(resolver instanceof NonDelegatingURIResolver) ) { |
| 435 |
resolver = context.getController().getStandardURIResolver(); |
| 436 |
try { |
| 437 |
if (resolver instanceof RelativeURIResolver && documentKey != null) { |
| 438 |
source = ((RelativeURIResolver) resolver).dereference(documentKey); |
| 439 |
} else { |
| 440 |
source = resolver.resolve(href, baseURI); |
| 441 |
} |
| 442 |
} catch (TransformerException ex) { |
| 443 |
throw XPathException.makeXPathException(ex); |
| 444 |
} |
| 445 |
} |
| 446 |
return source; |
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
protected static DocumentURI computeDocumentKey(String href, String baseURI, PackageData packageData, XPathContext c) throws XPathException { |
| 455 |
|
| 456 |
Controller controller = c.getController(); |
| 457 |
|
| 458 |
URIResolver resolver = controller.getURIResolver(); |
| 459 |
if (resolver == null) { |
| 460 |
resolver = controller.getStandardURIResolver(); |
| 461 |
} |
| 462 |
return computeDocumentKey(href, baseURI, packageData, resolver, true); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public static DocumentURI computeDocumentKey( |
| 477 |
String href, String baseURI, PackageData packageData, URIResolver resolver, boolean strip) { |
| 478 |
String documentKey; |
| 479 |
if (resolver instanceof RelativeURIResolver) { |
| 480 |
|
| 481 |
try { |
| 482 |
documentKey = ((RelativeURIResolver) resolver).makeAbsolute(href, baseURI); |
| 483 |
} catch (TransformerException e) { |
| 484 |
documentKey = '/' + href; |
| 485 |
} |
| 486 |
} else { |
| 487 |
|
| 488 |
href = ResolveURI.escapeSpaces(href); |
| 489 |
if (baseURI == null) { |
| 490 |
try { |
| 491 |
|
| 492 |
documentKey = new URI(href).toString(); |
| 493 |
} catch (URISyntaxException err) { |
| 494 |
|
| 495 |
documentKey = '/' + href; |
| 496 |
} |
| 497 |
} else if (href.isEmpty()) { |
| 498 |
|
| 499 |
documentKey = baseURI; |
| 500 |
} else { |
| 501 |
try { |
| 502 |
URI uri = new URI(baseURI).resolve(href); |
| 503 |
documentKey = uri.toString(); |
| 504 |
} catch (URISyntaxException | IllegalArgumentException err) { |
| 505 |
documentKey = baseURI + "/../" + href; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
if (strip && packageData instanceof StylesheetPackage && |
| 510 |
((StylesheetPackage) packageData).getSpaceStrippingRule() != NoElementsSpaceStrippingRule.getInstance()) { |
| 511 |
String name = ((StylesheetPackage) packageData).getPackageName(); |
| 512 |
if (name != null) { |
| 513 |
documentKey = name + " " + ((StylesheetPackage) packageData).getPackageVersion() + " " + documentKey; |
| 514 |
} |
| 515 |
} |
| 516 |
return new DocumentURI(documentKey); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public static NodeInfo preLoadDoc(String href, String baseURI, Configuration config, SourceLocator locator) |
| 532 |
throws XPathException { |
| 533 |
|
| 534 |
int hash = href.indexOf('#'); |
| 535 |
if (hash >= 0) { |
| 536 |
throw new XPathException("Fragment identifier not supported for preloaded documents"); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
String documentKey; |
| 541 |
URIResolver resolver = config.getURIResolver(); |
| 542 |
if (resolver instanceof RelativeURIResolver) { |
| 543 |
try { |
| 544 |
documentKey = ((RelativeURIResolver) resolver).makeAbsolute(href, baseURI); |
| 545 |
} catch (TransformerException e) { |
| 546 |
documentKey = '/' + href; |
| 547 |
baseURI = ""; |
| 548 |
} |
| 549 |
} else { |
| 550 |
if (baseURI == null) { |
| 551 |
try { |
| 552 |
|
| 553 |
documentKey = new URI(href).toString(); |
| 554 |
} catch (URISyntaxException err) { |
| 555 |
|
| 556 |
documentKey = '/' + href; |
| 557 |
baseURI = ""; |
| 558 |
} |
| 559 |
} else if (href.isEmpty()) { |
| 560 |
|
| 561 |
documentKey = baseURI; |
| 562 |
} else { |
| 563 |
try { |
| 564 |
URI uri = new URI(baseURI).resolve(href); |
| 565 |
documentKey = uri.toString(); |
| 566 |
} catch (URISyntaxException err) { |
| 567 |
documentKey = baseURI + "/../" + href; |
| 568 |
} catch (IllegalArgumentException err) { |
| 569 |
documentKey = baseURI + "/../" + href; |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
TreeInfo doc = config.getGlobalDocumentPool().find(documentKey); |
| 577 |
if (doc != null) { |
| 578 |
return doc.getRootNode(); |
| 579 |
} |
| 580 |
|
| 581 |
try { |
| 582 |
|
| 583 |
|
| 584 |
URIResolver r = resolver; |
| 585 |
Source source = null; |
| 586 |
if (r != null) { |
| 587 |
try { |
| 588 |
source = r.resolve(href, baseURI); |
| 589 |
} catch (Exception ex) { |
| 590 |
XPathException de = new XPathException("Exception thrown by URIResolver", ex); |
| 591 |
if (config.getBooleanProperty(Feature.TRACE_EXTERNAL_FUNCTIONS)) { |
| 592 |
ex.printStackTrace(); |
| 593 |
} |
| 594 |
de.setLocator(locator); |
| 595 |
throw de; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
if (source == null && !(r instanceof NonDelegatingURIResolver)) { |
| 602 |
r = config.getSystemURIResolver(); |
| 603 |
source = r.resolve(href, baseURI); |
| 604 |
} |
| 605 |
|
| 606 |
source = config.getSourceResolver().resolveSource(source, config); |
| 607 |
|
| 608 |
|
| 609 |
TreeInfo newdoc = config.buildDocumentTree(source); |
| 610 |
config.getGlobalDocumentPool().add(newdoc, documentKey); |
| 611 |
return newdoc.getRootNode(); |
| 612 |
|
| 613 |
} catch (TransformerException err) { |
| 614 |
XPathException xerr = XPathException.makeXPathException(err); |
| 615 |
xerr.setLocator(locator); |
| 616 |
xerr.setErrorCode("FODC0002"); |
| 617 |
throw new XPathException(err); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static void sendDoc(String href, String baseURL, XPathContext c, |
| 635 |
Location locator, Receiver out, |
| 636 |
ParseOptions parseOptions) throws XPathException { |
| 637 |
|
| 638 |
PipelineConfiguration pipe = out.getPipelineConfiguration(); |
| 639 |
if (pipe == null) { |
| 640 |
pipe = c.getController().makePipelineConfiguration(); |
| 641 |
pipe.setXPathContext(c); |
| 642 |
out.setPipelineConfiguration(pipe); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
String documentKey; |
| 648 |
if (baseURL == null) { |
| 649 |
try { |
| 650 |
|
| 651 |
documentKey = new URI(href).toString(); |
| 652 |
} catch (URISyntaxException err) { |
| 653 |
|
| 654 |
documentKey = '/' + href; |
| 655 |
baseURL = ""; |
| 656 |
} |
| 657 |
} else if (href.isEmpty()) { |
| 658 |
|
| 659 |
documentKey = baseURL; |
| 660 |
} else { |
| 661 |
try { |
| 662 |
URI url = new URI(baseURL).resolve(href); |
| 663 |
documentKey = url.toString(); |
| 664 |
} catch (URISyntaxException err) { |
| 665 |
documentKey = baseURL + "/../" + href; |
| 666 |
} catch (IllegalArgumentException err) { |
| 667 |
documentKey = baseURL + "/../" + href; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
Controller controller = c.getController(); |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
TreeInfo doc = controller.getDocumentPool().find(documentKey); |
| 676 |
Source source = null; |
| 677 |
if (doc != null) { |
| 678 |
source = doc.getRootNode(); |
| 679 |
} else { |
| 680 |
|
| 681 |
try { |
| 682 |
|
| 683 |
|
| 684 |
URIResolver r = controller.getURIResolver(); |
| 685 |
if (r != null) { |
| 686 |
source = r.resolve(href, baseURL); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
if (source == null) { |
| 692 |
r = controller.getStandardURIResolver(); |
| 693 |
source = r.resolve(href, baseURL); |
| 694 |
} |
| 695 |
if (source instanceof NodeInfo || source instanceof DOMSource) { |
| 696 |
NodeInfo startNode = controller.prepareInputTree(source); |
| 697 |
source = startNode.getRoot(); |
| 698 |
} |
| 699 |
} catch (TransformerException err) { |
| 700 |
XPathException xerr = XPathException.makeXPathException(err); |
| 701 |
xerr.setLocator(locator); |
| 702 |
xerr.maybeSetErrorCode("FODC0005"); |
| 703 |
throw xerr; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
if (controller.getConfiguration().isTiming()) { |
| 708 |
controller.getConfiguration().getLogger().info("Streaming input document " + source.getSystemId()); |
| 709 |
} |
| 710 |
out.setPipelineConfiguration(pipe); |
| 711 |
try { |
| 712 |
Sender.send(source, out, parseOptions); |
| 713 |
} catch (XPathException e) { |
| 714 |
e.maybeSetLocation(locator); |
| 715 |
e.maybeSetErrorCode("FODC0002"); |
| 716 |
throw e; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
private static NodeInfo getFragment(TreeInfo doc, String fragmentId, XPathContext context, SourceLocator locator) |
| 734 |
throws XPathException { |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
if (fragmentId == null) { |
| 739 |
return doc.getRootNode(); |
| 740 |
} |
| 741 |
if (!NameChecker.isValidNCName(fragmentId)) { |
| 742 |
XPathException err = new XPathException("Invalid fragment identifier in URI"); |
| 743 |
err.setXPathContext(context); |
| 744 |
err.setErrorCode("XTRE1160"); |
| 745 |
err.setLocator(locator); |
| 746 |
try { |
| 747 |
context.getController().recoverableError(err); |
| 748 |
} catch (XPathException dynamicError) { |
| 749 |
throw err; |
| 750 |
} |
| 751 |
return doc.getRootNode(); |
| 752 |
} |
| 753 |
return doc.selectID(fragmentId, false); |
| 754 |
} |
| 755 |
|
| 756 |
} |
| 757 |
|