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 t) 924 { 925 m_transformer = t; 926 927 if ((m_transformer instanceof SerializerTrace) && 931 (((SerializerTrace) m_transformer).hasTraceListeners())) { 932 m_tracer = (SerializerTrace) m_transformer; 933 } else { 934 m_tracer = null; 935 } 936 } 937 942 public Transformer getTransformer() 943 { 944 return m_transformer; 945 } 946 947 953 public void characters(org.w3c.dom.Node node) 954 throws org.xml.sax.SAXException 955 { 956 flushPending(); 957 String data = node.getNodeValue(); 958 if (data != null) 959 { 960 final int length = data.length(); 961 if (length > m_charsBuff.length) 962 { 963 m_charsBuff = new char[length * 2 + 1]; 964 } 965 data.getChars(0, length, m_charsBuff, 0); 966 characters(m_charsBuff, 0, length); 967 } 968 } 969 970 971 974 public void error(SAXParseException exc) throws SAXException { 975 } 976 977 980 public void fatalError(SAXParseException exc) throws SAXException { 981 982 m_elemContext.m_startTagOpen = false; 983 984 } 985 986 989 public void warning(SAXParseException exc) throws SAXException 990 { 991 } 992 993 997 protected void fireStartEntity(String name) 998 throws org.xml.sax.SAXException 999 { 1000 if (m_tracer != null) 1001 { 1002 flushMyWriter(); 1003 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_ENTITYREF, name); 1004 } 1005 } 1006 1007 1013 1021 1030 private void flushMyWriter() 1031 { 1032 if (m_writer != null) 1033 { 1034 try 1035 { 1036 m_writer.flush(); 1037 } 1038 catch(IOException ioe) 1039 { 1040 1041 } 1042 } 1043 } 1044 1050 protected void fireCDATAEvent(char[] chars, int start, int length) 1051 throws org.xml.sax.SAXException 1052 { 1053 if (m_tracer != null) 1054 { 1055 flushMyWriter(); 1056 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_CDATA, chars, start,length); 1057 } 1058 } 1059 1060 1066 protected void fireCommentEvent(char[] chars, int start, int length) 1067 throws org.xml.sax.SAXException 1068 { 1069 if (m_tracer != null) 1070 { 1071 flushMyWriter(); 1072 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_COMMENT, new String (chars, start, length)); 1073 } 1074 } 1075 1076 1077 1081 public void fireEndEntity(String name) 1082 throws org.xml.sax.SAXException 1083 { 1084 if (m_tracer != null) 1085 flushMyWriter(); 1086 } 1088 1089 1092 protected void fireStartDoc() 1093 throws org.xml.sax.SAXException 1094 { 1095 if (m_tracer != null) 1096 { 1097 flushMyWriter(); 1098 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_STARTDOCUMENT); 1099 } 1100 } 1101 1102 1103 1106 protected void fireEndDoc() 1107 throws org.xml.sax.SAXException 1108 { 1109 if (m_tracer != null) 1110 { 1111 flushMyWriter(); 1112 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_ENDDOCUMENT); 1113 } 1114 } 1115 1116 1123 protected void fireStartElem(String elemName) 1124 throws org.xml.sax.SAXException 1125 { 1126 if (m_tracer != null) 1127 { 1128 flushMyWriter(); 1129 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_STARTELEMENT, 1130 elemName, m_attributes); 1131 } 1132 } 1133 1134 1135 1139 1146 1147 1151 protected void fireEscapingEvent(String name, String data) 1152 throws org.xml.sax.SAXException 1153 { 1154 1155 if (m_tracer != null) 1156 { 1157 flushMyWriter(); 1158 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_PI,name, data); 1159 } 1160 } 1161 1162 1163 1167 protected void fireEntityReference(String name) 1168 throws org.xml.sax.SAXException 1169 { 1170 if (m_tracer != null) 1171 { 1172 flushMyWriter(); 1173 m_tracer.fireGenerateEvent(SerializerTrace.EVENTTYPE_ENTITYREF,name, (Attributes )null); 1174 } 1175 } 1176 1177 1191 public void startDocument() throws org.xml.sax.SAXException 1192 { 1193 1194 startDocumentInternal(); 1196 m_needToCallStartDocument = false; 1197 return; 1198 } 1199 1200 1216 protected void startDocumentInternal() throws org.xml.sax.SAXException 1217 { 1218 if (m_tracer != null) 1219 this.fireStartDoc(); 1220 } 1221 1228 public void setSourceLocator(SourceLocator locator) 1229 { 1230 m_sourceLocator = locator; 1231 } 1232 1233 1234 1240 public void setNamespaceMappings(NamespaceMappings mappings) { 1241 m_prefixMap = mappings; 1242 } 1243 1244 public boolean reset() 1245 { 1246 resetSerializerBase(); 1247 return true; 1248 } 1249 1250 1254 private void resetSerializerBase() 1255 { 1256 this.m_attributes.clear(); 1257 this.m_cdataSectionElements = null; 1258 this.m_elemContext = new ElemContext(); 1259 this.m_doctypePublic = null; 1260 this.m_doctypeSystem = null; 1261 this.m_doIndent = false; 1262 this.m_encoding = null; 1263 this.m_indentAmount = 0; 1264 this.m_inEntityRef = false; 1265 this.m_inExternalDTD = false; 1266 this.m_mediatype = null; 1267 this.m_needToCallStartDocument = true; 1268 this.m_needToOutputDocTypeDecl = false; 1269 if (this.m_prefixMap != null) 1270 this.m_prefixMap.reset(); 1271 this.m_shouldNotWriteXMLHeader = false; 1272 this.m_sourceLocator = null; 1273 this.m_standalone = null; 1274 this.m_standaloneWasSpecified = false; 1275 this.m_tracer = null; 1276 this.m_transformer = null; 1277 this.m_version = null; 1278 } 1281 1282} 1283 | Popular Tags |