1 package org.apache.maven.shared.utils.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import org.apache.maven.shared.utils.StringUtils;
24
25
26
27
28
29
30
31 public class XmlWriterUtil
32 {
33
34 public static final String LS = System.getProperty( "line.separator" );
35
36
37 public static final int DEFAULT_INDENTATION_SIZE = 2;
38
39
40 public static final int DEFAULT_COLUMN_LINE = 80;
41
42
43
44
45
46
47
48 public static void writeLineBreak( XMLWriter writer ) throws IOException
49 {
50 writeLineBreak( writer, 1 );
51 }
52
53
54
55
56
57
58
59
60 public static void writeLineBreak( XMLWriter writer, int repeat ) throws IOException
61 {
62 for ( int i = 0; i < repeat; i++ )
63 {
64 writer.writeMarkup( LS );
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public static void writeLineBreak( XMLWriter writer, int repeat, int indent ) throws IOException
79 {
80 writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE );
81 }
82
83
84
85
86
87
88
89
90
91
92 public static void writeLineBreak( XMLWriter writer, int repeat, int indent, int indentSize ) throws IOException
93 {
94 writeLineBreak( writer, repeat );
95
96 if ( indent < 0 )
97 {
98 indent = 0;
99 }
100
101 if ( indentSize < 0 )
102 {
103 indentSize = 0;
104 }
105
106 writer.writeText( StringUtils.repeat( " ", indent * indentSize ) );
107 }
108
109
110
111
112
113
114
115
116
117 public static void writeCommentLineBreak( XMLWriter writer ) throws IOException
118 {
119 writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE );
120 }
121
122
123
124
125
126
127
128
129 public static void writeCommentLineBreak( XMLWriter writer, int columnSize ) throws IOException
130 {
131 if ( columnSize < 10 )
132 {
133 columnSize = DEFAULT_COLUMN_LINE;
134 }
135
136 writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
137 }
138
139
140
141
142
143
144
145
146
147
148
149 public static void writeComment( XMLWriter writer, String comment ) throws IOException
150 {
151 writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165 public static void writeComment( XMLWriter writer, String comment, int indent ) throws IOException
166 {
167 writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize ) throws IOException
183 {
184 writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
199 throws IOException
200 {
201 if ( comment == null )
202 {
203 comment = "null";
204 }
205
206 if ( indent < 0 )
207 {
208 indent = 0;
209 }
210
211 if ( indentSize < 0 )
212 {
213 indentSize = 0;
214 }
215
216 if ( columnSize < 0 )
217 {
218 columnSize = DEFAULT_COLUMN_LINE;
219 }
220
221 String indentation = StringUtils.repeat( " ", indent * indentSize );
222 int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
223 String[] sentences = StringUtils.split( comment, LS );
224
225 StringBuffer line = new StringBuffer( indentation + "<!-- " );
226 for ( String sentence : sentences )
227 {
228 String[] words = StringUtils.split( sentence, " " );
229 for ( String word : words )
230 {
231 StringBuilder sentenceTmp = new StringBuilder( line.toString() );
232 sentenceTmp.append( word ).append( ' ' );
233 if ( sentenceTmp.length() > magicNumber )
234 {
235 if ( line.length() != indentation.length() + "<!-- ".length() )
236 {
237 if ( magicNumber - line.length() > 0 )
238 {
239 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
240 }
241
242 line.append( "-->" ).append( LS );
243 writer.writeMarkup( line.toString() );
244 }
245 line = new StringBuffer( indentation + "<!-- " );
246 line.append( word ).append( ' ' );
247 }
248 else
249 {
250 line.append( word ).append( ' ' );
251 }
252 }
253
254 if ( magicNumber - line.length() > 0 )
255 {
256 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
257 }
258 }
259
260 if ( line.length() <= magicNumber )
261 {
262 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
263 }
264
265 line.append( "-->" ).append( LS );
266
267 writer.writeMarkup( line.toString() );
268 }
269
270
271
272
273
274
275
276
277
278
279
280 public static void writeCommentText( XMLWriter writer, String comment ) throws IOException
281 {
282 writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public static void writeCommentText( XMLWriter writer, String comment, int indent ) throws IOException
298 {
299 writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314 public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize )
315 throws IOException
316 {
317 writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
333 throws IOException
334 {
335 if ( indent < 0 )
336 {
337 indent = 0;
338 }
339
340 if ( indentSize < 0 )
341 {
342 indentSize = 0;
343 }
344
345 if ( columnSize < 0 )
346 {
347 columnSize = DEFAULT_COLUMN_LINE;
348 }
349
350 writeLineBreak( writer, 1 );
351
352 writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
353 writeCommentLineBreak( writer, columnSize );
354
355 writeComment( writer, comment, indent, indentSize, columnSize );
356
357 writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
358 writeCommentLineBreak( writer, columnSize );
359
360 writeLineBreak( writer, 1, indent, indentSize );
361 }
362 }