1 57 58 59 66 67 package org.enhydra.apache.xml.serialize; 68 69 70 import org.w3c.dom.Document ; 71 import org.w3c.dom.DocumentType ; 72 import org.w3c.dom.Node ; 73 import org.w3c.dom.html.HTMLDocument; 74 75 76 99 public class OutputFormat 100 { 101 102 103 public static class DTD 104 { 105 106 109 public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN"; 110 111 114 public static final String HTMLSystemId = 115 "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; 116 117 120 public static final String XHTMLPublicId = 121 "-//W3C//DTD XHTML 1.0 Strict//EN"; 122 123 126 public static final String XHTMLSystemId = 127 "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; 128 129 } 130 131 132 public static class Defaults 133 { 134 135 141 public static final int Indent = 4; 142 143 148 public static final String Encoding = "UTF-8"; 149 150 154 public static final int LineWidth = 72; 155 156 } 157 158 159 163 private String _method; 164 165 166 169 private String _version; 170 171 172 176 private int _indent = 0; 177 178 179 183 private String _encoding = Defaults.Encoding; 184 185 188 private EncodingInfo _encodingInfo = null; 189 190 193 private String _mediaType; 194 195 196 199 private String _doctypeSystem; 200 201 202 205 private String _doctypePublic; 206 207 208 211 private boolean _omitXmlDeclaration = false; 212 213 214 217 private boolean _omitDoctype = false; 218 219 220 223 private boolean _omitComments = false; 224 225 226 229 private boolean _stripComments = false; 230 231 232 235 private boolean _standalone = false; 236 237 238 242 private String [] _cdataElements; 243 244 245 249 private String [] _nonEscapingElements; 250 251 252 255 private String _lineSeparator = LineSeparator.Web; 256 257 258 261 private int _lineWidth = Defaults.LineWidth; 262 263 264 268 private boolean _preserve = false; 269 273 private boolean _preserveEmptyAttributes = false; 274 275 278 public OutputFormat() 279 { 280 } 281 282 283 296 public OutputFormat( String method, String encoding, boolean indenting ) 297 { 298 setMethod( method ); 299 setEncoding( encoding ); 300 setIndenting( indenting ); 301 } 302 303 304 312 public OutputFormat( Document doc ) 313 { 314 setMethod( whichMethod( doc ) ); 315 setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) ); 316 setMediaType( whichMediaType( getMethod() ) ); 317 } 318 319 320 334 public OutputFormat( Document doc, String encoding, boolean indenting ) 335 { 336 this( doc ); 337 setEncoding( encoding ); 338 setIndenting( indenting ); 339 } 340 341 342 352 public String getMethod() 353 { 354 return _method; 355 } 356 357 358 364 public void setMethod( String method ) 365 { 366 _method = method; 367 } 368 369 370 379 public String getVersion() 380 { 381 return _version; 382 } 383 384 385 393 public void setVersion( String version ) 394 { 395 _version = version; 396 } 397 398 399 407 public int getIndent() 408 { 409 return _indent; 410 } 411 412 413 416 public boolean getIndenting() 417 { 418 return ( _indent > 0 ); 419 } 420 421 422 430 public void setIndent( int indent ) 431 { 432 if ( indent < 0 ) 433 _indent = 0; 434 else 435 _indent = indent; 436 } 437 438 439 448 public void setIndenting( boolean on ) 449 { 450 if ( on ) { 451 _indent = Defaults.Indent; 452 _lineWidth = Defaults.LineWidth; 453 } else { 454 _indent = 0; 455 _lineWidth = 0; 456 } 457 } 458 459 460 466 public String getEncoding() 467 { 468 return _encoding; 469 } 470 471 472 481 public void setEncoding( String encoding ) 482 { 483 _encoding = encoding; 484 _encodingInfo = null; 485 } 486 487 491 public void setEncoding(EncodingInfo encInfo) { 492 _encoding = encInfo.getName(); 493 _encodingInfo = encInfo; 494 } 495 496 501 public EncodingInfo getEncodingInfo() { 502 if (_encodingInfo == null) 503 _encodingInfo = Encodings.getEncodingInfo(_encoding); 504 return _encodingInfo; 505 } 506 507 514 public String getMediaType() 515 { 516 return _mediaType; 517 } 518 519 520 526 public void setMediaType( String mediaType ) 527 { 528 _mediaType = mediaType; 529 } 530 531 532 543 public void setDoctype( String publicId, String systemId ) 544 { 545 _doctypePublic = publicId; 546 _doctypeSystem = systemId; 547 } 548 549 550 554 public String getDoctypePublic() 555 { 556 return _doctypePublic; 557 } 558 559 560 564 public String getDoctypeSystem() 565 { 566 return _doctypeSystem; 567 } 568 569 570 574 public boolean getOmitComments() 575 { 576 return _omitComments; 577 } 578 579 580 585 public void setOmitComments( boolean omit ) 586 { 587 _omitComments = omit; 588 } 589 590 591 595 public boolean getOmitDocumentType() 596 { 597 return _omitDoctype; 598 } 599 600 601 606 public void setOmitDocumentType( boolean omit ) 607 { 608 _omitDoctype = omit; 609 } 610 611 612 616 public boolean getOmitXMLDeclaration() 617 { 618 return _omitXmlDeclaration; 619 } 620 621 622 627 public void setOmitXMLDeclaration( boolean omit ) 628 { 629 _omitXmlDeclaration = omit; 630 } 631 632 633 637 public boolean getStandalone() 638 { 639 return _standalone; 640 } 641 642 643 650 public void setStandalone( boolean standalone ) 651 { 652 _standalone = standalone; 653 } 654 655 656 661 public String [] getCDataElements() 662 { 663 return _cdataElements; 664 } 665 666 667 674 public boolean isCDataElement( String tagName ) 675 { 676 int i; 677 678 if ( _cdataElements == null ) 679 return false; 680 for ( i = 0 ; i < _cdataElements.length ; ++i ) 681 if ( _cdataElements[ i ].equals( tagName ) ) 682 return true; 683 return false; 684 } 685 686 687 693 public void setCDataElements( String [] cdataElements ) 694 { 695 _cdataElements = cdataElements; 696 } 697 698 699 704 public String [] getNonEscapingElements() 705 { 706 return _nonEscapingElements; 707 } 708 709 710 717 public boolean isNonEscapingElement( String tagName ) 718 { 719 int i; 720 721 if ( _nonEscapingElements == null ) 722 return false; 723 for ( i = 0 ; i < _nonEscapingElements.length ; ++i ) 724 if ( _nonEscapingElements[ i ].equals( tagName ) ) 725 return true; 726 return false; 727 } 728 729 730 736 public void setNonEscapingElements( String [] nonEscapingElements ) 737 { 738 _nonEscapingElements = nonEscapingElements; 739 } 740 741 742 743 750 public String getLineSeparator() 751 { 752 return _lineSeparator; 753 } 754 755 756 765 public void setLineSeparator( String lineSeparator ) 766 { 767 if ( lineSeparator == null ) 768 _lineSeparator = LineSeparator.Web; 769 else 770 _lineSeparator = lineSeparator; 771 } 772 773 774 781 public boolean getPreserveSpace() 782 { 783 return _preserve; 784 } 785 786 787 794 public void setPreserveSpace( boolean preserve ) 795 { 796 _preserve = preserve; 797 } 798 799 800 806 public int getLineWidth() 807 { 808 return _lineWidth; 809 } 810 811 812 821 public void setLineWidth( int lineWidth ) 822 { 823 if ( lineWidth <= 0 ) 824 _lineWidth = 0; 825 else 826 _lineWidth = lineWidth; 827 } 828 829 835 public boolean getPreserveEmptyAttributes () { 836 return _preserveEmptyAttributes; 837 } 838 844 public void setPreserveEmptyAttributes (boolean preserve) { 845 _preserveEmptyAttributes = preserve; 846 } 847 848 853 public char getLastPrintable() 854 { 855 if ( getEncoding() != null && 856 ( getEncoding().equalsIgnoreCase( "ASCII" ) ) ) 857 return 0xFF; 858 else 859 return 0xFFFF; 860 } 861 862 863 874 public static String whichMethod( Document doc ) 875 { 876 Node node; 877 String value; 878 int i; 879 880 if ( doc instanceof HTMLDocument ) 883 return Method.HTML; 884 885 889 891 node = doc.getFirstChild(); 892 while (node != null) { 893 if ( node.getNodeType() == Node.ELEMENT_NODE ) { 895 if ( node.getNodeName().equalsIgnoreCase( "html" ) ) { 896 return Method.HTML; 897 } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) { 898 return Method.FOP; 899 } else { 900 return Method.XML; 901 } 902 } else if ( node.getNodeType() == Node.TEXT_NODE ) { 903 value = node.getNodeValue(); 907 for ( i = 0 ; i < value.length() ; ++i ) 908 if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A && 909 value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D ) 910 return Method.XML; 911 } 912 node = node.getNextSibling(); 913 } 914 return Method.XML; 916 } 917 918 919 923 public static String whichDoctypePublic( Document doc ) 924 { 925 DocumentType doctype; 926 927 928 doctype = doc.getDoctype(); 929 if ( doctype != null ) { 930 try { 933 return doctype.getPublicId(); 934 } catch ( Error except ) { } 935 } 936 937 if ( doc instanceof HTMLDocument ) 938 return DTD.XHTMLPublicId; 939 return null; 940 } 941 942 943 947 public static String whichDoctypeSystem( Document doc ) 948 { 949 DocumentType doctype; 950 951 952 doctype = doc.getDoctype(); 953 if ( doctype != null ) { 954 try { 957 return doctype.getSystemId(); 958 } catch ( Error except ) { } 959 } 960 961 if ( doc instanceof HTMLDocument ) 962 return DTD.XHTMLSystemId; 963 return null; 964 } 965 966 967 971 public static String whichMediaType( String method ) 972 { 973 if ( method.equalsIgnoreCase( Method.XML ) ) 974 return "text/xml"; 975 if ( method.equalsIgnoreCase( Method.HTML ) ) 976 return "text/html"; 977 if ( method.equalsIgnoreCase( Method.XHTML ) ) 978 return "text/html"; 979 if ( method.equalsIgnoreCase( Method.TEXT ) ) 980 return "text/plain"; 981 if ( method.equalsIgnoreCase( Method.FOP ) ) 982 return "application/pdf"; 983 return null; 984 } 985 986 987 } 988 989 | Popular Tags |