1 7 8 package org.dom4j.io; 9 10 19 public class OutputFormat implements Cloneable { 20 21 protected static final String STANDARD_INDENT = " "; 22 23 27 private boolean suppressDeclaration = false; 28 29 33 private boolean newLineAfterDeclaration = true; 34 35 36 private String encoding = "UTF-8"; 37 38 42 private boolean omitEncoding = false; 43 44 45 private String indent = null; 46 47 51 private boolean expandEmptyElements = false; 52 53 57 private boolean newlines = false; 58 59 60 private String lineSeparator = "\n"; 61 62 63 private boolean trimText = false; 64 65 66 private boolean padText = false; 67 68 69 private boolean doXHTML = false; 70 71 75 private int newLineAfterNTags = 0; 77 78 private char attributeQuoteChar = '\"'; 79 80 85 public OutputFormat() { 86 } 87 88 96 public OutputFormat(String indent) { 97 this.indent = indent; 98 } 99 100 111 public OutputFormat(String indent, boolean newlines) { 112 this.indent = indent; 113 this.newlines = newlines; 114 } 115 116 128 public OutputFormat(String indent, boolean newlines, String encoding) { 129 this.indent = indent; 130 this.newlines = newlines; 131 this.encoding = encoding; 132 } 133 134 public String getLineSeparator() { 135 return lineSeparator; 136 } 137 138 151 public void setLineSeparator(String separator) { 152 lineSeparator = separator; 153 } 154 155 public boolean isNewlines() { 156 return newlines; 157 } 158 159 168 public void setNewlines(boolean newlines) { 169 this.newlines = newlines; 170 } 171 172 public String getEncoding() { 173 return encoding; 174 } 175 176 182 public void setEncoding(String encoding) { 183 if (encoding != null) { 184 this.encoding = encoding; 185 } 186 } 187 188 public boolean isOmitEncoding() { 189 return omitEncoding; 190 } 191 192 204 public void setOmitEncoding(boolean omitEncoding) { 205 this.omitEncoding = omitEncoding; 206 } 207 208 220 public void setSuppressDeclaration(boolean suppressDeclaration) { 221 this.suppressDeclaration = suppressDeclaration; 222 } 223 224 231 public boolean isSuppressDeclaration() { 232 return suppressDeclaration; 233 } 234 235 245 public void setNewLineAfterDeclaration(boolean newLineAfterDeclaration) { 246 this.newLineAfterDeclaration = newLineAfterDeclaration; 247 } 248 249 254 public boolean isNewLineAfterDeclaration() { 255 return newLineAfterDeclaration; 256 } 257 258 public boolean isExpandEmptyElements() { 259 return expandEmptyElements; 260 } 261 262 273 public void setExpandEmptyElements(boolean expandEmptyElements) { 274 this.expandEmptyElements = expandEmptyElements; 275 } 276 277 public boolean isTrimText() { 278 return trimText; 279 } 280 281 299 public void setTrimText(boolean trimText) { 300 this.trimText = trimText; 301 } 302 303 public boolean isPadText() { 304 return padText; 305 } 306 307 331 public void setPadText(boolean padText) { 332 this.padText = padText; 333 } 334 335 public String getIndent() { 336 return indent; 337 } 338 339 350 public void setIndent(String indent) { 351 if ((indent != null) && (indent.length() <= 0)) { 353 indent = null; 354 } 355 356 this.indent = indent; 357 } 358 359 366 public void setIndent(boolean doIndent) { 367 if (doIndent) { 368 this.indent = STANDARD_INDENT; 369 } else { 370 this.indent = null; 371 } 372 } 373 374 384 public void setIndentSize(int indentSize) { 385 StringBuffer indentBuffer = new StringBuffer (); 386 387 for (int i = 0; i < indentSize; i++) { 388 indentBuffer.append(" "); 389 } 390 391 this.indent = indentBuffer.toString(); 392 } 393 394 409 public boolean isXHTML() { 410 return doXHTML; 411 } 412 413 430 public void setXHTML(boolean xhtml) { 431 doXHTML = xhtml; 432 } 433 434 public int getNewLineAfterNTags() { 435 return newLineAfterNTags; 436 } 437 438 450 public void setNewLineAfterNTags(int tagCount) { 451 newLineAfterNTags = tagCount; 452 } 453 454 public char getAttributeQuoteCharacter() { 455 return attributeQuoteChar; 456 } 457 458 470 public void setAttributeQuoteCharacter(char quoteChar) { 471 if ((quoteChar == '\'') || (quoteChar == '"')) { 472 attributeQuoteChar = quoteChar; 473 } else { 474 throw new IllegalArgumentException ("Invalid attribute quote " 475 + "character (" + quoteChar + ")"); 476 } 477 } 478 479 490 public int parseOptions(String [] args, int i) { 491 for (int size = args.length; i < size; i++) { 492 if (args[i].equals("-suppressDeclaration")) { 493 setSuppressDeclaration(true); 494 } else if (args[i].equals("-omitEncoding")) { 495 setOmitEncoding(true); 496 } else if (args[i].equals("-indent")) { 497 setIndent(args[++i]); 498 } else if (args[i].equals("-indentSize")) { 499 setIndentSize(Integer.parseInt(args[++i])); 500 } else if (args[i].startsWith("-expandEmpty")) { 501 setExpandEmptyElements(true); 502 } else if (args[i].equals("-encoding")) { 503 setEncoding(args[++i]); 504 } else if (args[i].equals("-newlines")) { 505 setNewlines(true); 506 } else if (args[i].equals("-lineSeparator")) { 507 setLineSeparator(args[++i]); 508 } else if (args[i].equals("-trimText")) { 509 setTrimText(true); 510 } else if (args[i].equals("-padText")) { 511 setPadText(true); 512 } else if (args[i].startsWith("-xhtml")) { 513 setXHTML(true); 514 } else { 515 return i; 516 } 517 } 518 519 return i; 520 } 521 522 529 public static OutputFormat createPrettyPrint() { 530 OutputFormat format = new OutputFormat(); 531 format.setIndentSize(2); 532 format.setNewlines(true); 533 format.setTrimText(true); 534 format.setPadText(true); 535 536 return format; 537 } 538 539 546 public static OutputFormat createCompactFormat() { 547 OutputFormat format = new OutputFormat(); 548 format.setIndent(false); 549 format.setNewlines(false); 550 format.setTrimText(true); 551 552 return format; 553 } 554 } 555 556 592 | Popular Tags |