1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * Utility class for the <code>XmlWriter</code> class.
27 *
28 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
29 *
30 */
31 public class XmlWriterUtil {
32 /** The vm line separator */
33 public static final String LS = System.getProperty("line.separator");
34
35 /** Platform independent line separator */
36 private static final String CRLF = "\r\n";
37
38 /** The default line indenter size i.e. 2. */
39 public static final int DEFAULT_INDENTATION_SIZE = 2;
40
41 /** The default column before line wrapping i.e. 80. */
42 public static final int DEFAULT_COLUMN_LINE = 80;
43
44 /**
45 * Convenience method to write one <code>CRLF</code>.
46 *
47 * @param writer not null writer
48 * @throws IOException if writing fails
49 */
50 public static void writeLineBreak(XMLWriter writer) throws IOException {
51 writeLineBreak(writer, 1);
52 }
53
54 /**
55 * Convenience method to repeat <code>CRLF</code>.
56 *
57 * @param writer not null
58 * @param repeat positive number
59 * @throws IOException if writing fails
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 * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>2</code>.
69 *
70 * @param writer not null
71 * @param repeat The number of repetitions of the indent
72 * @param indent positive number
73 * @see #DEFAULT_INDENTATION_SIZE
74 * @see #writeLineBreak(XMLWriter, int, int, int)
75 * @throws IOException if writing fails
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 * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>indentSize</code>.
83 *
84 * @param writer not null
85 * @param repeat The number of repetitions of the indent
86 * @param indent positive number
87 * @param indentSize positive number
88 * @throws IOException if writing fails
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 * 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 * @throws IOException if writing fails
111 */
112 public static void writeCommentLineBreak(XMLWriter writer) throws IOException {
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 * @throws IOException if writing fails
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 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
133 * <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 * @throws IOException if writing fails
140 */
141 public static void writeComment(XMLWriter writer, String comment) throws IOException {
142 writeComment(writer, comment, 0, DEFAULT_INDENTATION_SIZE);
143 }
144
145 /**
146 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
147 * <code>80</code> 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 * @throws IOException if writing fails
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 * Convenience method to write XML comment line. The <code>comment</code> is split 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 * @throws IOException if writing fails
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 * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
178 * <code>columnSize</code> and is indented by <code>indent</code> using <code>indentSize</code>.
179 *
180 * @param writer not null
181 * @param comment The comment to write
182 * @param indent positive number
183 * @param indentSize positive number
184 * @param columnSize positive number
185 * @throws IOException if writing fails
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 * Convenience method to write XML comments between two comments line break.
247 * The XML comment block is not indented.
248 *
249 * @param writer not null
250 * @param comment The comment to write
251 * @see #DEFAULT_INDENTATION_SIZE
252 * @see #writeCommentText(XMLWriter, String, int, int)
253 * @throws IOException if writing fails
254 */
255 public static void writeCommentText(XMLWriter writer, String comment) throws IOException {
256 writeCommentText(writer, comment, 0, DEFAULT_INDENTATION_SIZE);
257 }
258
259 /**
260 * Convenience method to write XML comments between two comments line break.
261 * The XML comment block is also indented by <code>indent</code> using
262 * <code>2</code> as indentation size.
263 *
264 * @param writer not null
265 * @param comment The comment to write
266 * @param indent positive number
267 * @see #DEFAULT_INDENTATION_SIZE
268 * @see #writeCommentText(XMLWriter, String, int, int)
269 * @throws IOException if writing fails
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 * Convenience method to write XML comment between two comment line break.
277 * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
278 *
279 * @param writer not null
280 * @param comment The comment to write
281 * @param indent positive number
282 * @param indentSize positive number
283 * @see #DEFAULT_COLUMN_LINE
284 * @see #writeCommentText(XMLWriter, String, int, int, int)
285 * @throws IOException if writing fails
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 * Convenience method to write XML comments between two comments line break.
294 * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
295 * The column size could be also be specified.
296 *
297 * @param writer not null
298 * @param comment The comment to write
299 * @param indent positive number
300 * @param indentSize positive number
301 * @param columnSize positive number
302 * @throws IOException if writing fails
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 }