1 56 57 package org.jdom.output; 58 59 import java.lang.reflect.Method ; 60 61 74 public class Format implements Cloneable { 75 76 private static final String CVS_ID = 77 "@(#) $RCSfile: Format.java,v $ $Revision: 1.10 $ $Date: 2004/09/07 06:37:20 $ $Name: $"; 78 79 88 public static Format getRawFormat() { 89 return new Format(); 90 } 91 92 102 public static Format getPrettyFormat() { 103 Format f = new Format(); 104 f.setIndent(STANDARD_INDENT); 105 f.setTextMode(TextMode.TRIM); 106 return f; 107 } 108 109 118 public static Format getCompactFormat() { 119 Format f = new Format(); 120 f.setTextMode(TextMode.NORMALIZE); 121 return f; 122 } 123 124 125 private static final String STANDARD_INDENT = " "; 126 127 128 private static final String STANDARD_LINE_SEPARATOR = "\r\n"; 129 130 131 private static final String STANDARD_ENCODING = "UTF-8"; 132 133 134 135 String indent = null; 136 137 138 String lineSeparator = STANDARD_LINE_SEPARATOR; 139 140 141 String encoding = STANDARD_ENCODING; 142 143 145 boolean omitDeclaration = false; 146 147 149 boolean omitEncoding = false; 150 151 153 boolean expandEmptyElements = false; 154 155 157 boolean ignoreTrAXEscapingPIs = false; 158 159 160 TextMode mode = TextMode.PRESERVE; 161 162 163 EscapeStrategy escapeStrategy = new DefaultEscapeStrategy(encoding); 164 165 168 private Format() { } 169 170 176 public Format setEscapeStrategy(EscapeStrategy strategy) { 177 escapeStrategy = strategy; 178 return this; 179 } 180 181 186 public EscapeStrategy getEscapeStrategy() { 187 return escapeStrategy; 188 } 189 190 217 public Format setLineSeparator(String separator) { 218 this.lineSeparator = separator; 219 return this; 220 } 221 222 227 public String getLineSeparator() { 228 return lineSeparator; 229 } 230 231 242 public Format setOmitEncoding(boolean omitEncoding) { 243 this.omitEncoding = omitEncoding; 244 return this; 245 } 246 247 252 public boolean getOmitEncoding() { 253 return omitEncoding; 254 } 255 256 266 public Format setOmitDeclaration(boolean omitDeclaration) { 267 this.omitDeclaration = omitDeclaration; 268 return this; 269 } 270 271 276 public boolean getOmitDeclaration() { 277 return omitDeclaration; 278 } 279 280 289 public Format setExpandEmptyElements(boolean expandEmptyElements) { 290 this.expandEmptyElements = expandEmptyElements; 291 return this; 292 } 293 294 299 public boolean getExpandEmptyElements() { 300 return expandEmptyElements; 301 } 302 303 330 public void setIgnoreTrAXEscapingPIs(boolean ignoreTrAXEscapingPIs) { 331 this.ignoreTrAXEscapingPIs = ignoreTrAXEscapingPIs; 332 } 333 334 340 public boolean getIgnoreTrAXEscapingPIs() { 341 return ignoreTrAXEscapingPIs; 342 } 343 344 350 public Format setTextMode(Format.TextMode mode) { 351 this.mode = mode; 352 return this; 353 } 354 355 360 public Format.TextMode getTextMode() { 361 return mode; 362 } 363 364 373 public Format setIndent(String indent) { 374 if ("".equals(indent)) { 378 indent = null; 379 } 380 this.indent = indent; 381 return this; 382 } 383 384 389 public String getIndent() { 390 return indent; 391 } 392 393 401 public Format setEncoding(String encoding) { 402 this.encoding = encoding; 403 escapeStrategy = new DefaultEscapeStrategy(encoding); 404 return this; 405 } 406 407 412 public String getEncoding() { 413 return encoding; 414 } 415 416 protected Object clone() { 417 Format format = null; 418 419 try { 420 format = (Format) super.clone(); 421 } 422 catch (CloneNotSupportedException ce) { 423 } 424 425 return format; 426 } 427 428 429 434 class DefaultEscapeStrategy implements EscapeStrategy { 435 private int bits; 436 Object encoder; 437 Method canEncode; 438 439 public DefaultEscapeStrategy(String encoding) { 440 if ("UTF-8".equalsIgnoreCase(encoding) || 441 "UTF-16".equalsIgnoreCase(encoding)) { 442 bits = 16; 443 } 444 else if ("ISO-8859-1".equalsIgnoreCase(encoding) || 445 "Latin1".equalsIgnoreCase(encoding)) { 446 bits = 8; 447 } 448 else if ("US-ASCII".equalsIgnoreCase(encoding) || 449 "ASCII".equalsIgnoreCase(encoding)) { 450 bits = 7; 451 } 452 else { 453 bits = 0; 454 try { 456 Class charsetClass = Class.forName("java.nio.charset.Charset"); 457 Class encoderClass = Class.forName("java.nio.charset.CharsetEncoder"); 458 Method forName = charsetClass.getMethod("forName", new Class []{String .class}); 459 Object charsetObj = forName.invoke(null, new Object []{encoding}); 460 Method newEncoder = charsetClass.getMethod("newEncoder", null); 461 encoder = newEncoder.invoke(charsetObj, null); 462 canEncode = encoderClass.getMethod("canEncode", new Class []{char.class}); 463 } 464 catch (Exception ignored) { 465 } 466 } 467 } 468 469 public boolean shouldEscape(char ch) { 470 if (bits == 16) { 471 return false; 472 } 473 if (bits == 8) { 474 if ((int) ch > 255) 475 return true; 476 else 477 return false; 478 } 479 if (bits == 7) { 480 if ((int) ch > 127) 481 return true; 482 else 483 return false; 484 } 485 else { 486 if (canEncode != null && encoder != null) { 487 try { 488 Boolean val = (Boolean ) canEncode.invoke(encoder, new Object []{new Character (ch)}); 489 return !val.booleanValue(); 490 } 491 catch (Exception ignored) { 492 } 493 } 494 return false; 498 } 499 } 500 } 501 502 503 573 public static class TextMode { 574 577 public static final TextMode PRESERVE = new TextMode("PRESERVE"); 578 579 582 public static final TextMode TRIM = new TextMode("TRIM"); 583 584 589 public static final TextMode NORMALIZE = new TextMode("NORMALIZE"); 590 591 595 public static final TextMode TRIM_FULL_WHITE = 596 new TextMode("TRIM_FULL_WHITE"); 597 598 private final String name; 599 600 private TextMode(String name) { 601 this.name = name; 602 } 603 604 public String toString() { 605 return name; 606 } 607 } 608 } 609 | Popular Tags |