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 public class XmlWriterUtil
31 {
32 /** The vm line separator */
33 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 writeLineBreak( writer, 1 );
49 }
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 for ( int i = 0; i < repeat; i++ )
60 {
61 writer.writeMarkup( LS );
62 }
63 }
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 writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE );
77 }
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 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 * 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 writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE );
114 }
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 if ( columnSize < 10 )
125 {
126 columnSize = DEFAULT_COLUMN_LINE;
127 }
128
129 writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
130 }
131
132 /**
133 * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of
134 * <code>80</code>.
135 *
136 * @param writer not null
137 * @param comment The comment to write
138 * @see #DEFAULT_INDENTATION_SIZE
139 * @see #writeComment(XMLWriter, String, int, int)
140 */
141 public static void writeComment( XMLWriter writer, String comment )
142 {
143 writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
144 }
145
146 /**
147 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
148 * <code>80</code> and is indented by <code>indent</code> using <code>2</code> as indentation size.
149 *
150 * @param writer not null
151 * @param comment The comment to write
152 * @param indent positive number
153 * @see #DEFAULT_INDENTATION_SIZE
154 * @see #writeComment(XMLWriter, String, int, int)
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 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of <code>80</code>
163 * and is indented by <code>indent</code> using <code>indentSize</code>.
164 *
165 * @param writer not null
166 * @param comment The comment to write
167 * @param indent positive number
168 * @param indentSize positive number
169 * @see #DEFAULT_COLUMN_LINE
170 * @see #writeComment(XMLWriter, String, int, int, int)
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 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
179 * <code>columnSize</code> and is indented by <code>indent</code> using <code>indentSize</code>.
180 *
181 * @param writer not null
182 * @param comment The comment to write
183 * @param indent positive number
184 * @param indentSize positive number
185 * @param columnSize positive number
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 * Convenience method to write XML comments between two comments line break.
260 * The XML comment block is not indented.
261 *
262 * @param writer not null
263 * @param comment The comment to write
264 * @see #DEFAULT_INDENTATION_SIZE
265 * @see #writeCommentText(XMLWriter, String, int, int)
266 */
267 public static void writeCommentText( XMLWriter writer, String comment )
268 {
269 writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
270 }
271
272 /**
273 * Convenience method to write XML comments between two comments line break.
274 * The XML comment block is also indented by <code>indent</code> using
275 * <code>2</code> as indentation size.
276 *
277 * @param writer not null
278 * @param comment The comment to write
279 * @param indent positive number
280 * @see #DEFAULT_INDENTATION_SIZE
281 * @see #writeCommentText(XMLWriter, String, int, int)
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 * Convenience method to write XML comment between two comment line break.
290 * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
291 *
292 * @param writer not null
293 * @param comment The comment to write
294 * @param indent positive number
295 * @param indentSize positive number
296 * @see #DEFAULT_COLUMN_LINE
297 * @see #writeCommentText(XMLWriter, String, int, int, int)
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 * Convenience method to write XML comments between two comments line break.
306 * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
307 * The column size could be also be specified.
308 *
309 * @param writer not null
310 * @param comment The comment to write
311 * @param indent positive number
312 * @param indentSize positive number
313 * @param columnSize positive number
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 }