KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > template > PrettyPrintWriter


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.template;
6
7 import java.io.OutputStream JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.io.Writer JavaDoc;
10
11 /**
12  * Extends the PrintWriter class by adding the possibility of emiting empty lines.
13  *
14  * @author Andreas "Mad" Schaefer (andreas.schaefer@madplane.com)
15  * @author Ara Abrahamian (ara_e@email.com)
16  * @created March 7, 2001
17  * @version $Revision: 1.5 $
18  */

19 public class PrettyPrintWriter extends PrintWriter JavaDoc
20 {
21     /**
22      * Convenience variable for printing and matching line separators in a system portable manner.
23      */

24     public final static String JavaDoc LINE_SEPARATOR = "\n";
25     // System.getProperty( "line.separator" );
26
public final static int LINE_SEPARATOR_LEN = LINE_SEPARATOR.length();
27
28     private boolean prevCharWasNewLine = false;
29     private StringBuffer JavaDoc lineBuffer = new StringBuffer JavaDoc();
30     private int lineBufferLength;
31
32     /**
33      * Describe what the PrettyPrintWriter constructor does
34      *
35      * @param pOut Describe what the parameter does
36      */

37     public PrettyPrintWriter(OutputStream JavaDoc pOut)
38     {
39         super(pOut);
40     }
41
42     /**
43      * Describe what the PrettyPrintWriter constructor does
44      *
45      * @param pOut Describe what the parameter does
46      */

47     public PrettyPrintWriter(Writer JavaDoc pOut)
48     {
49         super(pOut);
50     }
51
52
53     /**
54      * Describe what the PrettyPrintWriter constructor does
55      *
56      * @param pOut Describe what the parameter does
57      * @param pAutoFlush Describe what the parameter does
58      */

59     public PrettyPrintWriter(Writer JavaDoc pOut, boolean pAutoFlush)
60     {
61         super(pOut, pAutoFlush);
62     }
63
64     /**
65      * Closes the output stream and writes the last line.
66      */

67     public void close()
68     {
69         if (lineBuffer.length() > 0) {
70             writeLine();
71         }
72
73         super.close();
74     }
75
76     /**
77      * Describe what the method does
78      *
79      * @param pBuffer Describe what the parameter does
80      * @param pOffset Describe what the parameter does
81      * @param pLength Describe what the parameter does
82      */

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     /**
116      * Describe what the method does
117      *
118      * @param pText Describe what the parameter does
119      * @param pOffset Describe what the parameter does
120      * @param pLength Describe what the parameter does
121      */

122     public void write(String JavaDoc 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 if( lineBufferLength == 2 && lineBuffer.charAt( 0 ) == 10 && lineBuffer.charAt( 1 ) == 13 )
144
// {
145
// allSpaces = false;
146
// }
147
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             // if a useful line then write it, otherwise ignore garbage lines
161
char[] lineChars = lineBuffer.toString().toCharArray();
162
163             super.write(lineChars, 0, lineChars.length);
164         }
165         lineBuffer = new StringBuffer JavaDoc();
166
167         // reset line buffer
168
lineBufferLength = 0;
169     }
170 }
171
Popular Tags