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
25 /**
26 * Helper class to format multiline messages to the console
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; // 4 chars = 2 box_char + 2 spaces
45 List<String> result = new ArrayList<>();
46 StringBuilder sb = new StringBuilder( size );
47 // first line
48 sb.setLength( 0 );
49 repeat( sb, BOX_CHAR, size );
50 result.add( sb.toString() );
51 // lines
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 // last line
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 }