| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 14 |
import net.sf.saxon.expr.parser.Location; |
| 15 |
import net.sf.saxon.lib.Logger; |
| 16 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 17 |
import net.sf.saxon.lib.TraceListener; |
| 18 |
import net.sf.saxon.om.Item; |
| 19 |
import net.sf.saxon.om.NamespaceBinding; |
| 20 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 21 |
import net.sf.saxon.om.NodeName; |
| 22 |
import net.sf.saxon.trace.InstructionInfo; |
| 23 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 24 |
import net.sf.saxon.trans.XPathException; |
| 25 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 26 |
import net.sf.saxon.type.SchemaException; |
| 27 |
import net.sf.saxon.type.SchemaType; |
| 28 |
import net.sf.saxon.type.SimpleType; |
| 29 |
import net.sf.saxon.value.Whitespace; |
| 30 |
import org.xml.sax.ContentHandler; |
| 31 |
import org.xml.sax.Locator; |
| 32 |
import org.xml.sax.SAXException; |
| 33 |
import org.xml.sax.ext.LexicalHandler; |
| 34 |
|
| 35 |
import javax.xml.transform.Result; |
| 36 |
import javax.xml.transform.sax.TransformerHandler; |
| 37 |
import java.util.Properties; |
| 38 |
import java.util.Stack; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class ContentHandlerProxy implements Receiver { |
| 55 |
private PipelineConfiguration pipe; |
| 56 |
private String systemId; |
| 57 |
protected ContentHandler handler; |
| 58 |
protected LexicalHandler lexicalHandler; |
| 59 |
private int depth = 0; |
| 60 |
private boolean requireWellFormed = false; |
| 61 |
private boolean undeclareNamespaces = false; |
| 62 |
private Stack<String> elementStack = new Stack<>(); |
| 63 |
private Stack<String> namespaceStack = new Stack<>(); |
| 64 |
private ContentHandlerProxyTraceListener traceListener; |
| 65 |
protected AttributeCollectionImpl pendingAttributes; |
| 66 |
private NodeName pendingElement = null; |
| 67 |
private Location currentLocation = ExplicitLocation.UNKNOWN_LOCATION; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static final String MARKER = "##"; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void setUnderlyingContentHandler(ContentHandler handler) { |
| 83 |
this.handler = handler; |
| 84 |
if (handler instanceof LexicalHandler) { |
| 85 |
lexicalHandler = (LexicalHandler) handler; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public ContentHandler getUnderlyingContentHandler() { |
| 96 |
return handler; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public void setLexicalHandler(LexicalHandler handler) { |
| 108 |
lexicalHandler = handler; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void setPipelineConfiguration( PipelineConfiguration pipe) { |
| 118 |
this.pipe = pipe; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public PipelineConfiguration getPipelineConfiguration() { |
| 127 |
return pipe; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public Configuration getConfiguration() { |
| 137 |
return pipe.getConfiguration(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public void setSystemId(String systemId) { |
| 147 |
this.systemId = systemId; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public String getSystemId() { |
| 157 |
return systemId; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public ContentHandlerProxyTraceListener getTraceListener() { |
| 168 |
if (traceListener == null) { |
| 169 |
traceListener = new ContentHandlerProxyTraceListener(); |
| 170 |
} |
| 171 |
return traceListener; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public Location getCurrentLocation() { |
| 181 |
return currentLocation; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException { |
| 193 |
|
| 194 |
|
| 195 |
if (handler instanceof TransformerHandler) { |
| 196 |
try { |
| 197 |
((TransformerHandler)handler).unparsedEntityDecl(name, publicID, systemID, "unknown"); |
| 198 |
} catch (SAXException e) { |
| 199 |
throw new XPathException(e); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public void setOutputProperties(Properties details) { |
| 212 |
String prop = details.getProperty(SaxonOutputKeys.REQUIRE_WELL_FORMED); |
| 213 |
if (prop != null) { |
| 214 |
requireWellFormed = prop.equals("yes"); |
| 215 |
} |
| 216 |
prop = details.getProperty(SaxonOutputKeys.UNDECLARE_PREFIXES); |
| 217 |
if (prop != null) { |
| 218 |
undeclareNamespaces = prop.equals("yes"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public boolean isRequireWellFormed() { |
| 231 |
return requireWellFormed; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void setRequireWellFormed(boolean wellFormed) { |
| 244 |
requireWellFormed = wellFormed; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public boolean isUndeclareNamespaces() { |
| 255 |
return undeclareNamespaces; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public void setUndeclareNamespaces(boolean undeclareNamespaces) { |
| 266 |
this.undeclareNamespaces = undeclareNamespaces; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public void open() throws XPathException { |
| 274 |
pendingAttributes = new AttributeCollectionImpl(getPipelineConfiguration().getConfiguration()); |
| 275 |
if (handler == null) { |
| 276 |
throw new IllegalStateException("ContentHandlerProxy.open(): no underlying handler provided"); |
| 277 |
} |
| 278 |
try { |
| 279 |
Locator locator = new ContentHandlerProxyLocator(this); |
| 280 |
handler.setDocumentLocator(locator); |
| 281 |
handler.startDocument(); |
| 282 |
} catch (SAXException err) { |
| 283 |
handleSAXException(err); |
| 284 |
} |
| 285 |
depth = 0; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public void close() throws XPathException { |
| 293 |
if (depth >= 0) { |
| 294 |
try { |
| 295 |
handler.endDocument(); |
| 296 |
} catch (SAXException err) { |
| 297 |
handleSAXException(err); |
| 298 |
} |
| 299 |
} |
| 300 |
depth = -1; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public void startDocument(int properties) throws XPathException { |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public void endDocument() throws XPathException { |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 322 |
depth++; |
| 323 |
if (depth <= 0 && requireWellFormed) { |
| 324 |
notifyNotWellFormed(); |
| 325 |
} |
| 326 |
pendingElement = nameCode; |
| 327 |
currentLocation = location.saveLocation(); |
| 328 |
namespaceStack.push(MARKER); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 337 |
for (NamespaceBinding ns : namespaceBindings) { |
| 338 |
String prefix = ns.getPrefix(); |
| 339 |
if (prefix.equals("xml")) { |
| 340 |
return; |
| 341 |
} |
| 342 |
String uri = ns.getURI(); |
| 343 |
if (!undeclareNamespaces && uri.isEmpty() && !prefix.isEmpty()) { |
| 344 |
|
| 345 |
return; |
| 346 |
} |
| 347 |
try { |
| 348 |
handler.startPrefixMapping(prefix, uri); |
| 349 |
namespaceStack.push(prefix); |
| 350 |
} catch (SAXException err) { |
| 351 |
handleSAXException(err); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) |
| 362 |
throws XPathException { |
| 363 |
int index = pendingAttributes.findByNodeName(nameCode); |
| 364 |
if (index < 0) { |
| 365 |
pendingAttributes.addAttribute(nameCode, typeCode, value.toString(), locationId, properties); |
| 366 |
} else { |
| 367 |
pendingAttributes.setAttribute(index, nameCode, typeCode, value.toString(), locationId, properties); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public void startContent() throws XPathException { |
| 379 |
try { |
| 380 |
if (depth > 0 || !requireWellFormed) { |
| 381 |
String uri = pendingElement.getURI(); |
| 382 |
String localName = pendingElement.getLocalPart(); |
| 383 |
String qname = pendingElement.getDisplayName(); |
| 384 |
|
| 385 |
handler.startElement(uri, localName, qname, pendingAttributes); |
| 386 |
|
| 387 |
elementStack.push(uri); |
| 388 |
elementStack.push(localName); |
| 389 |
elementStack.push(qname); |
| 390 |
|
| 391 |
pendingAttributes.clear(); |
| 392 |
pendingElement = null; |
| 393 |
} |
| 394 |
} catch (SAXException err) { |
| 395 |
handleSAXException(err); |
| 396 |
|
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public void endElement() throws XPathException { |
| 406 |
if (depth > 0) { |
| 407 |
try { |
| 408 |
assert !elementStack.isEmpty(); |
| 409 |
String qname = elementStack.pop(); |
| 410 |
String localName = elementStack.pop(); |
| 411 |
String uri = elementStack.pop(); |
| 412 |
handler.endElement(uri, localName, qname); |
| 413 |
} catch (SAXException err) { |
| 414 |
handleSAXException(err); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
while (true) { |
| 419 |
String prefix = namespaceStack.pop(); |
| 420 |
if (prefix.equals(MARKER)) { |
| 421 |
break; |
| 422 |
} |
| 423 |
try { |
| 424 |
handler.endPrefixMapping(prefix); |
| 425 |
} catch (SAXException err) { |
| 426 |
handleSAXException(err); |
| 427 |
} |
| 428 |
} |
| 429 |
depth--; |
| 430 |
|
| 431 |
|
| 432 |
if (requireWellFormed && depth <= 0) { |
| 433 |
depth = Integer.MIN_VALUE; |
| 434 |
} |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 443 |
currentLocation = locationId; |
| 444 |
boolean disable = (properties & ReceiverOptions.DISABLE_ESCAPING) != 0; |
| 445 |
if (disable) { |
| 446 |
setEscaping(false); |
| 447 |
} |
| 448 |
try { |
| 449 |
if (depth <= 0 && requireWellFormed) { |
| 450 |
if (Whitespace.isWhite(chars)) { |
| 451 |
|
| 452 |
} else { |
| 453 |
notifyNotWellFormed(); |
| 454 |
} |
| 455 |
} else { |
| 456 |
handler.characters(chars.toString().toCharArray(), 0, chars.length()); |
| 457 |
} |
| 458 |
} catch (SAXException err) { |
| 459 |
handleSAXException(err); |
| 460 |
} |
| 461 |
if (disable) { |
| 462 |
setEscaping(true); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
protected void notifyNotWellFormed() throws XPathException { |
| 472 |
XPathException err = new XPathException("The result tree cannot be supplied to the ContentHandler because it is not well-formed XML"); |
| 473 |
err.setErrorCode(SaxonErrorCode.SXCH0002); |
| 474 |
throw err; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) |
| 483 |
throws XPathException { |
| 484 |
currentLocation = locationId; |
| 485 |
try { |
| 486 |
handler.processingInstruction(target, data.toString()); |
| 487 |
} catch (SAXException err) { |
| 488 |
handleSAXException(err); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public void comment(CharSequence chars, Location locationId, int properties) |
| 498 |
throws XPathException { |
| 499 |
currentLocation = locationId; |
| 500 |
try { |
| 501 |
if (lexicalHandler != null) { |
| 502 |
lexicalHandler.comment(chars.toString().toCharArray(), 0, chars.length()); |
| 503 |
} |
| 504 |
} catch (SAXException err) { |
| 505 |
handleSAXException(err); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
public boolean usesTypeAnnotations() { |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
private void setEscaping(boolean escaping) { |
| 531 |
try { |
| 532 |
handler.processingInstruction(escaping ? Result.PI_ENABLE_OUTPUT_ESCAPING : PI_DISABLE_OUTPUT_ESCAPING, ""); |
| 533 |
} catch (SAXException err) { |
| 534 |
throw new AssertionError(err); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
private void handleSAXException(SAXException err) throws XPathException { |
| 547 |
Exception nested = err.getException(); |
| 548 |
if (nested instanceof XPathException) { |
| 549 |
throw (XPathException) nested; |
| 550 |
} else if (nested instanceof SchemaException) { |
| 551 |
throw new XPathException(nested); |
| 552 |
} else { |
| 553 |
XPathException de = new XPathException(err); |
| 554 |
de.setErrorCode(SaxonErrorCode.SXCH0003); |
| 555 |
throw de; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public static class ContentHandlerProxyTraceListener implements TraceListener { |
| 566 |
|
| 567 |
private Stack<Item> contextItemStack; |
| 568 |
|
| 569 |
public void setOutputDestination(Logger stream) { |
| 570 |
|
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public Stack getContextItemStack() { |
| 581 |
return contextItemStack; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public void open(Controller controller) { |
| 589 |
contextItemStack = new Stack<>(); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public void close() { |
| 597 |
contextItemStack = null; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
public void enter(InstructionInfo instruction, XPathContext context) { |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public void leave(InstructionInfo instruction) { |
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public void startCurrentItem(Item currentItem) { |
| 637 |
if (contextItemStack == null) { |
| 638 |
contextItemStack = new Stack<>(); |
| 639 |
} |
| 640 |
contextItemStack.push(currentItem); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public void endCurrentItem(Item currentItem) { |
| 655 |
contextItemStack.pop(); |
| 656 |
} |
| 657 |
|
| 658 |
} |
| 659 |
|
| 660 |
} |
| 661 |
|