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