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