1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.Writer ; 24 import java.util.Properties ; 25 26 import javax.xml.transform.Result ; 27 28 import org.w3c.dom.Node ; 29 import org.xml.sax.Attributes ; 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 44 public class ToXMLSAXHandler extends ToSAXHandler 45 { 46 47 50 protected boolean m_escapeSetting = false; 51 52 public ToXMLSAXHandler() 53 { 54 m_prefixMap = new NamespaceMappings(); 56 initCDATA(); 57 } 58 59 62 public Properties getOutputFormat() 63 { 64 return null; 65 } 66 67 70 public OutputStream getOutputStream() 71 { 72 return null; 73 } 74 75 78 public Writer getWriter() 79 { 80 return null; 81 } 82 83 86 public void indent(int n) throws SAXException 87 { 88 } 89 90 91 94 public void serialize(Node node) throws IOException 95 { 96 } 97 98 101 public boolean setEscaping(boolean escape) throws SAXException 102 { 103 boolean oldEscapeSetting = m_escapeSetting; 104 m_escapeSetting = escape; 105 106 if (escape) { 107 processingInstruction(Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 108 } else { 109 processingInstruction(Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 110 } 111 112 return oldEscapeSetting; 113 } 114 115 118 public void setOutputFormat(Properties format) 119 { 120 } 121 122 125 public void setOutputStream(OutputStream output) 126 { 127 } 128 129 132 public void setWriter(Writer writer) 133 { 134 } 135 136 139 public void attributeDecl( 140 String arg0, 141 String arg1, 142 String arg2, 143 String arg3, 144 String arg4) 145 throws SAXException 146 { 147 } 148 149 152 public void elementDecl(String arg0, String arg1) throws SAXException 153 { 154 } 155 156 159 public void externalEntityDecl(String arg0, String arg1, String arg2) 160 throws SAXException 161 { 162 } 163 164 167 public void internalEntityDecl(String arg0, String arg1) 168 throws SAXException 169 { 170 } 171 172 176 public void endDocument() throws SAXException 177 { 178 179 flushPending(); 180 181 m_saxHandler.endDocument(); 183 184 if (m_tracer != null) 185 super.fireEndDoc(); 186 } 187 188 192 protected void closeStartTag() throws SAXException 193 { 194 195 m_elemContext.m_startTagOpen = false; 196 197 final String localName = getLocalName(m_elemContext.m_elementName); 198 final String uri = getNamespaceURI(m_elemContext.m_elementName, true); 199 200 if (m_needToCallStartDocument) 202 { 203 startDocumentInternal(); 204 } 205 m_saxHandler.startElement(uri, localName, m_elemContext.m_elementName, m_attributes); 206 m_attributes.clear(); 209 210 if(m_state != null) 211 m_state.setCurrentNode(null); 212 } 213 214 221 public void closeCDATA() throws SAXException 222 { 223 224 if (m_lexHandler != null && m_cdataTagOpen) { 226 m_lexHandler.endCDATA(); 227 } 228 229 230 m_cdataTagOpen = false; 235 } 236 237 240 public void endElement(String namespaceURI, String localName, String qName) 241 throws SAXException 242 { 243 flushPending(); 245 246 if (namespaceURI == null) 247 { 248 if (m_elemContext.m_elementURI != null) 249 namespaceURI = m_elemContext.m_elementURI; 250 else 251 namespaceURI = getNamespaceURI(qName, true); 252 } 253 254 if (localName == null) 255 { 256 if (m_elemContext.m_elementLocalName != null) 257 localName = m_elemContext.m_elementLocalName; 258 else 259 localName = getLocalName(qName); 260 } 261 262 m_saxHandler.endElement(namespaceURI, localName, qName); 263 264 if (m_tracer != null) 265 super.fireEndElem(qName); 266 267 270 m_prefixMap.popNamespaces(m_elemContext.m_currentElemDepth, 271 m_saxHandler); 272 m_elemContext = m_elemContext.m_prev; 273 } 274 275 278 public void endPrefixMapping(String prefix) throws SAXException 279 { 280 283 return; 284 } 285 286 289 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) 290 throws SAXException 291 { 292 m_saxHandler.ignorableWhitespace(arg0,arg1,arg2); 293 } 294 295 298 public void setDocumentLocator(Locator arg0) 299 { 300 m_saxHandler.setDocumentLocator(arg0); 301 } 302 303 306 public void skippedEntity(String arg0) throws SAXException 307 { 308 m_saxHandler.skippedEntity(arg0); 309 } 310 311 316 public void startPrefixMapping(String prefix, String uri) 317 throws SAXException 318 { 319 startPrefixMapping(prefix, uri, true); 320 } 321 322 331 332 public boolean startPrefixMapping( 333 String prefix, 334 String uri, 335 boolean shouldFlush) 336 throws org.xml.sax.SAXException 337 { 338 339 344 345 boolean pushed; 346 int pushDepth; 347 if (shouldFlush) 348 { 349 flushPending(); 350 pushDepth = m_elemContext.m_currentElemDepth + 1; 352 } 353 else 354 { 355 pushDepth = m_elemContext.m_currentElemDepth; 357 } 358 pushed = m_prefixMap.pushNamespace(prefix, uri, pushDepth); 359 360 if (pushed) 361 { 362 m_saxHandler.startPrefixMapping(prefix,uri); 363 364 if (getShouldOutputNSAttr()) 365 { 366 367 372 String name; 373 if (EMPTYSTRING.equals(prefix)) 374 { 375 name = "xmlns"; 376 addAttributeAlways(XMLNS_URI, prefix, name,"CDATA",uri); 377 } 378 else 379 { 380 if (!EMPTYSTRING.equals(uri)) { name = "xmlns:" + prefix; 383 384 388 addAttributeAlways(XMLNS_URI, prefix, name,"CDATA",uri); 389 } 390 } 391 } 392 } 393 return pushed; 394 } 395 396 397 400 public void comment(char[] arg0, int arg1, int arg2) throws SAXException 401 { 402 flushPending(); 403 if (m_lexHandler != null) 404 m_lexHandler.comment(arg0, arg1, arg2); 405 406 if (m_tracer != null) 407 super.fireCommentEvent(arg0, arg1, arg2); 408 } 409 410 413 public void endCDATA() throws SAXException 414 { 415 436 } 437 438 441 public void endDTD() throws SAXException 442 { 443 if (m_lexHandler != null) 444 m_lexHandler.endDTD(); 445 } 446 447 450 public void startEntity(String arg0) throws SAXException 451 { 452 if (m_lexHandler != null) 453 m_lexHandler.startEntity(arg0); 454 } 455 456 459 public void characters(String chars) throws SAXException 460 { 461 final int length = chars.length(); 462 if (length > m_charsBuff.length) 463 { 464 m_charsBuff = new char[length*2 + 1]; 465 } 466 chars.getChars(0, length, m_charsBuff, 0); 467 this.characters(m_charsBuff, 0, length); 468 } 469 470 public ToXMLSAXHandler(ContentHandler handler, String encoding) 472 { 473 super(handler, encoding); 474 475 initCDATA(); 476 m_prefixMap = new NamespaceMappings(); 478 } 479 480 public ToXMLSAXHandler( 481 ContentHandler handler, 482 LexicalHandler lex, 483 String encoding) 484 { 485 super(handler, lex, encoding); 486 487 initCDATA(); 488 m_prefixMap = new NamespaceMappings(); 490 } 491 492 496 public void startElement( 497 String elementNamespaceURI, 498 String elementLocalName, 499 String elementName) throws SAXException 500 { 501 startElement( 502 elementNamespaceURI,elementLocalName,elementName, null); 503 504 505 } 506 public void startElement(String elementName) throws SAXException 507 { 508 startElement(null, null, elementName, null); 509 } 510 511 512 public void characters(char[] ch, int off, int len) throws SAXException 513 { 514 if (m_needToCallStartDocument) 517 { 518 startDocumentInternal(); 519 m_needToCallStartDocument = false; 520 } 521 522 if (m_elemContext.m_startTagOpen) 523 { 524 closeStartTag(); 525 m_elemContext.m_startTagOpen = false; 526 } 527 528 if (m_elemContext.m_isCdataSection && !m_cdataTagOpen 529 && m_lexHandler != null) 530 { 531 m_lexHandler.startCDATA(); 532 m_cdataTagOpen = true; 536 } 537 538 542 m_saxHandler.characters(ch, off, len); 543 544 if (m_tracer != null) 546 fireCharEvent(ch, off, len); 547 } 548 549 550 553 public void endElement(String elemName) throws SAXException 554 { 555 endElement(null, null, elemName); 556 } 557 558 559 564 public void namespaceAfterStartElement( 565 final String prefix, 566 final String uri) 567 throws SAXException 568 { 569 startPrefixMapping(prefix,uri,false); 570 } 571 572 577 public void processingInstruction(String target, String data) 578 throws SAXException 579 { 580 flushPending(); 581 582 m_saxHandler.processingInstruction(target, data); 584 585 if (m_tracer != null) 588 super.fireEscapingEvent(target, data); 589 } 590 591 595 protected boolean popNamespace(String prefix) 596 { 597 try 598 { 599 if (m_prefixMap.popNamespace(prefix)) 600 { 601 m_saxHandler.endPrefixMapping(prefix); 602 return true; 603 } 604 } 605 catch (SAXException e) 606 { 607 } 609 return false; 610 } 611 612 public void startCDATA() throws SAXException 613 { 614 622 if (!m_cdataTagOpen ) 623 { 624 flushPending(); 625 if (m_lexHandler != null) { 626 m_lexHandler.startCDATA(); 627 628 m_cdataTagOpen = true; 632 } 633 } 634 } 635 636 639 public void startElement( 640 String namespaceURI, 641 String localName, 642 String name, 643 Attributes atts) 644 throws SAXException 645 { 646 flushPending(); 647 super.startElement(namespaceURI, localName, name, atts); 648 649 if (m_needToOutputDocTypeDecl) 651 { 652 String doctypeSystem = getDoctypeSystem(); 653 if (doctypeSystem != null && m_lexHandler != null) 654 { 655 String doctypePublic = getDoctypePublic(); 656 if (doctypeSystem != null) 657 m_lexHandler.startDTD( 658 name, 659 doctypePublic, 660 doctypeSystem); 661 } 662 m_needToOutputDocTypeDecl = false; 663 } 664 m_elemContext = m_elemContext.push(namespaceURI, localName, name); 665 666 if (namespaceURI != null) 669 ensurePrefixIsDeclared(namespaceURI, name); 670 671 if (atts != null) 673 addAttributes(atts); 674 675 676 m_elemContext.m_isCdataSection = isCdataSection(); 678 679 } 680 681 private void ensurePrefixIsDeclared(String ns, String rawName) 682 throws org.xml.sax.SAXException 683 { 684 685 if (ns != null && ns.length() > 0) 686 { 687 int index; 688 String prefix = 689 (index = rawName.indexOf(":")) < 0 690 ? "" 691 : rawName.substring(0, index); 692 693 if (null != prefix) 694 { 695 String foundURI = m_prefixMap.lookupNamespace(prefix); 696 697 if ((null == foundURI) || !foundURI.equals(ns)) 698 { 699 this.startPrefixMapping(prefix, ns, false); 700 701 if (getShouldOutputNSAttr()) { 702 this.addAttributeAlways( 705 "http://www.w3.org/2000/xmlns/", 706 prefix, 707 "xmlns" + (prefix.length() == 0 ? "" : ":") + prefix, 708 "CDATA", 709 ns); 710 } 711 } 712 713 } 714 } 715 } 716 728 public void addAttribute( 729 String uri, 730 String localName, 731 String rawName, 732 String type, 733 String value) 734 throws SAXException 735 { 736 if (m_elemContext.m_startTagOpen) 737 { 738 ensurePrefixIsDeclared(uri, rawName); 739 addAttributeAlways(uri, localName, rawName, type, value); 740 } 741 742 } 743 744 752 public boolean reset() 753 { 754 boolean wasReset = false; 755 if (super.reset()) 756 { 757 resetToXMLSAXHandler(); 758 wasReset = true; 759 } 760 return wasReset; 761 } 762 763 767 private void resetToXMLSAXHandler() 768 { 769 this.m_escapeSetting = false; 770 } 771 772 } 773 | Popular Tags |