1 package org.apache.maven.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25
26
27
28 public class MultilineMessageHelper
29 {
30
31 private static final int DEFAULT_MAX_SIZE = 65;
32 private static final char BOX_CHAR = '*';
33
34 public static String separatorLine()
35 {
36 StringBuilder sb = new StringBuilder( DEFAULT_MAX_SIZE );
37 repeat( sb, '*', DEFAULT_MAX_SIZE );
38 return sb.toString();
39 }
40
41 public static List<String> format( String... lines )
42 {
43 int size = DEFAULT_MAX_SIZE;
44 int remainder = size - 4;
45 List<String> result = new ArrayList<>();
46 StringBuilder sb = new StringBuilder( size );
47
48 sb.setLength( 0 );
49 repeat( sb, BOX_CHAR, size );
50 result.add( sb.toString() );
51
52 for ( String line : lines )
53 {
54 sb.setLength( 0 );
55 String[] words = line.split( "\\s+" );
56 for ( String word : words )
57 {
58 if ( sb.length() >= remainder - word.length() - ( sb.length() > 0 ? 1 : 0 ) )
59 {
60 repeat( sb, ' ', remainder - sb.length() );
61 result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
62 sb.setLength( 0 );
63 }
64 if ( sb.length() > 0 )
65 {
66 sb.append( ' ' );
67 }
68 sb.append( word );
69 }
70
71 while ( sb.length() < remainder )
72 {
73 sb.append( ' ' );
74 }
75 result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
76 }
77
78 sb.setLength( 0 );
79 repeat( sb, BOX_CHAR, size );
80 result.add( sb.toString() );
81 return result;
82 }
83
84 private static void repeat( StringBuilder sb, char c, int nb )
85 {
86 for ( int i = 0; i < nb; i++ )
87 {
88 sb.append( c );
89 }
90 }
91 }