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 import java.util.regex.Pattern;
24
25
26
27
28 public class MultilineMessageHelper {
29
30 private static final int DEFAULT_MAX_SIZE = 65;
31 private static final char BOX_CHAR = '*';
32
33 private static final Pattern S_FILTER = Pattern.compile("\\s+");
34
35 public static String separatorLine() {
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 int size = DEFAULT_MAX_SIZE;
43 int remainder = size - 4;
44 List<String> result = new ArrayList<>();
45 StringBuilder sb = new StringBuilder(size);
46
47 sb.setLength(0);
48 repeat(sb, BOX_CHAR, size);
49 result.add(sb.toString());
50
51 for (String line : lines) {
52 sb.setLength(0);
53 String[] words = S_FILTER.split(line);
54 for (String word : words) {
55 if (sb.length() >= remainder - word.length() - (sb.length() > 0 ? 1 : 0)) {
56 repeat(sb, ' ', remainder - sb.length());
57 result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
58 sb.setLength(0);
59 }
60 if (sb.length() > 0) {
61 sb.append(' ');
62 }
63 sb.append(word);
64 }
65
66 while (sb.length() < remainder) {
67 sb.append(' ');
68 }
69 result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
70 }
71
72 sb.setLength(0);
73 repeat(sb, BOX_CHAR, size);
74 result.add(sb.toString());
75 return result;
76 }
77
78 private static void repeat(StringBuilder sb, char c, int nb) {
79 for (int i = 0; i < nb; i++) {
80 sb.append(c);
81 }
82 }
83 }