| 1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 import java.util.Stack ; 24 import java.util.Vector ; 25 26 import javax.xml.transform.SourceLocator ; 27 import javax.xml.transform.Transformer ; 28 29 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 30 import com.sun.org.apache.xml.internal.res.XMLMessages; 31 import com.sun.org.apache.xml.internal.utils.BoolStack; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.Locator ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.SAXParseException ; 37 38 import javax.xml.XMLConstants ; 39 40 41 48 abstract public class SerializerBase 49 implements SerializationHandler, SerializerConstants, com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.CharacterNodeHandler 50 { 51 52 53 57 protected void fireEndElem(String name) 58 throws org.xml.sax.SAXException  59 { 60 if (m_tracer != null) 61 { 62 flushMyWriter(); 63 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_ENDELEMENT,name, (Attributes )null); 64 } 65 } 66 67 73 protected void fireCharEvent(char[] chars, int start, int length) 74 throws org.xml.sax.SAXException  75 { 76 if (m_tracer != null) 77 { 78 flushMyWriter(); 79 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_CHARACTERS, chars, start,length); 80 } 81 } 82 83 86 protected boolean m_needToCallStartDocument = true; 87 88 91 protected boolean m_cdataTagOpen = false; 92 93 98 protected AttributesImplSerializer m_attributes = new AttributesImplSerializer(); 99 100 103 protected boolean m_inEntityRef = false; 104 105 106 protected boolean m_inExternalDTD = false; 107 108 111 private String m_doctypeSystem; 112 113 116 private String m_doctypePublic; 117 118 122 boolean m_needToOutputDocTypeDecl = true; 123 124 128 private String m_encoding = null; 129 130 133 private boolean m_shouldNotWriteXMLHeader = false; 134 135 138 private String m_standalone; 139 140 143 protected boolean m_standaloneWasSpecified = false; 144 145 148 protected boolean m_doIndent = false; 149 152 protected int m_indentAmount = 0; 153 154 157 private String m_version = null; 158 159 162 private String m_mediatype; 163 164 168 private Transformer m_transformer; 169 170 175 protected Vector m_cdataSectionElements = null; 176 177 182 protected NamespaceMappings m_prefixMap; 183 184 188 protected SerializerTrace m_tracer; 189 190 protected SourceLocator m_sourceLocator; 191 192 193 198 protected java.io.Writer m_writer = null; 199 200 206 protected ElemContext m_elemContext = new ElemContext(); 207 208 214 protected char[] m_charsBuff = new char[60]; 215 216 222 protected char[] m_attrBuff = new char[30]; 223 224 229 public void comment(String data) throws SAXException  230 { 231 final int length = data.length(); 232 if (length > m_charsBuff.length) 233 { 234 m_charsBuff = new char[length * 2 + 1]; 235 } 236 data.getChars(0, length, m_charsBuff, 0); 237 comment(m_charsBuff, 0, length); 238 } 239 240 248 protected String patchName(String qname) 249 { 250 251 252 final int lastColon = qname.lastIndexOf(':'); 253 254 if (lastColon > 0) { 255 final int firstColon = qname.indexOf(':'); 256 final String prefix = qname.substring(0, firstColon); 257 final String localName = qname.substring(lastColon + 1); 258 259 final String uri = m_prefixMap.lookupNamespace(prefix); 261 if (uri != null && uri.length() == 0) { 262 return localName; 263 } 264 else if (firstColon != lastColon) { 265 return prefix + ':' + localName; 266 } 267 } 268 return qname; 269 } 270 271 277 protected static String getLocalName(String qname) 278 { 279 final int col = qname.lastIndexOf(':'); 280 return (col > 0) ? qname.substring(col + 1) : qname; 281 } 282 283 309 public void setDocumentLocator(Locator locator) 310 { 311 return; 312 313 } 315 316 333 public void addAttribute( 334 String uri, 335 String localName, 336 String rawName, 337 String type, 338 String value) 339 throws SAXException  340 { 341 if (m_elemContext.m_startTagOpen) 342 { 343 addAttributeAlways(uri, localName, rawName, type, value); 344 } 345 346 } 347 348 359 public void addAttributeAlways( 360 String uri, 361 String localName, 362 String rawName, 363 String type, 364 String value) 365 { 366 int index; 370 index = m_attributes.getIndex(rawName); 377 if (index >= 0) 378 { 379 383 m_attributes.setValue(index,value); 384 } 385 else 386 { 387 m_attributes.addAttribute(uri, localName, rawName, type, value); 389 } 390 391 } 392 393 394 402 public void addAttribute(String name, final String value) 403 { 404 if (m_elemContext.m_startTagOpen) 405 { 406 final String patchedName = patchName(name); 407 final String localName = getLocalName(patchedName); 408 final String uri = getNamespaceURI(patchedName, false); 409 410 addAttributeAlways(uri,localName, patchedName, "CDATA", value); 411 } 412 } 413 414 415 427 public void addAttributes(Attributes atts) throws SAXException  428 { 429 430 int nAtts = atts.getLength(); 431 432 for (int i = 0; i < nAtts; i++) 433 { 434 String uri = atts.getURI(i); 435 436 if (null == uri) 437 uri = ""; 438 439 addAttributeAlways( 440 uri, 441 atts.getLocalName(i), 442 atts.getQName(i), 443 atts.getType(i), 444 atts.getValue(i)); 445 446 } 447 } 448 449 458 public ContentHandler asContentHandler() throws IOException  459 { 460 return this; 461 } 462 463 470 public void endEntity(String name) throws org.xml.sax.SAXException  471 { 472 if (name.equals("[dtd]")) 473 m_inExternalDTD = false; 474 m_inEntityRef = false; 475 476 if (m_tracer != null) 477 this.fireEndEntity(name); 478 } 479 480 485 public void close() 486 { 487 } 489 490 493 protected void initCDATA() 494 { 495 } 499 500 504 public String getEncoding() 505 { 506 return m_encoding; 507 } 508 509 513 public void setEncoding(String m_encoding) 514 { 515 this.m_encoding = m_encoding; 516 } 517 518 523 public void setOmitXMLDeclaration(boolean b) 524 { 525 this.m_shouldNotWriteXMLHeader = b; 526 } 527 528 529 533 public boolean getOmitXMLDeclaration() 534 { 535 return m_shouldNotWriteXMLHeader; 536 } 537 538 545 public String getDoctypePublic() 546 { 547 return m_doctypePublic; 548 } 549 550 554 public void setDoctypePublic(String doctypePublic) 555 { 556 this.m_doctypePublic = doctypePublic; 557 } 558 559 560 567 public String getDoctypeSystem() 568 { 569 return m_doctypeSystem; 570 } 571 572 576 public void setDoctypeSystem(String doctypeSystem) 577 { 578 this.m_doctypeSystem = doctypeSystem; 579 } 580 581 587 public void setDoctype(String doctypeSystem, String doctypePublic) 588 { 589 this.m_doctypeSystem = doctypeSystem; 590 this.m_doctypePublic = doctypePublic; 591 } 592 593 600 public void setStandalone(String standalone) 601 { 602 if (standalone != null) 603 { 604 m_standaloneWasSpecified = true; 605 setStandaloneInternal(standalone); 606 } 607 } 608 613 protected void setStandaloneInternal(String standalone) 614 { 615 if ("yes".equals(standalone)) 616 m_standalone = "yes"; 617 else 618 m_standalone = "no"; 619 620 } 621 622 628 public String getStandalone() 629 { 630 return m_standalone; 631 } 632 633 637 public boolean getIndent() 638 { 639 return m_doIndent; 640 } 641 647 public String getMediaType() 648 { 649 return m_mediatype; 650 } 651 652 656 public String getVersion() 657 { 658 return m_version; 659 } 660 661 666 public void setVersion(String version) 667 { 668 m_version = version; 669 } 670 671 678 public void setMediaType(String mediaType) 679 { 680 m_mediatype = mediaType; 681 } 682 683 686 public int getIndentAmount() 687 { 688 return m_indentAmount; 689 } 690 691 695 public void setIndentAmount(int m_indentAmount) 696 { 697 this.m_indentAmount = m_indentAmount; 698 } 699 700 707 public void setIndent(boolean doIndent) 708 { 709 m_doIndent = doIndent; 710 } 711 712 723 public void namespaceAfterStartElement(String uri, String prefix) 724 throws SAXException  725 { 726 } 728 729 739 public DOMSerializer asDOMSerializer() throws IOException  740 { 741 return this; 742 } 743 744 760 protected boolean isCdataSection() 761 { 762 763 boolean b = false; 764 765 if (null != m_cdataSectionElements) 766 { 767 if (m_elemContext.m_elementLocalName == null) 768 m_elemContext.m_elementLocalName = 769 getLocalName(m_elemContext.m_elementName); 770 if (m_elemContext.m_elementURI == null) 771 { 772 String prefix = getPrefixPart(m_elemContext.m_elementName); 773 if (prefix != null) 774 m_elemContext.m_elementURI = 775 m_prefixMap.lookupNamespace(prefix); 776 777 } 778 779 if ((null != m_elemContext.m_elementURI) 780 && m_elemContext.m_elementURI.length() == 0) 781 m_elemContext.m_elementURI = null; 782 783 int nElems = m_cdataSectionElements.size(); 784 785 for (int i = 0; i < nElems; i += 2) 787 { 788 String uri = (String ) m_cdataSectionElements.elementAt(i); 789 String loc = (String ) m_cdataSectionElements.elementAt(i + 1); 790 if (loc.equals(m_elemContext.m_elementLocalName) 791 && subPartMatch(m_elemContext.m_elementURI, uri)) 792 { 793 b = true; 794 795 break; 796 } 797 } 798 } 799 return b; 800 } 801 802 810 private static final boolean subPartMatch(String p, String t) 811 { 812 return (p == t) || ((null != p) && (p.equals(t))); 813 } 814 815 824 protected static final String getPrefixPart(String qname) 825 { 826 final int col = qname.indexOf(':'); 827 return (col > 0) ? qname.substring(0, col) : null; 828 } 830 831 836 public NamespaceMappings getNamespaceMappings() 837 { 838 return m_prefixMap; 839 } 840 841 847 public String getPrefix(String namespaceURI) 848 { 849 String prefix = m_prefixMap.lookupPrefix(namespaceURI); 850 return prefix; 851 } 852 853 861 public String getNamespaceURI(String qname, boolean isElement) 862 { 863 String uri = EMPTYSTRING; 864 int col = qname.lastIndexOf(':'); 865 final String prefix = (col > 0) ? qname.substring(0, col) : EMPTYSTRING; 866 867 if (!EMPTYSTRING.equals(prefix) || isElement) 868 { 869 if (m_prefixMap != null) 870 { 871 uri = m_prefixMap.lookupNamespace(prefix); 872 if (uri == null && !prefix.equals(XMLNS_PREFIX)) 873 { 874 throw new RuntimeException ( 875 XMLMessages.createXMLMessage( 876 XMLErrorResources.ER_NAMESPACE_PREFIX, 877 new Object [] { qname.substring(0, col) } )); 878 } 879 } 880 } 881 return uri; 882 } 883 884 891 public String getNamespaceURIFromPrefix(String prefix) 892 { 893 String uri = null; 894 if (m_prefixMap != null) 895 uri = m_prefixMap.lookupNamespace(prefix); 896 return uri; 897 } 898 899 906 public void entityReference(String name) throws org.xml.sax.SAXException  907 { 908 909 flushPending(); 910 911 startEntity(name); 912 endEntity(name); 913 914 if (m_tracer != null) 915 fireEntityReference(name); 916 } 917 918 923 public void setTransformer(Transformer <
|