1 17 18 22 23 package org.apache.geronimo.system.configuration; 24 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.DocumentType ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.html.HTMLDocument; 29 30 31 53 public class OutputFormat 54 { 55 56 57 public static class DTD 58 { 59 60 63 public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN"; 64 65 68 public static final String HTMLSystemId = 69 "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; 70 71 74 public static final String XHTMLPublicId = 75 "-//W3C//DTD XHTML 1.0 Strict//EN"; 76 77 80 public static final String XHTMLSystemId = 81 "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; 82 83 } 84 85 86 public static class Defaults 87 { 88 89 95 public static final int Indent = 4; 96 97 102 public static final String Encoding = "UTF-8"; 103 104 108 public static final int LineWidth = 72; 109 110 } 111 112 113 117 private String method; 118 119 120 123 private String version; 124 125 126 130 private int indent = 0; 131 132 133 137 private String encoding = Defaults.Encoding; 138 139 142 private EncodingInfo encodingInfo = null; 143 144 147 private String mediaType; 148 149 150 153 private String doctypeSystem; 154 155 156 159 private String doctypePublic; 160 161 162 165 private boolean omitXmlDeclaration = false; 166 167 168 171 private boolean omitDoctype = false; 172 173 174 177 private boolean omitComments = false; 178 179 180 183 private boolean standalone = false; 184 185 186 190 private String [] cdataElements; 191 192 193 197 private String [] nonEscapingElements; 198 199 200 203 private String lineSeparator = "\n"; 204 205 206 209 private int _lineWidth = Defaults.LineWidth; 210 211 212 216 private boolean preserve = false; 217 218 222 private boolean preserveEmptyAttributes = false; 223 224 227 public OutputFormat() 228 { 229 } 230 231 232 245 public OutputFormat( String method, String encoding, boolean indenting ) 246 { 247 setMethod( method ); 248 setEncoding( encoding ); 249 setIndenting( indenting ); 250 } 251 252 253 261 public OutputFormat( Document doc ) 262 { 263 setMethod( whichMethod( doc ) ); 264 setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) ); 265 setMediaType( whichMediaType( getMethod() ) ); 266 } 267 268 269 283 public OutputFormat( Document doc, String encoding, boolean indenting ) 284 { 285 this( doc ); 286 setEncoding( encoding ); 287 setIndenting( indenting ); 288 } 289 290 291 301 public String getMethod() 302 { 303 return method; 304 } 305 306 307 313 public void setMethod( String method ) 314 { 315 this.method = method; 316 } 317 318 319 328 public String getVersion() 329 { 330 return version; 331 } 332 333 334 342 public void setVersion( String version ) 343 { 344 this.version = version; 345 } 346 347 348 356 public int getIndent() 357 { 358 return indent; 359 } 360 361 362 365 public boolean getIndenting() 366 { 367 return ( indent > 0 ); 368 } 369 370 371 379 public void setIndent( int indent ) 380 { 381 if ( indent < 0 ) 382 this.indent = 0; 383 else 384 this.indent = indent; 385 } 386 387 388 397 public void setIndenting( boolean on ) 398 { 399 if ( on ) { 400 indent = Defaults.Indent; 401 _lineWidth = Defaults.LineWidth; 402 } else { 403 indent = 0; 404 _lineWidth = 0; 405 } 406 } 407 408 409 415 public String getEncoding() 416 { 417 return encoding; 418 } 419 420 421 430 public void setEncoding( String encoding ) 431 { 432 this.encoding = encoding; 433 encodingInfo = null; 434 } 435 436 440 public void setEncoding(EncodingInfo encInfo) { 441 encoding = encInfo.getName(); 442 encodingInfo = encInfo; 443 } 444 445 450 public EncodingInfo getEncodingInfo() { 451 if (encodingInfo == null) 452 encodingInfo = Encodings.getEncodingInfo(encoding); 453 return encodingInfo; 454 } 455 456 463 public String getMediaType() 464 { 465 return mediaType; 466 } 467 468 469 475 public void setMediaType( String mediaType ) 476 { 477 this.mediaType = mediaType; 478 } 479 480 481 492 public void setDoctype( String publicId, String systemId ) 493 { 494 doctypePublic = publicId; 495 doctypeSystem = systemId; 496 } 497 498 499 503 public String getDoctypePublic() 504 { 505 return doctypePublic; 506 } 507 508 509 513 public String getDoctypeSystem() 514 { 515 return doctypeSystem; 516 } 517 518 519 523 public boolean getOmitComments() 524 { 525 return omitComments; 526 } 527 528 529 534 public void setOmitComments( boolean omit ) 535 { 536 omitComments = omit; 537 } 538 539 540 544 public boolean getOmitDocumentType() 545 { 546 return omitDoctype; 547 } 548 549 550 555 public void setOmitDocumentType( boolean omit ) 556 { 557 omitDoctype = omit; 558 } 559 560 561 565 public boolean getOmitXMLDeclaration() 566 { 567 return omitXmlDeclaration; 568 } 569 570 571 576 public void setOmitXMLDeclaration( boolean omit ) 577 { 578 omitXmlDeclaration = omit; 579 } 580 581 582 586 public boolean getStandalone() 587 { 588 return standalone; 589 } 590 591 592 599 public void setStandalone( boolean standalone ) 600 { 601 this.standalone = standalone; 602 } 603 604 605 610 public String [] getCDataElements() 611 { 612 return cdataElements; 613 } 614 615 616 623 public boolean isCDataElement( String tagName ) 624 { 625 int i; 626 627 if ( cdataElements == null ) 628 return false; 629 for ( i = 0 ; i < cdataElements.length ; ++i ) 630 if ( cdataElements[ i ].equals( tagName ) ) 631 return true; 632 return false; 633 } 634 635 636 642 public void setCDataElements( String [] cdataElements ) 643 { 644 this.cdataElements = cdataElements; 645 } 646 647 648 653 public String [] getNonEscapingElements() 654 { 655 return nonEscapingElements; 656 } 657 658 659 666 public boolean isNonEscapingElement( String tagName ) 667 { 668 int i; 669 670 if ( nonEscapingElements == null ) 671 return false; 672 for ( i = 0 ; i < nonEscapingElements.length ; ++i ) 673 if ( nonEscapingElements[ i ].equals( tagName ) ) 674 return true; 675 return false; 676 } 677 678 679 685 public void setNonEscapingElements( String [] nonEscapingElements ) 686 { 687 this.nonEscapingElements = nonEscapingElements; 688 } 689 690 691 692 699 public String getLineSeparator() 700 { 701 return lineSeparator; 702 } 703 704 705 714 public void setLineSeparator( String lineSeparator ) 715 { 716 if ( lineSeparator == null ) 717 this.lineSeparator = "\n"; 718 else 719 this.lineSeparator = lineSeparator; 720 } 721 722 723 730 public boolean getPreserveSpace() 731 { 732 return preserve; 733 } 734 735 736 743 public void setPreserveSpace( boolean preserve ) 744 { 745 this.preserve = preserve; 746 } 747 748 749 755 public int getLineWidth() 756 { 757 return _lineWidth; 758 } 759 760 761 770 public void setLineWidth( int lineWidth ) 771 { 772 if ( lineWidth <= 0 ) 773 _lineWidth = 0; 774 else 775 _lineWidth = lineWidth; 776 } 777 778 784 public boolean getPreserveEmptyAttributes () { 785 return preserveEmptyAttributes; 786 } 787 793 public void setPreserveEmptyAttributes (boolean preserve) { 794 preserveEmptyAttributes = preserve; 795 } 796 797 802 public char getLastPrintable() 803 { 804 if ( getEncoding() != null && 805 ( getEncoding().equalsIgnoreCase( "ASCII" ) ) ) 806 return 0xFF; 807 else 808 return 0xFFFF; 809 } 810 811 812 823 public static String whichMethod( Document doc ) 824 { 825 Node node; 826 String value; 827 int i; 828 829 if ( doc instanceof HTMLDocument ) 832 return Method.HTML; 833 834 838 840 node = doc.getFirstChild(); 841 while (node != null) { 842 if ( node.getNodeType() == Node.ELEMENT_NODE ) { 844 if ( node.getNodeName().equalsIgnoreCase( "html" ) ) { 845 return Method.HTML; 846 } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) { 847 return Method.FOP; 848 } else { 849 return Method.XML; 850 } 851 } else if ( node.getNodeType() == Node.TEXT_NODE ) { 852 value = node.getNodeValue(); 856 for ( i = 0 ; i < value.length() ; ++i ) 857 if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A && 858 value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D ) 859 return Method.XML; 860 } 861 node = node.getNextSibling(); 862 } 863 return Method.XML; 865 } 866 867 868 872 public static String whichDoctypePublic( Document doc ) 873 { 874 DocumentType doctype; 875 876 877 doctype = doc.getDoctype(); 878 if ( doctype != null ) { 879 try { 882 return doctype.getPublicId(); 883 } catch ( Error except ) { } 884 } 885 886 if ( doc instanceof HTMLDocument ) 887 return DTD.XHTMLPublicId; 888 return null; 889 } 890 891 892 896 public static String whichDoctypeSystem( Document doc ) 897 { 898 DocumentType doctype; 899 900 901 doctype = doc.getDoctype(); 902 if ( doctype != null ) { 903 try { 906 return doctype.getSystemId(); 907 } catch ( Error except ) { } 908 } 909 910 if ( doc instanceof HTMLDocument ) 911 return DTD.XHTMLSystemId; 912 return null; 913 } 914 915 916 920 public static String whichMediaType( String method ) 921 { 922 if ( method.equalsIgnoreCase( Method.XML ) ) 923 return "text/xml"; 924 if ( method.equalsIgnoreCase( Method.HTML ) ) 925 return "text/html"; 926 if ( method.equalsIgnoreCase( Method.XHTML ) ) 927 return "text/html"; 928 if ( method.equalsIgnoreCase( Method.TEXT ) ) 929 return "text/plain"; 930 if ( method.equalsIgnoreCase( Method.FOP ) ) 931 return "application/pdf"; 932 return null; 933 } 934 935 936 } 937 938 | Popular Tags |