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 <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 writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
143 }
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 writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
158 }
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 writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
174 }
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 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 * 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 writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
268 }
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 writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
284 }
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 writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
300 }
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 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 }