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 import java.util.regex.Pattern;
25
26
27
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;
48 List<String> result = new ArrayList<>();
49 StringBuilder sb = new StringBuilder( size );
50
51 sb.setLength( 0 );
52 repeat( sb, BOX_CHAR, size );
53 result.add( sb.toString() );
54
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
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 }