1 16 19 package org.apache.xml.serializer; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.Writer ; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 27 import javax.xml.transform.SourceLocator ; 28 import javax.xml.transform.Transformer ; 29 30 import org.w3c.dom.Node ; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.Locator ; 34 import org.xml.sax.SAXException ; 35 36 37 49 public class ToUnknownStream extends SerializerBase 50 { 51 52 55 private SerializationHandler m_handler; 56 57 60 private static final String EMPTYSTRING = ""; 61 62 65 private boolean m_wrapped_handler_not_initialized = false; 66 67 68 71 private String m_firstElementPrefix; 72 75 private String m_firstElementName; 76 77 80 private String m_firstElementURI; 81 82 85 private String m_firstElementLocalName = null; 86 87 90 private boolean m_firstTagNotEmitted = true; 91 92 96 private Vector m_namespaceURI = null; 97 101 private Vector m_namespacePrefix = null; 102 103 107 private boolean m_needToCallStartDocument = false; 108 112 private boolean m_setVersion_called = false; 113 117 private boolean m_setDoctypeSystem_called = false; 118 122 private boolean m_setDoctypePublic_called = false; 123 127 private boolean m_setMediaType_called = false; 128 129 134 public ToUnknownStream() 135 { 136 m_handler = new ToXMLStream(); 137 } 138 139 143 public ContentHandler asContentHandler() throws IOException 144 { 145 150 return this; 151 } 152 153 156 public void close() 157 { 158 m_handler.close(); 159 } 160 161 165 public Properties getOutputFormat() 166 { 167 return m_handler.getOutputFormat(); 168 } 169 170 174 public OutputStream getOutputStream() 175 { 176 return m_handler.getOutputStream(); 177 } 178 179 183 public Writer getWriter() 184 { 185 return m_handler.getWriter(); 186 } 187 188 193 public boolean reset() 194 { 195 return m_handler.reset(); 196 } 197 198 204 public void serialize(Node node) throws IOException 205 { 206 if (m_firstTagNotEmitted) 207 { 208 flush(); 209 } 210 m_handler.serialize(node); 211 } 212 213 216 public boolean setEscaping(boolean escape) throws SAXException 217 { 218 return m_handler.setEscaping(escape); 219 } 220 221 226 public void setOutputFormat(Properties format) 227 { 228 m_handler.setOutputFormat(format); 229 } 230 231 236 public void setOutputStream(OutputStream output) 237 { 238 m_handler.setOutputStream(output); 239 } 240 241 246 public void setWriter(Writer writer) 247 { 248 m_handler.setWriter(writer); 249 } 250 251 260 public void addAttribute( 261 String uri, 262 String localName, 263 String rawName, 264 String type, 265 String value) 266 throws SAXException 267 { 268 if (m_firstTagNotEmitted) 269 { 270 flush(); 271 } 272 m_handler.addAttribute(uri, localName, rawName, type, value); 273 } 274 280 public void addAttribute(String rawName, String value) 281 { 282 if (m_firstTagNotEmitted) 283 { 284 flush(); 285 } 286 m_handler.addAttribute(rawName, value); 287 288 } 289 290 293 public void addUniqueAttribute(String rawName, String value, int flags) 294 throws SAXException 295 { 296 if (m_firstTagNotEmitted) 297 { 298 flush(); 299 } 300 m_handler.addUniqueAttribute(rawName, value, flags); 301 302 } 303 304 310 public void characters(String chars) throws SAXException 311 { 312 final int length = chars.length(); 313 if (length > m_charsBuff.length) 314 { 315 m_charsBuff = new char[length*2 + 1]; 316 } 317 chars.getChars(0, length, m_charsBuff, 0); 318 this.characters(m_charsBuff, 0, length); 319 } 320 321 325 public void endElement(String elementName) throws SAXException 326 { 327 if (m_firstTagNotEmitted) 328 { 329 flush(); 330 } 331 m_handler.endElement(elementName); 332 } 333 334 335 340 public void startPrefixMapping(String prefix, String uri) throws SAXException 341 { 342 this.startPrefixMapping(prefix,uri, true); 343 } 344 345 356 public void namespaceAfterStartElement(String prefix, String uri) 357 throws SAXException 358 { 359 if (m_firstTagNotEmitted && m_firstElementURI == null && m_firstElementName != null) 361 { 362 String prefix1 = getPrefixPart(m_firstElementName); 363 if (prefix1 == null && EMPTYSTRING.equals(prefix)) 364 { 365 m_firstElementURI = uri; 370 } 371 } 372 startPrefixMapping(prefix,uri, false); 373 } 374 375 public boolean startPrefixMapping(String prefix, String uri, boolean shouldFlush) 376 throws SAXException 377 { 378 boolean pushed = false; 379 if (m_firstTagNotEmitted) 380 { 381 if (m_firstElementName != null && shouldFlush) 382 { 383 387 flush(); 388 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush); 389 } 390 else 391 { 392 if (m_namespacePrefix == null) 393 { 394 m_namespacePrefix = new Vector (); 395 m_namespaceURI = new Vector (); 396 } 397 m_namespacePrefix.addElement(prefix); 398 m_namespaceURI.addElement(uri); 399 400 if (m_firstElementURI == null) 401 { 402 if (prefix.equals(m_firstElementPrefix)) 403 m_firstElementURI = uri; 404 } 405 } 406 407 } 408 else 409 { 410 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush); 411 } 412 return pushed; 413 } 414 415 419 420 public void setVersion(String version) 421 { 422 m_handler.setVersion(version); 423 424 m_setVersion_called = true; 427 } 428 429 432 public void startDocument() throws SAXException 433 { 434 m_needToCallStartDocument = true; 435 } 436 437 438 439 public void startElement(String qName) throws SAXException 440 { 441 this.startElement(null, null, qName, null); 442 } 443 444 public void startElement(String namespaceURI, String localName, String qName) throws SAXException 445 { 446 this.startElement(namespaceURI, localName, qName, null); 447 } 448 449 public void startElement( 450 String namespaceURI, 451 String localName, 452 String elementName, 453 Attributes atts) throws SAXException 454 { 455 456 if (m_firstTagNotEmitted) 457 { 458 459 if (m_firstElementName != null) 460 { 461 465 flush(); 466 m_handler.startElement(namespaceURI, localName, elementName, atts); 467 } 468 else 469 { 470 474 475 m_wrapped_handler_not_initialized = true; 476 m_firstElementName = elementName; 477 478 m_firstElementPrefix = getPrefixPartUnknown(elementName); 480 481 m_firstElementURI = namespaceURI; 483 484 m_firstElementLocalName = localName; 486 487 if (m_tracer != null) 488 firePseudoElement(elementName); 489 490 497 if (atts != null) 498 super.addAttributes(atts); 499 500 if (atts != null) 504 flush(); 505 506 } 507 } 508 else 509 { 510 m_handler.startElement(namespaceURI, localName, elementName, atts); 513 } 514 } 515 516 520 public void comment(String comment) throws SAXException 521 { 522 if (m_firstTagNotEmitted && m_firstElementName != null) 523 { 524 emitFirstTag(); 525 } 526 else if (m_needToCallStartDocument) 527 { 528 m_handler.startDocument(); 529 m_needToCallStartDocument = false; 530 } 531 532 m_handler.comment(comment); 533 } 534 535 539 public String getDoctypePublic() 540 { 541 542 return m_handler.getDoctypePublic(); 543 } 544 545 549 public String getDoctypeSystem() 550 { 551 return m_handler.getDoctypeSystem(); 552 } 553 554 558 public String getEncoding() 559 { 560 return m_handler.getEncoding(); 561 } 562 563 567 public boolean getIndent() 568 { 569 return m_handler.getIndent(); 570 } 571 572 576 public int getIndentAmount() 577 { 578 return m_handler.getIndentAmount(); 579 } 580 581 585 public String getMediaType() 586 { 587 return m_handler.getMediaType(); 588 } 589 590 594 public boolean getOmitXMLDeclaration() 595 { 596 return m_handler.getOmitXMLDeclaration(); 597 } 598 599 603 public String getStandalone() 604 { 605 return m_handler.getStandalone(); 606 } 607 608 612 public String getVersion() 613 { 614 return m_handler.getVersion(); 615 } 616 617 620 public void setDoctype(String system, String pub) 621 { 622 m_handler.setDoctypePublic(pub); 623 m_handler.setDoctypeSystem(system); 624 } 625 626 632 public void setDoctypePublic(String doctype) 633 { 634 m_handler.setDoctypePublic(doctype); 635 m_setDoctypePublic_called = true; 636 } 637 638 644 public void setDoctypeSystem(String doctype) 645 { 646 m_handler.setDoctypeSystem(doctype); 647 m_setDoctypeSystem_called = true; 648 } 649 650 654 public void setEncoding(String encoding) 655 { 656 m_handler.setEncoding(encoding); 657 } 658 659 663 public void setIndent(boolean indent) 664 { 665 m_handler.setIndent(indent); 666 } 667 668 671 public void setIndentAmount(int value) 672 { 673 m_handler.setIndentAmount(value); 674 } 675 676 679 public void setMediaType(String mediaType) 680 { 681 m_handler.setMediaType(mediaType); 682 m_setMediaType_called = true; 683 } 684 685 689 public void setOmitXMLDeclaration(boolean b) 690 { 691 m_handler.setOmitXMLDeclaration(b); 692 } 693 694 698 public void setStandalone(String standalone) 699 { 700 m_handler.setStandalone(standalone); 701 } 702 703 706 707 711 public void attributeDecl( 712 String arg0, 713 String arg1, 714 String arg2, 715 String arg3, 716 String arg4) 717 throws SAXException 718 { 719 m_handler.attributeDecl(arg0, arg1, arg2, arg3, arg4); 720 } 721 722 726 public void elementDecl(String arg0, String arg1) throws SAXException 727 { 728 if (m_firstTagNotEmitted) 729 { 730 emitFirstTag(); 731 } 732 m_handler.elementDecl(arg0, arg1); 733 } 734 735 739 public void externalEntityDecl( 740 String name, 741 String publicId, 742 String systemId) 743 throws SAXException 744 { 745 if (m_firstTagNotEmitted) 746 { 747 flush(); 748 } 749 m_handler.externalEntityDecl(name, publicId, systemId); 750 } 751 752 756 public void internalEntityDecl(String arg0, String arg1) 757 throws SAXException 758 { 759 if (m_firstTagNotEmitted) 760 { 761 flush(); 762 } 763 m_handler.internalEntityDecl(arg0, arg1); 764 } 765 766 770 public void characters(char[] characters, int offset, int length) 771 throws SAXException 772 { 773 if (m_firstTagNotEmitted) 774 { 775 flush(); 776 } 777 778 m_handler.characters(characters, offset, length); 779 780 } 781 782 786 public void endDocument() throws SAXException 787 { 788 if (m_firstTagNotEmitted) 789 { 790 flush(); 791 } 792 793 m_handler.endDocument(); 794 795 796 } 797 798 802 public void endElement(String namespaceURI, String localName, String qName) 803 throws SAXException 804 { 805 if (m_firstTagNotEmitted) 806 { 807 flush(); 808 if (namespaceURI == null && m_firstElementURI != null) 809 namespaceURI = m_firstElementURI; 810 811 812 if (localName == null && m_firstElementLocalName != null) 813 localName = m_firstElementLocalName; 814 } 815 816 m_handler.endElement(namespaceURI, localName, qName); 817 } 818 819 823 public void endPrefixMapping(String prefix) throws SAXException 824 { 825 m_handler.endPrefixMapping(prefix); 826 } 827 828 832 public void ignorableWhitespace(char[] ch, int start, int length) 833 throws SAXException 834 { 835 if (m_firstTagNotEmitted) 836 { 837 flush(); 838 } 839 m_handler.ignorableWhitespace(ch, start, length); 840 } 841 842 846 public void processingInstruction(String target, String data) 847 throws SAXException 848 { 849 if (m_firstTagNotEmitted) 850 { 851 flush(); 852 } 853 854 m_handler.processingInstruction(target, data); 855 } 856 857 861 public void setDocumentLocator(Locator locator) 862 { 863 m_handler.setDocumentLocator(locator); 864 } 865 866 870 public void skippedEntity(String name) throws SAXException 871 { 872 m_handler.skippedEntity(name); 873 } 874 875 876 877 881 public void comment(char[] ch, int start, int length) throws SAXException 882 { 883 if (m_firstTagNotEmitted) 884 { 885 flush(); 886 } 887 888 m_handler.comment(ch, start, length); 889 } 890 891 895 public void endCDATA() throws SAXException 896 { 897 898 m_handler.endCDATA(); 899 } 900 901 905 public void endDTD() throws SAXException 906 { 907 908 m_handler.endDTD(); 909 } 910 911 915 public void endEntity(String name) throws SAXException 916 { 917 if (m_firstTagNotEmitted) 918 { 919 emitFirstTag(); 920 } 921 m_handler.endEntity(name); 922 } 923 924 928 public void startCDATA() throws SAXException 929 { 930 m_handler.startCDATA(); 931 } 932 933 937 public void startDTD(String name, String publicId, String systemId) 938 throws SAXException 939 { 940 m_handler.startDTD(name, publicId, systemId); 941 } 942 943 947 public void startEntity(String name) throws SAXException 948 { 949 m_handler.startEntity(name); 950 } 951 952 959 private void initStreamOutput() throws SAXException 960 { 961 962 boolean firstElementIsHTML = isFirstElemHTML(); 964 965 if (firstElementIsHTML) 966 { 967 969 SerializationHandler oldHandler = m_handler; 971 972 976 977 Properties htmlProperties = 978 OutputPropertiesFactory.getDefaultMethodProperties(Method.HTML); 979 Serializer serializer = 980 SerializerFactory.getSerializer(htmlProperties); 981 982 m_handler = (SerializationHandler) serializer; 987 989 Writer writer = oldHandler.getWriter(); 990 991 if (null != writer) 992 m_handler.setWriter(writer); 993 else 994 { 995 OutputStream os = oldHandler.getOutputStream(); 996 997 if (null != os) 998 m_handler.setOutputStream(os); 999 } 1000 1001 1003 m_handler.setVersion(oldHandler.getVersion()); 1006 m_handler.setDoctypeSystem(oldHandler.getDoctypeSystem()); 1010 m_handler.setDoctypePublic(oldHandler.getDoctypePublic()); 1014 m_handler.setMediaType(oldHandler.getMediaType()); 1018 1020 m_handler.setTransformer(oldHandler.getTransformer()); 1021 } 1022 1023 1026 if (m_needToCallStartDocument) 1028 { 1029 m_handler.startDocument(); 1030 m_needToCallStartDocument = false; 1031 } 1032 1033 m_wrapped_handler_not_initialized = false; 1035 } 1036 1037 private void emitFirstTag() throws SAXException 1038 { 1039 if (m_firstElementName != null) 1040 { 1041 if (m_wrapped_handler_not_initialized) 1042 { 1043 initStreamOutput(); 1044 m_wrapped_handler_not_initialized = false; 1045 } 1046 m_handler.startElement(m_firstElementURI, null, m_firstElementName, m_attributes); 1048 m_attributes = null; 1050 1051 if (m_namespacePrefix != null) 1053 { 1054 final int n = m_namespacePrefix.size(); 1055 for (int i = 0; i < n; i++) 1056 { 1057 final String prefix = 1058 (String ) m_namespacePrefix.elementAt(i); 1059 final String uri = (String ) m_namespaceURI.elementAt(i); 1060 m_handler.startPrefixMapping(prefix, uri, false); 1061 } 1062 m_namespacePrefix = null; 1063 m_namespaceURI = null; 1064 } 1065 m_firstTagNotEmitted = false; 1066 } 1067 } 1068 1069 1075 private String getLocalNameUnknown(String value) 1076 { 1077 int idx = value.lastIndexOf(':'); 1078 if (idx >= 0) 1079 value = value.substring(idx + 1); 1080 idx = value.lastIndexOf('@'); 1081 if (idx >= 0) 1082 value = value.substring(idx + 1); 1083 return (value); 1084 } 1085 1086 1092 private String getPrefixPartUnknown(String qname) 1093 { 1094 final int index = qname.indexOf(':'); 1095 return (index > 0) ? qname.substring(0, index) : EMPTYSTRING; 1096 } 1097 1098 1105 private boolean isFirstElemHTML() 1106 { 1107 boolean isHTML; 1108 1109 isHTML = 1111 getLocalNameUnknown(m_firstElementName).equalsIgnoreCase("html"); 1112 1113 if (isHTML 1115 && m_firstElementURI != null 1116 && !EMPTYSTRING.equals(m_firstElementURI)) 1117 { 1118 isHTML = false; 1120 } 1121 if (isHTML && m_namespacePrefix != null) 1123 { 1124 1128 final int max = m_namespacePrefix.size(); 1129 for (int i = 0; i < max; i++) 1130 { 1131 final String prefix = (String ) m_namespacePrefix.elementAt(i); 1132 final String uri = (String ) m_namespaceURI.elementAt(i); 1133 1134 if (m_firstElementPrefix != null 1135 && m_firstElementPrefix.equals(prefix) 1136 && !EMPTYSTRING.equals(uri)) 1137 { 1138 isHTML = false; 1140 break; 1141 } 1142 } 1143 1144 } 1145 return isHTML; 1146 } 1147 1150 public DOMSerializer asDOMSerializer() throws IOException 1151 { 1152 return m_handler.asDOMSerializer(); 1153 } 1154 1155 1160 public void setCdataSectionElements(Vector URI_and_localNames) 1161 { 1162 m_handler.setCdataSectionElements(URI_and_localNames); 1163 } 1164 1167 public void addAttributes(Attributes atts) throws SAXException 1168 { 1169 m_handler.addAttributes(atts); 1170 } 1171 1172 1177 public NamespaceMappings getNamespaceMappings() 1178 { 1179 NamespaceMappings mappings = null; 1180 if (m_handler != null) 1181 { 1182 mappings = m_handler.getNamespaceMappings(); 1183 } 1184 return mappings; 1185 } 1186 1189 public void flushPending() throws SAXException 1190 { 1191 1192 flush(); 1193 1194 m_handler.flushPending(); 1195 } 1196 1197 private void flush() 1198 { 1199 try 1200 { 1201 if (m_firstTagNotEmitted) 1202 { 1203 emitFirstTag(); 1204 } 1205 if (m_needToCallStartDocument) 1206 { 1207 m_handler.startDocument(); 1208 m_needToCallStartDocument = false; 1209 } 1210 } 1211 catch(SAXException e) 1212 { 1213 throw new RuntimeException (e.toString()); 1214 } 1215 1216 1217 } 1218 1219 1222 public String getPrefix(String namespaceURI) 1223 { 1224 return m_handler.getPrefix(namespaceURI); 1225 } 1226 1229 public void entityReference(String entityName) throws SAXException 1230 { 1231 m_handler.entityReference(entityName); 1232 } 1233 1234 1237 public String getNamespaceURI(String qname, boolean isElement) 1238 { 1239 return m_handler.getNamespaceURI(qname, isElement); 1240 } 1241 1242 public String getNamespaceURIFromPrefix(String prefix) 1243 { 1244 return m_handler.getNamespaceURIFromPrefix(prefix); 1245 } 1246 1247 public void setTransformer(Transformer t) 1248 { 1249 m_handler.setTransformer(t); 1250 if ((t instanceof SerializerTrace) && 1251 (((SerializerTrace) t).hasTraceListeners())) { 1252 m_tracer = (SerializerTrace) t; 1253 } else { 1254 m_tracer = null; 1255 } 1256 } 1257 public Transformer getTransformer() 1258 { 1259 return m_handler.getTransformer(); 1260 } 1261 1262 1265 public void setContentHandler(ContentHandler ch) 1266 { 1267 m_handler.setContentHandler(ch); 1268 } 1269 1276 public void setSourceLocator(SourceLocator locator) 1277 { 1278 m_handler.setSourceLocator(locator); 1279 } 1280 1281 protected void firePseudoElement(String elementName) 1282 { 1283 1284 if (m_tracer != null) { 1285 StringBuffer sb = new StringBuffer (); 1286 1287 sb.append('<'); 1288 sb.append(elementName); 1289 1290 char ch[] = sb.toString().toCharArray(); 1294 m_tracer.fireGenerateEvent( 1295 SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS, 1296 ch, 1297 0, 1298 ch.length); 1299 } 1300 } 1301} 1302 | Popular Tags |