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 public static void writeComment( XMLWriter writer, String comment )
141 {
142 writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
143 }
144
145
146
147
148
149
150
151
152
153
154
155 public static void writeComment( XMLWriter writer, String comment, int indent )
156 {
157 writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize )
172 {
173 writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
174 }
175
176
177
178
179
180
181
182
183
184
185 public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
186 {
187 if ( comment == null )
188 {
189 comment = "null";
190 }
191
192 if ( indent < 0 )
193 {
194 indent = 0;
195 }
196
197 if ( indentSize < 0 )
198 {
199 indentSize = 0;
200 }
201
202 if ( columnSize < 0 )
203 {
204 columnSize = DEFAULT_COLUMN_LINE;
205 }
206
207 String indentation = StringUtils.repeat( " ", indent * indentSize );
208 int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
209 String[] sentences = StringUtils.split( comment, LS );
210
211 StringBuffer line = new StringBuffer( indentation + "<!-- " );
212 for ( String sentence : sentences )
213 {
214 String[] words = StringUtils.split( sentence, " " );
215 for ( String word : words )
216 {
217 StringBuilder sentenceTmp = new StringBuilder( line.toString() );
218 sentenceTmp.append( word ).append( ' ' );
219 if ( sentenceTmp.length() > magicNumber )
220 {
221 if ( line.length() != indentation.length() + "<!-- ".length() )
222 {
223 if ( magicNumber - line.length() > 0 )
224 {
225 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
226 }
227
228 line.append( "-->" ).append( LS );
229 writer.writeMarkup( line.toString() );
230 }
231 line = new StringBuffer( indentation + "<!-- " );
232 line.append( word ).append( ' ' );
233 }
234 else
235 {
236 line.append( word ).append( ' ' );
237 }
238 }
239
240 if ( magicNumber - line.length() > 0 )
241 {
242 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
243 }
244 }
245
246 if ( line.length() <= magicNumber )
247 {
248 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
249 }
250
251 line.append( "-->" ).append( LS );
252
253 writer.writeMarkup( line.toString() );
254 }
255
256
257
258
259
260
261
262
263
264
265 public static void writeCommentText( XMLWriter writer, String comment )
266 {
267 writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281 public static void writeCommentText( XMLWriter writer, String comment, int indent )
282 {
283 writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297 public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize )
298 {
299 writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313 public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
314 {
315 if ( indent < 0 )
316 {
317 indent = 0;
318 }
319
320 if ( indentSize < 0 )
321 {
322 indentSize = 0;
323 }
324
325 if ( columnSize < 0 )
326 {
327 columnSize = DEFAULT_COLUMN_LINE;
328 }
329
330 writeLineBreak( writer, 1 );
331
332 writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
333 writeCommentLineBreak( writer, columnSize );
334
335 writeComment( writer, comment, indent, indentSize, columnSize );
336
337 writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
338 writeCommentLineBreak( writer, columnSize );
339
340 writeLineBreak( writer, 1, indent, indentSize );
341 }
342 }