1 23 package org.enhydra.xml.io; 24 25 import org.enhydra.xml.xmlc.codegen.JavaCode; 26 import org.enhydra.xml.xmlc.codegen.JavaLang; 27 28 53 public final class OutputOptions { 54 57 60 public static class Format { 61 62 private final String fName; 63 64 65 Format(String name) { 66 fName = name; 67 } 68 69 70 public String toString() { 71 return fName; 72 } 73 } 74 75 79 public static final Format FORMAT_AUTO = new Format("FORMAT_AUTO"); 80 81 84 public static final Format FORMAT_HTML = new Format("FORMAT_HTML"); 85 86 89 public static final Format FORMAT_XML = new Format("FORMAT_XML"); 90 91 94 private Format fFormat = FORMAT_AUTO; 95 96 99 private String fEncoding = null; 100 101 104 private boolean fPrettyPrinting = false; 105 106 109 private int fIndentSize = 4; 110 111 115 private boolean fPreserveSpace = true; 116 117 120 private boolean fOmitXMLHeader = false; 121 122 125 private boolean fOmitDocType = false; 126 127 130 private boolean fOmitEncoding = false; 131 132 135 private boolean fDropHtmlSpanIds = false; 136 137 140 private boolean fOmitAttributeCharEntityRefs = false; 141 142 145 private URLRewriter fURLRewriter; 146 147 150 private String fPublicId; 151 152 155 private String fSystemId; 156 157 160 private String fMIMEType; 161 162 167 private boolean fEnableXHTMLCompatibility = false; 168 169 172 private boolean fUseAposEntity = true; 173 174 177 private boolean fReadOnly = false; 178 179 183 private boolean fUseHTML4Entities = false; 184 185 188 public OutputOptions() { 189 } 190 191 195 public OutputOptions(OutputOptions src) { 196 fFormat = src.fFormat; 197 fEncoding = src.fEncoding; 198 fPrettyPrinting = src.fPrettyPrinting; 199 fIndentSize = src.fIndentSize; 200 fPreserveSpace = src.fPreserveSpace; 201 fOmitXMLHeader = src.fOmitXMLHeader; 202 fOmitDocType = src.fOmitDocType; 203 fOmitEncoding = src.fOmitEncoding; 204 fDropHtmlSpanIds = src.fDropHtmlSpanIds; 205 fOmitAttributeCharEntityRefs = src.fOmitAttributeCharEntityRefs; 206 fURLRewriter = src.fURLRewriter; 207 fPublicId = src.fPublicId; 208 fSystemId = src.fSystemId; 209 fMIMEType = src.fMIMEType; 210 fUseHTML4Entities = src.fUseHTML4Entities; 211 fEnableXHTMLCompatibility = src.fEnableXHTMLCompatibility; 212 fUseAposEntity = src.fUseAposEntity; 213 } 214 215 218 private void readOnlyCheck() { 219 if (fReadOnly) { 220 throw new XMLIOError(OutputOptions.class.getName() + " object is marked as read-only"); 221 } 222 } 223 224 230 public void markReadOnly() { 231 fReadOnly = true; 232 } 233 234 241 public void setFormat(Format format) { 242 readOnlyCheck(); 243 fFormat = format; 244 } 245 246 251 public Format getFormat() { 252 return fFormat; 253 } 254 255 260 public String getEncoding() { 261 return fEncoding; 262 } 263 264 269 public void setEncoding(String encoding) { 270 readOnlyCheck(); 271 fEncoding = encoding; 272 } 273 274 280 public String getMIMEEncoding() { 281 if (fEncoding != null) { 282 String mimeEncoding = Encodings.getEncodings().getMIMEPreferred(fEncoding); 283 if (mimeEncoding != null) { 284 return mimeEncoding; 285 } 286 } 287 return fEncoding; 288 } 289 290 295 public boolean getUseAposEntity() { 296 return fUseAposEntity; 297 } 298 299 310 public void setUseAposEntity(boolean enable) { 311 readOnlyCheck(); 312 fUseAposEntity = enable; 313 } 314 315 321 public boolean getEnableXHTMLCompatibility() { 322 return fEnableXHTMLCompatibility; 323 } 324 325 350 public void setEnableXHTMLCompatibility(boolean enable) { 351 readOnlyCheck(); 352 fEnableXHTMLCompatibility = enable; 353 } 354 355 360 public boolean getPrettyPrinting() { 361 return fPrettyPrinting; 362 } 363 364 369 public void setPrettyPrinting(boolean enable) { 370 readOnlyCheck(); 371 fPrettyPrinting = enable; 372 } 373 374 379 public int getIndentSize() { 380 return fIndentSize; 381 } 382 383 388 public void setIndentSize(int size) { 389 readOnlyCheck(); 390 fIndentSize = size; 391 if (fIndentSize < 0) { 392 fIndentSize = 0; 393 } 394 } 395 396 402 public boolean getPreserveSpace() { 403 return fPreserveSpace; 404 } 405 406 412 public void setPreserveSpace(boolean preserve) { 413 readOnlyCheck(); 414 fPreserveSpace = preserve; 415 } 416 417 422 public boolean getOmitXMLHeader() { 423 return fOmitXMLHeader; 424 } 425 426 431 public void setOmitXMLHeader(boolean omit) { 432 readOnlyCheck(); 433 fOmitXMLHeader = omit; 434 } 435 436 441 public boolean getOmitDocType() { 442 return fOmitDocType; 443 } 444 445 450 public void setOmitDocType(boolean omit) { 451 readOnlyCheck(); 452 fOmitDocType = omit; 453 } 454 455 460 public boolean getOmitEncoding() { 461 return fOmitEncoding; 462 } 463 464 471 public void setOmitEncoding(boolean omit) { 472 readOnlyCheck(); 473 fOmitEncoding = omit; 474 } 475 476 481 public boolean getDropHtmlSpanIds() { 482 return fDropHtmlSpanIds; 483 } 484 485 496 public void setDropHtmlSpanIds(boolean drop) { 497 readOnlyCheck(); 498 fDropHtmlSpanIds = drop; 499 } 500 501 507 public boolean getOmitAttributeCharEntityRefs() { 508 return fOmitAttributeCharEntityRefs; 509 } 510 511 529 public void setOmitAttributeCharEntityRefs(boolean omit) { 530 fOmitAttributeCharEntityRefs = omit; 531 } 532 533 546 public void setURLRewriter(URLRewriter urlRewriter) { 547 readOnlyCheck(); 548 fURLRewriter = urlRewriter; 549 } 550 551 557 public URLRewriter getURLRewriter() { 558 return fURLRewriter; 559 } 560 561 566 public String getPublicId() { 567 return fPublicId; 568 } 569 570 576 public void setPublicId(String publicId) { 577 readOnlyCheck(); 578 fPublicId = publicId; 579 } 580 581 586 public String getSystemId() { 587 return fSystemId; 588 } 589 590 596 public void setSystemId(String systemId) { 597 readOnlyCheck(); 598 fSystemId = systemId; 599 } 600 601 606 public String getMIMEType() { 607 return fMIMEType; 608 } 609 610 618 public void setMIMEType(String mimeType) { 619 readOnlyCheck(); 620 fMIMEType = mimeType; 621 } 622 623 626 private void genSet(String varName, 627 JavaCode code, 628 String setMethod, 629 String value) { 630 code.addln(varName + "." + setMethod + "(" 631 + JavaLang.createStringConst(value) + ");"); 632 } 633 634 637 private void genSet(String varName, 638 JavaCode code, 639 String setMethod, 640 boolean value) { 641 code.addln(varName + "." + setMethod + "(" 642 + (value ? "true" : "false") + ");"); 643 } 644 645 648 private void genSet(String varName, 649 JavaCode code, 650 String setMethod, 651 int value) { 652 code.addln(varName + "." + setMethod + "(" 653 + Integer.toString(value) + ");"); 654 } 655 656 671 public void createCodeGenerator(String varName, 672 boolean makeReadOnly, 673 JavaCode code) { 674 code.addln(varName + " = new " + OutputOptions.class.getName() + "();"); 676 code.addln(varName + ".setFormat(" 677 + OutputOptions.class.getName() 678 + "." + fFormat.toString() + ");"); 679 680 genSet(varName, code, "setEncoding", fEncoding); 682 genSet(varName, code, "setPrettyPrinting", fPrettyPrinting); 683 genSet(varName, code, "setEnableXHTMLCompatibility", fEnableXHTMLCompatibility); 684 genSet(varName, code, "setUseAposEntity", fUseAposEntity); 685 genSet(varName, code, "setIndentSize", fIndentSize); 686 genSet(varName, code, "setPreserveSpace", fPreserveSpace); 687 genSet(varName, code, "setOmitXMLHeader", fOmitXMLHeader); 688 genSet(varName, code, "setOmitDocType", fOmitDocType); 689 genSet(varName, code, "setOmitEncoding", fOmitEncoding); 690 genSet(varName, code, "setDropHtmlSpanIds", fDropHtmlSpanIds); 691 genSet(varName, code, "setOmitAttributeCharEntityRefs", fOmitAttributeCharEntityRefs); 692 genSet(varName, code, "setPublicId", fPublicId); 693 genSet(varName, code, "setSystemId", fSystemId); 694 genSet(varName, code, "setMIMEType", fMIMEType); 695 if (makeReadOnly) { 696 code.addln(varName + ".markReadOnly();"); 697 } 698 } 699 700 703 public String toString() { 704 return "readOnly=" + fReadOnly 705 + ", format=" + fFormat 706 + ", encoding=" + fEncoding 707 + ", prettyPrinting=" + fPrettyPrinting 708 + ", useAposEntity=" + fUseAposEntity 709 + ", enableXHTMLCompatibility=" + fEnableXHTMLCompatibility 710 + ", indentSize=" + fIndentSize 711 + ", preserveSpace=" + fPreserveSpace 712 + ", omitXMLHeader=" + fOmitXMLHeader 713 + ", omitDocType=" + fOmitDocType 714 + ", omitEncoding=" + fOmitEncoding 715 + ", dropHtmlSpanIds=" + fDropHtmlSpanIds 716 + ", omitAttributeCharEntityRefs=" + fOmitAttributeCharEntityRefs 717 + ", urlRewriter=" + fURLRewriter 718 + ", publicId=" + fPublicId 719 + ", systemId=" + fSystemId 720 + ", mimeType=" + fMIMEType; 721 } 722 723 730 public String getJavaEncoding() { 731 return getEncoding(); 732 } 733 734 740 public void setJavaEncoding(String newJavaEncoding) { 741 setEncoding(newJavaEncoding); 742 } 743 744 750 public String getXmlEncoding() { 751 return getMIMEEncoding(); 752 } 753 754 759 public void setXmlEncoding(String newXmlEncoding) { 760 setEncoding(newXmlEncoding); 761 } 762 763 769 public boolean getUseHTML4Entities() { 770 return fUseHTML4Entities; 771 } 772 773 779 public void setUseHTML4Entities(boolean useHTML4Entities) { 780 fUseHTML4Entities = useHTML4Entities; 781 } 782 783 } 784 | Popular Tags |