Coverage Report - org.codehaus.plexus.util.xml.XmlWriterUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlWriterUtil
83%
71/85
72%
29/40
2.429
 
 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  
  * @version $Id$
 26  
  */
 27  0
 public class XmlWriterUtil
 28  
 {
 29  
     /** The vm line separator */
 30  1
     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  1
         writeLineBreak( writer, 1 );
 46  1
     }
 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  51
         for ( int i = 0; i < repeat; i++ )
 57  
         {
 58  39
             writer.writeMarkup( LS );
 59  
         }
 60  12
     }
 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
 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  1
         writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE );
 74  1
     }
 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
 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  6
         writeLineBreak( writer, repeat );
 87  
 
 88  6
         if ( indent < 0 )
 89  
         {
 90  0
             indent = 0;
 91  
         }
 92  
 
 93  6
         if ( indentSize < 0 )
 94  
         {
 95  0
             indentSize = 0;
 96  
         }
 97  
 
 98  6
         writer.writeText( StringUtils.repeat( " ", indent * indentSize ) );
 99  6
     }
 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  1
         writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE );
 111  1
     }
 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  11
         if ( columnSize < 10 )
 122  
         {
 123  0
             columnSize = DEFAULT_COLUMN_LINE;
 124  
         }
 125  
 
 126  11
         writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
 127  11
     }
 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
 135  
      * @see #DEFAULT_INDENTATION_SIZE
 136  
      * @see #writeComment(XMLWriter, String, int, int)
 137  
      */
 138  
     public static void writeComment( XMLWriter writer, String comment )
 139  
     {
 140  6
         writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
 141  6
     }
 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
 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  2
         writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
 156  2
     }
 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
 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  10
         writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
 172  10
     }
 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
 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  16
         if ( comment == null )
 187  
         {
 188  1
             comment = "null";
 189  
         }
 190  
 
 191  16
         while ( comment.contains( "<!--" ) )
 192  
         {
 193  0
             comment = comment.replace( "<!--", "" );
 194  
         }
 195  
 
 196  16
         while ( comment.contains( "-->" ) )
 197  
         {
 198  0
             comment = comment.replace( "-->", "" );
 199  
         }
 200  
 
 201  16
         if ( indent < 0 )
 202  
         {
 203  0
             indent = 0;
 204  
         }
 205  
 
 206  16
         if ( indentSize < 0 )
 207  
         {
 208  0
             indentSize = 0;
 209  
         }
 210  
 
 211  16
         if ( columnSize < 0 )
 212  
         {
 213  0
             columnSize = DEFAULT_COLUMN_LINE;
 214  
         }
 215  
 
 216  16
         String indentation = StringUtils.repeat( " ", indent * indentSize );
 217  16
         int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
 218  16
         String[] sentences = StringUtils.split( comment, LS );
 219  
 
 220  16
         StringBuffer line = new StringBuffer( indentation + "<!-- " );
 221  36
         for ( String sentence : sentences )
 222  
         {
 223  20
             String[] words = StringUtils.split( sentence, " " );
 224  84
             for ( String word : words )
 225  
             {
 226  64
                 StringBuilder sentenceTmp = new StringBuilder( line.toString() );
 227  64
                 sentenceTmp.append( word ).append( ' ' );
 228  64
                 if ( sentenceTmp.length() > magicNumber )
 229  
                 {
 230  11
                     if ( line.length() != indentation.length() + "<!-- ".length() )
 231  
                     {
 232  9
                         if ( magicNumber - line.length() > 0 )
 233  
                         {
 234  5
                             line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
 235  
                         }
 236  
 
 237  9
                         line.append( "-->" ).append( LS );
 238  9
                         writer.writeMarkup( line.toString() );
 239  
                     }
 240  11
                     line = new StringBuffer( indentation + "<!-- " );
 241  11
                     line.append( word ).append( ' ' );
 242  
                 }
 243  
                 else
 244  
                 {
 245  53
                     line.append( word ).append( ' ' );
 246  
                 }
 247  
             }
 248  
 
 249  20
             if ( magicNumber - line.length() > 0 )
 250  
             {
 251  18
                 line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
 252  
             }
 253  
         }
 254  
 
 255  16
         if ( line.length() <= magicNumber )
 256  
         {
 257  14
             line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
 258  
         }
 259  
 
 260  16
         line.append( "-->" ).append( LS );
 261  
 
 262  16
         writer.writeMarkup( line.toString() );
 263  16
     }
 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
 270  
      * @see #DEFAULT_INDENTATION_SIZE
 271  
      * @see #writeCommentText(XMLWriter, String, int, int)
 272  
      */
 273  
     public static void writeCommentText( XMLWriter writer, String comment )
 274  
     {
 275  0
         writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
 276  0
     }
 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
 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  2
         writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
 291  2
     }
 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
 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  3
         writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
 307  3
     }
 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
 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  4
         if ( indent < 0 )
 322  
         {
 323  0
             indent = 0;
 324  
         }
 325  
 
 326  4
         if ( indentSize < 0 )
 327  
         {
 328  0
             indentSize = 0;
 329  
         }
 330  
 
 331  4
         if ( columnSize < 0 )
 332  
         {
 333  0
             columnSize = DEFAULT_COLUMN_LINE;
 334  
         }
 335  
 
 336  4
         writeLineBreak( writer, 1 );
 337  
 
 338  4
         writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
 339  4
         writeCommentLineBreak( writer, columnSize );
 340  
 
 341  4
         writeComment( writer, comment, indent, indentSize, columnSize );
 342  
 
 343  4
         writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
 344  4
         writeCommentLineBreak( writer, columnSize );
 345  
 
 346  4
         writeLineBreak( writer, 1, indent, indentSize );
 347  4
     }
 348  
 }