Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlWriterUtil |
|
| 2.2857142857142856;2.286 |
1 | package org.apache.maven.shared.utils.xml; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import org.apache.maven.shared.utils.StringUtils; | |
23 | ||
24 | /** | |
25 | * Utility class for the <code>XmlWriter</code> class. | |
26 | * | |
27 | * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> | |
28 | * @version $Id$ | |
29 | */ | |
30 | 0 | public class XmlWriterUtil |
31 | { | |
32 | /** The vm line separator */ | |
33 | 1 | public static final String LS = System.getProperty( "line.separator" ); |
34 | ||
35 | /** The default line indenter size i.e. 2. */ | |
36 | public static final int DEFAULT_INDENTATION_SIZE = 2; | |
37 | ||
38 | /** The default column before line wrapping i.e. 80. */ | |
39 | public static final int DEFAULT_COLUMN_LINE = 80; | |
40 | ||
41 | /** | |
42 | * Convenience method to write one <code>CRLF</code>. | |
43 | * | |
44 | * @param writer not null writer | |
45 | */ | |
46 | public static void writeLineBreak( XMLWriter writer ) | |
47 | { | |
48 | 1 | writeLineBreak( writer, 1 ); |
49 | 1 | } |
50 | ||
51 | /** | |
52 | * Convenience method to repeat <code>CRLF</code>. | |
53 | * | |
54 | * @param writer not null | |
55 | * @param repeat positive number | |
56 | */ | |
57 | public static void writeLineBreak( XMLWriter writer, int repeat ) | |
58 | { | |
59 | 51 | for ( int i = 0; i < repeat; i++ ) |
60 | { | |
61 | 39 | writer.writeMarkup( LS ); |
62 | } | |
63 | 12 | } |
64 | ||
65 | /** | |
66 | * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>2</code>. | |
67 | * | |
68 | * @param writer not null | |
69 | * @param repeat The number of repetitions of the indent | |
70 | * @param indent positive number | |
71 | * @see #DEFAULT_INDENTATION_SIZE | |
72 | * @see #writeLineBreak(XMLWriter, int, int, int) | |
73 | */ | |
74 | public static void writeLineBreak( XMLWriter writer, int repeat, int indent ) | |
75 | { | |
76 | 1 | writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE ); |
77 | 1 | } |
78 | ||
79 | /** | |
80 | * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>indentSize</code>. | |
81 | * | |
82 | * @param writer not null | |
83 | * @param repeat The number of repetitions of the indent | |
84 | * @param indent positive number | |
85 | * @param indentSize positive number | |
86 | */ | |
87 | public static void writeLineBreak( XMLWriter writer, int repeat, int indent, int indentSize ) | |
88 | { | |
89 | 6 | writeLineBreak( writer, repeat ); |
90 | ||
91 | 6 | if ( indent < 0 ) |
92 | { | |
93 | 0 | indent = 0; |
94 | } | |
95 | ||
96 | 6 | if ( indentSize < 0 ) |
97 | { | |
98 | 0 | indentSize = 0; |
99 | } | |
100 | ||
101 | 6 | writer.writeText( StringUtils.repeat( " ", indent * indentSize ) ); |
102 | 6 | } |
103 | ||
104 | /** | |
105 | * Convenience method to write XML comment line break. Its size is <code>80</code>. | |
106 | * | |
107 | * @param writer not null | |
108 | * @see #DEFAULT_COLUMN_LINE | |
109 | * @see #writeCommentLineBreak(XMLWriter, int) | |
110 | */ | |
111 | public static void writeCommentLineBreak( XMLWriter writer ) | |
112 | { | |
113 | 1 | writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE ); |
114 | 1 | } |
115 | ||
116 | /** | |
117 | * Convenience method to write XML comment line break with <code>columnSize</code> as length. | |
118 | * | |
119 | * @param writer not null | |
120 | * @param columnSize positive number | |
121 | */ | |
122 | public static void writeCommentLineBreak( XMLWriter writer, int columnSize ) | |
123 | { | |
124 | 11 | if ( columnSize < 10 ) |
125 | { | |
126 | 0 | columnSize = DEFAULT_COLUMN_LINE; |
127 | } | |
128 | ||
129 | 11 | writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS ); |
130 | 11 | } |
131 | ||
132 | /** | |
133 | * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code>. | |
134 | * | |
135 | * @param writer not null | |
136 | * @param comment The comment to write | |
137 | * @see #DEFAULT_INDENTATION_SIZE | |
138 | * @see #writeComment(XMLWriter, String, int, int) | |
139 | */ | |
140 | public static void writeComment( XMLWriter writer, String comment ) | |
141 | { | |
142 | 6 | writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE ); |
143 | 6 | } |
144 | ||
145 | /** | |
146 | * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code> | |
147 | * and is indented by <code>indent</code> using <code>2</code> as indentation size. | |
148 | * | |
149 | * @param writer not null | |
150 | * @param comment The comment to write | |
151 | * @param indent positive number | |
152 | * @see #DEFAULT_INDENTATION_SIZE | |
153 | * @see #writeComment(XMLWriter, String, int, int) | |
154 | */ | |
155 | public static void writeComment( XMLWriter writer, String comment, int indent ) | |
156 | { | |
157 | 2 | writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE ); |
158 | 2 | } |
159 | ||
160 | /** | |
161 | * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code> | |
162 | * and is indented by <code>indent</code> using <code>indentSize</code>. | |
163 | * | |
164 | * @param writer not null | |
165 | * @param comment The comment to write | |
166 | * @param indent positive number | |
167 | * @param indentSize positive number | |
168 | * @see #DEFAULT_COLUMN_LINE | |
169 | * @see #writeComment(XMLWriter, String, int, int, int) | |
170 | */ | |
171 | public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize ) | |
172 | { | |
173 | 10 | writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE ); |
174 | 10 | } |
175 | /** | |
176 | * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>columnSize</code> | |
177 | * and is indented by <code>indent</code> using <code>indentSize</code>. | |
178 | * | |
179 | * @param writer not null | |
180 | * @param comment The comment to write | |
181 | * @param indent positive number | |
182 | * @param indentSize positive number | |
183 | * @param columnSize positive number | |
184 | */ | |
185 | public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize ) | |
186 | { | |
187 | 16 | if ( comment == null ) |
188 | { | |
189 | 1 | comment = "null"; |
190 | } | |
191 | ||
192 | 16 | if ( indent < 0 ) |
193 | { | |
194 | 0 | indent = 0; |
195 | } | |
196 | ||
197 | 16 | if ( indentSize < 0 ) |
198 | { | |
199 | 0 | indentSize = 0; |
200 | } | |
201 | ||
202 | 16 | if ( columnSize < 0 ) |
203 | { | |
204 | 0 | columnSize = DEFAULT_COLUMN_LINE; |
205 | } | |
206 | ||
207 | 16 | String indentation = StringUtils.repeat( " ", indent * indentSize ); |
208 | 16 | int magicNumber = indentation.length() + columnSize - "-->".length() - 1; |
209 | 16 | String[] sentences = StringUtils.split( comment, LS ); |
210 | ||
211 | 16 | StringBuffer line = new StringBuffer( indentation + "<!-- " ); |
212 | 36 | for ( String sentence : sentences ) |
213 | { | |
214 | 20 | String[] words = StringUtils.split( sentence, " " ); |
215 | 84 | for ( String word : words ) |
216 | { | |
217 | 64 | StringBuilder sentenceTmp = new StringBuilder( line.toString() ); |
218 | 64 | sentenceTmp.append( word ).append( ' ' ); |
219 | 64 | if ( sentenceTmp.length() > magicNumber ) |
220 | { | |
221 | 11 | if ( line.length() != indentation.length() + "<!-- ".length() ) |
222 | { | |
223 | 9 | if ( magicNumber - line.length() > 0 ) |
224 | { | |
225 | 5 | line.append( StringUtils.repeat( " ", magicNumber - line.length() ) ); |
226 | } | |
227 | ||
228 | 9 | line.append( "-->" ).append( LS ); |
229 | 9 | writer.writeMarkup( line.toString() ); |
230 | } | |
231 | 11 | line = new StringBuffer( indentation + "<!-- " ); |
232 | 11 | line.append( word ).append( ' ' ); |
233 | } | |
234 | else | |
235 | { | |
236 | 53 | line.append( word ).append( ' ' ); |
237 | } | |
238 | } | |
239 | ||
240 | 20 | if ( magicNumber - line.length() > 0 ) |
241 | { | |
242 | 18 | line.append( StringUtils.repeat( " ", magicNumber - line.length() ) ); |
243 | } | |
244 | } | |
245 | ||
246 | 16 | if ( line.length() <= magicNumber ) |
247 | { | |
248 | 14 | line.append( StringUtils.repeat( " ", magicNumber - line.length() ) ); |
249 | } | |
250 | ||
251 | 16 | line.append( "-->" ).append( LS ); |
252 | ||
253 | 16 | writer.writeMarkup( line.toString() ); |
254 | 16 | } |
255 | ||
256 | /** | |
257 | * Convenience method to write XML comments between two comments line break. | |
258 | * The XML comment block is not indented. | |
259 | * | |
260 | * @param writer not null | |
261 | * @param comment The comment to write | |
262 | * @see #DEFAULT_INDENTATION_SIZE | |
263 | * @see #writeCommentText(XMLWriter, String, int, int) | |
264 | */ | |
265 | public static void writeCommentText( XMLWriter writer, String comment ) | |
266 | { | |
267 | 0 | writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE ); |
268 | 0 | } |
269 | ||
270 | /** | |
271 | * Convenience method to write XML comments between two comments line break. | |
272 | * The XML comment block is also indented by <code>indent</code> using | |
273 | * <code>2</code> as indentation size. | |
274 | * | |
275 | * @param writer not null | |
276 | * @param comment The comment to write | |
277 | * @param indent positive number | |
278 | * @see #DEFAULT_INDENTATION_SIZE | |
279 | * @see #writeCommentText(XMLWriter, String, int, int) | |
280 | */ | |
281 | public static void writeCommentText( XMLWriter writer, String comment, int indent ) | |
282 | { | |
283 | 2 | writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE ); |
284 | 2 | } |
285 | ||
286 | /** | |
287 | * Convenience method to write XML comment between two comment line break. | |
288 | * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>. | |
289 | * | |
290 | * @param writer not null | |
291 | * @param comment The comment to write | |
292 | * @param indent positive number | |
293 | * @param indentSize positive number | |
294 | * @see #DEFAULT_COLUMN_LINE | |
295 | * @see #writeCommentText(XMLWriter, String, int, int, int) | |
296 | */ | |
297 | public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize ) | |
298 | { | |
299 | 3 | writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE ); |
300 | 3 | } |
301 | ||
302 | /** | |
303 | * Convenience method to write XML comments between two comments line break. | |
304 | * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>. | |
305 | * The column size could be also be specified. | |
306 | * | |
307 | * @param writer not null | |
308 | * @param comment The comment to write | |
309 | * @param indent positive number | |
310 | * @param indentSize positive number | |
311 | * @param columnSize positive number | |
312 | */ | |
313 | public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize ) | |
314 | { | |
315 | 4 | if ( indent < 0 ) |
316 | { | |
317 | 0 | indent = 0; |
318 | } | |
319 | ||
320 | 4 | if ( indentSize < 0 ) |
321 | { | |
322 | 0 | indentSize = 0; |
323 | } | |
324 | ||
325 | 4 | if ( columnSize < 0 ) |
326 | { | |
327 | 0 | columnSize = DEFAULT_COLUMN_LINE; |
328 | } | |
329 | ||
330 | 4 | writeLineBreak( writer, 1 ); |
331 | ||
332 | 4 | writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) ); |
333 | 4 | writeCommentLineBreak( writer, columnSize ); |
334 | ||
335 | 4 | writeComment( writer, comment, indent, indentSize, columnSize ); |
336 | ||
337 | 4 | writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) ); |
338 | 4 | writeCommentLineBreak( writer, columnSize ); |
339 | ||
340 | 4 | writeLineBreak( writer, 1, indent, indentSize ); |
341 | 4 | } |
342 | } |