View Javadoc
1   package org.apache.maven.internal;
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.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Pattern;
25  
26  /**
27   * Helper class to format multiline messages to the console
28   */
29  public class MultilineMessageHelper
30  {
31  
32      private static final int DEFAULT_MAX_SIZE = 65;
33      private static final char BOX_CHAR = '*';
34  
35      private static final Pattern S_FILTER = Pattern.compile( "\\s+" );
36  
37      public static String separatorLine()
38      {
39          StringBuilder sb = new StringBuilder( DEFAULT_MAX_SIZE );
40          repeat( sb, '*', DEFAULT_MAX_SIZE );
41          return sb.toString();
42      }
43  
44      public static List<String> format( String... lines )
45      {
46          int size = DEFAULT_MAX_SIZE;
47          int remainder = size - 4; // 4 chars = 2 box_char + 2 spaces
48          List<String> result = new ArrayList<>();
49          StringBuilder sb = new StringBuilder( size );
50          // first line
51          sb.setLength( 0 );
52          repeat( sb, BOX_CHAR, size );
53          result.add( sb.toString() );
54          // lines
55          for ( String line : lines )
56          {
57              sb.setLength( 0 );
58              String[] words = S_FILTER.split( line );
59              for ( String word : words )
60              {
61                  if ( sb.length() >= remainder - word.length() - ( sb.length() > 0 ? 1 : 0 ) )
62                  {
63                      repeat( sb, ' ', remainder - sb.length() );
64                      result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
65                      sb.setLength( 0 );
66                  }
67                  if ( sb.length() > 0 )
68                  {
69                      sb.append( ' ' );
70                  }
71                  sb.append( word );
72              }
73  
74              while ( sb.length() < remainder )
75              {
76                  sb.append( ' ' );
77              }
78              result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
79          }
80          // last line
81          sb.setLength( 0 );
82          repeat( sb, BOX_CHAR, size );
83          result.add( sb.toString() );
84          return result;
85      }
86  
87      private static void repeat( StringBuilder sb, char c, int nb )
88      {
89          for ( int i = 0; i < nb; i++ )
90          {
91              sb.append( c );
92          }
93      }
94  }