1 16 package com.google.gwt.dev.util; 17 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 import java.util.Arrays ; 21 22 25 public final class DefaultTextOutput implements TextOutput { 26 27 private final boolean compact; 28 private int identLevel = 0; 29 private int indentGranularity = 2; 30 private char[][] indents = new char[][] {new char[0]}; 31 private boolean justNewlined; 32 private final PrintWriter p; 33 private final StringWriter sw; 34 35 public DefaultTextOutput(boolean compact) { 36 this.compact = compact; 37 sw = new StringWriter (); 38 p = new PrintWriter (sw, false); 39 } 40 41 public void indentIn() { 42 ++identLevel; 43 if (identLevel >= indents.length) { 44 char[] newIndentLevel = new char[identLevel * indentGranularity]; 47 Arrays.fill(newIndentLevel, ' '); 48 char[][] newIndents = new char[indents.length + 1][]; 49 System.arraycopy(indents, 0, newIndents, 0, indents.length); 50 newIndents[identLevel] = newIndentLevel; 51 indents = newIndents; 52 } 53 } 54 55 public void indentOut() { 56 --identLevel; 57 } 58 59 public void newline() { 60 if (compact) { 61 p.print('\n'); 62 } else { 63 p.println(); 64 } 65 justNewlined = true; 66 } 67 68 public void newlineOpt() { 69 if (!compact) { 70 p.println(); 71 justNewlined = true; 72 } 73 } 74 75 public void print(char c) { 76 maybeIndent(); 77 p.print(c); 78 justNewlined = false; 79 } 80 81 public void print(char[] s) { 82 maybeIndent(); 83 p.print(s); 84 justNewlined = false; 85 } 86 87 public void print(String s) { 88 maybeIndent(); 89 p.print(s); 90 justNewlined = false; 91 } 92 93 public void printOpt(char c) { 94 if (!compact) { 95 maybeIndent(); 96 p.print(c); 97 } 98 } 99 100 public void printOpt(char[] s) { 101 if (!compact) { 102 maybeIndent(); 103 p.print(s); 104 } 105 } 106 107 public void printOpt(String s) { 108 if (!compact) { 109 maybeIndent(); 110 p.print(s); 111 } 112 } 113 114 public String toString() { 115 p.flush(); 116 return sw.toString(); 117 } 118 119 private void maybeIndent() { 120 if (justNewlined && !compact) { 121 p.print(indents[identLevel]); 122 justNewlined = false; 123 } 124 } 125 } 126 | Popular Tags |