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