1 57 58 59 66 67 package com.sun.org.apache.xml.internal.serialize; 68 69 70 import java.io.UnsupportedEncodingException ; 71 72 import org.w3c.dom.Document ; 73 import org.w3c.dom.DocumentType ; 74 import org.w3c.dom.Node ; 75 import org.w3c.dom.html.HTMLDocument; 76 77 78 101 public class OutputFormat 102 { 103 104 105 public static class DTD 106 { 107 108 111 public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN"; 112 113 116 public static final String HTMLSystemId = 117 "http://www.w3.org/TR/html4/strict.dtd"; 118 119 122 public static final String XHTMLPublicId = 123 "-//W3C//DTD XHTML 1.0 Strict//EN"; 124 125 128 public static final String XHTMLSystemId = 129 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; 130 131 } 132 133 134 public static class Defaults 135 { 136 137 143 public static final int Indent = 4; 144 145 150 public static final String Encoding = "UTF-8"; 151 152 156 public static final int LineWidth = 72; 157 158 } 159 160 161 165 private String _method; 166 167 168 171 private String _version; 172 173 174 178 private int _indent = 0; 179 180 181 185 private String _encoding = Defaults.Encoding; 186 187 190 private EncodingInfo _encodingInfo = null; 191 192 private boolean _allowJavaNames = false; 194 195 198 private String _mediaType; 199 200 201 204 private String _doctypeSystem; 205 206 207 210 private String _doctypePublic; 211 212 213 216 private boolean _omitXmlDeclaration = false; 217 218 219 222 private boolean _omitDoctype = false; 223 224 225 228 private boolean _omitComments = false; 229 230 231 234 private boolean _stripComments = false; 235 236 237 240 private boolean _standalone = false; 241 242 243 247 private String [] _cdataElements; 248 249 250 254 private String [] _nonEscapingElements; 255 256 257 260 private String _lineSeparator = LineSeparator.Web; 261 262 263 266 private int _lineWidth = Defaults.LineWidth; 267 268 269 273 private boolean _preserve = false; 274 278 private boolean _preserveEmptyAttributes = false; 279 280 283 public OutputFormat() 284 { 285 } 286 287 288 301 public OutputFormat( String method, String encoding, boolean indenting ) 302 { 303 setMethod( method ); 304 setEncoding( encoding ); 305 setIndenting( indenting ); 306 } 307 308 309 317 public OutputFormat( Document doc ) 318 { 319 setMethod( whichMethod( doc ) ); 320 setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) ); 321 setMediaType( whichMediaType( getMethod() ) ); 322 } 323 324 325 339 public OutputFormat( Document doc, String encoding, boolean indenting ) 340 { 341 this( doc ); 342 setEncoding( encoding ); 343 setIndenting( indenting ); 344 } 345 346 347 357 public String getMethod() 358 { 359 return _method; 360 } 361 362 363 369 public void setMethod( String method ) 370 { 371 _method = method; 372 } 373 374 375 384 public String getVersion() 385 { 386 return _version; 387 } 388 389 390 398 public void setVersion( String version ) 399 { 400 _version = version; 401 } 402 403 404 412 public int getIndent() 413 { 414 return _indent; 415 } 416 417 418 421 public boolean getIndenting() 422 { 423 return ( _indent > 0 ); 424 } 425 426 427 435 public void setIndent( int indent ) 436 { 437 if ( indent < 0 ) 438 _indent = 0; 439 else 440 _indent = indent; 441 } 442 443 444 453 public void setIndenting( boolean on ) 454 { 455 if ( on ) { 456 _indent = Defaults.Indent; 457 _lineWidth = Defaults.LineWidth; 458 } else { 459 _indent = 0; 460 _lineWidth = 0; 461 } 462 } 463 464 465 471 public String getEncoding() 472 { 473 return _encoding; 474 } 475 476 477 486 public void setEncoding( String encoding ) 487 { 488 _encoding = encoding; 489 _encodingInfo = null; 490 } 491 492 496 public void setEncoding(EncodingInfo encInfo) { 497 _encoding = encInfo.getIANAName(); 498 _encodingInfo = encInfo; 499 } 500 501 506 public EncodingInfo getEncodingInfo() throws UnsupportedEncodingException { 507 if (_encodingInfo == null) 508 _encodingInfo = Encodings.getEncodingInfo(_encoding, _allowJavaNames); 509 return _encodingInfo; 510 } 511 512 515 public void setAllowJavaNames (boolean allow) { 516 _allowJavaNames = allow; 517 } 518 519 522 public boolean setAllowJavaNames () { 523 return _allowJavaNames; 524 } 525 526 533 public String getMediaType() 534 { 535 return _mediaType; 536 } 537 538 539 545 public void setMediaType( String mediaType ) 546 { 547 _mediaType = mediaType; 548 } 549 550 551 562 public void setDoctype( String publicId, String systemId ) 563 { 564 _doctypePublic = publicId; 565 _doctypeSystem = systemId; 566 } 567 568 569 573 public String getDoctypePublic() 574 { 575 return _doctypePublic; 576 } 577 578 579 583 public String getDoctypeSystem() 584 { 585 return _doctypeSystem; 586 } 587 588 589 593 public boolean getOmitComments() 594 { 595 return _omitComments; 596 } 597 598 599 604 public void setOmitComments( boolean omit ) 605 { 606 _omitComments = omit; 607 } 608 609 610 614 public boolean getOmitDocumentType() 615 { 616 return _omitDoctype; 617 } 618 619 620 625 public void setOmitDocumentType( boolean omit ) 626 { 627 _omitDoctype = omit; 628 } 629 630 631 635 public boolean getOmitXMLDeclaration() 636 { 637 return _omitXmlDeclaration; 638 } 639 640 641 646 public void setOmitXMLDeclaration( boolean omit ) 647 { 648 _omitXmlDeclaration = omit; 649 } 650 651 652 656 public boolean getStandalone() 657 { 658 return _standalone; 659 } 660 661 662 669 public void setStandalone( boolean standalone ) 670 { 671 _standalone = standalone; 672 } 673 674 675 680 public String [] getCDataElements() 681 { 682 return _cdataElements; 683 } 684 685 686 693 public boolean isCDataElement( String tagName ) 694 { 695 int i; 696 697 if ( _cdataElements == null ) 698 return false; 699 for ( i = 0 ; i < _cdataElements.length ; ++i ) 700 if ( _cdataElements[ i ].equals( tagName ) ) 701 return true; 702 return false; 703 } 704 705 706 712 public void setCDataElements( String [] cdataElements ) 713 { 714 _cdataElements = cdataElements; 715 } 716 717 718 723 public String [] getNonEscapingElements() 724 { 725 return _nonEscapingElements; 726 } 727 728 729 736 public boolean isNonEscapingElement( String tagName ) 737 { 738 int i; 739 740 if ( _nonEscapingElements == null ) { 741 return false; 742 } 743 for ( i = 0 ; i < _nonEscapingElements.length ; ++i ) 744 if ( _nonEscapingElements[ i ].equals( tagName ) ) 745 return true; 746 return false; 747 } 748 749 750 756 public void setNonEscapingElements( String [] nonEscapingElements ) 757 { 758 _nonEscapingElements = nonEscapingElements; 759 } 760 761 762 763 770 public String getLineSeparator() 771 { 772 return _lineSeparator; 773 } 774 775 776 785 public void setLineSeparator( String lineSeparator ) 786 { 787 if ( lineSeparator == null ) 788 _lineSeparator = LineSeparator.Web; 789 else 790 _lineSeparator = lineSeparator; 791 } 792 793 794 801 public boolean getPreserveSpace() 802 { 803 return _preserve; 804 } 805 806 807 814 public void setPreserveSpace( boolean preserve ) 815 { 816 _preserve = preserve; 817 } 818 819 820 826 public int getLineWidth() 827 { 828 return _lineWidth; 829 } 830 831 832 841 public void setLineWidth( int lineWidth ) 842 { 843 if ( lineWidth <= 0 ) 844 _lineWidth = 0; 845 else 846 _lineWidth = lineWidth; 847 } 848 public boolean getPreserveEmptyAttributes () { return _preserveEmptyAttributes; } public void setPreserveEmptyAttributes (boolean preserve) { _preserveEmptyAttributes = preserve; } 859 860 865 public char getLastPrintable() 866 { 867 if ( getEncoding() != null && 868 ( getEncoding().equalsIgnoreCase( "ASCII" ) ) ) 869 return 0xFF; 870 else 871 return 0xFFFF; 872 } 873 874 875 886 public static String whichMethod( Document doc ) 887 { 888 Node node; 889 String value; 890 int i; 891 892 if ( doc instanceof HTMLDocument ) 895 return Method.HTML; 896 897 901 903 node = doc.getFirstChild(); 904 while (node != null) { 905 if ( node.getNodeType() == Node.ELEMENT_NODE ) { 907 if ( node.getNodeName().equalsIgnoreCase( "html" ) ) { 908 return Method.HTML; 909 } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) { 910 return Method.FOP; 911 } else { 912 return Method.XML; 913 } 914 } else if ( node.getNodeType() == Node.TEXT_NODE ) { 915 value = node.getNodeValue(); 919 for ( i = 0 ; i < value.length() ; ++i ) 920 if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A && 921 value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D ) 922 return Method.XML; 923 } 924 node = node.getNextSibling(); 925 } 926 return Method.XML; 928 } 929 930 931 935 public static String whichDoctypePublic( Document doc ) 936 { 937 DocumentType doctype; 938 939 940 doctype = doc.getDoctype(); 941 if ( doctype != null ) { 942 try { 945 return doctype.getPublicId(); 946 } catch ( Error except ) { } 947 } 948 949 if ( doc instanceof HTMLDocument ) 950 return DTD.XHTMLPublicId; 951 return null; 952 } 953 954 955 959 public static String whichDoctypeSystem( Document doc ) 960 { 961 DocumentType doctype; 962 963 964 doctype = doc.getDoctype(); 965 if ( doctype != null ) { 966 try { 969 return doctype.getSystemId(); 970 } catch ( Error except ) { } 971 } 972 973 if ( doc instanceof HTMLDocument ) 974 return DTD.XHTMLSystemId; 975 return null; 976 } 977 978 979 983 public static String whichMediaType( String method ) 984 { 985 if ( method.equalsIgnoreCase( Method.XML ) ) 986 return "text/xml"; 987 if ( method.equalsIgnoreCase( Method.HTML ) ) 988 return "text/html"; 989 if ( method.equalsIgnoreCase( Method.XHTML ) ) 990 return "text/html"; 991 if ( method.equalsIgnoreCase( Method.TEXT ) ) 992 return "text/plain"; 993 if ( method.equalsIgnoreCase( Method.FOP ) ) 994 return "application/pdf"; 995 return null; 996 } 997 998 999 } 1000 1001 | Popular Tags |