View Javadoc
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 java.io.IOException;
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   * @version $Id$
30   */
31  public class XmlWriterUtil
32  {
33      /** The vm line separator */
34      public static final String LS = System.getProperty( "line.separator" );
35  
36      /** The default line indenter size i.e. 2. */
37      public static final int DEFAULT_INDENTATION_SIZE = 2;
38  
39      /** The default column before line wrapping i.e. 80. */
40      public static final int DEFAULT_COLUMN_LINE = 80;
41  
42      /**
43       * Convenience method to write one <code>CRLF</code>.
44       *
45       * @param writer not null writer
46       * @throws IOException if writing fails.
47       */
48      public static void writeLineBreak( XMLWriter writer ) throws IOException
49      {
50          writeLineBreak( writer, 1 );
51      }
52  
53      /**
54       * Convenience method to repeat <code>CRLF</code>.
55       *
56       * @param writer not null
57       * @param repeat positive number
58       * @throws IOException if writing fails.
59       */
60      public static void writeLineBreak( XMLWriter writer, int repeat ) throws IOException
61      {
62          for ( int i = 0; i < repeat; i++ )
63          {
64              writer.writeMarkup( LS );
65          }
66      }
67  
68      /**
69       * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>2</code>.
70       *
71       * @param writer not null
72       * @param repeat The number of repetitions of the indent
73       * @param indent positive number
74       * @see #DEFAULT_INDENTATION_SIZE
75       * @see #writeLineBreak(XMLWriter, int, int, int)
76       * @throws IOException if writing fails.
77       */
78      public static void writeLineBreak( XMLWriter writer, int repeat, int indent ) throws IOException
79      {
80          writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE );
81      }
82  
83      /**
84       * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>indentSize</code>.
85       *
86       * @param writer not null
87       * @param repeat The number of repetitions of the indent
88       * @param indent positive number
89       * @param indentSize positive number
90       * @throws IOException if writing fails.
91       */
92      public static void writeLineBreak( XMLWriter writer, int repeat, int indent, int indentSize ) throws IOException
93      {
94          writeLineBreak( writer, repeat );
95  
96          if ( indent < 0 )
97          {
98              indent = 0;
99          }
100 
101         if ( indentSize < 0 )
102         {
103             indentSize = 0;
104         }
105 
106         writer.writeText( StringUtils.repeat( " ", indent * indentSize ) );
107     }
108 
109     /**
110      * Convenience method to write XML comment line break. Its size is <code>80</code>.
111      *
112      * @param writer not null
113      * @see #DEFAULT_COLUMN_LINE
114      * @see #writeCommentLineBreak(XMLWriter, int)
115      * @throws IOException if writing fails.
116      */
117     public static void writeCommentLineBreak( XMLWriter writer ) throws IOException
118     {
119         writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE );
120     }
121 
122     /**
123      * Convenience method to write XML comment line break with <code>columnSize</code> as length.
124      *
125      * @param writer not null
126      * @param columnSize positive number
127      * @throws IOException if writing fails.
128      */
129     public static void writeCommentLineBreak( XMLWriter writer, int columnSize ) throws IOException
130     {
131         if ( columnSize < 10 )
132         {
133             columnSize = DEFAULT_COLUMN_LINE;
134         }
135 
136         writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
137     }
138 
139     /**
140      * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of
141      * <code>80</code>.
142      *
143      * @param writer not null
144      * @param comment The comment to write
145      * @see #DEFAULT_INDENTATION_SIZE
146      * @see #writeComment(XMLWriter, String, int, int)
147      * @throws IOException if writing fails.
148      */
149     public static void writeComment( XMLWriter writer, String comment ) throws IOException
150     {
151         writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
152     }
153 
154     /**
155      * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
156      * <code>80</code> and is indented by <code>indent</code> using <code>2</code> as indentation size.
157      *
158      * @param writer not null
159      * @param comment The comment to write
160      * @param indent positive number
161      * @see #DEFAULT_INDENTATION_SIZE
162      * @see #writeComment(XMLWriter, String, int, int)
163      * @throws IOException if writing fails.
164      */
165     public static void writeComment( XMLWriter writer, String comment, int indent ) throws IOException
166     {
167         writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
168     }
169 
170     /**
171      * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of <code>80</code>
172      * and is indented by <code>indent</code> using <code>indentSize</code>.
173      *
174      * @param writer not null
175      * @param comment The comment to write
176      * @param indent positive number
177      * @param indentSize positive number
178      * @see #DEFAULT_COLUMN_LINE
179      * @see #writeComment(XMLWriter, String, int, int, int)
180      * @throws IOException if writing fails.
181      */
182     public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize ) throws IOException
183     {
184         writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
185     }
186     
187     /**
188      * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
189      * <code>columnSize</code> and is indented by <code>indent</code> using <code>indentSize</code>.
190      *
191      * @param writer not null
192      * @param comment The comment to write
193      * @param indent positive number
194      * @param indentSize positive number
195      * @param columnSize positive number
196      * @throws IOException if writing fails.
197      */
198     public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
199         throws IOException
200     {
201         if ( comment == null )
202         {
203             comment = "null";
204         }
205 
206         if ( indent < 0 )
207         {
208             indent = 0;
209         }
210 
211         if ( indentSize < 0 )
212         {
213             indentSize = 0;
214         }
215 
216         if ( columnSize < 0 )
217         {
218             columnSize = DEFAULT_COLUMN_LINE;
219         }
220 
221         String indentation = StringUtils.repeat( " ", indent * indentSize );
222         int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
223         String[] sentences = StringUtils.split( comment, LS );
224 
225         StringBuffer line = new StringBuffer( indentation + "<!-- " );
226         for ( String sentence : sentences )
227         {
228             String[] words = StringUtils.split( sentence, " " );
229             for ( String word : words )
230             {
231                 StringBuilder sentenceTmp = new StringBuilder( line.toString() );
232                 sentenceTmp.append( word ).append( ' ' );
233                 if ( sentenceTmp.length() > magicNumber )
234                 {
235                     if ( line.length() != indentation.length() + "<!-- ".length() )
236                     {
237                         if ( magicNumber - line.length() > 0 )
238                         {
239                             line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
240                         }
241 
242                         line.append( "-->" ).append( LS );
243                         writer.writeMarkup( line.toString() );
244                     }
245                     line = new StringBuffer( indentation + "<!-- " );
246                     line.append( word ).append( ' ' );
247                 }
248                 else
249                 {
250                     line.append( word ).append( ' ' );
251                 }
252             }
253 
254             if ( magicNumber - line.length() > 0 )
255             {
256                 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
257             }
258         }
259 
260         if ( line.length() <= magicNumber )
261         {
262             line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
263         }
264 
265         line.append( "-->" ).append( LS );
266 
267         writer.writeMarkup( line.toString() );
268     }
269 
270     /**
271      * Convenience method to write XML comments between two comments line break.
272      * The XML comment block is not indented.
273      *
274      * @param writer not null
275      * @param comment The comment to write
276      * @see #DEFAULT_INDENTATION_SIZE
277      * @see #writeCommentText(XMLWriter, String, int, int)
278      * @throws IOException if writing fails.
279      */
280     public static void writeCommentText( XMLWriter writer, String comment ) throws IOException
281     {
282         writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
283     }
284 
285     /**
286      * Convenience method to write XML comments between two comments line break.
287      * The XML comment block is also indented by <code>indent</code> using
288      * <code>2</code> as indentation size.
289      *
290      * @param writer not null
291      * @param comment The comment to write
292      * @param indent positive number
293      * @see #DEFAULT_INDENTATION_SIZE
294      * @see #writeCommentText(XMLWriter, String, int, int)
295      * @throws IOException if writing fails.
296      */
297     public static void writeCommentText( XMLWriter writer, String comment, int indent ) throws IOException
298     {
299         writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
300     }
301 
302     /**
303      * Convenience method to write XML comment between two comment line break.
304      * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
305      *
306      * @param writer not null
307      * @param comment The comment to write
308      * @param indent positive number
309      * @param indentSize positive number
310      * @see #DEFAULT_COLUMN_LINE
311      * @see #writeCommentText(XMLWriter, String, int, int, int)
312      * @throws IOException if writing fails.
313      */
314     public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize )
315         throws IOException
316     {
317         writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
318     }
319 
320     /**
321      * Convenience method to write XML comments between two comments line break.
322      * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
323      * The column size could be also be specified.
324      *
325      * @param writer not null
326      * @param comment The comment to write
327      * @param indent positive number
328      * @param indentSize positive number
329      * @param columnSize positive number
330      * @throws IOException if writing fails.
331      */
332     public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
333         throws IOException
334     {
335         if ( indent < 0 )
336         {
337             indent = 0;
338         }
339 
340         if ( indentSize < 0 )
341         {
342             indentSize = 0;
343         }
344 
345         if ( columnSize < 0 )
346         {
347             columnSize = DEFAULT_COLUMN_LINE;
348         }
349 
350         writeLineBreak( writer, 1 );
351 
352         writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
353         writeCommentLineBreak( writer, columnSize );
354 
355         writeComment( writer, comment, indent, indentSize, columnSize );
356 
357         writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
358         writeCommentLineBreak( writer, columnSize );
359 
360         writeLineBreak( writer, 1, indent, indentSize );
361     }
362 }