1 16 17 18 25 26 package org.apache.xml.serialize; 27 28 29 import java.io.UnsupportedEncodingException ; 30 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.DocumentType ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.html.HTMLDocument; 35 36 37 60 public class OutputFormat 61 { 62 63 64 public static class DTD 65 { 66 67 70 public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN"; 71 72 75 public static final String HTMLSystemId = 76 "http://www.w3.org/TR/html4/strict.dtd"; 77 78 81 public static final String XHTMLPublicId = 82 "-//W3C//DTD XHTML 1.0 Strict//EN"; 83 84 87 public static final String XHTMLSystemId = 88 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; 89 90 } 91 92 93 public static class Defaults 94 { 95 96 102 public static final int Indent = 4; 103 104 109 public static final String Encoding = "UTF-8"; 110 111 115 public static final int LineWidth = 72; 116 117 } 118 119 120 124 private String _method; 125 126 127 130 private String _version; 131 132 133 137 private int _indent = 0; 138 139 140 144 private String _encoding = Defaults.Encoding; 145 146 149 private EncodingInfo _encodingInfo = null; 150 151 private boolean _allowJavaNames = false; 153 154 157 private String _mediaType; 158 159 160 163 private String _doctypeSystem; 164 165 166 169 private String _doctypePublic; 170 171 172 175 private boolean _omitXmlDeclaration = false; 176 177 178 181 private boolean _omitDoctype = false; 182 183 184 187 private boolean _omitComments = false; 188 189 190 193 private boolean _stripComments = false; 194 195 196 199 private boolean _standalone = false; 200 201 202 206 private String [] _cdataElements; 207 208 209 213 private String [] _nonEscapingElements; 214 215 216 219 private String _lineSeparator = LineSeparator.Web; 220 221 222 225 private int _lineWidth = Defaults.LineWidth; 226 227 228 232 private boolean _preserve = false; 233 237 private boolean _preserveEmptyAttributes = false; 238 239 242 public OutputFormat() 243 { 244 } 245 246 247 260 public OutputFormat( String method, String encoding, boolean indenting ) 261 { 262 setMethod( method ); 263 setEncoding( encoding ); 264 setIndenting( indenting ); 265 } 266 267 268 276 public OutputFormat( Document doc ) 277 { 278 setMethod( whichMethod( doc ) ); 279 setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) ); 280 setMediaType( whichMediaType( getMethod() ) ); 281 } 282 283 284 298 public OutputFormat( Document doc, String encoding, boolean indenting ) 299 { 300 this( doc ); 301 setEncoding( encoding ); 302 setIndenting( indenting ); 303 } 304 305 306 316 public String getMethod() 317 { 318 return _method; 319 } 320 321 322 328 public void setMethod( String method ) 329 { 330 _method = method; 331 } 332 333 334 343 public String getVersion() 344 { 345 return _version; 346 } 347 348 349 357 public void setVersion( String version ) 358 { 359 _version = version; 360 } 361 362 363 371 public int getIndent() 372 { 373 return _indent; 374 } 375 376 377 380 public boolean getIndenting() 381 { 382 return ( _indent > 0 ); 383 } 384 385 386 394 public void setIndent( int indent ) 395 { 396 if ( indent < 0 ) 397 _indent = 0; 398 else 399 _indent = indent; 400 } 401 402 403 412 public void setIndenting( boolean on ) 413 { 414 if ( on ) { 415 _indent = Defaults.Indent; 416 _lineWidth = Defaults.LineWidth; 417 } else { 418 _indent = 0; 419 _lineWidth = 0; 420 } 421 } 422 423 424 430 public String getEncoding() 431 { 432 return _encoding; 433 } 434 435 436 445 public void setEncoding( String encoding ) 446 { 447 _encoding = encoding; 448 _encodingInfo = null; 449 } 450 451 455 public void setEncoding(EncodingInfo encInfo) { 456 _encoding = encInfo.getIANAName(); 457 _encodingInfo = encInfo; 458 } 459 460 465 public EncodingInfo getEncodingInfo() throws UnsupportedEncodingException { 466 if (_encodingInfo == null) 467 _encodingInfo = Encodings.getEncodingInfo(_encoding, _allowJavaNames); 468 return _encodingInfo; 469 } 470 471 474 public void setAllowJavaNames (boolean allow) { 475 _allowJavaNames = allow; 476 } 477 478 481 public boolean setAllowJavaNames () { 482 return _allowJavaNames; 483 } 484 485 492 public String getMediaType() 493 { 494 return _mediaType; 495 } 496 497 498 504 public void setMediaType( String mediaType ) 505 { 506 _mediaType = mediaType; 507 } 508 509 510 521 public void setDoctype( String publicId, String systemId ) 522 { 523 _doctypePublic = publicId; 524 _doctypeSystem = systemId; 525 } 526 527 528 532 public String getDoctypePublic() 533 { 534 return _doctypePublic; 535 } 536 537 538 542 public String getDoctypeSystem() 543 { 544 return _doctypeSystem; 545 } 546 547 548 552 public boolean getOmitComments() 553 { 554 return _omitComments; 555 } 556 557 558 563 public void setOmitComments( boolean omit ) 564 { 565 _omitComments = omit; 566 } 567 568 569 573 public boolean getOmitDocumentType() 574 { 575 return _omitDoctype; 576 } 577 578 579 584 public void setOmitDocumentType( boolean omit ) 585 { 586 _omitDoctype = omit; 587 } 588 589 590 594 public boolean getOmitXMLDeclaration() 595 { 596 return _omitXmlDeclaration; 597 } 598 599 600 605 public void setOmitXMLDeclaration( boolean omit ) 606 { 607 _omitXmlDeclaration = omit; 608 } 609 610 611 615 public boolean getStandalone() 616 { 617 return _standalone; 618 } 619 620 621 628 public void setStandalone( boolean standalone ) 629 { 630 _standalone = standalone; 631 } 632 633 634 639 public String [] getCDataElements() 640 { 641 return _cdataElements; 642 } 643 644 645 652 public boolean isCDataElement( String tagName ) 653 { 654 int i; 655 656 if ( _cdataElements == null ) 657 return false; 658 for ( i = 0 ; i < _cdataElements.length ; ++i ) 659 if ( _cdataElements[ i ].equals( tagName ) ) 660 return true; 661 return false; 662 } 663 664 665 671 public void setCDataElements( String [] cdataElements ) 672 { 673 _cdataElements = cdataElements; 674 } 675 676 677 682 public String [] getNonEscapingElements() 683 { 684 return _nonEscapingElements; 685 } 686 687 688 695 public boolean isNonEscapingElement( String tagName ) 696 { 697 int i; 698 699 if ( _nonEscapingElements == null ) { 700 return false; 701 } 702 for ( i = 0 ; i < _nonEscapingElements.length ; ++i ) 703 if ( _nonEscapingElements[ i ].equals( tagName ) ) 704 return true; 705 return false; 706 } 707 708 709 715 public void setNonEscapingElements( String [] nonEscapingElements ) 716 { 717 _nonEscapingElements = nonEscapingElements; 718 } 719 720 721 722 729 public String getLineSeparator() 730 { 731 return _lineSeparator; 732 } 733 734 735 744 public void setLineSeparator( String lineSeparator ) 745 { 746 if ( lineSeparator == null ) 747 _lineSeparator = LineSeparator.Web; 748 else 749 _lineSeparator = lineSeparator; 750 } 751 752 753 760 public boolean getPreserveSpace() 761 { 762 return _preserve; 763 } 764 765 766 773 public void setPreserveSpace( boolean preserve ) 774 { 775 _preserve = preserve; 776 } 777 778 779 785 public int getLineWidth() 786 { 787 return _lineWidth; 788 } 789 790 791 800 public void setLineWidth( int lineWidth ) 801 { 802 if ( lineWidth <= 0 ) 803 _lineWidth = 0; 804 else 805 _lineWidth = lineWidth; 806 } 807 public boolean getPreserveEmptyAttributes () { return _preserveEmptyAttributes; } public void setPreserveEmptyAttributes (boolean preserve) { _preserveEmptyAttributes = preserve; } 818 819 824 public char getLastPrintable() 825 { 826 if ( getEncoding() != null && 827 ( getEncoding().equalsIgnoreCase( "ASCII" ) ) ) 828 return 0xFF; 829 else 830 return 0xFFFF; 831 } 832 833 834 845 public static String whichMethod( Document doc ) 846 { 847 Node node; 848 String value; 849 int i; 850 851 if ( doc instanceof HTMLDocument ) 854 return Method.HTML; 855 856 860 862 node = doc.getFirstChild(); 863 while (node != null) { 864 if ( node.getNodeType() == Node.ELEMENT_NODE ) { 866 if ( node.getNodeName().equalsIgnoreCase( "html" ) ) { 867 return Method.HTML; 868 } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) { 869 return Method.FOP; 870 } else { 871 return Method.XML; 872 } 873 } else if ( node.getNodeType() == Node.TEXT_NODE ) { 874 value = node.getNodeValue(); 878 for ( i = 0 ; i < value.length() ; ++i ) 879 if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A && 880 value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D ) 881 return Method.XML; 882 } 883 node = node.getNextSibling(); 884 } 885 return Method.XML; 887 } 888 889 890 894 public static String whichDoctypePublic( Document doc ) 895 { 896 DocumentType doctype; 897 898 899 doctype = doc.getDoctype(); 900 if ( doctype != null ) { 901 try { 904 return doctype.getPublicId(); 905 } catch ( Error except ) { } 906 } 907 908 if ( doc instanceof HTMLDocument ) 909 return DTD.XHTMLPublicId; 910 return null; 911 } 912 913 914 918 public static String whichDoctypeSystem( Document doc ) 919 { 920 DocumentType doctype; 921 922 923 doctype = doc.getDoctype(); 924 if ( doctype != null ) { 925 try { 928 return doctype.getSystemId(); 929 } catch ( Error except ) { } 930 } 931 932 if ( doc instanceof HTMLDocument ) 933 return DTD.XHTMLSystemId; 934 return null; 935 } 936 937 938 942 public static String whichMediaType( String method ) 943 { 944 if ( method.equalsIgnoreCase( Method.XML ) ) 945 return "text/xml"; 946 if ( method.equalsIgnoreCase( Method.HTML ) ) 947 return "text/html"; 948 if ( method.equalsIgnoreCase( Method.XHTML ) ) 949 return "text/html"; 950 if ( method.equalsIgnoreCase( Method.TEXT ) ) 951 return "text/plain"; 952 if ( method.equalsIgnoreCase( Method.FOP ) ) 953 return "application/pdf"; 954 return null; 955 } 956 957 958 } 959 960 | Popular Tags |