1 5 package xdoclet.template; 6 7 import java.io.OutputStream ; 8 import java.io.PrintWriter ; 9 import java.io.Writer ; 10 11 19 public class PrettyPrintWriter extends PrintWriter 20 { 21 24 public final static String LINE_SEPARATOR = "\n"; 25 public final static int LINE_SEPARATOR_LEN = LINE_SEPARATOR.length(); 27 28 private boolean prevCharWasNewLine = false; 29 private StringBuffer lineBuffer = new StringBuffer (); 30 private int lineBufferLength; 31 32 37 public PrettyPrintWriter(OutputStream pOut) 38 { 39 super(pOut); 40 } 41 42 47 public PrettyPrintWriter(Writer pOut) 48 { 49 super(pOut); 50 } 51 52 53 59 public PrettyPrintWriter(Writer pOut, boolean pAutoFlush) 60 { 61 super(pOut, pAutoFlush); 62 } 63 64 67 public void close() 68 { 69 if (lineBuffer.length() > 0) { 70 writeLine(); 71 } 72 73 super.close(); 74 } 75 76 83 public void write(char pBuffer[], int pOffset, int pLength) 84 { 85 for (int i = pOffset; i < pOffset + pLength; i++) { 86 char c = pBuffer[i]; 87 88 if (c != 13) { 89 if (c == 10) { 90 lineBuffer.append(LINE_SEPARATOR); 91 lineBufferLength += LINE_SEPARATOR_LEN; 92 93 writeLine(); 94 } 95 else { 96 lineBuffer.append(c); 97 lineBufferLength++; 98 } 99 } 100 } 101 } 102 103 public void write(int c) 104 { 105 if (c != 13) { 106 lineBuffer.append(c); 107 lineBufferLength++; 108 109 if (c == 10) { 110 writeLine(); 111 } 112 } 113 } 114 115 122 public void write(String pText, int pOffset, int pLength) 123 { 124 this.write(pText.toCharArray(), pOffset, pLength); 125 } 126 127 public void println() 128 { 129 write(LINE_SEPARATOR); 130 } 131 132 protected void writeLine() 133 { 134 boolean allSpaces = true; 135 char tempChar; 136 137 if (lineBufferLength == 1 && lineBuffer.charAt(0) == 10) { 138 if (prevCharWasNewLine == false) { 139 allSpaces = false; 140 prevCharWasNewLine = true; 141 } 142 } 143 else { 148 for (int j = 0; j < lineBufferLength; j++) { 149 tempChar = lineBuffer.charAt(j); 150 151 if (!Character.isWhitespace(tempChar)) { 152 prevCharWasNewLine = false; 153 allSpaces = false; 154 break; 155 } 156 } 157 } 158 159 if (allSpaces == false) { 160 char[] lineChars = lineBuffer.toString().toCharArray(); 162 163 super.write(lineChars, 0, lineChars.length); 164 } 165 lineBuffer = new StringBuffer (); 166 167 lineBufferLength = 0; 169 } 170 } 171 | Popular Tags |