1 package org.apache.maven.doxia.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.IOUtil;
23
24 import java.io.BufferedWriter;
25 import java.io.IOException;
26 import java.io.Writer;
27
28
29
30
31 public class LineBreaker
32 {
33
34 public static final int DEFAULT_MAX_LINE_LENGTH = 78;
35
36
37 private static final String EOL = System.getProperty( "line.separator" );
38
39
40 private Writer destination;
41
42
43 private BufferedWriter writer;
44
45
46 private int maxLineLength;
47
48
49 private int lineLength = 0;
50
51
52 private StringBuilder word = new StringBuilder( 1024 );
53
54
55
56
57
58
59 public LineBreaker( Writer out )
60 {
61 this( out, DEFAULT_MAX_LINE_LENGTH );
62 }
63
64
65
66
67
68
69
70 public LineBreaker( Writer out, int max )
71 {
72 if ( max <= 0 )
73 {
74 throw new IllegalArgumentException( "maxLineLength <= 0" );
75 }
76
77 destination = out;
78 this.maxLineLength = max;
79 writer = new BufferedWriter( out );
80 }
81
82
83
84
85
86
87 public Writer getDestination()
88 {
89 return destination;
90 }
91
92
93
94
95
96
97
98 public void write( String text )
99 throws IOException
100 {
101 write( text, false );
102 }
103
104
105
106
107
108
109
110 public void write( String text, boolean preserveSpace )
111 {
112 int length = text.length();
113
114 try
115 {
116 for ( int i = 0; i < length; ++i )
117 {
118 char c = text.charAt( i );
119
120 switch ( c )
121 {
122 case ' ':
123 if ( preserveSpace )
124 {
125 word.append( c );
126 }
127 else
128 {
129 writeWord();
130 }
131 break;
132
133 case '\r':
134
135 if ( i + 1 < length && text.charAt( i + 1 ) == '\n' )
136 {
137 break;
138 }
139
140 case '\n':
141 writeWord();
142 writer.write( EOL );
143 lineLength = 0;
144 break;
145
146 default:
147 word.append( c );
148 }
149
150 }
151 }
152 catch ( Exception e )
153 {
154
155 }
156 }
157
158
159
160
161
162 public void flush()
163 {
164 try
165 {
166 writeWord();
167 writer.flush();
168 }
169 catch ( IOException e )
170 {
171
172 }
173 }
174
175
176
177
178
179
180 private void writeWord()
181 throws IOException
182 {
183 int length = word.length();
184 if ( length > 0 )
185 {
186 if ( lineLength > 0 )
187 {
188 if ( lineLength + 1 + length > maxLineLength )
189 {
190 writer.write( EOL );
191 lineLength = 0;
192 }
193 else
194 {
195 writer.write( ' ' );
196 ++lineLength;
197 }
198 }
199
200 writer.write( word.toString() );
201 word.setLength( 0 );
202
203 lineLength += length;
204 }
205 }
206
207
208
209
210 public void close()
211 {
212 IOUtil.close( writer );
213 }
214 }