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