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